changes for 3.0
[monitor.git] / prep_power_users.py
1 #!/usr/bin/python
2
3 import plc
4 import sys
5 import time
6
7 email_list = [ 'monitor@planet-lab.org', 
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 import config
19 api   = plc.PLC(config.API_AUTH, config.API_SERVER)
20 api06 = plc.PLC(config.API_AUTH06, config.API_SERVER06)
21
22
23 # add planetlab-15.cs.princeton.edu, and use the key on the CD.
24 id = api06.AddNode(1, {'boot_state': 'rins', 'model': 'Dell Optiplex',
25                                                   'hostname' : 'planetlab-15.cs.princeton.edu',
26                                                   'version' : '3.3'})
27 api06.AddNodeNetwork(id, {'ip': '128.112.139.39',
28                                                                   'type' : 'ipv4',
29                                                                   'is_primary' : True,
30                                                                   'method' : 'dhcp', })
31 api06.UpdateNode(id, {'key': "wptNagk8SgRxzN1lXfKMAjUYhQbOBymKnKg9Uv0LwGM"})
32
33
34 #print "adding vsys attributes"
35 #api06.AddSliceAttribute('princeton_slicestat', 'vsys', 'pl-ps')
36 #api06.AddSliceAttribute('princeton_slicestat', 'vsys', 'vtop')
37 #api06.AddSliceAttribute('pl_netflow', 'vsys', 'pfmount')
38
39 #print "preserve princeton_chopstix"
40
41 #sys.exit(1)
42
43 #attr_types   = api.GetSliceAttributeTypes()
44 #attr_types06 = api06.GetSliceAttributeTypes()
45 #attr_types06_names = [a['name'] for a in attr_types06]
46 #for type in attr_types:
47 #    if type['name'] not in attr_types06_names:
48 #        print "adding %s " % type
49 #        api06.AddSliceAttributeType(type)
50 def person_exists(user):
51     try:
52         x = api06.GetPersons({'email':user['email']})
53         if len(x) == 0:
54             return False
55         else:
56             return True
57     except:
58         return False
59
60 def site_exists(site):
61     try:
62         x = api06.GetSites({'login_base':site['login_base']})
63         if len(x) == 0:
64             return False
65         else:
66             return True
67     except:
68         return False
69
70 def slice_exists(slice):
71     try:
72         x = api06.GetSlices({'name':slice['name']})
73         if len(x) == 0:
74             return False
75         else:
76             return True
77     except:
78         return False
79
80 # Renew  slices
81 slices = api06.GetSlices()
82 for slice in slices:
83         print "Updating expiration of %s" % slice['name']
84         api06.UpdateSlice(slice['name'], {'expires': int(time.time()) + 7*24*60*60})
85
86 #sys.exit(1)
87
88 for email in email_list:
89     user = api.GetPersons({'email': email})
90     if len(user) == 0:
91         print "User not found: %s" % email
92         continue
93
94     user = user[0]
95
96     print "adding person %s " % user['email']
97     if not person_exists(user):
98         api06.AddPerson(user)
99
100         api06.UpdatePerson(email, {'enabled': True})
101
102         print "adding person keys %s " % user['email']
103         key = api.GetKeys({'person_id': user['person_id']})[0]
104         key06 = {'key': key['key'], 'key_type': key['key_type']}
105         api06.AddPersonKey(user['email'], key06)
106
107         print "updating person roles: ",
108         for role in user['roles']:
109             print "%s" % role,
110             api06.AddRoleToPerson(role, user['email'])
111             sys.stdout.flush()
112         print ""
113
114     sites = api.GetSites(user['site_ids'])
115     print "Adding sites:",
116     for site in sites:
117         if not site_exists(site):
118             print "%s" % site['login_base'],
119             api06.AddSite(site)
120             api06.AddPersonToSite(user['email'], site['login_base'])
121             sys.stdout.flush()
122     print ""
123
124     nodes = api06.GetNodes()
125     slices = api.GetSlices(user['slice_ids'])
126     for slice in slices:
127         # create slice
128         no_add = True
129         if not slice_exists(slice):
130             print "Adding slice %s" % slice['name']
131             try: 
132                 api06.AddSlice(slice)
133                 attr = api.GetSliceAttributes({'slice_attribute_id' : 
134                                             slice['slice_attribute_ids']})
135                 print "adding attributes:",
136                 added_attr = []
137                 for a in attr:
138                     print "%s" % a['name'],
139                     #if a['name'] not in added_attr:
140                     api06.AddSliceAttribute(slice['name'], a['name'], a['value'])
141                     #    added_attr.append(a['name'])
142                     sys.stdout.flush()
143                 print ""
144             except:
145                 no_add = False
146                 print "error with ", slice['name']
147
148         if no_add:
149             print "adding nodes and %s to slice %s" % (user['email'], slice['name'])
150             # add all api06 nodes to slice
151             api06.AddSliceToNodes(slice['name'], [n['hostname'] for n in nodes])
152             # add user to slice
153             api06.AddPersonToSlice(user['email'], slice['name'])