setup default values if they're not already set.
[monitor.git] / prep_power_users.py
1 #!/usr/bin/python
2
3 from monitor.wrapper import plc
4 import sys
5 import time
6
7 email_list = [ 'soltesz@cs.princeton.edu',
8                          # 'soltesz@cs.princeton.edu',
9                          # 'justin@cs.arizona.edu',
10              # 'bakers@cs.arizona.edu',
11              # 'jhh@cs.arizona.edu',
12                  # 'mfreed@cs.nyu.edu',
13                  # 'kyoungso@cs.princeton.edu',
14                          # 'nspring@cs.umd.edu',
15                  # 'vivek@cs.princeton.edu',
16                 ]
17
18 from monitor import config
19 api   = plc.PLC(plc.Auth(config.API_AUTH_USER,   config.API_AUTH_PASSWORD).auth,   config.API_SERVER)
20 api06 = plc.PLC(plc.Auth(config.API06_AUTH_USER, config.API06_AUTH_PASSWORD).auth, config.API06_SERVER)
21
22 # add planetlab-15.cs.princeton.edu, and use the key on the CD.
23 #id = api06.AddNode(1, {'boot_state': 'rins', 'model': 'Dell Optiplex',
24 #                                                 'hostname' : 'planetlab-15.cs.princeton.edu',
25 #                                                 'version' : '3.3'})
26 #api06.AddNodeNetwork(id, {'ip': '128.112.139.39',
27 #                                                                 'type' : 'ipv4',
28 ##                                                                'is_primary' : True,
29 #                                                                 'method' : 'dhcp', })
30 #api06.UpdateNode(id, {'key': "wptNagk8SgRxzN1lXfKMAjUYhQbOBymKnKg9Uv0LwGM"})
31 #
32
33 #print "adding vsys attributes"
34 #api06.AddSliceAttribute('princeton_slicestat', 'vsys', 'pl-ps')
35 #api06.AddSliceAttribute('princeton_slicestat', 'vsys', 'vtop')
36 #api06.AddSliceAttribute('pl_netflow', 'vsys', 'pfmount')
37
38 #print "preserve princeton_chopstix"
39
40 #sys.exit(1)
41
42 #attr_types   = api.GetSliceAttributeTypes()
43 #attr_types06 = api06.GetSliceAttributeTypes()
44 #attr_types06_names = [a['name'] for a in attr_types06]
45 #for type in attr_types:
46 #    if type['name'] not in attr_types06_names:
47 #        print "adding %s " % type
48 #        api06.AddSliceAttributeType(type)
49 def person_exists(user):
50     try:
51         x = api06.GetPersons({'email':user['email']})
52         if len(x) == 0:
53             return False
54         else:
55             return True
56     except:
57         return False
58
59 def site_exists(site):
60     try:
61         x = api06.GetSites({'login_base':site['login_base']})
62         if len(x) == 0:
63             return False
64         else:
65             return True
66     except:
67         return False
68
69 def slice_exists(slice):
70     try:
71         x = api06.GetSlices({'name':slice['name']})
72         if len(x) == 0:
73             return False
74         else:
75             return True
76     except:
77         return False
78
79 def node_exists(node):
80         x = api06.GetNodes({'hostname':node['hostname']})
81         if len(x) == 0:
82                 return False
83         else:
84                 return True
85
86 # Renew  slices
87 slices = api06.GetSlices()
88 for slice in slices:
89         print "Updating expiration of %s" % slice['name']
90         api06.UpdateSlice(slice['name'], {'expires': int(time.time()) + 7*24*60*60})
91
92 #sys.exit(1)
93
94 for email in email_list:
95     user = api.GetPersons({'email': email})
96     if len(user) == 0:
97         print "User not found: %s" % email
98         continue
99
100     user = user[0]
101
102     print "adding person %s " % user['email']
103     if not person_exists(user):
104         api06.AddPerson(user)
105
106         api06.UpdatePerson(email, {'enabled': True})
107
108         print "adding person keys %s " % user['email']
109         key = api.GetKeys({'person_id': user['person_id']})[0]
110         key06 = {'key': key['key'], 'key_type': key['key_type']}
111         api06.AddPersonKey(user['email'], key06)
112
113         print "updating person roles: ",
114         for role in user['roles']:
115             print "%s" % role,
116             api06.AddRoleToPerson(role, user['email'])
117             sys.stdout.flush()
118         print ""
119
120     sites = api.GetSites(user['site_ids'])
121     print "Adding sites:",
122     for site in sites:
123                 nodes = api.GetNodes(site['node_ids'])
124
125                 if not site_exists(site):
126                         print "%s" % site['login_base']
127                         api06.AddSite(site)
128                         api06.AddPersonToSite(user['email'], site['login_base'])
129                         sys.stdout.flush()
130
131                 for node in nodes:
132                         if not node_exists(node):
133                                 id = api06.AddNode(site['login_base'], {'boot_state': node['boot_state'],
134                                                                           'model': node['model'],
135                                                                           'hostname' : node['hostname'],
136                                                                           'version' : node['version']})
137                                 nnets = api.GetInterfaces(node['interface_ids'])
138                                 for nnet in nnets:
139                                         del nnet['interface_id']
140                                         del nnet['nodenetwork_setting_ids']
141                                         api06.AddNodeNetwork(id, nnet) 
142     print ""
143
144     nodes = api06.GetNodes()
145     slices = api.GetSlices(user['slice_ids'])
146     for slice in slices:
147         # create slice
148         no_add = True
149         if not slice_exists(slice):
150             print "Adding slice %s" % slice['name']
151             try: 
152                 api06.AddSlice(slice)
153                 attr = api.GetSliceAttributes({'slice_attribute_id' : 
154                                             slice['slice_attribute_ids']})
155                 print "adding attributes:",
156                 added_attr = []
157                 for a in attr:
158                     print "%s" % a['name'],
159                     #if a['name'] not in added_attr:
160                     api06.AddSliceAttribute(slice['name'], a['name'], a['value'])
161                     #    added_attr.append(a['name'])
162                     sys.stdout.flush()
163                 print ""
164             except:
165                 no_add = False
166                 print "error with ", slice['name']
167
168         if no_add:
169             print "adding nodes and %s to slice %s" % (user['email'], slice['name'])
170             # add all api06 nodes to slice
171             api06.AddSliceToNodes(slice['name'], [n['hostname'] for n in nodes])
172             # add user to slice
173             api06.AddPersonToSlice(user['email'], slice['name'])