2018-12-19 14:25:58 +01:00
|
|
|
import sys
|
2018-02-16 10:25:26 +01:00
|
|
|
|
2020-12-14 22:09:08 +01:00
|
|
|
from django.contrib.auth.models import User
|
2020-02-12 08:34:01 +01:00
|
|
|
from startup_script_utils import load_yaml, set_permissions
|
|
|
|
from users.models import Token
|
2020-02-05 15:31:01 +01:00
|
|
|
|
2021-02-08 11:59:33 +01:00
|
|
|
users = load_yaml("/opt/netbox/initializers/users.yml")
|
2020-02-05 15:31:01 +01:00
|
|
|
if users is None:
|
2021-02-08 11:59:33 +01:00
|
|
|
sys.exit()
|
2018-12-19 14:25:58 +01:00
|
|
|
|
2020-02-05 15:31:01 +01:00
|
|
|
for username, user_details in users.items():
|
2021-02-08 11:59:33 +01:00
|
|
|
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(),
|
|
|
|
)
|
2020-02-05 15:31:01 +01:00
|
|
|
|
2021-02-08 11:59:33 +01:00
|
|
|
print("👤 Created user", username)
|
2020-02-05 15:31:01 +01:00
|
|
|
|
2021-02-08 11:59:33 +01:00
|
|
|
if user_details.get("api_token", 0):
|
|
|
|
Token.objects.create(user=user, key=user_details["api_token"])
|
2020-02-05 15:31:01 +01:00
|
|
|
|
2021-02-08 11:59:33 +01:00
|
|
|
yaml_permissions = user_details.get("permissions", [])
|
|
|
|
set_permissions(user.user_permissions, yaml_permissions)
|