2018-12-19 07:25:58 -06:00
|
|
|
import sys
|
2018-02-16 03:25:26 -06:00
|
|
|
|
2020-12-14 15:09:08 -06:00
|
|
|
from django.contrib.auth.models import User
|
2021-04-20 16:47:49 -05:00
|
|
|
from startup_script_utils import load_yaml
|
2020-02-12 01:34:01 -06:00
|
|
|
from users.models import Token
|
2020-02-05 08:31:01 -06:00
|
|
|
|
2021-02-08 04:59:33 -06:00
|
|
|
users = load_yaml("/opt/netbox/initializers/users.yml")
|
2020-02-05 08:31:01 -06:00
|
|
|
if users is None:
|
2021-02-08 04:59:33 -06:00
|
|
|
sys.exit()
|
2018-12-19 07:25:58 -06:00
|
|
|
|
2020-02-05 08:31:01 -06:00
|
|
|
for username, user_details in users.items():
|
|
|
|
|
2022-04-05 01:34:08 -05:00
|
|
|
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()
|
2020-02-05 08:31:01 -06:00
|
|
|
|
2022-04-05 01:34:08 -05:00
|
|
|
if api_token:
|
|
|
|
Token.objects.get_or_create(user=user, key=api_token)
|
|
|
|
|
|
|
|
print("👤 Created user", username)
|