Permalink
Browse files

Data bags, roles, cleanup, version bump

It's now possible to distribute roles and data bags
Kicked out the gem installation method
Bumped up the version
  • Loading branch information...
1 parent 13661ed commit 33bb8139792a4bf40015e97d8e3ddc6133a2ab09 @flashingpumpkin flashingpumpkin committed Dec 13, 2012
Showing with 65 additions and 64 deletions.
  1. +63 −62 provision/__init__.py
  2. +2 −2 setup.py
View
125 provision/__init__.py
@@ -7,74 +7,58 @@
import sys
import tempfile
-__version__ = '0.0.7'
+__version__ = '0.1.0'
DEFAULTS = dict(
path='/var/chef',
+ data_bags=['data_bags'],
+ roles = ['roles'],
cookbooks=['cookbooks'],
log_level='info',
- gems='1.8.10',
recipes=[],
+ run_list=[],
json={},
- use_omnibus_installer = False,
)
SOLO_RB = """
log_level :%(log_level)s
log_location STDOUT
file_cache_path "%(path)s"
+data_bag_path [ "%(path)s/data_bags" ]
+role_path [ "%(path)s/roles" ]
cookbook_path [ "%(path)s/cookbooks" ]
Chef::Log::Formatter.show_time = true
"""
-CHEF_DEPENDENCIES = """
-libopenid-ruby
-liberubis-ruby
-libjson-ruby
-libextlib-ruby
-libstomp-ruby
-libohai-ruby
-libopenssl-ruby
-"""
-
class ChefDict(_AttributeDict):
def add_recipe(self, recipe):
- self.recipes.append(recipe)
+ self.run_list.append('recipe[{}]'.format(recipe))
+
+ def add_role(self, role):
+ self.run_list.append('role[{}]'.format(role))
def _get_json(self):
json = self['json'].copy()
- json['recipes'] = self['recipes']
+
+ # Maintain compatibility with <v0.1
+ if self['recipes']:
+ map(self.add_recipe, self['recipes'])
+ json['run_list'] = self['run_list']
return json
json = property(fget=_get_json)
chef = ChefDict(DEFAULTS)
-chef.apt = True
+chef.apt = True
+""" Wether to run ``apt-get update`` and ``apt-get upgrade`` on provisioning """
def apt():
if chef.apt:
sudo('apt-get update')
sudo('apt-get -y upgrade')
-def gems():
- sudo('apt-get install -y ruby ruby-dev wget %s' % ' '.join(CHEF_DEPENDENCIES.split('\n')))
- ctx = {
- 'filename':'%(path)s/rubygems-%(gems)s.tgz' % chef,
- 'url':'http://production.cf.rubygems.org/rubygems/rubygems-%(gems)s.tgz' % chef,
- }
- if not files.exists(ctx['filename']):
- sudo('wget -O %(filename)s %(url)s' % ctx)
-
- with cd(chef.path):
- sudo('tar -xf %(filename)s' % ctx)
- with cd(os.path.split(os.path.splitext(ctx['filename'])[0])[1]):
- sudo('ruby setup.rb install --no-format-executable --no-rdoc --no-ri')
-
- if not files.exists('/usr/local/bin/chef-solo'):
- sudo('gem install chef --no-rdoc --no-ri -n /usr/local/bin')
-
-def omnibus_install():
+def omnibus():
"""
Install Chef from Opscode's Omnibus installer
"""
@@ -89,34 +73,50 @@ def omnibus_install():
def upload():
ctx = {
- 'cookbooks': '%(path)s/cookbooks' % chef,
- 'node.json': '%(path)s/node.json' % chef,
- 'solo.rb': '%(path)s/solo.rb' % chef,
+ 'roles' : '%(path)s/roles' % chef,
+ 'data_bags' : '%(path)s/data_bags' % chef,
+ 'cookbooks' : '%(path)s/cookbooks' % chef,
+ 'node.json' : '%(path)s/node.json' % chef,
+ 'solo.rb' : '%(path)s/solo.rb' % chef,
}
+
+ folders = ['roles', 'data_bags', 'cookbooks']
- tmpfolder = tempfile.mkdtemp()
+ listify = lambda what: what if isinstance(what, list) else [what]
+ chef.roles = listify(chef.roles)
+ chef.data_bags = listify(chef.data_bags)
+ chef.cookbooks = listify(chef.cookbooks)
+
+ tmpfolder = tempfile.mkdtemp()
+
+ local('mkdir %s/roles' % tmpfolder)
+ local('mkdir %s/data_bags' % tmpfolder)
local('mkdir %s/cookbooks' % tmpfolder)
- if not isinstance(chef.cookbooks, list):
- chef.cookbooks = [chef.cookbooks]
-
- for folder in chef.cookbooks:
- local('cp -r %s/* %s/cookbooks/' % (os.path.normpath(folder), tmpfolder))
+ def copyfolder(folder, what):
+ if not os.path.exists(folder):
+ os.makedirs(folder)
+
+ with settings(warn_only = True):
+ local('cp -r %(folder)s/* %(tmpfolder)s/%(what)s' % dict(
+ folder = folder,
+ tmpfolder = tmpfolder,
+ what = what))
+
+ # Prepare new cookbooks, data bags etc
+ for what in folders:
+ map(lambda f: copyfolder(f, what), getattr(chef, what))
- local('cd %s && tar -f cookbooks.tgz -cz ./cookbooks' % tmpfolder)
-
+ local('cd %s && tar -f cookbooks.tgz -cz ./cookbooks ./data_bags ./roles' % tmpfolder)
+
+ # Get rid of old files
+ with settings(warn_only = True):
+ map(lambda what: sudo('rm -rf %s' % ctx[what]), folders + ['node.json', 'solo.rb'])
+
+ # Upload
put('%s/cookbooks.tgz' % tmpfolder, chef.path, use_sudo=True)
-
- if files.exists(ctx['cookbooks']):
- sudo('rm -rf %(cookbooks)s' % ctx)
-
- if files.exists(ctx['node.json']):
- sudo('rm -rf %(node.json)s' % ctx)
-
- if files.exists(ctx['solo.rb']):
- sudo('rm -rf %(solo.rb)s' % ctx)
-
+
with cd(chef.path):
sudo('tar -xf cookbooks.tgz')
@@ -125,17 +125,18 @@ def upload():
@task(default=True)
@parallel
-def provision(omnibus=True):
+def provision(_omnibus=True):
sudo('mkdir -p %(path)s' % chef)
apt()
-
- if omnibus or chef.use_omnibus_installer:
- omnibus_install()
- else:
- gems()
-
+ omnibus()
upload()
+ runchef()
+
+@task
+@parallel
+def runchef():
with cd(chef.path):
sudo('chef-solo -c solo.rb -j node.json')
+
View
4 setup.py
@@ -3,10 +3,10 @@
METADATA = dict(
name='fabric-provision',
- version='0.0.7',
+ version='0.1.0',
author='Alen Mujezinovic',
author_email='alen@caffeinehit.com',
- description='Server provisioning with Chef',
+ description='Server provisioning with Chef Solo',
long_description=open('README.rst').read(),
url='http://github.com/caffeinehit/fabric-provision',
keywords='server provisioning fabric chef',

0 comments on commit 33bb813

Please sign in to comment.