def onboard(trusted_sources, icontrollx_package_urls, phone_home_url,
            phone_home_url_verify_tls, phone_home_cli):
    """Implements the onboarding business logic"""
    onboard_script = TMSH_CMD_FILE_DIR + '/onboard.sh'
    if os.path.isfile(onboard_script):
        util.del_file(onboard_script)
    script_files = os.listdir(TMSH_CMD_FILE_DIR)
    script_files.sort()
    with open(onboard_script, 'w') as obs:
        obs.write("#!/bin/bash\n\n")
        obs.write("function check_mcpd_up() {\n")
        obs.write("    checks=0\n")
        obs.write("    while [ $checks -lt 120 ]; do\n")
        obs.write(
            "        if tmsh -a show sys mcp-state field-fmt 2> /dev/null | grep -q running; then\n"
        )
        obs.write("            break\n")
        obs.write("        fi\n")
        obs.write("        echo 'waiting for mcpd to reach running state'\n")
        obs.write("        let checks=checks+1\n")
        obs.write("        sleep 10\n")
        obs.write("    done\n")
        obs.write("}\n\n")
        obs.write("function exec_phases() {\n")
        for script_file in script_files:
            obs.write("    /bin/bash %s/%s\n" %
                      (TMSH_CMD_FILE_DIR, script_file))
        obs.write("}\n\n")
        obs.write("check_mcpd_up\n")
        obs.write("exec_phases\n")
        obs.write("echo 1 > " + ONBOARD_COMPLETE_FLAG_FILE + '\n')
    os.chmod(onboard_script, 0775)
    with open(LOG_FILE, 'a+') as onboardlog:
        subprocess.call(['nohup', 'sh', '-c', onboard_script, '&'],
                        stdout=onboardlog,
                        stderr=onboardlog)
    end_time = time.time() + ONBOARD_TIMEOUT
    while (end_time - time.time()) > 0:
        if not os.path.exists(ONBOARD_COMPLETE_FLAG_FILE):
            time.sleep(1)
        else:
            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)
            end_time = 0
            tmos_onboard_utils.install_extensions(trusted_sources)
    onboard_status = 'SUCCESS'

    if not os.path.exists(ONBOARD_COMPLETE_FLAG_FILE):
        LOG.error('Onboarding scripts failed to complete...exiting')
        onboard_status = ERROR

    if phone_home_url:
        tmos_onboard_utils.phone_home(phone_home_url, False, False,
                                      onboard_status,
                                      phone_home_url_verify_tls)

    if phone_home_cli and onboard_status == SUCCESS:
        tmos_onboard_utils.run_cmd(phone_home_cli)

    LOG.info('onboarding ended with status: %s', onboard_status)
Ejemplo n.º 2
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)