clearer names for actions, and infer actions better
[monitor.git] / keys / setup.py
1 #!/usr/bin/python
2
3 # Register the monitor user with your instance of PLC.
4
5 # NOTE: Need some way of collecting information about where the target PLC is
6 # located.  Clearly this script needs to be run by someone with ADMIN
7 # privilges in order to setup an ssh key, add a user, and upload the key to
8 # the user, make the user a member of the PLC site, etc.
9
10 import sys
11 import os
12 try:
13         import auth
14 except:
15         print """
16         Create a file named: 'auth.py' which contains two variables:
17
18 plc = "https://yourplc.hostname.org/PLCAPI/"
19 auth = {'Username': 'root@localhost.localdomain',
20         'AuthMethod': 'password',
21         'AuthString': 'root'}
22
23 """
24         sys.exit(1)
25         
26 import plc
27
28 def filevalue(filename):
29         f = open(filename, 'r')
30         ret = f.read()
31         f.close()
32         return ret
33
34 api = plc.PLC(auth.auth, auth.plc)
35
36 # Add user : 'Site', 'Assistant', 'monitor@planet-lab.org'
37 person = { 'first_name': 'Site', 
38                         'last_name' : 'Assistant',
39                         'email' : 'monitor@planet-lab.org',
40                         'url' : 'http://monitor.planet-lab.org', }
41 ret = api.GetPersons(person['email'], ['person_id'])
42 if len(ret) == 0:
43         # entry does not exist in Database, so Add it.
44         id = api.AddPerson(person)
45 else:
46         # use existing entry.
47         id = ret[0]['person_id']
48
49 # Generate ssh key.
50 if not os.path.exists("%s.pub" % keyname):
51         keyname = "monitor_rsa"
52         print "Creating ssh key pair for %s" % person['email']
53         os.system("ssh-keygen -t rsa -b 2048 -f %s < /dev/null" % keyname)
54
55 if not os.path.exists("%s.pub" % keyname):
56         print "Error generating public/private key pair."
57         print "Please try running the command manually."
58         print "ssh-keygen -t rsa -b 2048 -f %s < /dev/null" % keyname
59         sys.exit(1)
60
61 # Upload key.
62 keys = api.GetKeys({ 'person_id' : id })
63 if len(keys) == 0:      
64         key_id = api.AddPersonKey(id, { 'key_type' : 'ssh', 
65                                                                         'key' : filevalue("%s.pub" % keyname)} )
66 else:
67         key_id = keys[0]['key_id']
68
69 # Copy private key into the directory from which the monitor scripts will be
70 #       run/activated.
71 ### os.system("cp %s /usr/local/monitor/keys" % keyname)
72
73 # Add cron entries to periodically poll nodes, and PCUs.
74 ### os.system("crontab ---")