- if name not in _name_worker: _name_worker[name] = Worker(name)
- return _name_worker[name]
- finally: _name_worker_lock.release()
-
-
-def install_ssh_keys(rec):
- """Write <rec['ssh_keys']> to <rec['name']>'s authorized_keys file."""
- dot_ssh = '/home/%s/.ssh' % rec['name']
- def do_installation():
- if not os.access(dot_ssh, os.F_OK): os.mkdir(dot_ssh)
- tools.write_file(dot_ssh + '/authorized_keys',
- lambda thefile: thefile.write(rec['ssh_keys']))
- logger.log('%s: installing ssh keys' % rec['name'])
- tools.fork_as(rec['name'], do_installation)
-
+ if name not in name_worker: name_worker[name] = Worker(name)
+ return name_worker[name]
+ finally: name_worker_lock.release()
+
+
+class Account:
+ def __init__(self, rec):
+ logger.verbose('Initing account %s'%rec['name'])
+ self.name = rec['name']
+ self.keys = ''
+ self.initscriptchanged = False
+ self.configure(rec)
+
+ @staticmethod
+ def create(name, vref = None): abstract
+ @staticmethod
+ def destroy(name): abstract
+
+ def configure(self, rec):
+ """Write <rec['keys']> to my authorized_keys file."""
+ logger.verbose('%s: in accounts:configure'%self.name)
+ new_keys = rec['keys']
+ if new_keys != self.keys:
+ # get the unix account info
+ gid = grp.getgrnam("slices")[2]
+ pw_info = pwd.getpwnam(self.name)
+ uid = pw_info[2]
+ pw_dir = pw_info[5]
+
+ # write out authorized_keys file and conditionally create
+ # the .ssh subdir if need be.
+ dot_ssh = os.path.join(pw_dir,'.ssh')
+ if not os.path.isdir(dot_ssh):
+ if not os.path.isdir(pw_dir):
+ logger.verbose('WARNING: homedir %s does not exist for %s!'%(pw_dir,self.name))
+ os.mkdir(pw_dir)
+ os.chown(pw_dir, uid, gid)
+ os.mkdir(dot_ssh)
+
+ auth_keys = os.path.join(dot_ssh,'authorized_keys')
+ tools.write_file(auth_keys, lambda f: f.write(new_keys))
+
+ # set access permissions and ownership properly
+ os.chmod(dot_ssh, 0700)
+ os.chown(dot_ssh, uid, gid)
+ os.chmod(auth_keys, 0600)
+ os.chown(auth_keys, uid, gid)
+
+ # set self.keys to new_keys only when all of the above ops succeed
+ self.keys = new_keys
+
+ logger.log('%s: installed ssh keys' % self.name)
+
+ def start(self, delay=0): pass
+ def stop(self): pass
+ def is_running(self): pass