Colin Copeland avatar Colin Copeland committed e240ff5

break up logic and start example for Linode

Comments (0)

Files changed (6)

 syntax: glob
 *.pyc
-
+*.cfg

Empty file added.

mallet/client_vm.py

 logger = logging.getLogger('mallet.client_vm')
 
 
-class SCM(PackageGroup):
-    packages = ['subversion', 'git-core', 'mercurial']
-
-
-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):
-    packages = ['postgresql', 'postgresql-server-dev-8.4', 'pwgen']
-
-    def post_install(self, node_wrapper):
-        call(['locale-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 Python(PackageGroup):
-    packages = ['ipython', 'python-setuptools', 'python-dev', 'libpq-dev',
-                'libapache2-mod-wsgi', 'python-imaging']
-
-    def post_install(self, node_wrapper):
-        call(['easy_install', '-U', 'pip'])
-        call(['pip', 'install', '-U', 'virtualenv'])
-        call(['pip', 'install', '-U', 'distribute'])
-        call(['pip', 'install', '-U', 'distribute'])
-        call(['pip', 'install', '-U', 'psycopg2'])
-
-
-class Apache(lenny.Apache):
-
-    def post_install(self, node_wrapper):
-        super(Apache, self).post_install(node_wrapper)
-        logger.info('Creating clients group')
-        call(['addgroup', 'clients'])
-        logger.info('Creating sudoers file')
-        sudoers = ['%clients ALL=NOPASSWD: /etc/init.d/apache2 '
-                   'reload, /etc/init.d/apache2 restart']
-        writelines('/etc/sudoers', sudoers)
 
 
 class NodeWrapper(mallet.NodeWrapper):

mallet/templates/__init__.py

+import logging
+
+from libcloud.types import Provider
+
+from mallet import Template
+from mallet.bootstrap import apt
+
+from templates import packages
+from templates.node import NodeWrapper
+
+
+logger = logging.getLogger('mallet.client_vm')
+
+
+__all__ = ('RackspaceClientVM',)
+
+
+class RackspaceClientVM(Template):
+
+    provider = Provider.RACKSPACE
+    driver_config = {'image_id': 14362, 'size_id': 1}
+    distribution = apt.Apt
+    release = apt.lenny.Lenny
+    packages = [
+        packages.Apache,
+        packages.Postgres,
+        packages.Python,
+        packages.lenny.Memcache,
+        packages.BuildEssential,
+        packages.SSH,
+        packages.SCM,
+    ]
+    node_wrapper = NodeWrapper
+
+
+class LinodeClientVM(Template):
+
+    provider = Provider.LINODE
+    driver_config = {'image_id': 64, 'size_id': 1, 'location_id': 6}
+    distribution = apt.Apt
+    release = apt.lenny.Lenny
+    packages = [
+        packages.Apache,
+        packages.Postgres,
+        packages.Python,
+        packages.lenny.Memcache,
+        packages.BuildEssential,
+        packages.SSH,
+        packages.SCM,
+    ]
+    node_wrapper = NodeWrapper

mallet/templates/node.py

+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])

mallet/templates/packages.py

+import logging
+
+from fabric.api import env
+
+from mallet.bootstrap.apt import lenny
+from mallet.bootstrap.base import PackageGroup
+from mallet.utils import create_ssh, call, write, writelines, chown
+
+
+logger = logging.getLogger('mallet.templates.node')
+
+
+class SCM(PackageGroup):
+    packages = ['subversion', 'git-core', 'mercurial']
+
+
+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):
+    packages = ['postgresql', 'postgresql-server-dev-8.4', 'pwgen']
+
+    def post_install(self, node_wrapper):
+        call(['locale-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 Python(PackageGroup):
+    packages = ['ipython', 'python-setuptools', 'python-dev', 'libpq-dev',
+                'libapache2-mod-wsgi', 'python-imaging']
+
+    def post_install(self, node_wrapper):
+        call(['easy_install', '-U', 'pip'])
+        call(['pip', 'install', '-U', 'virtualenv'])
+        call(['pip', 'install', '-U', 'distribute'])
+        call(['pip', 'install', '-U', 'distribute'])
+        call(['pip', 'install', '-U', 'psycopg2'])
+
+
+class Apache(lenny.Apache):
+
+    def post_install(self, node_wrapper):
+        super(Apache, self).post_install(node_wrapper)
+        logger.info('Creating clients group')
+        call(['addgroup', 'clients'])
+        logger.info('Creating sudoers file')
+        sudoers = ['%clients ALL=NOPASSWD: /etc/init.d/apache2 '
+                   'reload, /etc/init.d/apache2 restart']
+        writelines('/etc/sudoers', sudoers)
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.