This is a template script for adding the 'Site Assistant' user into the myPLC
[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 auth
11 import plc
12 import sys
13 import os
14
15 def filevalue(filename):
16         f = open(filename, 'r')
17         ret = f.read()
18         f.close()
19         return ret
20
21 api = plc.PLC(auth.auth, auth.plc)
22
23 # Add user : 'Site', 'Assistant', 'monitor@planet-lab.org'
24 person = { 'first_name': 'Site', 
25                         'last_name' : 'Assistant',
26                         'email' : 'monitor@planet-lab.org',
27                         'url' : 'http://monitor.planet-lab.org', }
28 ret = api.GetPersons(person['email'], ['person_id'])
29 if len(ret) == 0:
30         # entry does not exist in Database, so Add it.
31         id = api.AddPerson(person)
32 else:
33         # use existing entry.
34         id = ret[0]['person_id']
35
36 # Generate ssh key.
37 if not os.path.exists("%s.pub" % keyname):
38         keyname = "monitor_rsa"
39         print "Creating ssh key pair for %s" % person['email']
40         os.system("ssh-keygen -t rsa -b 2048 -f %s < /dev/null" % keyname)
41
42 if not os.path.exists("%s.pub" % keyname):
43         print "Error generating public/private key pair."
44         print "Please try running the command manually."
45         print "ssh-keygen -t rsa -b 2048 -f %s < /dev/null" % keyname
46         sys.exit(1)
47
48 # Upload key.
49 keys = api.GetKeys({ 'person_id' : id })
50 if len(keys) == 0:      
51         key_id = api.AddPersonKey(id, { 'key_type' : 'ssh', 
52                                                                         'key' : filevalue("%s.pub" % keyname)} )
53 else:
54         key_id = keys[0]['key_id']
55
56 # Copy private key into the directory from which the monitor scripts will be
57 #       run/activated.
58 ### os.system("cp %s /usr/local/monitor/keys" % keyname)
59
60 # Add cron entries to periodically poll nodes, and PCUs.
61 ### os.system("crontab ---")