Exemplo n.º 1
0
def handle(name, cloud_config, cloud, log, args):
    """Cloud-init processing function"""
    tag = MODULE_NAME
    # force /PLATFORM to KVM generic
    with open('/PLATFORM', 'w') as platform:
        platform.write(
            "platform=Z100\nfamily=0xC0000000\nhost=Z100\nsystype=0x71\n")
    # force more memory for control plane sevices to complete
    tmos_onboard_utils.run_cmd("/usr/bin/setdb provision.extramb 500")
    tmos_onboard_utils.run_cmd("/usr/bin/setdb restjavad.useextramb true")
    # find SSH key in vendor_data file
    if os.path.exists(VENDOR_DATA_RAW_FILE):
        LOG.debug('attempting to extract SSH key from vendor_data')
        public_keys = []
        with open(VENDOR_DATA_RAW_FILE, 'r') as vdf:
            for line in vdf:
                if 'ssh-rsa' in line:
                    public_keys.append(line[line.index('ssh-rsa'):])
        if public_keys:
            LOG.debug('injecting %d SSH authorized keys for root' %
                      len(public_keys))
            tmos_onboard_utils.inject_public_ssh_keys(public_keys)
            # randomize root and admin accounts
            LOG.debug('randomizing default account passwords')
            root_password = '******' + ''.join([
                random.choice(string.ascii_letters + string.digits)
                for n in range(32)
            ]) + '\n'
            try:
                util.subp(['chpasswd'], root_password)
            except Exception as e:
                LOG.error('failed to randomize password for root user: %s', e)
            admin_password = '******' + ''.join([
                random.choice(string.ascii_letters + string.digits)
                for n in range(32)
            ]) + '\n'
            try:
                util.subp(['chpasswd'], admin_password)
            except Exception as e:
                LOG.error('failed to randomize password for admin user: %s', e)
    try:
        # forcing hostname from metadata file
        if "local-hostname" in cloud.datasource.metadata:
            LOG.debug('forcing localhost name to %s' %
                      cloud.datasource.metadata['local-hostname'])
            tmos_onboard_utils.wait_for_mcpd()
            tmos_onboard_utils.run_cmd(
                "tmsh modify sys global-settings hostname %s.local" %
                cloud.datasource.metadata['local-hostname'])
    except Exception as ex:
        LOG.debug('exception setting hostname from metadata: %s', ex)
def handle(name, cloud_config, cloud, log, args):
    """Cloud-init processing function"""
    tag = MODULE_NAME
    enabled = False
    if tag in cloud_config:
        try:
            enabled = bool(cloud_config[tag]['enabled'])
        except Exception:
            LOG.error("%s missing enabled attribute", tag)
            return
    if enabled:
        mgmt_ip = None
        if 'ip' in cloud_config[tag]:
            mgmt_ip = cloud_config[tag]['ip']
        if not mgmt_ip:
            LOG.error("%s missing ip attribute", tag)
            return
        mgmt_netmask = None
        if 'netmask' in cloud_config[tag]:
            mgmt_netmask = cloud_config[tag]['netmask']
        mgmt_gw = None
        if 'gw' in cloud_config[tag]:
            mgmt_gw = cloud_config[tag]['gw']
        mgmt_mtu = 1500
        if 'mtu' in cloud_config[tag]:
            mgmt_mtu = cloud_config[tag]['mtu']
        hostname = None
        if 'hostname' in cloud_config[tag]:
            hostname = cloud_config[tag]['hostname']
        nameservers = []
        if 'nameservers' in cloud_config[tag]:
            nameservers = cloud_config[tag]['nameservers']
        searchdomain = None
        if 'searchdomain' in cloud_config[tag]:
            searchdomain = cloud_config[tag]['searchdomain']
        ntpservers = []
        if 'ntpservers' in cloud_config[tag]:
            ntpservers = cloud_config[tag]['ntpservers']
        post_onboard_enabled = False
        if 'post_onboard_enabled' in cloud_config[tag]:
            post_onboard_enabled = bool(
                cloud_config[tag]['post_onboard_enabled'])
        post_onboard_commands = []
        if 'post_onboard_commands' in cloud_config[
                tag] and post_onboard_enabled:
            post_onboard_commands = cloud_config[tag]['post_onboard_commands']
        mgmt_cidr = mgmt_ip
        if mgmt_ip.find('/') < 0:
            if not mgmt_netmask:
                LOG.error(
                    "%s ip is not in CIDR format and no netmask supplied", tag)
                return
            if tmos_onboard_utils.is_v4(mgmt_ip):
                mgmt_nm = tmos_onboard_utils.ipv4_cidr_from_netmask(
                    mgmt_netmask)
                mgmt_cidr = "%s/%s" % (mgmt_ip, mgmt_nm)
            else:
                mgmt_cidr = "%s/64" % mgmt_ip
        keys = []
        if "ssh_authorized_keys" in cloud_config:
            cfgkeys = cloud_config["ssh_authorized_keys"]
            keys.extend(cfgkeys)
            LOG.info('%s found ssh_authorized_keys', tag)
        tmos_onboard_utils.inject_public_ssh_keys(keys)
        # download referenced extensions
        icontrollx_trusted_sources = True
        if 'icontrollx_trusted_sources' in cloud_config[tag]:
            icontrollx_trusted_sources = cloud_config[tag][
                'icontrollx_trusted_sources']
        icontrollx_package_urls = []
        if 'icontrollx_package_urls' in cloud_config[tag]:
            icontrollx_package_urls = cloud_config[tag][
                'icontrollx_package_urls']
            LOG.info('%s found icontrollx_package_urls', tag)
        create_onboard_artifacts(mgmt_cidr, mgmt_gw, mgmt_mtu, hostname,
                                 nameservers, searchdomain, ntpservers,
                                 post_onboard_commands)
        phone_home_url = None
        if 'phone_home_url' in cloud_config[tag]:
            phone_home_url = cloud_config[tag]['phone_home_url']

        phone_home_url_verify_tls = True
        if 'phone_home_url_verify_tls' in cloud_config[tag]:
            phone_home_url_verify_tls = cloud_config[tag][
                'phone_home_url_verify_tls']

        phone_home_cli = None
        if 'phone_home_cli' in cloud_config[tag]:
            phone_home_cli = cloud_config[tag]['phone_home_cli']

        try:
            onboard(icontrollx_trusted_sources, icontrollx_package_urls,
                    phone_home_url, phone_home_url_verify_tls, phone_home_cli)
        except Exception as err:
            LOG.error('onboard exception - %s', err)
