3
# Author: Avishai Ish-Shalom <avishai@fewbytes.com>
5
# This program is free software: you can redistribute it and/or modify
6
# it under the terms of the GNU General Public License version 3, as
7
# published by the Free Software Foundation.
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU General Public License for more details.
14
# You should have received a copy of the GNU General Public License
15
# along with this program. If not, see <http://www.gnu.org/licenses/>.
22
import cloudinit.CloudConfig as cc
23
import cloudinit.util as util
25
ruby_packages = {'1.8': ('ruby', 'rubygems', 'ruby-dev', 'libopenssl-ruby'),
26
'1.9.1': ('ruby1.9.1', 'ruby1.9.1-dev', 'libruby1.9.1'),
27
'1.9': ('ruby1.9', 'ruby1.9-dev', 'libruby1.9') }
29
def handle(name,cfg,cloud,log,args):
30
# If there isn't a chef key in the configuration don't do anything
31
if not cfg.has_key('chef'): return
32
chef_cfg = cfg['chef']
34
# Install chef packages from selected source
35
if not os.path.isfile('/usr/bin/chef-client'):
36
if chef_cfg['install_type'] == "gems":
37
if chef_cfg.has_key('version'):
38
chef_version = chef_cfg['version']
41
install_chef_from_gems(
42
util.get_cfg_option_str(chef_cfg, 'ruby_version', '1.8'),
45
cc.install_packages(('chef',))
47
# set the validation cert
48
if chef_cfg.has_key('validation_cert'):
49
with open('/etc/chef/validation.cert', 'w') as validation_cert_fh:
50
validation_cert_fh.write(chef_cfg['validation_cert'])
52
validation_name = chef_cfg.get('validation_name','chef-validator')
53
# create the chef config from template
54
util.render_to_file('chef_client.rb', '/etc/chef/client.rb',
55
{'server_url': chef_cfg['server_url'],
56
'validation_name': chef_cfg['validation_name']})
59
# set the firstboot json
60
if chef_cfg.has_key('run_list'):
61
with open('/etc/chef/firstboot.json') as firstboot_json_fh:
62
firstboot_json_fh.write("{\n\"run_list\":\n[\n")
63
for runlist_item in chef_cfg['run_list']:
64
firstboot_json_fh.write(runlist_item + "\n")
65
firstboot_json_fh.write("]\n\}")
66
chef_args.append('-j /etc/chef/firstboot.json')
68
# and finally, run chef
69
log.debug("running chef-client %s" % chef_args)
70
subprocess.check_call(['/usr/bin/chef-client'] + chef_args)
72
def install_chef_from_gems(ruby_version, chef_version = None):
73
cc.install_packages(ruby_packages[ruby_version])
75
if chef_version: chef_version_arg = "-v %s" % chef_version
76
subprocess.check_call([gem_bin,'install','chef',chef_version_arg, '--no-ri','--no-rdoc','--no-test','-q'])
77
os.mkdirs('/etc/chef', '/var/log/chef', '/var/lib/chef', '/var/cache/chef', '/var/backups/chef', '/var/run/chef')
78
os.symlink('/var/lib/gem/%s/bin/chef-client' % ruby_version, '/usr/bin/chef-client')
79
# Ohai ruby plugin breaks if there is no ruby or gem binaries at /usr/bin, so
80
try: os.symlink('/usr/bin/gem%s' % ruby_version, '/usr/bin/gem')
82
try: os.symlink('/usr/bin/ruby%s' % ruby_version, '/usr/bin/ruby')