Skip to content

Commit

Permalink
Merge pull request #729 from kr3ator/feature/separate_default_params
Browse files Browse the repository at this point in the history
feat: Make startup scripts idempotent
  • Loading branch information
tobiasge authored Apr 8, 2022
2 parents a6eb4fe + a63af05 commit 5c4a1cc
Show file tree
Hide file tree
Showing 42 changed files with 275 additions and 95 deletions.
1 change: 1 addition & 0 deletions initializers/users.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# password: reader
# writer:
# password: writer
# api_token: "" # a token is generated automatically unless the value is explicity set to empty
# jdoe:
# first_name: John
# last_name: Doe
Expand Down
20 changes: 12 additions & 8 deletions startup_scripts/000_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@
sys.exit()

for username, user_details in users.items():
if not User.objects.filter(username=username):
user = User.objects.create_user(
username=username,
password=user_details.get("password", 0) or User.objects.make_random_password(),
)

print("👤 Created user", username)
api_token = user_details.pop("api_token", Token.generate_key())
password = user_details.pop("password", User.objects.make_random_password())

user, created = User.objects.get_or_create(username=username, defaults=user_details)

if created:
user.set_password(password)
user.save()

if user_details.get("api_token", 0):
Token.objects.create(user=user, key=user_details["api_token"])
if api_token:
Token.objects.get_or_create(user=user, key=api_token)

print("👤 Created user", username)
8 changes: 5 additions & 3 deletions startup_scripts/020_object_permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@

object_permission, created = ObjectPermission.objects.get_or_create(
name=permission_name,
description=permission_details["description"],
enabled=permission_details["enabled"],
actions=permission_details["actions"],
defaults={
"description": permission_details["description"],
"enabled": permission_details["enabled"],
"actions": permission_details["actions"],
},
)

if permission_details.get("object_types", 0):
Expand Down
6 changes: 4 additions & 2 deletions startup_scripts/040_custom_links.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from django.contrib.contenttypes.models import ContentType
from extras.models import CustomLink
from startup_script_utils import load_yaml
from startup_script_utils import load_yaml, split_params

custom_links = load_yaml("/opt/netbox/initializers/custom_links.yml")

Expand All @@ -28,6 +28,8 @@ def get_content_type_id(content_type):
)
continue

custom_link, created = CustomLink.objects.get_or_create(**link)
matching_params, defaults = split_params(link)
custom_link, created = CustomLink.objects.get_or_create(**matching_params, defaults=defaults)

if created:
print("🔗 Created Custom Link '{0}'".format(custom_link.name))
5 changes: 3 additions & 2 deletions startup_scripts/050_tags.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import sys

from extras.models import Tag
from startup_script_utils import load_yaml
from startup_script_utils import load_yaml, split_params
from utilities.choices import ColorChoices

tags = load_yaml("/opt/netbox/initializers/tags.yml")
Expand All @@ -17,7 +17,8 @@
if color in color_tpl:
params["color"] = color_tpl[0]

tag, created = Tag.objects.get_or_create(**params)
matching_params, defaults = split_params(params)
tag, created = Tag.objects.get_or_create(**matching_params, defaults=defaults)

if created:
print("🎨 Created Tag", tag.name)
6 changes: 4 additions & 2 deletions startup_scripts/060_webhooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from django.contrib.contenttypes.models import ContentType
from extras.models import Webhook
from startup_script_utils import load_yaml
from startup_script_utils import load_yaml, split_params

webhooks = load_yaml("/opt/netbox/initializers/webhooks.yml")

Expand All @@ -26,7 +26,9 @@ def get_content_type_id(hook_name, content_type):
except ContentType.DoesNotExist:
continue

webhook, created = Webhook.objects.get_or_create(**hook)
matching_params, defaults = split_params(hook)
webhook, created = Webhook.objects.get_or_create(**matching_params, defaults=defaults)

if created:
webhook.content_types.set(obj_type_ids)
webhook.save()
Expand Down
5 changes: 3 additions & 2 deletions startup_scripts/070_tenant_groups.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import sys

