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
install_type = util.get_cfg_option_str(chef_cfg, "install_type", "packages")
36
if not os.path.isfile('/usr/bin/chef-client'):
37
if install_type == "gems":
38
if chef_cfg.has_key('version'):
39
chef_version = chef_cfg['version']
42
install_chef_from_gems(
43
util.get_cfg_option_str(chef_cfg, 'ruby_version', '1.8'),
46
cc.install_packages(('chef',))
48
# set the validation cert
49
if chef_cfg.has_key('validation_cert'):
50
with open('/etc/chef/validation.pem', 'w') as validation_cert_fh:
51
validation_cert_fh.write(chef_cfg['validation_cert'])
53
validation_name = chef_cfg.get('validation_name','chef-validator')
54
# create the chef config from template
55
util.render_to_file('chef_client.rb', '/etc/chef/client.rb',
56
{'server_url': chef_cfg['server_url'],
57
'validation_name': chef_cfg['validation_name']})
60
# set the firstboot json
61
if chef_cfg.has_key('run_list'):
62
with open('/etc/chef/firstboot.json', 'w') as firstboot_json_fh:
63
firstboot_json_fh.write("{\n\"run_list\":\n[\n")
64
firstboot_json_fh.write(
65
",\n".join(["\"%s\"" % runlist_item for runlist_item in chef_cfg['run_list']])
67
firstboot_json_fh.write("]\n\}")
68
chef_args.append('-j /etc/chef/firstboot.json')
70
# and finally, run chef
71
log.debug("running chef-client %s" % chef_args)
72
subprocess.check_call(['/usr/bin/chef-client'] + chef_args)
74
def install_chef_from_gems(ruby_version, chef_version = None):
75
cc.install_packages(ruby_packages[ruby_version])
76
gem_bin = get_gem_bin()
77
if not os.path.exists('/usr/bin/gem'): os.symlink(gem_bin, '/usr/bin/gem')
79
if chef_version: chef_version_arg = "-v %s" % chef_version
80
subprocess.check_call([gem_bin,'install','chef',chef_version_arg, '--no-ri','--no-rdoc','--no-test','-q'])
81
os.mkdirs('/etc/chef', '/var/log/chef', '/var/lib/chef', '/var/cache/chef', '/var/backups/chef', '/var/run/chef')
82
os.symlink('/var/lib/gem/%s/bin/chef-client' % ruby_version, '/usr/bin/chef-client')
83
# Ohai ruby plugin breaks if there is no ruby or gem binaries at /usr/bin, so
84
try: os.symlink('/usr/bin/gem%s' % ruby_version, '/usr/bin/gem')
86
try: os.symlink('/usr/bin/ruby%s' % ruby_version, '/usr/bin/ruby')
90
return '/usr/bin/gem%s' % util.get_cfg_option_str(chef_cfg, 'ruby_version', '1.8')