def get_action(self, exists, exists_prompt_yn=False): if exists: print( f'\nDo you want to sync the local {self.repo_name}\nrepository' ' at this time?\n') print('This can take a few minutes.\n') ch = 'Y' if get_yesno(prompt='Sync Repo? ', yesno='Y/n') else 'n' else: print( f'\nDo you want to create a local {self.repo_name}\n repository' ' at this time?\n') print('This can take a significant amount of time') ch = 'Y' if get_yesno(prompt='Create Repo? ', yesno='Y/n') else 'n' return ch
def create_pup_repo_mirror(repo_base_dir): """ Download POWER-Up public repository in full Args: repo_base_dir (str): Base directory path arch (str, optional): Select package architecture to save py_ver (int, optional): Python version number """ log = logger.getlogger() if not repo_base_dir.endswith('.git'): repo_base_dir = path.join(repo_base_dir, 'power-up.git') makedirs(repo_base_dir, exist_ok=True) if len(listdir(repo_base_dir)) != 0: log.info(f"The directory '{repo_base_dir}' already exists and is not" " empty.") if get_yesno("Permanently remove existing contents and re-clone? "): rmtree(repo_base_dir) makedirs(repo_base_dir, exist_ok=True) else: log.debug("User selected to continue without re-cloning") return url_path = rlinput("POWER-Up Repository path/URL: ", get_github_url()) resp, err, rc = sub_proc_exec(f'git clone --mirror {url_path} ' f'{repo_base_dir}') if rc != 0: log.error('An error occurred while cloning mirror of power-up repo: ' f'{err}')
def create_base_dir(base_dir): log = logger.getlogger() print('\nMove or Copy the existing software server directories?') ch, action = get_selection('move\ncopy', ('m', 'c')) if action == 'copy': statvfs = os.statvfs(base_dir) freespace = statvfs.f_frsize * statvfs.f_bavail if freespace < 18000000000: sys.exit('Insufficient space on disk') arch = '' exists = glob('/srv/repos/dependencies/rhel7/*') if not exists: log.error('\nNo dependencies folder found. Unable to perform move.\n') sys.exit() for path in exists: if 'p8' in path or 'p9' in path: arch = 'ppc64le' break elif 'x86_64' in path: arch = 'x86_64' break if not arch: log.error('\nUnable to determine architecture. Unable to perform move.\n') sys.exit() if os.path.exists(f'{base_dir}/wmla120-{arch}'): print(f'Destination path {base_dir}/wmla120-{arch} already exists.') if action == 'copy': if not get_yesno('Okay to proceed with force copy? '): sys.exit('Exit at user request') else: os.mkdir(f'{base_dir}/wmla120-{arch}/') for _dir in (('repos', 'anaconda', 'spectrum-conductor', 'spectrum-dli', 'wmla-license',)): path = os.path.join('/srv/', _dir, '') if os.path.isdir(path): print(f'Found dir: {path}') if action == 'move': try: _dir = f'{base_dir}/wmla120-{arch}/' move(path, _dir) cmd = f'sudo chcon -Rv --type=httpd_sys_content_t {_dir}' _, err, rc = sub_proc_exec(cmd) if rc != 0: log.error(f'chtype of directory {_dir} failed {err}') except shutil_Error as exc: print(exc) elif action == 'copy': cmd = f'cp -rf {path} {base_dir}/wmla120-{arch}/' try: _, err, rc = sub_proc_exec(cmd) except: pass if rc != 0: log.error('Copy error {err}') else: log.error(f'Path {path} missing') print('Done')
def get_rpm_path(self, filepath='/home/**/*.rpm'): """Interactive search for the rpm path. Returns: Path to file or None """ while True: self.rpm_path = get_file_path(filepath) # Check for .rpm files in the chosen file cmd = 'rpm -qlp self.rpm_path' resp, err, rc = sub_proc_exec(cmd) if self.rpm_path: if '.rpm' not in resp: print('There are no ".rpm" files in the selected path') if get_yesno('Use selected path? ', default='n'): return self.rpm_path else: return None
def copy_dirs(self, src_dir=None): if os.path.exists(self.repo_dir): if get_yesno( f'Directory {self.repo_dir} already exists.\nOK to replace it? ' ): rmtree(os.path.dirname(self.repo_dir), ignore_errors=True) else: self.log.info('Directory not created') return None, None src_dir = get_dir(src_dir) if not src_dir: return None, None try: dest_dir = self.repo_dir copytree(src_dir, dest_dir) except Error as exc: print(f'Copy error: {exc}') return None, dest_dir else: return src_dir, dest_dir
def _validate_ansible_ping(software_hosts_file_path, hosts_list): """Validate Ansible connectivity and functionality on all hosts Args: software_hosts_file_path (str): Path to software inventory file host_list (list): List of hostnames or IP addresses Returns: bool: True if Ansible can connect to all hosts Raises: UserException: If any host fails """ log = logger.getlogger() cmd = ('{} -i {} -m ping all'.format(get_ansible_path(), software_hosts_file_path)) resp, err, rc = sub_proc_exec(cmd) if str(rc) != "0": msg = f'Ansible ping validation failed:\n{resp}' log.debug(msg) if 'WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!' in msg: print( '@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n' '@ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED @\n' '@ ON ONE OR MORE CLIENT NODES! @\n' '@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n' 'IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!\n' 'Someone could be eavesdropping on you right now ' '(man-in-the-middle attack)!\n' 'It is also possible that a host key has just been changed.\n') if get_yesno('Remove the existing known host keys? '): known_hosts_files = ( [os.path.join(Path.home(), ".ssh", "known_hosts")]) user_name, user_home_dir = get_user_and_home() if user_home_dir != str(Path.home()): known_hosts_files.append(os.path.join(user_home_dir, ".ssh", "known_hosts")) for host in hosts_list: print(f'Collecting new host key(s) for {host}') cmd = (f'ssh-keyscan -H {host}') new_host_key, err, rc = sub_proc_exec(cmd) for known_hosts in known_hosts_files: print(f'Removing host keys for {host} ' f'from {known_hosts}') cmd = (f'ssh-keygen -R {host} -f {known_hosts}') resp, err, rc = sub_proc_exec(cmd) print(f'Appending new host key for {host} to ' f'{known_hosts}') append_line(known_hosts, new_host_key, check_exists=False) if user_home_dir != str(Path.home()): user_known_hosts = os.path.join(user_home_dir, ".ssh", "known_hosts") user_uid = pwd.getpwnam(user_name).pw_uid user_gid = grp.getgrnam(user_name).gr_gid os.chown(user_known_hosts, user_uid, user_gid) os.chmod(user_known_hosts, 0o600) os.chown(user_known_hosts + '.old', user_uid, user_gid) os.chmod(user_known_hosts + '.old', 0o600) return _validate_ansible_ping(software_hosts_file_path, hosts_list) elif 'Permission denied' in msg: msg = ('The PowerUp software installer attempted to log into the ' 'the client node(s) but was unsuccessful. SSH key access ' 'may need to be configured.\n') print(msg) if get_yesno('OK to configure Client Nodes for SSH Key Access? '): configure_ssh_keys(software_hosts_file_path) return _validate_ansible_ping(software_hosts_file_path, hosts_list) raise UserException(msg) log.debug("Software inventory Ansible ping validation passed") return True