def __install_kubectl(k8s_conf): """ This function is used to install kubectl at bootstrap node """ lb_ip = "127.0.0.1" lb_ips = config_utils.get_ha_lb_ips(k8s_conf) if len(lb_ips) > 0: lb_ip = lb_ips[0] logger.info("Load balancer ip %s", lb_ip) host_name, ip = config_utils.get_first_master_host(k8s_conf) ha_enabled = len(lb_ips) > 0 pb_vars = { 'ip': ip, 'host_name': host_name, 'ha_enabled': ha_enabled, 'Project_name': config_utils.get_project_name(k8s_conf), 'lb_ip': lb_ip, 'CONFIG_DEMO_FILE': consts.KUBECTL_CONF_TMPLT, 'PROJ_ARTIFACT_DIR': config_utils.get_project_artifact_dir( k8s_conf), 'KUBERNETES_PATH': consts.NODE_K8S_PATH, } pb_vars.update(config_utils.get_proxy_dict(k8s_conf)) ansible_utils.apply_playbook(consts.K8_KUBECTL_INSTALLATION, variables=pb_vars)
def clean_k8(k8s_conf): """ This method is used for cleanup of kubernetes cluster :param k8s_conf :input configuration file """ if k8s_conf: try: logger.info('Cleanup post installation items') ansible_utils.apply_playbook( consts.K8_ENABLE_KUBECTL_CONTEXT, variables={ 'Project_name': config_utils.get_project_name(k8s_conf), 'PROJ_ARTIFACT_DIR': config_utils.get_project_artifact_dir(k8s_conf), }) __clean_up_flannel(k8s_conf) __macvlan_cleanup(k8s_conf) __dhcp_cleanup(k8s_conf) __clean_up_weave(k8s_conf) except Exception as e: logger.warn('Error cleaning up post installtion items - %s', e) try: logger.info('Cleanup k8s (kubespray)') multus_enabled = __get_multus_cni_value_for_dynamic_node(k8s_conf) aconf.clean_up_k8(k8s_conf, multus_enabled) except Exception as e: logger.warn('Error cleaning up k8s - %s', e)
def test_get_project_name(self): """ Ensures the project name value is properly parsed """ project_name = config_utils.get_project_name(self.config) expected = self.config[consts.K8S_KEY][consts.PROJECT_NAME_KEY] self.assertEqual(expected, project_name)
def clean_up_k8(k8s_conf, multus_enabled_str): """ This function is used for clean/Reset the kubernetes cluster """ multus_enabled = str(multus_enabled_str) project_name = config_utils.get_project_name(k8s_conf) kubespray_pb = "{}/{}".format(config_utils.get_kubespray_dir(k8s_conf), consts.KUBESPRAY_CLUSTER_RESET_PB) inv_filename = config_utils.get_kubespray_inv_file(k8s_conf) logger.info('Calling Kubespray reset.yaml with inventory %s', inv_filename) try: pb_vars = {'reset_confirmation': 'yes'} pb_vars.update(ANSIBLE_VERSION_DICT) ansible_utils.apply_playbook( kubespray_pb, host_user=consts.NODE_USER, variables=pb_vars, inventory_file=inv_filename, become_user='******') except Exception as e: logger.warn('Error running playbook %s with error %s', kubespray_pb, e) logger.info("Docker cleanup starts") ips = config_utils.get_host_ips(k8s_conf) try: ansible_utils.apply_playbook( consts.K8_DOCKER_CLEAN_UP_ON_NODES, ips, consts.NODE_USER) except Exception as e: logger.warn('Error running playbook %s with error %s', consts.K8_DOCKER_CLEAN_UP_ON_NODES, e) host_ips = config_utils.get_hostname_ips_dict(k8s_conf) for host_name, ip in host_ips.items(): pb_vars = { 'ip': ip, 'host_name': host_name, 'Project_name': project_name, 'multus_enabled': multus_enabled, } try: ansible_utils.apply_playbook( consts.K8_REMOVE_NODE_K8, [ip], consts.NODE_USER, variables=pb_vars) except Exception as e: logger.warn('Error running playbook %s with error %s', consts.K8_REMOVE_NODE_K8, e) logger.info('EXECUTING REMOVE PROJECT FOLDER PLAY') pb_vars = { 'PROJ_ARTIFACT_DIR': config_utils.get_project_artifact_dir(k8s_conf), 'Project_name': project_name, } try: ansible_utils.apply_playbook(consts.K8_REMOVE_FOLDER, variables=pb_vars) except Exception as e: logger.warn('Error running playbook %s with error %s', consts.K8_REMOVE_FOLDER, e)
def test_get_project_dir(self): """ Ensures the project location is properly parsed """ expected_artifact_dir = os.path.expanduser('~/tmp') project_name = config_utils.get_project_name(self.config) expected = "{}/{}/{}".format(expected_artifact_dir, consts.PROJ_DIR_NAME, project_name) proj_dir = config_utils.get_project_artifact_dir(self.config) self.assertEqual(expected, proj_dir)
def __launch(task, boot_tmplt_file, hb_conf_file, env_file, os_env_file): """ Deploys Adrenaline :param task: the task to perform :param boot_tmplt_file: the path to the snaps-boot template config file :param hb_conf_file: the path to the adrenaline configuration template file :param env_file: the Jinga2 environment file to apply against the boot_tmplt_filename and k8s_tmplt_filename :param os_env_file: if environment is on OpenStack, this file is required for rebooting nodes :raises Exception - exceptions can be raised """ boot_conf, hb_conf, k8s_conf = __generate_confs(boot_tmplt_file, hb_conf_file, env_file) if task.startswith('deploy'): logger.info('Starting Adrenaline boot process') user = hb_conf['node_info']['user'] if task.endswith('all') or task.endswith('boot'): logger.info('Starting Adrenaline boot process') boot_timeout = hb_conf['build_info']['reboot_timeout'] boot_deployer.deploy(boot_conf, hb_conf, user, os_env_file, boot_timeout) logger.info('Completed Adrenaline boot process') if task.endswith('all') or task.endswith('k8s'): k8s_conf_str = yaml.dump(k8s_conf, Dumper=yaml.RoundTripDumper) proj_name = k8s_config_utils.get_project_name(k8s_conf) file_utils.save_string_to_file( k8s_conf_str, '/tmp/deployment-{}.yaml'.format(proj_name)) logger.debug('Deploying k8s - %s', hb_conf) logger.info('Starting Kubernetes deployment') k8s_deployer.deploy(k8s_conf, user) logger.info('Completed Kubernetes deployment') else: logger.info('Starting Adrenaline cleanup') if task.endswith('all') or task.endswith('k8s'): try: k8s_deployer.undeploy(k8s_conf) except: pass if task.endswith('all') or task.endswith('boot'): try: boot_deployer.undeploy(boot_conf) except: pass
def __install_kubectl(k8s_conf): """ This function is used to install kubectl at bootstrap node """ host_name, ip = config_utils.get_first_master_host(k8s_conf) api_ip_url = config_utils.get_k8s_api_url(k8s_conf, ip) pb_vars = { 'ip': ip, 'api_ip_url': api_ip_url, 'node_user': config_utils.get_node_user(k8s_conf), 'host_name': host_name, 'Project_name': config_utils.get_project_name(k8s_conf), 'CONFIG_DEMO_FILE': consts.KUBECTL_CONF_TMPLT, 'PROJ_ARTIFACT_DIR': config_utils.get_project_artifact_dir(k8s_conf), 'KUBERNETES_PATH': consts.NODE_K8S_PATH, } pb_vars.update(config_utils.get_proxy_dict(k8s_conf)) ansible_utils.apply_playbook(consts.K8_KUBECTL_INSTALLATION, variables=pb_vars)