From b3e7cdb3f85cd913d1cfb0895be1b0b6acc319fd Mon Sep 17 00:00:00 2001 From: Avishai Ish-Shalom Date: Thu, 21 Apr 2011 17:57:54 +0300 Subject: Added Chef plugin --- cloudinit/CloudConfig/cc_chef.py | 68 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 cloudinit/CloudConfig/cc_chef.py (limited to 'cloudinit/CloudConfig/cc_chef.py') diff --git a/cloudinit/CloudConfig/cc_chef.py b/cloudinit/CloudConfig/cc_chef.py new file mode 100644 index 00000000..9953cb24 --- /dev/null +++ b/cloudinit/CloudConfig/cc_chef.py @@ -0,0 +1,68 @@ +# vi: ts=4 expandtab +# +# Author: Avishai Ish-Shalom +# +# 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 +# published by the Free Software Foundation. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +import os +import pwd +import socket +import subprocess +import StringIO +import ConfigParser +import cloudinit.CloudConfig as cc + +ruby_packages = {'1.8': ('ruby', 'rubygems', 'ruby-dev', 'libopenssl-ruby'), + '1.9.1': ('ruby1.9.1', 'rubygems1.9.1', 'ruby1.9.1-dev', 'libruby1.9.1'), + '1.9': ('ruby1.9', 'rubygems1.9', 'ruby1.9-dev', 'libruby1.9') } + +def handle(name,cfg,cloud,log,args): + # If there isn't a chef key in the configuration don't do anything + if not cfg.has_key('chef'): return + chef_cfg = cfg['chef'] + ruby_version = '1.8' + + # Install chef packages from selected source + if chef_cfg['install_type'] == "gems": + if chef_cfg.has_key('ruby_version'): + ruby_version = chef_cfg['ruby_version'] + cc.install_packages(ruby_packages['ruby_version']) + chef_version_arg = "" + if chef_cfg.has_key('version'): + chef_version_arg = '-v %s' % chef_cfg['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') + else: + cc.install_packages(('chef',)) + + # set the validation cert + if chef_cfg.has_key('validation_cert'): + with open('/etc/chef/validation.cert', 'w') as validation_cert_fh: + validation_cert_fh.write(chef_cfg['validation_cert']) + + # create the chef config from template + util.render_to_file('chef_client.rb', '/etc/chef/client.rb', + {'server_url': chef_cfg['server_url'], 'validation_name': chef_cfg['validation_name'] || 'chef-validator'}) + + chef_args = [] + # set the firstboot json + if chef_cfg.has_key('run_list'): + with open('/etc/chef/firstboot.json') as firstboot_json_fh: + firstboot_json_fh.write("{\n\"run_list\":\n[\n") + for runlist_item in chef_cfg['run_list']: + firstboot_json_fh.write(runlist_item + "\n") + firstboot_json_fh.write("]\n\}") + chef_args.append('-j /etc/chef/firstboot.json') + + # and finally, run chef + subprocess.check_call(['/usr/bin/chef-client'] + chef_args) -- cgit v1.2.3 From 24a3e3cdb876a3ad1169e16b331efc88881f9b75 Mon Sep 17 00:00:00 2001 From: Avishai Ish-Shalom Date: Sun, 24 Apr 2011 11:24:33 +0300 Subject: ruby_version should be configurable --- cloudinit/CloudConfig/cc_chef.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'cloudinit/CloudConfig/cc_chef.py') diff --git a/cloudinit/CloudConfig/cc_chef.py b/cloudinit/CloudConfig/cc_chef.py index 9953cb24..ca17f1e9 100644 --- a/cloudinit/CloudConfig/cc_chef.py +++ b/cloudinit/CloudConfig/cc_chef.py @@ -20,6 +20,7 @@ import subprocess import StringIO import ConfigParser import cloudinit.CloudConfig as cc +import cloudinit.util as util ruby_packages = {'1.8': ('ruby', 'rubygems', 'ruby-dev', 'libopenssl-ruby'), '1.9.1': ('ruby1.9.1', 'rubygems1.9.1', 'ruby1.9.1-dev', 'libruby1.9.1'), @@ -29,12 +30,10 @@ def handle(name,cfg,cloud,log,args): # If there isn't a chef key in the configuration don't do anything if not cfg.has_key('chef'): return chef_cfg = cfg['chef'] - ruby_version = '1.8' # Install chef packages from selected source if chef_cfg['install_type'] == "gems": - if chef_cfg.has_key('ruby_version'): - ruby_version = chef_cfg['ruby_version'] + ruby_version = util.get_cfg_option_str(chef_cfg, 'ruby_version', '1.8') cc.install_packages(ruby_packages['ruby_version']) chef_version_arg = "" if chef_cfg.has_key('version'): @@ -42,6 +41,11 @@ def handle(name,cfg,cloud,log,args): 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 else: cc.install_packages(('chef',)) @@ -54,7 +58,7 @@ def handle(name,cfg,cloud,log,args): util.render_to_file('chef_client.rb', '/etc/chef/client.rb', {'server_url': chef_cfg['server_url'], 'validation_name': chef_cfg['validation_name'] || 'chef-validator'}) - chef_args = [] + chef_args = ['-d'] # set the firstboot json if chef_cfg.has_key('run_list'): with open('/etc/chef/firstboot.json') as firstboot_json_fh: -- cgit v1.2.3 From a7781de5e106fe6087a39afa20ff9f1fa0e37ff4 Mon Sep 17 00:00:00 2001 From: Avishai Ish-Shalom Date: Sun, 24 Apr 2011 13:37:58 +0300 Subject: updated ruby packages list --- cloudinit/CloudConfig/cc_chef.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'cloudinit/CloudConfig/cc_chef.py') diff --git a/cloudinit/CloudConfig/cc_chef.py b/cloudinit/CloudConfig/cc_chef.py index ca17f1e9..335344d5 100644 --- a/cloudinit/CloudConfig/cc_chef.py +++ b/cloudinit/CloudConfig/cc_chef.py @@ -23,8 +23,8 @@ import cloudinit.CloudConfig as cc import cloudinit.util as util ruby_packages = {'1.8': ('ruby', 'rubygems', 'ruby-dev', 'libopenssl-ruby'), - '1.9.1': ('ruby1.9.1', 'rubygems1.9.1', 'ruby1.9.1-dev', 'libruby1.9.1'), - '1.9': ('ruby1.9', 'rubygems1.9', 'ruby1.9-dev', 'libruby1.9') } + '1.9.1': ('ruby1.9.1', 'ruby1.9.1-dev', 'libruby1.9.1'), + '1.9': ('ruby1.9', 'ruby1.9-dev', 'libruby1.9') } def handle(name,cfg,cloud,log,args): # If there isn't a chef key in the configuration don't do anything -- cgit v1.2.3 From 5682ecb6649f65173a92d80e024e32e7d1cf7359 Mon Sep 17 00:00:00 2001 From: Avishai Ish-Shalom Date: Fri, 29 Apr 2011 16:27:43 +0300 Subject: Seperated chef gems install to another function --- cloudinit/CloudConfig/cc_chef.py | 40 ++++++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 16 deletions(-) (limited to 'cloudinit/CloudConfig/cc_chef.py') diff --git a/cloudinit/CloudConfig/cc_chef.py b/cloudinit/CloudConfig/cc_chef.py index 335344d5..63e3808a 100644 --- a/cloudinit/CloudConfig/cc_chef.py +++ b/cloudinit/CloudConfig/cc_chef.py @@ -32,22 +32,17 @@ def handle(name,cfg,cloud,log,args): chef_cfg = cfg['chef'] # Install chef packages from selected source - if chef_cfg['install_type'] == "gems": - ruby_version = util.get_cfg_option_str(chef_cfg, 'ruby_version', '1.8') - cc.install_packages(ruby_packages['ruby_version']) - chef_version_arg = "" - if chef_cfg.has_key('version'): - chef_version_arg = '-v %s' % chef_cfg['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 - else: - cc.install_packages(('chef',)) + if not os.path.isfile('/usr/bin/chef-client'): + if chef_cfg['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',)) # set the validation cert if chef_cfg.has_key('validation_cert'): @@ -70,3 +65,16 @@ def handle(name,cfg,cloud,log,args): # and finally, run chef subprocess.check_call(['/usr/bin/chef-client'] + chef_args) + +def install_chef_from_gems(ruby_version, chef_version = None): + cc.install_packages(ruby_packages[ruby_version]) + 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 -- cgit v1.2.3