~smoser/ubuntu/quantal/cloud-init/sru

« back to all changes in this revision

Viewing changes to .pc/catchup-442-445.patch/cloudinit/CloudConfig/cc_chef.py

  • Committer: Package Import Robot
  • Author(s): Scott Moser
  • Date: 2011-10-06 17:11:29 UTC
  • mfrom: (132.1.15 oneiric)
  • Revision ID: package-import@ubuntu.com-20111006171129-7z3pzqavrxt6gbs0
Tags: 0.6.1-0ubuntu22
DataSourceEc2: catch a socket timeout when with a slow metadata
service (LP: #869492).

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
    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']
 
39
            else:
 
40
                chef_version = None
 
41
            install_chef_from_gems(
 
42
                    util.get_cfg_option_str(chef_cfg, 'ruby_version', '1.8'),
 
43
                    chef_version)
 
44
        else:
 
45
            cc.install_packages(('chef',))
 
46
 
 
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'])
 
51
 
 
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']})
 
57
 
 
58
    chef_args = ['-d']
 
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')
 
67
 
 
68
    # and finally, run chef
 
69
    log.debug("running chef-client %s" % chef_args)
 
70
    subprocess.check_call(['/usr/bin/chef-client'] + chef_args)
 
71
 
 
72
def install_chef_from_gems(ruby_version, chef_version = None):
 
73
    cc.install_packages(ruby_packages[ruby_version])
 
74
    chef_version_arg = ""
 
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')
 
81
    except: pass
 
82
    try: os.symlink('/usr/bin/ruby%s' % ruby_version, '/usr/bin/ruby')
 
83
    except: pass