Source

caktus-deployment / mallet / templates / node.py

Full commit
Colin Copeland e240ff5 
























































import os
import logging

from fabric.api import settings, env

from mallet import NodeWrapper
from mallet.bootstrap.base import PackageGroup
from mallet.utils import create_ssh, call, write, writelines, chown


logger = logging.getLogger('mallet.templates.node')


class NodeWrapper(NodeWrapper):

    def hostname(self):
        ssh_conn = create_ssh(self.node.public_ip[0], username='root')
        with settings(ssh_conn=ssh_conn):
            call(['hostname'])

    def add_client(self, name):
        ssh_conn = create_ssh(self.node.public_ip[0], username='root')
        with settings(ssh_conn=ssh_conn):
            call(['groupadd', 'clients'])
            logger.info('Creating user account')
            call(['useradd', '-G', 'clients', '-m',
                  '-s', '/bin/bash', name])
            # TODO add configuration option for database type
            logger.info('Creating database user')
            call(['sudo', '-u', 'postgres', 'createuser', '--no-superuser',
                  '--createdb', '--no-createrole', name])
            call(['sudo', '-u', 'postgres', 'createdb', name, '-O', name])

            # TODO add configuration option for apache
            logger.info('Adding Apache configuration')
            call(['sudo', '-u', name, 'mkdir',
                 '/home/%s/apache.conf.d/' % name])
            apache_conf = """
# Include user-controlled apache configs
Include /home/%(name)s/apache.conf.d/*
""" % {'name': name}
            write('/etc/apache2/sites-available/%s' % name, apache_conf)
            call(['a2ensite', name])
            call(['/etc/init.d/apache2', 'reload'])
            
            # add SSH public key
            config = env.cfg.config
            if config.has_option('mallet', 'public_key'):
                ssh_dir = os.path.join('/', 'home', name, '.ssh')
                call(['sudo', '-u', name, 'mkdir -p %s' % ssh_dir])
                public_key = open(config.get('mallet', 'public_key')).read()
                authorized_keys = os.path.join(ssh_dir, 'authorized_keys')
                write(authorized_keys, public_key, append=True)
                chown(authorized_keys, name)

            # logger.info('Setting password for user')
            # print 'Please choose a UNIX password for the "%s" user' % name
            # call(['passwd', name])