Colin Copeland avatar Colin Copeland committed 96c8ff3

client_vm.py

Comments (0)

Files changed (1)

mallet/client_vm.py

+import os
+
+import mallet
+import logging
+from mallet.bootstrap import apt
+from mallet.bootstrap.apt import lenny
+from mallet.utils import create_ssh, call, write
+from mallet.bootstrap.base import PackageGroup
+
+from libcloud.types import Provider
+from fabric.api import settings, env
+
+
+logger = logging.getLogger('mallet.client_vm')
+
+
+class BuildEssential(PackageGroup):
+    packages = ['build-essential']
+
+
+class SSH(PackageGroup):
+    packages = ['openssh-server']
+
+    def post_install(self, node_wrapper):
+        config = env.cfg.config
+        if config.has_option('mallet', 'public_key'):
+            call(['mkdir', '-p', '/root/.ssh'])
+            public_key = open(config.get('mallet', 'public_key')).read()
+            write('/root/.ssh/authorized_keys', public_key, append=True)
+
+
+class Postgres(lenny.Postgres):
+
+    def post_install(self, node_wrapper):
+        call(['local-gen', 'en_US.UTF-8'])
+        call(['update-locale', 'LANG=en_US.UTF-8'])
+        call(['pg_dropcluster', '--stop', '8.4', 'main'])
+        call(['pg_createcluster', '--start', '-e', 'UTF-8', '8.4', 'main'])
+
+
+class NodeWrapper(mallet.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])
+
+            # 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(['mkdir', '-p', 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)
+
+            # logger.info('Setting password for user')
+            # print 'Please choose a UNIX password for the "%s" user' % name
+            # call(['passwd', name])
+
+
+class SampleTemplate(mallet.Template):
+
+    provider = Provider.RACKSPACE
+    driver_config = {'image_id': 14362, 'size_id': 1}
+    distribution = apt.Apt
+    release = lenny.Lenny
+    packages = [
+        lenny.Apache,
+        Postgres,
+        lenny.Python,
+        lenny.Memcache,
+        BuildEssential,
+        SSH,
+    ]
+    node_wrapper = NodeWrapper
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.