Skip to content

Commit

Permalink
Don't delete /var/chef-solo/cache on every run. Fixes issue tobami#26
Browse files Browse the repository at this point in the history
  • Loading branch information
tobami committed Apr 20, 2011
1 parent cbb211d commit 4f7a3ac
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 46 deletions.
7 changes: 4 additions & 3 deletions CHANGELOG
Expand Up @@ -3,11 +3,12 @@ Little Chef Change Log
======================


Next version
Version 0.5.2 April 20, 2011
-----------------------------------------
* FIX: sirlantis fixed exception when a cookbook was only present in site-cookbooks
* FIX: sirlantis improved OSX support by not packing files named ._{filename}
* FIX: .svn directories are no longer packed and uploaded
* FIX: When configuring a node, the /var/chef-solo/cache directory won't be deleted any more. Solves issue #26
* OPTIMIZATION: sirlantis improved OSX support by not packing files named ._{filename}
* OPTIMIZATION: .svn directories are no longer packed and uploaded


Version 0.5.1 April 13, 2011
Expand Down
90 changes: 48 additions & 42 deletions littlechef.py
Expand Up @@ -23,7 +23,7 @@
from fabric.contrib.console import confirm
from fabric import colors

VERSION = (0, 5, 1)
VERSION = (0, 5, 2)
version = ".".join([str(x) for x in VERSION])

NODEPATH = "nodes/"
Expand Down Expand Up @@ -397,11 +397,13 @@ def _rpm_install():
# Install Chef Solo
sudo('yum -y install chef')

######################################
### Node configuration and syncing ###
######################################
##################################################################
### Node configuration and syncing ###
### See ###
### http://wiki.opscode.com/display/chef/Anatomy+of+a+Chef+Run ###
##################################################################
def _save_config(save, data, hostname):
'''Saves node configuration either to tmp_node.json or to hostname.json'''
"""Saves node configuration either to tmp_node.json or to hostname.json"""
filepath = NODEPATH + hostname + ".json"
if os.path.exists(filepath) and not save:
filepath = 'tmp_node.json'
Expand All @@ -412,44 +414,16 @@ def _save_config(save, data, hostname):
return filepath

def _sync_node(filepath):
'''Uploads cookbooks and configures a node'''
_update_cookbooks(filepath)
"""Buils, synchronizes and configures a node"""
_synchronize_node(filepath)
_configure_node(filepath)

def _configure_node(configfile):
'''Exectutes chef-solo to apply roles and recipes to a node'''
with hide('running'):
print "Uploading node.json..."
remote_file = '/root/{0}'.format(configfile.split("/")[-1])
with hide('stdout'):# Some shells output 'sudo password'
put(configfile, remote_file, use_sudo=True, mode=_file_mode)
sudo('chown root:root {0}'.format(remote_file)),
sudo('mv {0} /etc/chef/node.json'.format(remote_file)),

print "\n== Cooking ==\n"
with settings(hide('warnings'), warn_only=True):
output = sudo(
'chef-solo -l {0} -j /etc/chef/node.json'.format(env.loglevel))
if output.failed:
if 'chef-solo: command not found' in output:
print(
colors.red(
"\nFAILED: Chef Solo is not installed on this node"))
print(
"Type 'cook nodes:{0} deploy_chef' to install it".format(
env.host))
abort("")
else:
print(colors.red(
"\nFAILED: A problem occurred while executing chef-solo"))
abort("")
else:
print(colors.green("\nSUCCESS: Node correctly configured"))

def _update_cookbooks(configfile):
'''Uploads needed cookbooks and all roles to a node'''
def _synchronize_node(configfile):
"""Performs the Synchronize step of a Chef run:
Uploads needed cookbooks and all roles to a node
"""
# Clean up node
for path in ['roles', 'cache'] + _cookbook_paths:
for path in ['roles'] + _cookbook_paths:
with hide('stdout'):
sudo('rm -rf {0}/{1}'.format(_node_work_path, path))

Expand Down Expand Up @@ -518,10 +492,41 @@ def _update_cookbooks(configfile):
print "Uploading roles..."
_upload_and_unpack(['roles'])


def _configure_node(configfile):
"""Exectutes chef-solo to apply roles and recipes to a node"""
with hide('running'):
print "Uploading node.json..."
remote_file = '/root/{0}'.format(configfile.split("/")[-1])
with hide('stdout'):# Some shells output 'sudo password'
put(configfile, remote_file, use_sudo=True, mode=_file_mode)
sudo('chown root:root {0}'.format(remote_file)),
sudo('mv {0} /etc/chef/node.json'.format(remote_file)),

print "\n== Cooking ==\n"
with settings(hide('warnings'), warn_only=True):
output = sudo(
'chef-solo -l {0} -j /etc/chef/node.json'.format(env.loglevel))
if output.failed:
if 'chef-solo: command not found' in output:
print(
colors.red(
"\nFAILED: Chef Solo is not installed on this node"))
print(
"Type 'cook nodes:{0} deploy_chef' to install it".format(
env.host))
abort("")
else:
print(colors.red(
"\nFAILED: A problem occurred while executing chef-solo"))
abort("")
else:
print(colors.green("\nSUCCESS: Node correctly configured"))

def _upload_and_unpack(source):
'''Packs the given directories, uploads the tar.gz to the node
"""Packs the given directories, uploads the tar.gz to the node
and unpacks it in the _node_work_path (typically '/var/chef-solo') directory
'''
"""
with hide('running', 'stdout'):
# Local archive relative path
local_archive = 'temp.tar.gz'
Expand Down Expand Up @@ -562,6 +567,7 @@ def _upload_and_unpack(source):
# Remove the remote copy of archive
sudo('rm {0}'.format(remote_archive))


###########
### API ###
###########
Expand Down
2 changes: 1 addition & 1 deletion tests/tests.py
Expand Up @@ -28,7 +28,7 @@ def test_version(self):
"""Should output the correct Little Chef version"""
resp, error = self.execute(['../cook', '-v'])
self.assertEquals(error, "")
self.assertTrue('littlechef 0.5.1' in resp)
self.assertTrue('littlechef 0.5.' in resp)

def test_list_commands(self):
"""Should output a list of available commands"""
Expand Down

0 comments on commit 4f7a3ac

Please sign in to comment.