2018-12-19 07:25:58 -06:00
|
|
|
import sys
|
2018-02-16 03:25:26 -06:00
|
|
|
|
2020-02-12 01:34:01 -06:00
|
|
|
from django.contrib.auth.models import Group, User
|
|
|
|
from startup_script_utils import load_yaml, set_permissions
|
|
|
|
from users.models import Token
|
2020-02-05 08:31:01 -06:00
|
|
|
|
2020-02-12 01:34:01 -06:00
|
|
|
users = load_yaml('/opt/netbox/initializers/users.yml')
|
2020-02-05 08:31:01 -06:00
|
|
|
if users is None:
|
2018-12-19 07:25:58 -06:00
|
|
|
sys.exit()
|
|
|
|
|
2020-02-05 08:31:01 -06:00
|
|
|
for username, user_details in users.items():
|
|
|
|
if not User.objects.filter(username=username):
|
|
|
|
user = User.objects.create_user(
|
|
|
|
username = username,
|
2020-08-24 04:00:48 -05:00
|
|
|
password = user_details.get('password', 0) or User.objects.make_random_password())
|
2020-02-05 08:31:01 -06:00
|
|
|
|
|
|
|
print("👤 Created user",username)
|
|
|
|
|
|
|
|
if user_details.get('api_token', 0):
|
|
|
|
Token.objects.create(user=user, key=user_details['api_token'])
|
|
|
|
|
|
|
|
yaml_permissions = user_details.get('permissions', [])
|
2020-02-12 01:34:01 -06:00
|
|
|
set_permissions(user.user_permissions, yaml_permissions)
|