diff --git a/initializers/aggregates.yml b/initializers/aggregates.yml index d923a04..b04eb7e 100644 --- a/initializers/aggregates.yml +++ b/initializers/aggregates.yml @@ -1,5 +1,6 @@ # - prefix: 10.0.0.0/16 # rir: RFC1918 +# tenant: tenant1 # - prefix: fd00:ccdd::/32 # rir: RFC4193 ULA # - prefix: 2001:db8::/32 diff --git a/initializers/circuit_types.yml b/initializers/circuit_types.yml new file mode 100644 index 0000000..0e9c356 --- /dev/null +++ b/initializers/circuit_types.yml @@ -0,0 +1,6 @@ +# - name: VPLS +# slug: vpls +# - name: MPLS +# slug: mpls +# - name: Internet +# slug: internet diff --git a/initializers/circuits.yml b/initializers/circuits.yml new file mode 100644 index 0000000..d47b06f --- /dev/null +++ b/initializers/circuits.yml @@ -0,0 +1,7 @@ +# - cid: Circuit_ID-1 +# provider: Provider1 +# type: Internet +# tenant: tenant1 +# - cid: Circuit_ID-2 +# provider: Provider2 +# type: MPLS diff --git a/initializers/cluster_groups.yml b/initializers/cluster_groups.yml new file mode 100644 index 0000000..a611afa --- /dev/null +++ b/initializers/cluster_groups.yml @@ -0,0 +1,4 @@ +# - name: Group 1 +# slug: group-1 +# - name: Group 2 +# slug: group-2 diff --git a/initializers/clusters.yml b/initializers/clusters.yml index 1b1aca0..4e15477 100644 --- a/initializers/clusters.yml +++ b/initializers/clusters.yml @@ -1,5 +1,7 @@ # - name: cluster1 # type: Hyper-V +# group: Group 1 +# tenant: tenant1 # - name: cluster2 # type: Hyper-V # site: SING 1 diff --git a/initializers/power_feeds.yml b/initializers/power_feeds.yml new file mode 100644 index 0000000..7ef5891 --- /dev/null +++ b/initializers/power_feeds.yml @@ -0,0 +1,14 @@ +# - name: power feed 1 +# power_panel: power panel AMS 1 +# voltage: 208 +# amperage: 50 +# max_utilization: 80 +# phase: Single phase +# rack: rack-01 +# - name: power feed 2 +# power_panel: power panel SING 1 +# voltage: 208 +# amperage: 50 +# max_utilization: 80 +# phase: Three-phase +# rack: rack-03 diff --git a/initializers/power_panels.yml b/initializers/power_panels.yml new file mode 100644 index 0000000..1205851 --- /dev/null +++ b/initializers/power_panels.yml @@ -0,0 +1,5 @@ +# - name: power panel AMS 1 +# site: AMS 1 +# - name: power panel SING 1 +# site: SING 1 +# rack_group: cage 101 diff --git a/initializers/providers.yml b/initializers/providers.yml new file mode 100644 index 0000000..ca2ca1a --- /dev/null +++ b/initializers/providers.yml @@ -0,0 +1,6 @@ +# - name: Provider1 +# slug: provider1 +# asn: 121 +# - name: Provider2 +# slug: provider2 +# asn: 122 diff --git a/initializers/route_targets.yml b/initializers/route_targets.yml new file mode 100644 index 0000000..2ef1406 --- /dev/null +++ b/initializers/route_targets.yml @@ -0,0 +1,3 @@ +# - name: 65000:1001 +# tenant: tenant1 +# - name: 65000:1002 diff --git a/initializers/secret_roles.yml b/initializers/secret_roles.yml new file mode 100644 index 0000000..239906a --- /dev/null +++ b/initializers/secret_roles.yml @@ -0,0 +1,4 @@ +# - name: Super Secret Passwords +# slug: super-secret +# - name: SNMP Communities +# slug: snmp diff --git a/initializers/services.yml b/initializers/services.yml new file mode 100644 index 0000000..8d4441c --- /dev/null +++ b/initializers/services.yml @@ -0,0 +1,15 @@ +# - name: DNS +# protocol: TCP +# ports: +# - 53 +# virtual_machine: virtual machine 1 +# - name: DNS +# protocol: UDP +# ports: +# - 53 +# virtual_machine: virtual machine 1 +# - name: MISC +# protocol: UDP +# ports: +# - 4000 +# device: server01 diff --git a/initializers/tags.yml b/initializers/tags.yml new file mode 100644 index 0000000..1cbe3be --- /dev/null +++ b/initializers/tags.yml @@ -0,0 +1,12 @@ +# - name: Tag 1 +# slug: tag-1 +# color: Pink +# - name: Tag 2 +# slug: tag-2 +# color: Cyan +# - name: Tag 3 +# slug: tag-3 +# color: Grey +# - name: Tag 4 +# slug: tag-4 +# color: Teal diff --git a/startup_scripts/020_tags.py b/startup_scripts/020_tags.py new file mode 100644 index 0000000..2939e75 --- /dev/null +++ b/startup_scripts/020_tags.py @@ -0,0 +1,23 @@ +from extras.models import Tag +from utilities.choices import ColorChoices + +from startup_script_utils import load_yaml +import sys + +tags = load_yaml('/opt/netbox/initializers/tags.yml') + +if tags is None: + sys.exit() + +for params in tags: + if 'color' in params: + color = params.pop('color') + + for color_tpl in ColorChoices: + if color in color_tpl: + params['color'] = color_tpl[0] + + tag, created = Tag.objects.get_or_create(**params) + + if created: + print("🎨 Created Tag", tag.name) diff --git a/startup_scripts/160_aggregates.py b/startup_scripts/160_aggregates.py index cc1d220..40aea1b 100644 --- a/startup_scripts/160_aggregates.py +++ b/startup_scripts/160_aggregates.py @@ -3,6 +3,7 @@ import sys from ipam.models import Aggregate, RIR from netaddr import IPNetwork from startup_script_utils import * +from tenancy.models import Tenant aggregates = load_yaml('/opt/netbox/initializers/aggregates.yml') @@ -13,6 +14,10 @@ required_assocs = { 'rir': (RIR, 'name') } +optional_assocs = { + 'tenant': (Tenant, 'name'), +} + for params in aggregates: custom_field_data = pop_custom_fields(params) @@ -24,6 +29,13 @@ for params in aggregates: 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) + aggregate, created = Aggregate.objects.get_or_create(**params) if created: diff --git a/startup_scripts/165_cluster_groups.py b/startup_scripts/165_cluster_groups.py new file mode 100644 index 0000000..b52518a --- /dev/null +++ b/startup_scripts/165_cluster_groups.py @@ -0,0 +1,14 @@ +from virtualization.models import ClusterGroup +from startup_script_utils import load_yaml +import sys + +cluster_groups = load_yaml('/opt/netbox/initializers/cluster_groups.yml') + +if cluster_groups is None: + sys.exit() + +for params in cluster_groups: + cluster_group, created = ClusterGroup.objects.get_or_create(**params) + + if created: + print("🗄️ Created Cluster Group", cluster_group.name) diff --git a/startup_scripts/170_clusters.py b/startup_scripts/170_clusters.py index ffd965e..85e462f 100644 --- a/startup_scripts/170_clusters.py +++ b/startup_scripts/170_clusters.py @@ -3,6 +3,7 @@ import sys from dcim.models import Site from startup_script_utils import * from virtualization.models import Cluster, ClusterType, ClusterGroup +from tenancy.models import Tenant clusters = load_yaml('/opt/netbox/initializers/clusters.yml') @@ -15,7 +16,8 @@ required_assocs = { optional_assocs = { 'site': (Site, 'name'), - 'group': (ClusterGroup, 'name') + 'group': (ClusterGroup, 'name'), + 'tenant': (Tenant, 'name') } for params in clusters: diff --git a/startup_scripts/175_route_targets.py b/startup_scripts/175_route_targets.py new file mode 100644 index 0000000..efdaba6 --- /dev/null +++ b/startup_scripts/175_route_targets.py @@ -0,0 +1,31 @@ +import sys + +from ipam.models import RouteTarget +from startup_script_utils import * +from tenancy.models import Tenant + +route_targets = load_yaml('/opt/netbox/initializers/route_targets.yml') + +if route_targets is None: + sys.exit() + +optional_assocs = { + 'tenant': (Tenant, 'name') +} + +for params in route_targets: + custom_field_data = pop_custom_fields(params) + + 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) + + route_target, created = RouteTarget.objects.get_or_create(**params) + + if created: + set_custom_fields_values(route_target, custom_field_data) + + print("🎯 Created Route Target", route_target.name) diff --git a/startup_scripts/280_providers.py b/startup_scripts/280_providers.py new file mode 100644 index 0000000..e927dda --- /dev/null +++ b/startup_scripts/280_providers.py @@ -0,0 +1,18 @@ +from circuits.models import Provider +from startup_script_utils import * +import sys + +providers = load_yaml('/opt/netbox/initializers/providers.yml') + +if providers is None: + sys.exit() + +for params in providers: + custom_field_data = pop_custom_fields(params) + + provider, created = Provider.objects.get_or_create(**params) + + if created: + set_custom_fields_values(provider, custom_field_data) + + print("📡 Created provider", provider.name) diff --git a/startup_scripts/290_circuit_types.py b/startup_scripts/290_circuit_types.py new file mode 100644 index 0000000..441f059 --- /dev/null +++ b/startup_scripts/290_circuit_types.py @@ -0,0 +1,18 @@ +from circuits.models import CircuitType +from startup_script_utils import * +import sys + +circuit_types = load_yaml('/opt/netbox/initializers/circuit_types.yml') + +if circuit_types is None: + sys.exit() + +for params in circuit_types: + custom_field_data = pop_custom_fields(params) + + circuit_type, created = CircuitType.objects.get_or_create(**params) + + if created: + set_custom_fields_values(circuit_type, custom_field_data) + + print("⚡ Created Circuit Type", circuit_type.name) diff --git a/startup_scripts/300_circuits.py b/startup_scripts/300_circuits.py new file mode 100644 index 0000000..d53272c --- /dev/null +++ b/startup_scripts/300_circuits.py @@ -0,0 +1,41 @@ +from circuits.models import Circuit, Provider, CircuitType +from tenancy.models import Tenant +from startup_script_utils import * +import sys + +circuits = load_yaml('/opt/netbox/initializers/circuits.yml') + +if circuits is None: + sys.exit() + +required_assocs = { + 'provider': (Provider, 'name'), + 'type': (CircuitType, 'name') +} + +optional_assocs = { + 'tenant': (Tenant, 'name') +} + +for params in circuits: + custom_field_data = pop_custom_fields(params) + + 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) + + circuit, created = Circuit.objects.get_or_create(**params) + + if created: + set_custom_fields_values(circuit, custom_field_data) + + print("⚡ Created Circuit", circuit.cid) diff --git a/startup_scripts/310_secret_roles.py b/startup_scripts/310_secret_roles.py new file mode 100644 index 0000000..9b0e816 --- /dev/null +++ b/startup_scripts/310_secret_roles.py @@ -0,0 +1,14 @@ +from secrets.models import SecretRole +from startup_script_utils import load_yaml +import sys + +secret_roles = load_yaml('/opt/netbox/initializers/secret_roles.yml') + +if secret_roles is None: + sys.exit() + +for params in secret_roles: + secret_role, created = SecretRole.objects.get_or_create(**params) + + if created: + print("🔑 Created Secret Role", secret_role.name) diff --git a/startup_scripts/320_services.py b/startup_scripts/320_services.py new file mode 100644 index 0000000..800f215 --- /dev/null +++ b/startup_scripts/320_services.py @@ -0,0 +1,29 @@ +from ipam.models import Service +from dcim.models import Device +from virtualization.models import VirtualMachine +from startup_script_utils import load_yaml +import sys + +services = load_yaml('/opt/netbox/initializers/services.yml') + +if services is None: + sys.exit() + +optional_assocs = { + 'device': (Device, 'name'), + 'virtual_machine': (VirtualMachine, 'name') +} + +for params in services: + + 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) + + service, created = Service.objects.get_or_create(**params) + + if created: + print("🧰 Created Service", service.name) diff --git a/startup_scripts/330_power_panels.py b/startup_scripts/330_power_panels.py new file mode 100644 index 0000000..3057a96 --- /dev/null +++ b/startup_scripts/330_power_panels.py @@ -0,0 +1,41 @@ +import sys + +from dcim.models import Site, RackGroup, PowerPanel +from startup_script_utils import * +from tenancy.models import Tenant + +power_panels = load_yaml('/opt/netbox/initializers/power_panels.yml') + +if power_panels is None: + sys.exit() + +required_assocs = { + 'site': (Site, 'name') +} + +optional_assocs = { + 'rack_group': (RackGroup, 'name') +} + +for params in power_panels: + custom_field_data = pop_custom_fields(params) + + 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) + + power_panel, created = PowerPanel.objects.get_or_create(**params) + + if created: + set_custom_fields_values(power_panel, custom_field_data) + + print("⚡ Created Power Panel", power_panel.site, power_panel.name) diff --git a/startup_scripts/340_power_feeds.py b/startup_scripts/340_power_feeds.py new file mode 100644 index 0000000..fc7ee38 --- /dev/null +++ b/startup_scripts/340_power_feeds.py @@ -0,0 +1,41 @@ +import sys + +from dcim.models import Rack, PowerPanel, PowerFeed +from startup_script_utils import * +from tenancy.models import Tenant + +power_feeds = load_yaml('/opt/netbox/initializers/power_feeds.yml') + +if power_feeds is None: + sys.exit() + +required_assocs = { + 'power_panel': (PowerPanel, 'name') +} + +optional_assocs = { + 'rack': (Rack, 'name') +} + +for params in power_feeds: + custom_field_data = pop_custom_fields(params) + + 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) + + power_feed, created = PowerFeed.objects.get_or_create(**params) + + if created: + set_custom_fields_values(power_feed, custom_field_data) + + print("⚡ Created Power Feed", power_feed.name) diff --git a/startup_scripts/__main__.py b/startup_scripts/__main__.py index dc4cdc9..f85a1de 100644 --- a/startup_scripts/__main__.py +++ b/startup_scripts/__main__.py @@ -20,7 +20,7 @@ with scandir(this_dir) as it: if not f.name.endswith('.py'): continue - print(f"▶️ Running the startup script {f.path}") + print(f"▶️ Running the startup script {f.path}") try: runpy.run_path(f.path) except SystemExit as e: