def handle(_name, cfg, cloud, log, _args):
    (hostname, fqdn) = util.get_hostname_fqdn(cfg, cloud)

    manage_hosts = util.get_cfg_option_str(cfg, "manage_etc_hosts", False)
    if manage_hosts in ("True", "true", True, "template"):
        # render from template file
        try:
            if not hostname:
                log.info("manage_etc_hosts was set, but no hostname found")
                return

            util.render_to_file('hosts', '/etc/hosts',
                                {'hostname': hostname, 'fqdn': fqdn})
        except Exception:
            log.warn("failed to update /etc/hosts")
            raise
    elif manage_hosts == "localhost":
        log.debug("managing 127.0.1.1 in /etc/hosts")
        update_etc_hosts(hostname, fqdn, log)
        return
    else:
        if manage_hosts not in ("False", False):
            log.warn("Unknown value for manage_etc_hosts.  Assuming False")
        else:
            log.debug("not managing /etc/hosts")
Ejemplo n.º 2
0
def generate(repo_cfg):
    cloudinit.log.debug(" -- repo_yum handler/generate() --")

    if 'name' not in repo_cfg:
        # Pull name from python
        dist_full = platform.linux_distribution()
        name = dist_full[0]
        repo_cfg['name'] = name
    else:
        name = repo_cfg['name']

    if 'version' not in repo_cfg:
        repo_cfg['version'] = 'latest'

    # Most configs should use mirror in URL specifications
    repo_cfg['mirror'] = get_mirror(repo_cfg)

    if repo_cfg['mirror'] == None:
        cloudinit.log.error("No mirror for yum repo- bailing!")
        raise Exception("No mirror defined for yum-repo!")
    else:
        # render all the templates that match the glob $name*.repo.tmpl
        tmpl_glob = '%s*.repo.tmpl' % name
        for tmpl in glob.glob(os.path.join(util.templatesdir, tmpl_glob)):
            # remove the .tmpl from the glob filename
            reponame = os.path.basename(tmpl)[:-5]
            filename = os.path.join(YUMREPOS, reponame)
            util.render_to_file(reponame, filename, repo_cfg)
Ejemplo n.º 3
0
def apply_locale(locale, cfgfile):
    if os.path.exists('/usr/sbin/locale-gen'):
        subprocess.Popen(['locale-gen', locale]).communicate()
    if os.path.exists('/usr/sbin/update-locale'):
        subprocess.Popen(['update-locale', locale]).communicate()

    util.render_to_file('default-locale', cfgfile, {'locale': locale})
Ejemplo n.º 4
0
def generate(repo_cfg):
    cloudinit.log.debug(" -- repo_yum handler/generate() --")

    if 'name' not in repo_cfg:
        # Pull name from python
        dist_full = platform.linux_distribution()
        name = dist_full[0]
        repo_cfg['name'] = name
    else:
        name = repo_cfg['name']

    if 'version' not in repo_cfg:
        repo_cfg['version'] = 'latest'

    # Most configs should use mirror in URL specifications
    repo_cfg['mirror'] = get_mirror(repo_cfg)

    if repo_cfg['mirror'] == None:
        cloudinit.log.error("No mirror for yum repo- bailing!")
        raise Exception("No mirror defined for yum-repo!")
    else:
        # render all the templates that match the glob $name*.repo.tmpl
        tmpl_glob = '%s*.repo.tmpl' % name
        for tmpl in glob.glob(os.path.join(util.templatesdir, tmpl_glob)):
            # remove the .tmpl from the glob filename
            reponame = os.path.basename(tmpl)[:-5]
            filename = os.path.join(YUMREPOS, reponame)
            util.render_to_file(reponame, filename, repo_cfg)
Ejemplo n.º 5
0
def handle(_name, cfg, cloud, log, _args):
    (hostname, fqdn) = util.get_hostname_fqdn(cfg, cloud)

    manage_hosts = util.get_cfg_option_str(cfg, "manage_etc_hosts", False)
    if manage_hosts in ("True", "true", True, "template"):
        # render from template file
        try:
            if not hostname:
                log.info("manage_etc_hosts was set, but no hostname found")
                return

            util.render_to_file('hosts', '/etc/hosts', {
                'hostname': hostname,
                'fqdn': fqdn
            })
        except Exception:
            log.warn("failed to update /etc/hosts")
            raise
    elif manage_hosts == "localhost":
        log.debug("managing 127.0.1.1 in /etc/hosts")
        update_etc_hosts(hostname, fqdn, log)
        return
    else:
        if manage_hosts not in ("False", False):
            log.warn("Unknown value for manage_etc_hosts.  Assuming False")
        else:
            log.debug("not managing /etc/hosts")
