-
Notifications
You must be signed in to change notification settings - Fork 16
/
init-portal-admin
executable file
·67 lines (56 loc) · 1.74 KB
/
init-portal-admin
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env python
import os
import mongoengine as me
from mist.api.users.models import User, Organization
from mist.api.auth.models import ApiToken
def main():
email = os.getenv("PORTAL_ADMIN_EMAIL")
try:
user = User.objects.get(email=email)
except me.DoesNotExist:
print(f"Will create user with email {email}")
else:
print(f"User {email} exists, exiting")
return
password = os.getenv("PORTAL_ADMIN_PASSWORD")
user = User(email=email)
print("Setting user password.")
user.set_password(password)
user.role = 'Admin'
user.status = 'confirmed'
try:
user.save()
except me.ValidationError as exc:
print(f"Failed to save user with exception: {exc!r}")
print("Exiting")
return
org_name = os.getenv("PORTAL_ADMIN_ORGANIZATION")
org = Organization(name=org_name)
org.add_member_to_team('Owners', user)
try:
org.save()
except Exception as exc:
print(f"Failed to save org with exception: {exc!r}")
user.delete()
raise
token = os.getenv("PORTAL_ADMIN_API_TOKEN")
if token:
print("Creating api token")
if len(token) != 64:
print(
f"Expected apitoken of 64 chars, found {len(token)}. "
f"Skipping")
return
api_token = ApiToken()
api_token.name = "auto-created"
api_token.orgs = [org]
api_token.set_user(user)
api_token.token = token
try:
api_token.save()
except (me.ValidationError, me.NotUniqueError) as exc:
print(f"Failed to save api token with exception {exc}")
else:
print("Created api token")
if __name__ == '__main__':
main()