from startup_script_utils import load_yaml
from startup_script_utils import load_yaml, split_params
from tenancy.models import TenantGroup

tenant_groups = load_yaml("/opt/netbox/initializers/tenant_groups.yml")
Expand All @@ -9,7 +9,8 @@
sys.exit()

for params in tenant_groups:
tenant_group, created = TenantGroup.objects.get_or_create(**params)
matching_params, defaults = split_params(params)
tenant_group, created = TenantGroup.objects.get_or_create(**matching_params, defaults=defaults)

if created:
print("🔳 Created Tenant Group", tenant_group.name)
10 changes: 8 additions & 2 deletions startup_scripts/080_tenants.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import sys

from startup_script_utils import load_yaml, pop_custom_fields, set_custom_fields_values
from startup_script_utils import (
load_yaml,
pop_custom_fields,
set_custom_fields_values,
split_params,
)
from tenancy.models import Tenant, TenantGroup

tenants = load_yaml("/opt/netbox/initializers/tenants.yml")
Expand All @@ -20,7 +25,8 @@

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

tenant, created = Tenant.objects.get_or_create(**params)
matching_params, defaults = split_params(params)
tenant, created = Tenant.objects.get_or_create(**matching_params, defaults=defaults)

if created:
print("👩‍💻 Created Tenant", tenant.name)
Expand Down
5 changes: 3 additions & 2 deletions startup_scripts/090_regions.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import sys

from dcim.models import Region
from startup_script_utils import load_yaml
from startup_script_utils import load_yaml, split_params

regions = load_yaml("/opt/netbox/initializers/regions.yml")

Expand All @@ -19,7 +19,8 @@

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

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

if created:
print("🌐 Created region", region.name)
10 changes: 8 additions & 2 deletions startup_scripts/110_sites.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import sys

from dcim.models import Region, Site
from startup_script_utils import load_yaml, pop_custom_fields, set_custom_fields_values
from startup_script_utils import (
load_yaml,
pop_custom_fields,
set_custom_fields_values,
split_params,
)
from tenancy.models import Tenant

sites = load_yaml("/opt/netbox/initializers/sites.yml")
Expand All @@ -21,7 +26,8 @@

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

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

if created:
print("📍 Created site", site.name)
Expand Down
6 changes: 4 additions & 2 deletions startup_scripts/120_locations.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import sys

from dcim.models import Location, Site
from startup_script_utils import load_yaml
from startup_script_utils import load_yaml, split_params

rack_groups = load_yaml("/opt/netbox/initializers/locations.yml")

if rack_groups is None:
sys.exit()

match_params = ["name", "slug", "site"]
required_assocs = {"site": (Site, "name")}

for params in rack_groups:
Expand All @@ -17,7 +18,8 @@
query = {field: params.pop(assoc)}
params[assoc] = model.objects.get(**query)

location, created = Location.objects.get_or_create(**params)
matching_params, defaults = split_params(params, match_params)
location, created = Location.objects.get_or_create(**matching_params, defaults=defaults)

if created:
print("🎨 Created location", location.name)
5 changes: 3 additions & 2 deletions startup_scripts/130_rack_roles.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import sys

from dcim.models import RackRole
from startup_script_utils import load_yaml
from startup_script_utils import load_yaml, split_params
from utilities.choices import ColorChoices

rack_roles = load_yaml("/opt/netbox/initializers/rack_roles.yml")
Expand All @@ -17,7 +17,8 @@
if color in color_tpl:
params["color"] = color_tpl[0]

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

if created:
print("🎨 Created rack role", rack_role.name)
12 changes: 9 additions & 3 deletions startup_scripts/140_racks.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
import sys

from dcim.models import Location, Rack, RackRole, Site
from startup_script_utils import load_yaml, pop_custom_fields, set_custom_fields_values
from startup_script_utils import (
load_yaml,
pop_custom_fields,
set_custom_fields_values,
split_params,
)
from tenancy.models import Tenant

