def image_push(image_url, namespace): """ Function to push images to destination Args: image_url (str): Image url container image repo link namespace (str): Image to be uploaded namespace Returns: registry_path (str): Uploaded image path """ image = image_url.split("/")[-1] image_path = f"image-registry.openshift-image-registry.svc:5000/{namespace}/{image}" tag_cmd = f"podman tag {image_url} {image_path}" push_cmd = f"podman push {image_path}" cmd_list = get_oc_podman_login_cmd() cmd_list.append(tag_cmd) cmd_list.append(push_cmd) master_list = node.get_master_nodes() ocp_obj = ocp.OCP() ocp_obj.exec_oc_debug_cmd(node=master_list[0], cmd_list=cmd_list) logger.info(f"Pushed {image_path} to registry") image_list_all() return image_path
def get_oc_podman_login_cmd(skip_tls_verify=True): """ Function to get oc and podman login commands on node Args: skip_tls_verify (bool): If true, the server's certificate will not be checked for validity Returns: cmd_list (list): List of cmd for oc/podman login """ user = config.RUN["username"] filename = os.path.join(config.ENV_DATA["cluster_path"], config.RUN["password_location"]) with open(filename) as f: password = f.read().strip() cluster_name = config.ENV_DATA["cluster_name"] base_domain = config.ENV_DATA["base_domain"] cmd_list = [ "export KUBECONFIG=/home/core/auth/kubeconfig", f"oc login -u {user} -p {password} " f"https://api-int.{cluster_name}.{base_domain}:6443" f" --insecure-skip-tls-verify={skip_tls_verify}", f"podman login -u {user} -p $(oc whoami -t) image-registry.openshift-image-registry.svc:5000", ] master_list = node.get_master_nodes() helpers.rsync_kubeconf_to_node(node=master_list[0]) return cmd_list
def get_node_info(self, node_type="master"): """ Getting node type hardware information and update the main environment dictionary. Args: node_type (str): the node type to collect data about, can be : master / worker - the default is master """ if node_type == "master": nodes = node.get_master_nodes() elif node_type == "worker": nodes = node.get_worker_nodes() else: log.warning(f"Node type ({node_type}) is invalid") return oc_cmd = OCP(namespace=defaults.ROOK_CLUSTER_NAMESPACE) self.environment[f"{node_type}_nodes_num"] = len(nodes) self.environment[f"{node_type}_nodes_cpu_num"] = oc_cmd.exec_oc_debug_cmd( node=nodes[0], cmd_list=["lscpu | grep '^CPU(s):' | awk '{print $NF}'"], ).rstrip() self.environment[f"{node_type}_nodes_memory"] = oc_cmd.exec_oc_debug_cmd( node=nodes[0], cmd_list=["free | grep Mem | awk '{print $2}'"] ).rstrip()
def start_baremetal_machines_with_ipmi_ctx(self, ipmi_ctxs, wait=True): """ Start Baremetal Machines using Ipmi ctx Args: ipmi_ctxs (list): List of BM ipmi_ctx wait (bool): Wait for BMs to start """ for ipmi_ctx in ipmi_ctxs: ipmi_ctx.chassis_control_power_up() if wait: for ipmi_ctx in ipmi_ctxs: for status in TimeoutSampler(600, 5, self.get_power_status, ipmi_ctx): logger.info( f"Waiting for Baremetal Machine to power on. " f"Current Baremetal status: {status}" ) if status == VM_POWERED_ON: logger.info("Baremetal Machine reached poweredOn status") break wait_for_cluster_connectivity(tries=400) wait_for_nodes_status( node_names=get_master_nodes(), status=constants.NODE_READY, timeout=800 ) wait_for_nodes_status( node_names=get_worker_nodes(), status=constants.NODE_READY, timeout=800 )
def create_dummy_zone_labels(): """ Create dummy zone labels on cluster nodes: try to label all master and worker nodes based on values of ``worker_availability_zones`` and ``master_availability_zones`` options, but only if there are no zone labels already defined. Raises: UnexpectedDeploymentConfiguration: when either cluster or ocs-ci config file are in conflict with dummy zone labels. """ logger.info("trying to setup dummy_zone_node_labels") if are_zone_labels_missing(): to_label = [ ("master_availability_zones", get_master_nodes()), ("worker_availability_zones", get_worker_nodes()), ] for zone_opt, nodes in to_label: zones = config.ENV_DATA.get(zone_opt) if zones is None: msg = f"{zone_opt} is not defined in ENV_DATA conf" logger.error(msg) raise exceptions.UnexpectedDeploymentConfiguration(msg) assign_dummy_zones(zones, nodes) else: # don't use dummy zone labeling on a cluster with actuall zones msg = ("Cluster in unexpected state before dummy zone labeling: " "at least one node already have a zone label.") logger.error(msg) raise exceptions.UnexpectedDeploymentConfiguration(msg)
def start_powernodes_machines(self, powernode_machines, timeout=900, wait=True, force=True): """ Start PowerNode Machines Args: powernode_machines (list): List of PowerNode machines timeout (int): time in seconds to wait for node to reach 'not ready' state, and 'ready' state. wait (bool): Wait for PowerNodes to start - for future use force (bool): True for PowerNode ungraceful power off, False for graceful PowerNode shutdown - for future use """ ocpversion = get_ocp_version("-") for node in powernode_machines: result = exec_cmd( f"sudo virsh start test-ocp{ocpversion}-{node.name}") logger.info(f"Result of shutdown {result}") wait_for_cluster_connectivity(tries=900) wait_for_nodes_status(node_names=get_master_nodes(), status=constants.NODE_READY, timeout=timeout) wait_for_nodes_status(node_names=get_worker_nodes(), status=constants.NODE_READY, timeout=timeout)
def image_list_all(): """ Function to list the images in the podman registry Returns: image_list_output (str): Images present in cluster """ cmd_list = get_oc_podman_login_cmd() cmd_list.append("podman image list --format json") master_list = node.get_master_nodes() ocp_obj = ocp.OCP() return ocp_obj.exec_oc_debug_cmd(node=master_list[0], cmd_list=cmd_list)
def image_pull(image_url): """ Function to pull images from repositories Args: image_url (str): Image url container image repo link """ cmd_list = get_oc_podman_login_cmd() cmd_list.append(f"podman pull {image_url}") master_list = node.get_master_nodes() ocp_obj = ocp.OCP() ocp_obj.exec_oc_debug_cmd(node=master_list[0], cmd_list=cmd_list)
def image_rm(registry_path, image_url): """ Function to remove images from registry Args: registry_path (str): Image registry path image_url (str): Image url container image repo link """ cmd_list = get_oc_podman_login_cmd() cmd_list.append(f"podman rmi {registry_path}") cmd_list.append(f"podman rmi {image_url}") master_list = node.get_master_nodes() ocp_obj = ocp.OCP() ocp_obj.exec_oc_debug_cmd(node=master_list[0], cmd_list=cmd_list) logger.info(f"Image {registry_path} rm successful")
def start_baremetal_machines(self, baremetal_machine, wait=True): """ Start Baremetal Machines Args: baremetal_machine (list): BM objects wait (bool): Wait for BMs to start """ for node in baremetal_machine: if self.mgmt_details[node.name]: ipmi_ctx = self.get_ipmi_ctx( host=self.mgmt_details[node.name]["mgmt_console"], user=self.mgmt_details[node.name]["mgmt_username"], password=self.mgmt_details[node.name]["mgmt_password"], ) logger.info(f"Powering On {node.name}") ipmi_ctx.chassis_control_power_up() if wait: if self.mgmt_details[node.name]: ipmi_ctx = self.get_ipmi_ctx( host=self.mgmt_details[node.name]["mgmt_console"], user=self.mgmt_details[node.name]["mgmt_username"], password=self.mgmt_details[node.name]["mgmt_password"], ) for status in TimeoutSampler( 600, 5, self.get_power_status, ipmi_ctx ): logger.info( f"Waiting for Baremetal Machine {node.name} to power on. " f"Current Baremetal status: {status}" ) if status == VM_POWERED_ON: logger.info( f"Baremetal Machine {node.name} reached poweredOn status" ) ipmi_ctx.session.close() break wait_for_cluster_connectivity(tries=400) wait_for_nodes_status( node_names=get_master_nodes(), status=constants.NODE_READY, timeout=800 ) wait_for_nodes_status( node_names=get_worker_nodes(), status=constants.NODE_READY, timeout=800 )
def enable_route_and_create_ca_for_registry_access(): """ Function to enable route and to create ca, copy to respective location for registry access Raises: AssertionError: When failure in enabling registry default route """ ocp_obj = ocp.OCP(kind=constants.CONFIG, namespace=constants.OPENSHIFT_IMAGE_REGISTRY_NAMESPACE) assert ocp_obj.patch( resource_name=constants.IMAGE_REGISTRY_RESOURCE_NAME, params='{"spec": {"defaultRoute": true}}', format_type="merge", ), "Registry pod defaultRoute enable is not success" logger.info("Enabled defaultRoute to true") ocp_obj = ocp.OCP() crt_cmd = (f"get secret {constants.DEFAULT_ROUTE_CRT} " f"-n {constants.OPENSHIFT_INGRESS_NAMESPACE} -o yaml") crt_dict = ocp_obj.exec_oc_cmd(command=crt_cmd) crt = crt_dict.get("data").get("tls.crt") route = get_default_route_name() if not os.path.exists("/tmp/secret"): run_cmd(cmd="mkdir /tmp/secret") with open(f"/tmp/secret/{route}.crt", "wb") as temp: temp.write(base64.b64decode(crt)) master_list = node.get_master_nodes() ocp.rsync( src="/tmp/secret/", dst="/etc/pki/ca-trust/source/anchors", node=master_list[0], dst_node=True, ) ocp_obj.exec_oc_debug_cmd(node=master_list[0], cmd_list=["update-ca-trust enable"]) logger.info( "Created base64 secret, copied to source location and enabled ca-trust" )