Skip to content

Commit

Permalink
Merge pull request #104 from ninech/add-more-seeds
Browse files Browse the repository at this point in the history
Add seed/initialization scripts for additional resources
  • Loading branch information
cimnine authored Oct 31, 2018
2 parents 63062a2 + 2fe139c commit c16f72a
Show file tree
Hide file tree
Showing 23 changed files with 495 additions and 4 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ if created:

#### Initializers

Initializers are built-in startup scripts for defining NetBox custom fields, groups and users.
Initializers are built-in startup scripts for defining NetBox custom fields, groups, users and many other resources.
All you need to do is to mount you own `initializers` folder ([see `docker-compose.yml`][netbox-docker-compose]).
Look at the [`initializers` folder][netbox-docker-initializers] to learn how the files must look like.

Expand Down Expand Up @@ -325,10 +325,10 @@ Then make sure that the `redis` container and at least one `netbox-worker` are r
# check the container status
$ docker-compose ps
Name Command State Ports
Name Command State Ports
--------------------------------------------------------------------------------------------------------
netbox-docker_netbox-worker_1 /opt/netbox/docker-entrypo ... Up
netbox-docker_netbox_1 /opt/netbox/docker-entrypo ... Up
netbox-docker_netbox-worker_1 /opt/netbox/docker-entrypo ... Up
netbox-docker_netbox_1 /opt/netbox/docker-entrypo ... Up
netbox-docker_nginx_1 nginx -c /etc/netbox-nginx ... Up 80/tcp, 0.0.0.0:32776->8080/tcp
netbox-docker_postgres_1 docker-entrypoint.sh postgres Up 5432/tcp
netbox-docker_redis_1 docker-entrypoint.sh redis ... Up 6379/tcp
Expand Down
2 changes: 2 additions & 0 deletions initializers/custom_fields.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
# on_objects:
# - dcim.models.Device
# - dcim.models.Rack
# - dcim.models.Site
# - dcim.models.DeviceType
# - ipam.models.IPAddress
# - ipam.models.Prefix
# - tenancy.models.Tenant
Expand Down
15 changes: 15 additions & 0 deletions initializers/device_roles.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# - name: switch
# slug: switch
# color: Grey
# - name: router
# slug: router
# color: Cyan
# - name: load-balancer
# slug: load-balancer
# color: Red
# - name: server
# slug: server
# color: Blue
# - name: patchpanel
# slug: patchpanel
# color: Black
23 changes: 23 additions & 0 deletions initializers/device_types.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# - model: Model 1
# manufacturer: Manufacturer 1
# slug: model-1
# u_height: 2
# custom_fields:
# text_field: Description
# - model: Model 2
# manufacturer: Manufacturer 1
# slug: model-2
# custom_fields:
# text_field: Description
# - model: Model 3
# manufacturer: Manufacturer 1
# slug: model-3
# is_full_depth: false
# u_height: 0
# custom_fields:
# text_field: Description
# - model: Other
# manufacturer: NoName
# slug: other
# custom_fields:
# text_field: Description
27 changes: 27 additions & 0 deletions initializers/devices.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# - name: server01
# device_role: server
# device_type: Other
# site: AMS 1
# rack: rack-01
# face: Front
# position: 1
# custom_fields:
# text_field: Description
# - name: server02
# device_role: server
# device_type: Other
# site: AMS 2
# rack: rack-02
# face: Front
# position: 2
# custom_fields:
# text_field: Description
# - name: server03
# device_role: server
# device_type: Other
# site: SING 1
# rack: rack-03
# face: Front
# position: 3
# custom_fields:
# text_field: Description
6 changes: 6 additions & 0 deletions initializers/manufacturers.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# - name: Manufacturer 1
# slug: manufacturer-1
# - name: Manufacturer 2
# slug: manufacturer-2
# - name: NoName
# slug: noname
19 changes: 19 additions & 0 deletions initializers/platforms.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# # Allowed rpc clients are: juniper-junos, cisco-ios, opengear
# - name: Platform 1
# slug: platform-1
# manufacturer: Manufacturer 1
# napalm_driver: driver1
# napalm_args: "{'arg1': 'value1', 'arg2': 'value2'}"
# rpc_client: juniper-junos
# - name: Platform 2
# slug: platform-2
# manufacturer: Manufacturer 2
# napalm_driver: driver2
# napalm_args: "{'arg1': 'value1', 'arg2': 'value2'}"
# rpc_client: opengear
# - name: Platform 3
# slug: platform-3
# manufacturer: NoName
# napalm_driver: driver3
# napalm_args: "{'arg1': 'value1', 'arg2': 'value2'}"
# rpc_client: juniper-junos
12 changes: 12 additions & 0 deletions initializers/rack_roles.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# - name: Role 1
# slug: role-1
# color: Pink
# - name: Role 2
# slug: role-2
# color: Cyan
# - name: Role 3
# slug: role-3
# color: Grey
# - name: Role 4
# slug: role-4
# color: Teal
24 changes: 24 additions & 0 deletions initializers/racks.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# - site: AMS 1
# name: rack-01
# role: Role 1
# type: 4-post cabinet
# width: 19 inches
# u_height: 47
# custom_fields:
# text_field: Description
# - site: AMS 2
# name: rack-02
# role: Role 2
# type: 4-post cabinet
# width: 19 inches
# u_height: 47
# custom_fields:
# text_field: Description
# - site: SING 1
# name: rack-03
# role: Role 3
# type: 4-post cabinet
# width: 19 inches
# u_height: 47
# custom_fields:
# text_field: Description
10 changes: 10 additions & 0 deletions initializers/regions.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# - name: Singapore
# slug: singapore
# - name: Amsterdam
# slug: amsterdam
# - name: Downtown
# slug: downtown
# parent: Amsterdam
# - name: Suburbs
# slug: suburbs
# parent: Amsterdam
32 changes: 32 additions & 0 deletions initializers/sites.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# - name: AMS 1
# slug: ams1
# region: Downtown
# status: 1
# facility: Amsterdam 1
# asn: 12345
# custom_fields:
# text_field: Description
# - name: AMS 2
# slug: ams2
# region: Downtown
# status: 1
# facility: Amsterdam 2
# asn: 54321
# custom_fields:
# text_field: Description
# - name: AMS 3
# slug: ams3
# region: Suburbs
# status: 1
# facility: Amsterdam 3
# asn: 67890
# custom_fields:
# text_field: Description
# - name: SING 1
# slug: sing1
# region: Singapore
# status: 1
# facility: Singapore 1
# asn: 09876
# custom_fields:
# text_field: Description
File renamed without changes.
File renamed without changes.
File renamed without changes.
25 changes: 25 additions & 0 deletions startup_scripts/030_regions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from dcim.models import Region
from ruamel.yaml import YAML