racks = load_yaml("/opt/netbox/initializers/racks.yml")

if racks is None:
sys.exit()

match_params = ["name", "site"]
required_assocs = {"site": (Site, "name")}

optional_assocs = {
"role": (RackRole, "name"),
"tenant": (Tenant, "name"),
Expand All @@ -33,7 +38,8 @@

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

rack, created = Rack.objects.get_or_create(**params)
matching_params, defaults = split_params(params, match_params)
rack, created = Rack.objects.get_or_create(**matching_params, defaults=defaults)

if created:
print("🔳 Created rack", rack.site, rack.name)
Expand Down
12 changes: 9 additions & 3 deletions startup_scripts/150_power_panels.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
import sys

from dcim.models import Location, PowerPanel, Site
from startup_script_utils import load_yaml, pop_custom_fields, set_custom_fields_values
from startup_script_utils import (
load_yaml,
pop_custom_fields,
set_custom_fields_values,
split_params,
)

power_panels = load_yaml("/opt/netbox/initializers/power_panels.yml")

if power_panels is None:
sys.exit()

match_params = ["name", "site"]
required_assocs = {"site": (Site, "name")}

optional_assocs = {"location": (Location, "name")}

for params in power_panels:
Expand All @@ -28,7 +33,8 @@

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

power_panel, created = PowerPanel.objects.get_or_create(**params)
matching_params, defaults = split_params(params, match_params)
power_panel, created = PowerPanel.objects.get_or_create(**matching_params, defaults=defaults)

if created:
print("⚡ Created Power Panel", power_panel.site, power_panel.name)
Expand Down
12 changes: 9 additions & 3 deletions startup_scripts/160_power_feeds.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
import sys

from dcim.models import PowerFeed, PowerPanel, Rack
from startup_script_utils import load_yaml, pop_custom_fields, set_custom_fields_values
from startup_script_utils import (
load_yaml,
pop_custom_fields,
set_custom_fields_values,
split_params,
)

power_feeds = load_yaml("/opt/netbox/initializers/power_feeds.yml")

if power_feeds is None:
sys.exit()

match_params = ["name", "power_panel"]
required_assocs = {"power_panel": (PowerPanel, "name")}

optional_assocs = {"rack": (Rack, "name")}

for params in power_feeds:
Expand All @@ -28,7 +33,8 @@

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

power_feed, created = PowerFeed.objects.get_or_create(**params)
matching_params, defaults = split_params(params, match_params)
power_feed, created = PowerFeed.objects.get_or_create(**matching_params, defaults=defaults)

if created:
print("⚡ Created Power Feed", power_feed.name)
Expand Down
5 changes: 3 additions & 2 deletions startup_scripts/170_manufacturers.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import sys

from dcim.models import Manufacturer
from startup_script_utils import load_yaml
from startup_script_utils import load_yaml, split_params

manufacturers = load_yaml("/opt/netbox/initializers/manufacturers.yml")

if manufacturers is None:
sys.exit()

for params in manufacturers:
manufacturer, created = Manufacturer.objects.get_or_create(**params)
matching_params, defaults = split_params(params)
manufacturer, created = Manufacturer.objects.get_or_create(**matching_params, defaults=defaults)

if created:
print("🏭 Created Manufacturer", manufacturer.name)
5 changes: 3 additions & 2 deletions startup_scripts/180_device_roles.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import sys

from dcim.models import DeviceRole
from startup_script_utils import load_yaml
from startup_script_utils import load_yaml, split_params
from utilities.choices import ColorChoices

device_roles = load_yaml("/opt/netbox/initializers/device_roles.yml")
Expand All @@ -18,7 +18,8 @@
if color in color_tpl:
params["color"] = color_tpl[0]

device_role, created = DeviceRole.objects.get_or_create(**params)
matching_params, defaults = split_params(params)
device_role, created = DeviceRole.objects.get_or_create(**matching_params, defaults=defaults)

if created:
print("🎨 Created device role", device_role.name)
Loading

0 comments on commit 5c4a1cc

Please sign in to comment.