def bind_infra_env(self, name, cluster, force=False): infra_env_id = self.get_infra_env_id(name) cluster_id = self.get_cluster_id(cluster) for host in self.client.v2_list_hosts(infra_env_id=infra_env_id): host_id = host['id'] host_name = host['requested_hostname'] host_cluster_id = host.get('cluster_id') if host_cluster_id is not None: if host_cluster_id == cluster_id: info(f"Host {host_name} already bound to Cluster {cluster}") continue elif not force: info(f"Host {host_name} already bound another cluster") continue else: host_cluster = self.get_cluster_name(host_cluster_id) info(f"Unbinding Host {host_name} from Cluster {host_cluster}") self.client.unbind_host(infra_env_id=infra_env_id, host_id=host_id) while True: currenthost = self.client.v2_get_host(infra_env_id=infra_env_id, host_id=host_id) currentstatus = currenthost.status if currentstatus == 'known-unbound': break else: info(f"Waiting 5s for host {host_name} to get unbound") sleep(5) info(f"Binding Host {host_name} to Cluster {cluster}") bind_host_params = {'cluster_id': cluster_id} bind_host_params = models.BindHostParams(**bind_host_params) self.client.bind_host(infra_env_id, host_id, bind_host_params)
def set_disconnected_ignition_config_override(self, infra_env_id=None, overrides={}): ignition_config_override = None disconnected_url = overrides.get('disconnected_url') ca = overrides.get('disconnected_ca') if ca is None: if 'installconfig' in overrides and isinstance(overrides['installconfig'], dict)\ and 'additionalTrustBundle' in overrides['installconfig']: info("using cert from installconfig/additionalTrustBundle") ca = overrides['installconfig']['additionalTrustBundle'] if 'ignition_config_override' not in overrides and disconnected_url is not None and ca is not None: ignition_version = overrides.get('ignition_version') if ignition_version is None: ori = self.client.v2_download_infra_env_files(infra_env_id=infra_env_id, file_name="discovery.ign", _preload_content=False) ignition_version = json.loads(ori.read().decode("utf-8"))['ignition']['version'] ailibdir = os.path.dirname(warning.__code__.co_filename) with open(f"{ailibdir}/registries.conf.templ") as f: data = f.read() registries = data % {'url': disconnected_url} registries_encoded = base64.b64encode(registries.encode()).decode("UTF-8") ca_encoded = base64.b64encode(ca.encode()).decode("UTF-8") fil1 = {"path": "/etc/containers/registries.conf", "mode": 420, "overwrite": True, "user": {"name": "root"}, "contents": {"source": f"data:text/plain;base64,{registries_encoded}"}} fil2 = {"path": "/etc/pki/ca-trust/source/anchors/domain.crt", "mode": 420, "overwrite": True, "user": {"name": "root"}, "contents": {"source": f"data:text/plain;base64,{ca_encoded}"}} ignition_config_override = json.dumps({"ignition": {"version": ignition_version}, "storage": {"files": [fil1, fil2]}}) return ignition_config_override
def unbind_infra_env(self, name): infra_env_id = self.get_infra_env_id(name) for host in self.client.v2_list_hosts(infra_env_id=infra_env_id): host_id = host['id'] host_cluster_id = host.get('cluster_id') host_name = host['requested_hostname'] if host_cluster_id is None: info(f"Host {host_name} already unbound") continue info(f"Unbinding Host {host_name}") self.client.unbind_host(infra_env_id=infra_env_id, host_id=host_id)
def wait_cluster(self, name): cluster_id = self.get_cluster_id(name) while True: try: cluster_info = self.client.v2_get_cluster(cluster_id=cluster_id).to_dict() install_completed_at = str(cluster_info['install_completed_at']) if install_completed_at != '0001-01-01 00:00:00+00:00': return else: info(f"Waiting 5s for cluster {name} to be installed") sleep(5) self.refresh_token(self.token, self.offlinetoken) except KeyboardInterrupt: info("Leaving as per your request") sys.exit(0)
def wait_hosts(self, name, number=3, filter_installed=False): infra_env_id = self.get_infra_env_id(name) while True: try: current_hosts = [host for host in self.client.v2_list_hosts(infra_env_id=infra_env_id)] if filter_installed: current_hosts = [host for host in current_hosts if host['status'] != 'installed'] if len(current_hosts) >= number: return else: info("Waiting 5s for hosts to reach expected number") sleep(5) self.refresh_token(self.token, self.offlinetoken) except KeyboardInterrupt: info("Leaving as per your request") sys.exit(0)
def upload_manifests(self, name, directory, openshift=False): cluster_id = self.get_cluster_id(name) if not os.path.exists(directory): error(f"Directory {directory} not found") sys.exit(1) elif not os.path.isdir(directory): error(f"{directory} is not a directory") sys.exit(1) manifests_api = api.ManifestsApi(api_client=self.api) _fics = os.listdir(directory) if not _fics: error(f"No files found in directory {directory}") sys.exit(0) for _fic in _fics: if not _fic.endswith('.yml') and not _fic.endswith('.yaml'): warning(f"skipping file {_fic}") continue info(f"uploading file {_fic}") content = base64.b64encode(open(f"{directory}/{_fic}").read().encode()).decode("UTF-8") folder = 'manifests' if not openshift else 'openshift' manifest_info = {'file_name': _fic, 'content': content, 'folder': folder} create_manifest_params = models.CreateManifestParams(**manifest_info) manifests_api.v2_create_cluster_manifest(cluster_id, create_manifest_params)
def delete_host(self, hostname, overrides={}): infra_envs = {} if 'infraenv' in overrides: infraenv = overrides['infraenv'] infra_env_id = self.get_infra_env_id(infraenv) hosts = self.client.v2_list_hosts(infra_env_id=infra_env_id) matchingids = [host['id'] for host in hosts if host['requested_hostname'] == hostname or host['id'] == hostname] else: for infra_env in self.client.list_infra_envs(): infra_env_id = infra_env['id'] hosts = self.client.v2_list_hosts(infra_env_id=infra_env_id) matchingids = [host['id'] for host in hosts if host['requested_hostname'] == hostname or host['id'] == hostname] if matchingids: infra_envs[infra_env_id] = matchingids if not infra_envs: error(f"No Matching Host with name {hostname} found") for infra_env_id in infra_envs: host_ids = infra_envs[infra_env_id] for host_id in host_ids: info(f"Deleting Host with id {host_id} in infraenv {infra_env_id}") self.client.v2_deregister_host(infra_env_id, host_id)
def update_host(self, hostname, overrides): infra_envs = {} if 'infraenv' in overrides: infra_env = overrides['infraenv'] infra_env_id = self.get_infra_env_id(infra_env) hosts = self.client.v2_list_hosts(infra_env_id=infra_env_id) matchingids = [host['id'] for host in hosts if host['requested_hostname'] == hostname or host['id'] == hostname] else: for infra_env in self.client.list_infra_envs(): infra_env_id = infra_env['id'] hosts = self.client.v2_list_hosts(infra_env_id=infra_env_id) matchingids = [host['id'] for host in hosts if host['requested_hostname'] == hostname or host['id'] == hostname] if matchingids: infra_envs[infra_env_id] = matchingids if not infra_envs: error(f"No Matching Host with name {hostname} found") for infra_env_id in infra_envs: host_ids = infra_envs[infra_env_id] for index, host_id in enumerate(host_ids): role = None bind_updated = False host_update_params = {} if 'cluster' in overrides: cluster = overrides['cluster'] if cluster is None or cluster == '': self.client.unbind_host(infra_env_id=infra_env_id, host_id=host_id) else: cluster_id = self.get_cluster_id(cluster) bind_host_params = {'cluster_id': cluster_id} bind_host_params = models.BindHostParams(**bind_host_params) self.client.bind_host(infra_env_id, host_id, bind_host_params) bind_updated = True if 'role' in overrides: role = overrides['role'] host_update_params['host_role'] = role if 'name' in overrides or 'requested_hostname' in overrides: newname = overrides.get('name', overrides.get('requested_hostname')) if len(host_ids) > 1: newname = f"{newname}-{index}" host_update_params['host_name'] = newname if 'ignition' in overrides: ignition_path = overrides['ignition'] if not os.path.exists(ignition_path): warning(f"Ignition {ignition_path} not found. Ignoring") else: ignition_data = open(ignition_path).read() host_ignition_params = models.HostIgnitionParams(config=ignition_data) self.client.v2_update_host_ignition(infra_env_id, host_id, host_ignition_params) if 'extra_args' in overrides and cluster_id is not None: extra_args = overrides['extra_args'] installer_args_params = models.InstallerArgsParams(args=extra_args) self.client.v2_update_host_installer_args(cluster_id, host_id, installer_args_params) if 'mcp' in overrides: valid_status = ["discovering", "known", "disconnected", "insufficient", "pending-for-input"] currenthost = self.client.v2_get_host(infra_env_id=infra_env_id, host_id=host_id) currentstatus = currenthost.status if currentstatus not in valid_status: error(f"Mcp can't be set for host {hostname} because of incorrect status {currentstatus}") else: mcp = overrides['mcp'] host_update_params['machine_config_pool_name'] = mcp if host_update_params: info(f"Updating host with id {host_id}") host_update_params = models.HostUpdateParams(**host_update_params) self.client.v2_update_host(infra_env_id=infra_env_id, host_id=host_id, host_update_params=host_update_params) elif not bind_updated: warning("Nothing updated for this host")
def info_iso(self, name, overrides, minimal=False): infra_env = self.info_infra_env(name).to_dict() iso_url = infra_env['download_url'] if self._expired_iso(iso_url): iso_url = self.client.get_infra_env_download_url(infra_env['id']).url info(iso_url)