~ubuntu-branches/ubuntu/saucy/cloud-init/saucy

« back to all changes in this revision

Viewing changes to .pc/catchup-446-447.patch/cloudinit/CloudConfig/cc_chef.py

  • Committer: Package Import Robot
  • Author(s): Scott Moser, Mike Moulton, Avishai Ish-Shalom
  • Date: 2011-09-13 17:02:48 UTC
  • mto: (174.2.1 precise)
  • mto: This revision was merged to the branch mainline in revision 136.
  • Revision ID: package-import@ubuntu.com-20110913170248-4ty3t0epmq8cg9wz
* minor documentation improvement.

[Mike Moulton, Avishai Ish-Shalom]
* Chef support fixes. support for environment and initial attr (LP: #845208)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# vi: ts=4 expandtab
 
2
#
 
3
#    Author: Avishai Ish-Shalom <avishai@fewbytes.com>
 
4
#
 
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.
 
8
#
 
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.
 
13
#
 
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/>.
 
16
import os
 
17
import pwd
 
18
import socket
 
19
import subprocess
 
20
import StringIO
 
21
import ConfigParser
 
22
import cloudinit.CloudConfig as cc
 
23
import cloudinit.util as util
 
24
 
 
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') }
 
28
 
 
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']
 
33
 
 
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']
 
40
            else:
 
41
                chef_version = None
 
42
            install_chef_from_gems(
 
43
                    util.get_cfg_option_str(chef_cfg, 'ruby_version', '1.8'),
 
44
                    chef_version)
 
45
        else:
 
46
            cc.install_packages(('chef',))
 
47
 
 
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'])
 
52
 
 
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']})
 
58
 
 
59
    chef_args = ['-d']
 
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']])
 
66
                    )
 
67
            firstboot_json_fh.write("]\n\}")
 
68
        chef_args.append('-j /etc/chef/firstboot.json')
 
69
 
 
70
    # and finally, run chef
 
71
    log.debug("running chef-client %s" % chef_args)
 
72
    subprocess.check_call(['/usr/bin/chef-client'] + chef_args)
 
73
 
 
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')
 
78
    chef_version_arg = ""
 
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')
 
85
    except: pass
 
86
    try: os.symlink('/usr/bin/ruby%s' % ruby_version, '/usr/bin/ruby')
 
87
    except: pass
 
88
 
 
89
def get_gem_bin():
 
90
    return '/usr/bin/gem%s' % util.get_cfg_option_str(chef_cfg, 'ruby_version', '1.8')