Browse files

Initial commit

  • Loading branch information...
0 parents commit 08221018b480a65d227abca20ec2b137b4a720df @bfirsh committed Oct 4, 2012
Showing with 196 additions and 0 deletions.
  1. +2 −0 .gitignore
  2. +12 −0 README.md
  3. 0 loom/__init__.py
  4. +22 −0 loom/config.py
  5. +96 −0 loom/puppet.py
  6. +41 −0 loom/tasks.py
  7. +1 −0 requirements.txt
  8. +22 −0 setup.py
2 .gitignore
@@ -0,0 +1,2 @@
+*.pyc
+*.egg-info
12 README.md
@@ -0,0 +1,12 @@
+Loom
+====
+
+Elegant deployment with Fabric and Puppet.
+
+Install
+-------
+
+ $ sudo pip install loom
+
+
+
0 loom/__init__.py
No changes.
22 loom/config.py
@@ -0,0 +1,22 @@
+from fabric.api import env, put, task
+import json
+from StringIO import StringIO
+
+# Default system user
+env.user = 'ubuntu'
+
+# Default puppet environment
+env.app = 'prod'
+
+def host_role(host_string):
+ """
+ Returns the role of a given host string.
+ """
+ for role, hosts in env.roledefs.items():
+ if host_string in hosts:
+ return role
+
+def current_role():
+ return host_role(env.host_string)
+
+
96 loom/puppet.py
@@ -0,0 +1,96 @@
+from fabric.api import *
+from fabric.contrib.project import rsync_project
+from StringIO import StringIO
+from .config import current_role
+
+__all__ = ['update', 'install_master', 'install_agent', 'apply']
+
+env.puppet_conf = """
+[main]
+logdir=/var/log/puppet
+vardir=/var/lib/puppet
+ssldir=/var/lib/puppet/ssl
+rundir=/var/run/puppet
+factpath=$vardir/lib/facter
+templatedir=$confdir/templates
+prerun_command=/etc/puppet/etckeeper-commit-pre
+postrun_command=/etc/puppet/etckeeper-commit-post
+modulepath=/etc/puppet/modules:/etc/puppet/vendor
+
+[master]
+# These are needed when the puppetmaster is run by passenger
+# and can safely be removed if webrick is used.
+ssl_client_header = SSL_CLIENT_S_DN
+ssl_client_verify_header = SSL_CLIENT_VERIFY
+
+"""
+
+@task
+def update():
+ """
+ Upload puppet modules and manifests
+ """
+ # Install local modules
+ rsync_project(
+ local_dir="modules/",
+ remote_dir="/etc/puppet/modules",
+ delete=True,
+ extra_opts='--rsync-path="sudo rsync" --exclude=".git*" --copy-links',
+ ssh_opts='-oStrictHostKeyChecking=no'
+ )
+
+ # Install vendor modules
+ put('Puppetfile', '/etc/puppet/Puppetfile', use_sudo=True)
+ with cd('/etc/puppet'):
+ sudo('librarian-puppet install --path /etc/puppet/vendor --verbose')
+
+ put(StringIO('include "roles::$role"\n'), '/etc/puppet/manifests/site.pp', use_sudo=True)
+
+
+@task
+def install_master():
+ """
+ Install puppetmaster, update its modules and install agent.
+ """
+ # librarian-puppet depends on git
+ with settings(hide('stdout'), show('running')):
+ sudo('apt-get update')
+ sudo('apt-get -y install puppetmaster rubygems git')
+ # TODO: this installs a later version of puppet than the system version
+ # which might cause trouble.
+ sudo('gem install librarian-puppet --no-ri --no-rdoc')
+ put(StringIO(env.puppet_conf), '/etc/puppet/puppet.conf', use_sudo=True)
+ execute(update)
+ execute(install_agent)
+
+@task
+def install_agent():
+ """
+ Install the puppet agent.
+ """
+ with settings(hide('stdout'), show('running')):
+ sudo('apt-get update')
+ sudo('apt-get -y install puppet')
+ defaults = """
+START=yes
+DAEMON_OPTS="--server %(server)s --environment %(environment)s"
+export FACTER_role="%(role)s"
+""" % {
+ 'server': env.get('puppetmaster_host', env.roledefs['puppetmaster'][0]),
+ 'environment': env.environment,
+ 'role': current_role(),
+ }
+ put(StringIO(env.puppet_conf), '/etc/puppet/puppet.conf', use_sudo=True)
+ put(StringIO(defaults), '/etc/default/puppet', use_sudo=True)
+ sudo('/etc/init.d/puppet restart')
+
+@task
+def apply():
+ """
+ Apply puppet locally
+ """
+ sudo('HOME=/root FACTER_role=%s puppet apply --modulepath=/etc/puppet/vendor:/etc/puppet/modules /etc/puppet/manifests/site.pp' % current_role())
+
+
+
+
41 loom/tasks.py
@@ -0,0 +1,41 @@
+from fabric.api import *
+import subprocess
+
+from .config import host_role, current_role
+
+__all__ = ['ssh', 'all', 'uptime', 'upgrade']
+
+@task
+def all():
+ """
+ Select all hosts
+ """
+ env.hosts = []
+ for hosts in env.roledefs.values():
+ env.hosts.extend(env.hosts)
+ # remove dupes
+ env.hosts = list(set(env.hosts))
+
+@task
+def uptime():
+ run('uptime')
+
+@task
+def upgrade():
+ """
+ Upgrade apt packages
+ """
+ with settings(hide('stdout'), show('running')):
+ sudo('apt-get update')
+ sudo("apt-get upgrade -y")
+
+@task
+def ssh(*cmd):
+ """
+ Open an interactive ssh session
+ """
+ run = ["ssh", "-A", "-t", "-i", "epio.pem", '%s@%s' % (env.user, env.host_string)]
+ run += cmd
+ subprocess.call(run)
+
+
1 requirements.txt
@@ -0,0 +1 @@
+fabric==1.4.3
22 setup.py
@@ -0,0 +1,22 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+from setuptools import setup
+
+setup(
+ name='loom',
+ version='0.0.1',
+ description='',
+ author='',
+ author_email='',
+ url='http://github.com/bfirsh/loom',
+ packages = [
+ 'loom'
+ ],
+ package_data = {},
+ include_package_data=True,
+ install_requires = open('requirements.txt').readlines(),
+ entry_points={},
+ #test_suite = 'nose.collector',
+)
+

0 comments on commit 0822101

Please sign in to comment.