Ejemplo n.º 6
0
def handle(_name, cfg, cloud, log, _args):
    # If there isn't a chef key in the configuration don't do anything
    if 'chef' not in cfg:
        return
    chef_cfg = cfg['chef']

    # ensure the chef directories we use exist
    mkdirs(['/etc/chef', '/var/log/chef', '/var/lib/chef',
            '/var/cache/chef', '/var/backups/chef', '/var/run/chef'])

    # set the validation key based on the presence of either 'validation_key'
    # or 'validation_cert'. In the case where both exist, 'validation_key'
    # takes precedence
    for key in ('validation_key', 'validation_cert'):
        if key in chef_cfg and chef_cfg[key]:
            with open('/etc/chef/validation.pem', 'w') as validation_key_fh:
                validation_key_fh.write(chef_cfg[key])
            break

    # create the chef config from template
    util.render_to_file('chef_client.rb', '/etc/chef/client.rb',
        {'server_url': chef_cfg['server_url'],
         'node_name': util.get_cfg_option_str(chef_cfg, 'node_name',
                                          cloud.datasource.get_instance_id()),
         'environment': util.get_cfg_option_str(chef_cfg, 'environment',
                                                '_default'),
         'validation_name': chef_cfg['validation_name']})

    # set the firstboot json
    with open('/etc/chef/firstboot.json', 'w') as firstboot_json_fh:
        initial_json = {}
        if 'run_list' in chef_cfg:
            initial_json['run_list'] = chef_cfg['run_list']
        if 'initial_attributes' in chef_cfg:
            initial_attributes = chef_cfg['initial_attributes']
            for k in initial_attributes.keys():
                initial_json[k] = initial_attributes[k]
        firstboot_json_fh.write(json.dumps(initial_json))

    # If chef is not installed, we install chef based on 'install_type'
    if not os.path.isfile('/usr/bin/chef-client'):
        install_type = util.get_cfg_option_str(chef_cfg, 'install_type',
                                               'packages')
        if install_type == "gems":
            # this will install and run the chef-client from gems
            chef_version = util.get_cfg_option_str(chef_cfg, 'version', None)
            ruby_version = util.get_cfg_option_str(chef_cfg, 'ruby_version',
                                                   ruby_version_default)
            install_chef_from_gems(ruby_version, chef_version)
            # and finally, run chef-client
            log.debug('running chef-client')
            subprocess.check_call(['/usr/bin/chef-client', '-d', '-i', '1800',
                                   '-s', '20'])
        else:
            # this will install and run the chef-client from packages
            cc.install_packages(('chef',))
def generate_sources_list(codename, mirror):
    util.render_to_file('sources.list', '/etc/apt/sources.list', \
        {'mirror': mirror, 'codename': codename})
Ejemplo n.º 8
0
def handle(_name, cfg, cloud, log, _args):
    # If there isn't a chef key in the configuration don't do anything
    if 'chef' not in cfg:
        return
    chef_cfg = cfg['chef']

    # ensure the chef directories we use exist
    mkdirs([
        '/etc/chef', '/var/log/chef', '/var/lib/chef', '/var/cache/chef',
        '/var/backups/chef', '/var/run/chef'
    ])

    # set the validation key based on the presence of either 'validation_key'
    # or 'validation_cert'. In the case where both exist, 'validation_key'
    # takes precedence
    for key in ('validation_key', 'validation_cert'):
        if key in chef_cfg and chef_cfg[key]:
            with open('/etc/chef/validation.pem', 'w') as validation_key_fh:
                validation_key_fh.write(chef_cfg[key])
            break

    # create the chef config from template
    util.render_to_file(
        'chef_client.rb', '/etc/chef/client.rb', {
            'server_url':
            chef_cfg['server_url'],
            'node_name':
            util.get_cfg_option_str(chef_cfg, 'node_name',
                                    cloud.datasource.get_instance_id()),
            'environment':
            util.get_cfg_option_str(chef_cfg, 'environment', '_default'),
            'validation_name':
            chef_cfg['validation_name']
        })

    # set the firstboot json
    with open('/etc/chef/firstboot.json', 'w') as firstboot_json_fh:
        initial_json = {}
        if 'run_list' in chef_cfg:
            initial_json['run_list'] = chef_cfg['run_list']
        if 'initial_attributes' in chef_cfg:
            initial_attributes = chef_cfg['initial_attributes']
            for k in initial_attributes.keys():
                initial_json[k] = initial_attributes[k]
        firstboot_json_fh.write(json.dumps(initial_json))

    # If chef is not installed, we install chef based on 'install_type'
    if not os.path.isfile('/usr/bin/chef-client'):
        install_type = util.get_cfg_option_str(chef_cfg, 'install_type',
                                               'packages')
        if install_type == "gems":
            # this will install and run the chef-client from gems
            chef_version = util.get_cfg_option_str(chef_cfg, 'version', None)
            ruby_version = util.get_cfg_option_str(chef_cfg, 'ruby_version',
                                                   ruby_version_default)
            install_chef_from_gems(ruby_version, chef_version)
            # and finally, run chef-client
            log.debug('running chef-client')
            subprocess.check_call(
                ['/usr/bin/chef-client', '-d', '-i', '1800', '-s', '20'])
        else:
            # this will install and run the chef-client from packages
            cc.install_packages(('chef', ))