def _run_playbook(playbook, config_path, extra_vars=None, display=True, load_config_vars=False): log = logger.getlogger() config_pointer_file = gen.get_python_path() + '/config_pointer_file' with open(config_pointer_file, 'w') as f: f.write(config_path) ansible_playbook = gen.get_ansible_playbook_path() inventory = ' -i ' + gen.get_python_path() + '/inventory.py' playbook = ' ' + playbook cmd = ansible_playbook + inventory + playbook if load_config_vars: cmd += f" --extra-vars '@{config_path}'" if extra_vars is not None: cmd += f" --extra-vars '{' '.join(extra_vars)}'" command = ['bash', '-c', cmd] log.debug('Run subprocess: %s' % ' '.join(command)) if display: process = Popen(command, cwd=gen.get_playbooks_path()) process.wait() stdout = '' else: process = Popen(command, stdout=PIPE, stderr=PIPE, cwd=gen.get_playbooks_path()) stdout, stderr = process.communicate() try: stdout = stdout.decode('utf-8') except AttributeError: pass return (process.returncode, stdout)
def __init__(self, config_path=None, name=None): self.log = logger.getlogger() self.cfg = Config(config_path) self.cont_package_path = gen.get_container_package_path() self.cont_id_file = gen.get_container_id_file() self.cont_venv_path = gen.get_container_venv_path() self.cont_scripts_path = gen.get_container_scripts_path() self.cont_python_path = gen.get_container_python_path() self.cont_os_images_path = gen.get_container_os_images_path() self.cont_playbooks_path = gen.get_container_playbooks_path() self.depl_package_path = gen.get_package_path() self.depl_python_path = gen.get_python_path() self.depl_playbooks_path = gen.get_playbooks_path() if name is True or name is None: for vlan in self.cfg.yield_depl_netw_client_vlan('pxe'): break self.name = '{}-pxe{}'.format(self.DEFAULT_CONTAINER_NAME, vlan) else: self.name = name self.client = docker.from_env() try: self.image = self.client.images.get('power-up') except docker.errors.ImageNotFound: self.image = None try: self.cont = self.client.containers.get(self.name) except docker.errors.NotFound: self.cont = None
def remove_client_host_keys(config_path=None): log = logger.getlogger() inv = Inventory(config_path) for ipaddr in inv.yield_nodes_pxe_ipaddr(): log.info("Remove any stale ssh host keys for {}".format(ipaddr)) util.bash_cmd("ssh-keygen -R {}".format(ipaddr)) playbooks_known_hosts = (os.path.join(gen.get_playbooks_path(), 'known_hosts')) if os.path.isfile(playbooks_known_hosts): util.bash_cmd("ssh-keygen -R {} -f {}".format( ipaddr, playbooks_known_hosts))
def _run_playbook(playbook, config_path): log = logger.getlogger() config_pointer_file = gen.get_python_path() + '/config_pointer_file' with open(config_pointer_file, 'w') as f: f.write(config_path) ansible_playbook = gen.get_ansible_playbook_path() inventory = ' -i ' + gen.get_python_path() + '/inventory.py' playbook = ' ' + playbook cmd = ansible_playbook + inventory + playbook command = ['bash', '-c', cmd] log.debug('Run subprocess: %s' % ' '.join(command)) process = subprocess.Popen(command, cwd=gen.get_playbooks_path()) process.wait()
def __init__(self, config_path=None, name=None): self.log = logger.getlogger() self.cfg = Config(config_path) self.cont_package_path = gen.get_container_package_path() self.cont_id_file = gen.get_container_id_file() self.cont_venv_path = gen.get_container_venv_path() self.cont_scripts_path = gen.get_container_scripts_path() self.cont_python_path = gen.get_container_python_path() self.cont_os_images_path = gen.get_container_os_images_path() self.cont_playbooks_path = gen.get_container_playbooks_path() self.depl_package_path = gen.get_package_path() self.depl_python_path = gen.get_python_path() self.depl_playbooks_path = gen.get_playbooks_path() self.cont_ini = os.path.join(self.depl_package_path, 'container.ini') self.rootfs = self.ROOTFS # Check if architecture is supported arch = platform.machine() if arch not in self.ARCHITECTURE.keys(): msg = "Unsupported architecture '{}'".format(arch) self.log.error(msg) raise UserException(msg) self.rootfs.arch = self.ARCHITECTURE[arch] if name is True or name is None: for vlan in self.cfg.yield_depl_netw_client_vlan('pxe'): break self.name = '{}-pxe{}'.format(self.DEFAULT_CONTAINER_NAME, vlan) else: self.name = name self.cont = lxc.Container(self.name) # Get a file descriptor for stdout self.fd = open(os.path.join(gen.GEN_LOGS_PATH, self.name + '.stdout.log'), 'w')
def get_ansible_inventory(): log = logger.getlogger() inventory_choice = None dynamic_inventory_path = get_dynamic_inventory_path() software_hosts_file_path = ( os.path.join(get_playbooks_path(), 'software_hosts')) heading1("Software hosts inventory setup\n") dynamic_inventory = None # If dynamic inventory contains clients prompt user to use it if (dynamic_inventory is not None and len(set(_get_hosts_list(dynamic_inventory)) - set(['deployer', 'localhost'])) > 0): print("Ansible Dynamic Inventory found:") print("--------------------------------") print(_get_groups_hosts_string(dynamic_inventory)) print("--------------------------------") validate_software_inventory(dynamic_inventory) if click.confirm('Do you want to use this inventory?'): print("Using Ansible Dynamic Inventory") inventory_choice = dynamic_inventory_path else: print("NOT using Ansible Dynamic Inventory") # If dynamic inventory has no hosts or user declines to use it if inventory_choice is None: while True: # Check if software inventory file exists if os.path.isfile(software_hosts_file_path): print("Software inventory file found at '{}':" .format(software_hosts_file_path)) # If no software inventory file exists create one using template else: rlinput("Press enter to create client node inventory") _create_new_software_inventory(software_hosts_file_path) # If still no software inventory file exists prompt user to # exit (else start over to create one). if not os.path.isfile(software_hosts_file_path): print("No inventory file found at '{}'" .format(software_hosts_file_path)) if click.confirm('Do you want to exit the program?'): sys.exit(1) else: continue # Menu items can modified to show validation results continue_msg = 'Continue with current inventory' edit_msg = 'Edit inventory file' exit_msg = 'Exit program' ssh_config_msg = 'Configure Client Nodes for SSH Key Access' menu_items = [] # Validate software inventory inv_count = len(_validate_inventory_count(software_hosts_file_path, 0)) print(f'Validating software inventory ({inv_count} nodes)...') if validate_software_inventory(software_hosts_file_path): print(bold("Validation passed!")) else: print(bold("Unable to complete validation")) continue_msg = ("Continue with inventory as-is - " "WARNING: Validation incomplete") menu_items.append(ssh_config_msg) # Prompt user menu_items += [continue_msg, edit_msg, exit_msg] choice, item = get_selection(menu_items) print(f'Choice: {choice} Item: {item}') if item == ssh_config_msg: configure_ssh_keys(software_hosts_file_path) elif item == continue_msg: print("Using '{}' as inventory" .format(software_hosts_file_path)) inventory_choice = software_hosts_file_path break elif item == edit_msg: click.edit(filename=software_hosts_file_path) elif item == exit_msg: sys.exit(1) if inventory_choice is None: log.error("Software inventory file is required to continue!") sys.exit(1) log.debug("User software inventory choice: {}".format(inventory_choice)) return inventory_choice