summaryrefslogtreecommitdiff
path: root/cloudinit/CloudConfig/cc_chef.py
diff options
context:
space:
mode:
authorScott Moser <smoser@ubuntu.com>2011-09-13 08:02:37 -0400
committerScott Moser <smoser@ubuntu.com>2011-09-13 08:02:37 -0400
commitcee5b2376e96a9a282f23b85f5f9984d02564dc1 (patch)
tree42fc9d241f10393ba36bde182e7c465b78feb26a /cloudinit/CloudConfig/cc_chef.py
parentd88823191ed67fd1801931df43e6f6c26e1a0e42 (diff)
parent2e6b44be4a2c352a49910c437a830c27b0ad131c (diff)
downloadvyos-cloud-init-cee5b2376e96a9a282f23b85f5f9984d02564dc1.tar.gz
vyos-cloud-init-cee5b2376e96a9a282f23b85f5f9984d02564dc1.zip
Chef support fixes, and support for environment and initial attr (LP: #845208)
Thanks to Mike Mouulton and Avishai Ish-Shalom. LP: #845208
Diffstat (limited to 'cloudinit/CloudConfig/cc_chef.py')
-rw-r--r--cloudinit/CloudConfig/cc_chef.py101
1 files changed, 58 insertions, 43 deletions
diff --git a/cloudinit/CloudConfig/cc_chef.py b/cloudinit/CloudConfig/cc_chef.py
index 5f13c77d..807c3717 100644
--- a/cloudinit/CloudConfig/cc_chef.py
+++ b/cloudinit/CloudConfig/cc_chef.py
@@ -1,6 +1,7 @@
# vi: ts=4 expandtab
#
# Author: Avishai Ish-Shalom <avishai@fewbytes.com>
+# Author: Mike Moulton <mike@meltmedia.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3, as
@@ -17,6 +18,7 @@ import os
import pwd
import socket
import subprocess
+import json
import StringIO
import ConfigParser
import cloudinit.CloudConfig as cc
@@ -31,60 +33,73 @@ def handle(name,cfg,cloud,log,args):
if not cfg.has_key('chef'): return
chef_cfg = cfg['chef']
- # Install chef packages from selected source
- install_type = util.get_cfg_option_str(chef_cfg, "install_type", "packages")
- if not os.path.isfile('/usr/bin/chef-client'):
- if install_type == "gems":
- if chef_cfg.has_key('version'):
- chef_version = chef_cfg['version']
- else:
- chef_version = None
- install_chef_from_gems(
- util.get_cfg_option_str(chef_cfg, 'ruby_version', '1.8'),
- chef_version)
- else:
- cc.install_packages(('chef',))
+ # ensure the chef directories we use exist
+ mkdirs(['/etc/chef', '/var/log/chef', '/var/lib/chef',
+ '/var/cache/chef', '/var/backups/chef', '/var/run/chef'])
- # set the validation cert
- if chef_cfg.has_key('validation_cert'):
- with open('/etc/chef/validation.pem', 'w') as validation_cert_fh:
- validation_cert_fh.write(chef_cfg['validation_cert'])
+ # set the validation key based on the presence of either 'validation_key'
+ # or 'validation_cert'. In the case where both exist, 'validation_key'
+ # takes precedence
+ if chef_cfg.has_key('validation_key') or chef_cfg.has_key('validation_cert'):
+ validation_key = util.get_cfg_option_str(chef_cfg, 'validation_key',
+ chef_cfg['validation_cert'])
+ with open('/etc/chef/validation.pem', 'w') as validation_key_fh:
+ validation_key_fh.write(validation_key)
validation_name = chef_cfg.get('validation_name','chef-validator')
# create the chef config from template
util.render_to_file('chef_client.rb', '/etc/chef/client.rb',
{'server_url': chef_cfg['server_url'],
+ 'node_name': util.get_cfg_option_str(chef_cfg, 'node_name',
+ cloud.datasource.get_instance_id()),
+ 'environment': util.get_cfg_option_str(chef_cfg, 'environment',
+ '_default'),
'validation_name': chef_cfg['validation_name']})
- chef_args = ['-d']
# set the firstboot json
- if chef_cfg.has_key('run_list'):
- with open('/etc/chef/firstboot.json', 'w') as firstboot_json_fh:
- firstboot_json_fh.write("{\n\"run_list\":\n[\n")
- firstboot_json_fh.write(
- ",\n".join(["\"%s\"" % runlist_item for runlist_item in chef_cfg['run_list']])
- )
- firstboot_json_fh.write("]\n\}")
- chef_args.append('-j /etc/chef/firstboot.json')
+ with open('/etc/chef/firstboot.json', 'w') as firstboot_json_fh:
+ initial_json = {}
+ if chef_cfg.has_key('run_list'):
+ initial_json['run_list'] = chef_cfg['run_list']
+ if chef_cfg.has_key('initial_attributes'):
+ initial_attributes = chef_cfg['initial_attributes']
+ for k in initial_attributes.keys(): initial_json[k] = initial_attributes[k]
+ firstboot_json_fh.write(json.dumps(initial_json))
- # and finally, run chef
- log.debug("running chef-client %s" % chef_args)
- subprocess.check_call(['/usr/bin/chef-client'] + chef_args)
+ # If chef is not installed, we install chef based on 'install_type'
+ if not os.path.isfile('/usr/bin/chef-client'):
+ install_type = util.get_cfg_option_str(chef_cfg, 'install_type', 'packages')
+ if install_type == "gems":
+ # this will install and run the chef-client from gems
+ chef_version = util.get_cfg_option_str(chef_cfg, 'version', None)
+ ruby_version = util.get_cfg_option_str(chef_cfg, 'ruby_version', '1.8')
+ install_chef_from_gems(ruby_version, chef_version)
+ # and finally, run chef-client
+ log.debug('running chef-client')
+ subprocess.check_call(['/usr/bin/chef-client', '-d', '-i', '1800', '-s', '20'])
+ else:
+ # this will install and run the chef-client from packages
+ cc.install_packages(('chef',))
def install_chef_from_gems(ruby_version, chef_version = None):
cc.install_packages(ruby_packages[ruby_version])
- gem_bin = get_gem_bin()
- if not os.path.exists('/usr/bin/gem'): os.symlink(gem_bin, '/usr/bin/gem')
- chef_version_arg = ""
- if chef_version: chef_version_arg = "-v %s" % chef_version
- subprocess.check_call([gem_bin,'install','chef',chef_version_arg, '--no-ri','--no-rdoc','--no-test','-q'])
- os.mkdirs('/etc/chef', '/var/log/chef', '/var/lib/chef', '/var/cache/chef', '/var/backups/chef', '/var/run/chef')
- os.symlink('/var/lib/gem/%s/bin/chef-client' % ruby_version, '/usr/bin/chef-client')
- # Ohai ruby plugin breaks if there is no ruby or gem binaries at /usr/bin, so
- try: os.symlink('/usr/bin/gem%s' % ruby_version, '/usr/bin/gem')
- except: pass
- try: os.symlink('/usr/bin/ruby%s' % ruby_version, '/usr/bin/ruby')
- except: pass
+ if not os.path.exists('/usr/bin/gem'):
+ os.symlink('/usr/bin/gem%s' % ruby_version, '/usr/bin/gem')
+ if not os.path.exists('/usr/bin/ruby'):
+ os.symlink('/usr/bin/ruby%s' % ruby_version, '/usr/bin/ruby')
+ if chef_version:
+ subprocess.check_call(['/usr/bin/gem','install','chef',
+ '-v %s' % chef_version, '--no-ri',
+ '--no-rdoc','--bindir','/usr/bin','-q'])
+ else:
+ subprocess.check_call(['/usr/bin/gem','install','chef',
+ '--no-ri','--no-rdoc','--bindir',
+ '/usr/bin','-q'])
+
+def ensure_dir(d):
+ if not os.path.exists(d):
+ os.makedirs(d)
-def get_gem_bin():
- return '/usr/bin/gem%s' % util.get_cfg_option_str(chef_cfg, 'ruby_version', '1.8')
+def mkdirs(dirs):
+ for d in dirs:
+ ensure_dir(d)