with open('/opt/netbox/initializers/regions.yml', 'r') as stream:
yaml=YAML(typ='safe')
regions = yaml.load(stream)

optional_assocs = {
'parent': (Region, 'name')
}

if regions is not None:
for params in regions:

for assoc, details in optional_assocs.items():
if assoc in params:
model, field = details
query = { field: params.pop(assoc) }

params[assoc] = model.objects.get(**query)

region, created = Region.objects.get_or_create(**params)

if created:
print("🌐 Created region", region.name)
40 changes: 40 additions & 0 deletions startup_scripts/040_sites.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from dcim.models import Region, Site
from extras.models import CustomField, CustomFieldValue
from tenancy.models import Tenant
from ruamel.yaml import YAML

with open('/opt/netbox/initializers/sites.yml', 'r') as stream:
yaml = YAML(typ='safe')
sites = yaml.load(stream)

optional_assocs = {
'region': (Region, 'name'),
'tenant': (Tenant, 'name')
}

if sites is not None:
for params in sites:
custom_fields = params.pop('custom_fields', None)

for assoc, details in optional_assocs.items():
if assoc in params:
model, field = details
query = { field: params.pop(assoc) }

params[assoc] = model.objects.get(**query)

site, created = Site.objects.get_or_create(**params)

if created:
if custom_fields is not None:
for cf_name, cf_value in custom_fields.items():
custom_field = CustomField.objects.get(name=cf_name)
custom_field_value = CustomFieldValue.objects.create(
field=custom_field,
obj=site,
value=cf_value
)

site.custom_field_values.add(custom_field_value)

print("📍 Created site", site.name)
13 changes: 13 additions & 0 deletions startup_scripts/050_manufacturers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from dcim.models import Manufacturer
from ruamel.yaml import YAML

with open('/opt/netbox/initializers/manufacturers.yml', 'r') as stream:
yaml = YAML(typ='safe')
manufacturers = yaml.load(stream)

if manufacturers is not None:
for params in manufacturers:
manufacturer, created = Manufacturer.objects.get_or_create(**params)

if created:
print("🏭 Created Manufacturer", manufacturer.name)
51 changes: 51 additions & 0 deletions startup_scripts/060_device_types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
from dcim.models import DeviceType, Manufacturer, Region
from tenancy.models import Tenant
from extras.models import CustomField, CustomFieldValue
from ruamel.yaml import YAML

with open('/opt/netbox/initializers/device_types.yml', 'r') as stream:
yaml = YAML(typ='safe')
device_types = yaml.load(stream)

required_assocs = {
'manufacturer': (Manufacturer, 'name')
}

optional_assocs = {
'region': (Region, 'name'),
'tenant': (Tenant, 'name')
}

if device_types is not None:
for params in device_types:
custom_fields = params.pop('custom_fields', None)

for assoc, details in required_assocs.items():
model, field = details
query = { field: params.pop(assoc) }

params[assoc] = model.objects.get(**query)

for assoc, details in optional_assocs.items():
if assoc in params:
model, field = details
query = { field: params.pop(assoc) }

params[assoc] = model.objects.get(**query)

device_type, created = DeviceType.objects.get_or_create(**params)

if created:
if custom_fields is not None:
for cf_name, cf_value in custom_fields.items():
custom_field = CustomField.objects.get(name=cf_name)
custom_field_value = CustomFieldValue.objects.create(
field=custom_field,
obj=device_type,
value=cf_value
)

device_type.custom_field_values.add(custom_field_value)

print("🔡 Created device type", device_type.manufacturer, device_type.model)

21 changes: 21 additions & 0 deletions startup_scripts/070_rack_roles.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from dcim.models import RackRole
from ruamel.yaml import YAML
from utilities.forms import COLOR_CHOICES

with open('/opt/netbox/initializers/rack_roles.yml', 'r') as stream:
yaml=YAML(typ='safe')
rack_roles = yaml.load(stream)

if rack_roles is not None:
for params in rack_roles:
if 'color' in params:
color = params.pop('color')

for color_tpl in COLOR_CHOICES:
if color in color_tpl:
params['color'] = color_tpl[0]

rack_role, created = RackRole.objects.get_or_create(**params)

if created:
print("🎨 Created rack role", rack_role.name)
Loading

0 comments on commit c16f72a

Please sign in to comment.