Move the installation code to its own function

This commit is contained in:
Joshua Harlow 2014-10-10 19:09:27 -07:00
parent c67916d091
commit 806e6520ff

View File

@ -142,36 +142,42 @@ def handle(name, cfg, cloud, log, _args):
initial_json[k] = initial_attributes[k] initial_json[k] = initial_attributes[k]
util.write_file(fb_filename, json.dumps(initial_json)) util.write_file(fb_filename, json.dumps(initial_json))
# If chef is not installed, we install chef based on 'install_type' # Try to install chef, if its not already installed...
if (not os.path.isfile('/usr/bin/chef-client') or install_chef(cloud, chef_cfg, log)
util.get_cfg_option_bool(chef_cfg,
'force_install', default=False)):
install_type = util.get_cfg_option_str(chef_cfg, 'install_type',
'packages') def install_chef(cloud, chef_cfg, log):
if install_type == "gems": # If chef is not installed, we install chef based on 'install_type'
# this will install and run the chef-client from gems if os.path.isfile('/usr/bin/chef-client'):
chef_version = util.get_cfg_option_str(chef_cfg, 'version', None) return
ruby_version = util.get_cfg_option_str(chef_cfg, 'ruby_version', if not util.get_cfg_option_bool(chef_cfg, 'force_install', default=False):
RUBY_VERSION_DEFAULT) return
install_chef_from_gems(cloud.distro, ruby_version, chef_version) install_type = util.get_cfg_option_str(chef_cfg, 'install_type',
# and finally, run chef-client 'packages')
log.debug('Running chef-client') if install_type == "gems":
util.subp(['/usr/bin/chef-client', # This will install and run the chef-client from gems
'-d', '-i', '1800', '-s', '20'], capture=False) chef_version = util.get_cfg_option_str(chef_cfg, 'version', None)
elif install_type == 'packages': ruby_version = util.get_cfg_option_str(chef_cfg, 'ruby_version',
# this will install and run the chef-client from packages RUBY_VERSION_DEFAULT)
cloud.distro.install_packages(('chef',)) install_chef_from_gems(cloud.distro, ruby_version, chef_version)
elif install_type == 'omnibus': # And finally, run chef-client
url = util.get_cfg_option_str(chef_cfg, "omnibus_url", OMNIBUS_URL) log.debug('Running chef-client')
content = url_helper.readurl(url=url, retries=5) util.subp(['/usr/bin/chef-client',
with util.tempdir() as tmpd: '-d', '-i', '1800', '-s', '20'], capture=False)
# use tmpd over tmpfile to avoid 'Text file busy' on execute elif install_type == 'packages':
tmpf = "%s/chef-omnibus-install" % tmpd # This will install and run the chef-client from packages
util.write_file(tmpf, str(content), mode=0700) cloud.distro.install_packages(('chef',))
util.subp([tmpf], capture=False) elif install_type == 'omnibus':
else: # This will install as a omnibus unified package
log.warn("Unknown chef install type %s", install_type) url = util.get_cfg_option_str(chef_cfg, "omnibus_url", OMNIBUS_URL)
content = url_helper.readurl(url=url, retries=5)
with util.tempdir() as tmpd:
# Use tmpdir over tmpfile to avoid 'text file busy' on execute
tmpf = "%s/chef-omnibus-install" % tmpd
util.write_file(tmpf, str(content), mode=0700)
util.subp([tmpf], capture=False)
else:
log.warn("Unknown chef install type '%s'", install_type)
def get_ruby_packages(version): def get_ruby_packages(version):
@ -190,9 +196,9 @@ def install_chef_from_gems(ruby_version, chef_version, distro):
util.sym_link('/usr/bin/ruby%s' % ruby_version, '/usr/bin/ruby') util.sym_link('/usr/bin/ruby%s' % ruby_version, '/usr/bin/ruby')
if chef_version: if chef_version:
util.subp(['/usr/bin/gem', 'install', 'chef', util.subp(['/usr/bin/gem', 'install', 'chef',
'-v %s' % chef_version, '--no-ri', '-v %s' % chef_version, '--no-ri',
'--no-rdoc', '--bindir', '/usr/bin', '-q'], capture=False) '--no-rdoc', '--bindir', '/usr/bin', '-q'], capture=False)
else: else:
util.subp(['/usr/bin/gem', 'install', 'chef', util.subp(['/usr/bin/gem', 'install', 'chef',
'--no-ri', '--no-rdoc', '--bindir', '--no-ri', '--no-rdoc', '--bindir',
'/usr/bin', '-q'], capture=False) '/usr/bin', '-q'], capture=False)