NM plugin to pull down special account keys: specifically site_admin and root
[nodemanager.git] / plugins / specialaccounts.py
1 #!/usr/bin/python -tt
2 # vim:set ts=4 sw=4 expandtab:
3 # NodeManager plugin to create special accounts
4
5 """
6 Have NM create/populate accounts/ssh keys for special persons such as root, site_admin, etc.
7
8 """
9
10 import errno
11 import os
12 import random
13 import string
14 import tempfile
15 import grp
16 import pwd
17
18 import logger
19 import tools
20
21 def start(options, conf):
22     logger.log("personkeys plugin starting up...")
23
24 def GetSlivers(plc, data, conf):
25     if 'accounts' not in data: return
26     for account in data['accounts']:
27         name = account['name']
28         new_keys = account['keys']
29
30         # look up account name, which must exist
31         pw_info = pwd.getpwnam(name)
32         uid = pw_info[2]
33         gid = pw_info[3]
34         pw_dir = pw_info[5]
35
36         # populate account's .ssh/authorized_keys file
37         dot_ssh = pw_dir + '/.ssh'
38         if not os.access(dot_ssh, os.F_OK): os.mkdir(dot_ssh)
39         auth_keys = dot_ssh + '/authorized_keys'
40         logger.log("new keys = %s" % auth_keys)
41         auth_file = file(auth_keys,"w")
42         for key in new_keys:
43                 auth_file.write(key)
44                 auth_file.write("\n")
45         auth_file.close()
46
47         # set permissions properly
48         os.chmod(dot_ssh, 0700)
49         os.chmod(auth_keys, 0600)
50         os.chown(dot_ssh, uid,gid)
51         os.chown(auth_keys, uid,gid)
52
53         logger.log('specialacounts: installed ssh keys for %s' % name)