-
Notifications
You must be signed in to change notification settings - Fork 16
/
dbinit
executable file
·107 lines (93 loc) · 3.28 KB
/
dbinit
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/env python
import os
from time import time
import getpass
from mist.api.clouds.models import DockerCloud, OtherCloud
from mist.api.keys.models import SSHKey
from mist.api.users.models import User, Organization
from mist.api import config
def dbinit():
"""Set up a dev account in local fullstack container."""
# Add user
email = raw_input("email: ")
if not email:
return
password = getpass.getpass("password:")
try:
user = User.objects.get(email=email)
except:
user = User(email=email)
user.set_password(password)
user.role = 'Admin'
user.status = 'confirmed'
user.save()
# Add org
org = Organization.objects(members=user).first()
if org is None:
org = Organization(name=user.email)
org.add_member_to_team('Owners', user)
org.save()
org.insights_enabled = True
org.save()
# Add local docker host
cloud = None
try:
cloud = DockerCloud.objects.get(owner=org, name='Mist Docker')
try:
if cloud.host != 'socat' or cloud.port !=2375:
cloud.ctl.update(host='socat', port=2375)
except Exception as exc:
print "Error updating docker cloud: %r" % exc
except DockerCloud.DoesNotExist:
try:
cloud = DockerCloud.add(owner=org, name='Mist Docker',
host='socat', port=2375)
except Exception as exc:
print "Error adding docker cloud: %r" % exc
if cloud is not None:
try:
cloud.ctl.set_polling_interval(600)
except Exception as exc:
print "Error setting polling interval on docker cloud: %r" % exc
# Add ssh key
with open('/keys/id_rsa', 'r') as fobj:
priv = fobj.read()
try:
key = SSHKey.objects.get(owner=org, name='mist key')
if key.private != priv:
try:
key.private = priv
key.save()
except Exception as exc:
print "Error updating key: %r" % exc
except SSHKey.DoesNotExist:
key = SSHKey.add(org, 'mist key', private=priv)
# Add container as baremetal/other server
try:
cid = OtherCloud.objects.get(owner=org, name='Mist Debugger').id
except OtherCloud.DoesNotExist:
cid = ''
try:
cloud = OtherCloud.add(owner=org, name='Mist Debugger', id=cid,
host='debugger', ssh_user='root',
ssh_port=22, ssh_key=key.id)
cloud.ctl.set_polling_interval(600)
except Exception as exc:
print "Error adding Other cloud: %r" % exc
cloud = None
try:
cloud = OtherCloud.objects.get(owner=org, name='Mist Debugger')
except OtherCloud.DoesNotExist:
try:
cloud = OtherCloud.add(owner=org, name='Mist Debugger', id=cid,
host='debugger', ssh_user='root',
ssh_port=22, ssh_key=key.id)
except Exception as exc:
print "Error adding other cloud: %r" % exc
if cloud is not None:
try:
cloud.ctl.set_polling_interval(600)
except Exception as exc:
print "Error setting polling interval on other cloud: %r" % exc
if __name__ == '__main__':
dbinit()