Exemplo n.º 3
0
def handle(name, cloud_config, cloud, log, args):
    """Cloud-init processing function"""
    tag = MODULE_NAME
    enabled = False

    if tag in cloud_config:
        try:
            enabled = bool(cloud_config[tag]['enabled'])
        except Exception:
            LOG.error('%s missing enabled.. exiting', tag)
            return

    if enabled:
        LOG.info("%s enabled", tag)
        keys = []
        if "ssh_authorized_keys" in cloud_config:
            cfgkeys = cloud_config["ssh_authorized_keys"]
            keys.extend(cfgkeys)
            LOG.info('%s found ssh_authorized_keys', tag)
        tmos_onboard_utils.inject_public_ssh_keys(keys)

        # download referenced extensions
        icontrollx_trusted_sources = True
        if 'icontrollx_trusted_sources' in cloud_config[tag]:
            icontrollx_trusted_sources = cloud_config[tag][
                'icontrollx_trusted_sources']
        icontrollx_package_urls = []
        if 'icontrollx_package_urls' in cloud_config[tag]:
            icontrollx_package_urls = cloud_config[tag][
                'icontrollx_package_urls']
            LOG.info('%s found icontrollx_package_urls', tag)
        for ext_url in icontrollx_package_urls:
            LOG.info('downloading: %s', ext_url)
            if tmos_onboard_utils.download_extension(ext_url):
                LOG.info(
                    'extension %s downloaded in discovered iControl extensions',
                    ext_url)
            else:
                LOG.error('exenstion %s could not be donwloaded', ext_url)

        do_declaration = None
        if 'do_declaration' in cloud_config[tag]:
            do_declaration = cloud_config[tag]['do_declaration']
            LOG.info('%s found do_declaration', tag)

        ts_declaration = None
        if 'ts_declaration' in cloud_config[tag]:
            ts_declaration = cloud_config[tag]['ts_declaration']
            LOG.info('%s found ts_declaration', tag)

        as3_declaration = None
        if 'as3_declaration' in cloud_config[tag]:
            as3_declaration = cloud_config[tag]['as3_declaration']
            LOG.info('%s found as3_declaration', tag)

        post_onboard_enabled = False
        if 'post_onboard_enabled' in cloud_config[tag]:
            post_onboard_enabled = bool(
                cloud_config[tag]['post_onboard_enabled'])
        post_onboard_commands = []
        if 'post_onboard_commands' in cloud_config[tag]:
            post_onboard_commands = cloud_config[tag]['post_onboard_commands']

        phone_home_url = None
        if 'phone_home_url' in cloud_config[tag]:
            phone_home_url = cloud_config[tag]['phone_home_url']

        phone_home_url_verify_tls = True
        if 'phone_home_url_verify_tls' in cloud_config[tag]:
            phone_home_url_verify_tls = cloud_config[tag][
                'phone_home_url_verify_tls']

        phone_home_url_metadata = {}
        if 'phone_home_url_metadata' in cloud_config[tag]:
            phone_home_url_metadata = cloud_config[tag][
                'phone_home_url_metadata']

        phone_home_cli = None
        if 'phone_home_cli' in cloud_config[tag]:
            phone_home_cli = cloud_config[tag]['phone_home_cli']

        try:
            onboard(do_declaration, as3_declaration, ts_declaration,
                    icontrollx_trusted_sources, post_onboard_enabled,
                    post_onboard_commands, phone_home_url,
                    phone_home_url_verify_tls, phone_home_url_metadata,
                    phone_home_cli)
        except Exception as err:
            LOG.error('onboard exception - %s', err)
        try:
            tmos_onboard_utils.clean()
        except Exception as err:
            LOG.error('onboard cleanup exception - %s', err)