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 _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() 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 _get_dynamic_inventory(): log = logger.getlogger() dynamic_inventory = None config_pointer_file = get_python_path() + '/config_pointer_file' if os.path.isfile(config_pointer_file): with open(config_pointer_file) as f: config_path = f.read() else: config_path = CFG_FILE if os.path.isfile(config_path): try: dynamic_inventory = generate_dynamic_inventory() except UserException as exc: log.debug("UserException raised when attempting to generate " "dynamic inventory: {}".format(exc)) if dynamic_inventory is None: print("Dynamic inventory not found") return dynamic_inventory
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 generate_dynamic_inventory(): config_pointer_file = gen.get_python_path() + '/config_pointer_file' if os.path.isfile(config_pointer_file): with open(config_pointer_file) as f: config_path = f.read() else: config_path = None inv = Inventory(config_path) cfg = Config(config_path) # Initialize the empty inventory dynamic_inventory = INVENTORY_INIT meta_hostvars = dynamic_inventory['_meta']['hostvars'] # Add 'env_variables' to 'all' 'vars' dynamic_inventory['all']['vars']['env_variables'] = ( cfg.get_globals_env_variables()) # Add 'localhost' to inventory meta_hostvars['localhost'] = {} meta_hostvars['localhost']['ansible_connection'] = 'local' # Add 'deployer' (container) to inventory meta_hostvars['deployer'] = {} meta_hostvars['deployer']['ansible_host'] = cfg.get_depl_netw_cont_ip() meta_hostvars['deployer']['ansible_user'] = SSH_USER meta_hostvars['deployer']['ansible_ssh_private_key_file'] = SSH_PRIVATE_KEY # Add 'software_bootstrap' list to 'client_nodes' 'vars' if not empty software_bootstrap = cfg.get_software_bootstrap() if len(software_bootstrap) > 0: dynamic_inventory['client_nodes']['vars']['software_bootstrap'] = ( software_bootstrap) # Add client nodes to inventory for index, hostname in enumerate(inv.yield_nodes_hostname()): # Add node to top-level group (node-template label & 'client-nodes') label = _sanitize(inv.get_nodes_label(index)) if label not in dynamic_inventory: dynamic_inventory[label] = {'hosts': []} dynamic_inventory['client_nodes']['children'].append(label) dynamic_inventory[label]['hosts'].append(hostname) # Add node hostvars in '_meta' dictionary meta_hostvars[hostname] = inv.get_node_dict(index) meta_hostvars[hostname]['ansible_host'] = (inv.get_nodes_pxe_ipaddr( 0, index)) meta_hostvars[hostname]['ansible_user'] = SSH_USER meta_hostvars[hostname]['ansible_ssh_private_key_file'] = ( SSH_PRIVATE_KEY) # Add node to any additional groups ('roles') roles = inv.get_nodes_roles(index) if roles is not None: for role in roles: _role = _sanitize(role) if _role not in dynamic_inventory: dynamic_inventory[_role] = {'hosts': []} dynamic_inventory[_role]['hosts'].append(hostname) return dynamic_inventory
if ip is None: raise UserException('No PXE IP Address in Inventory for client ' '\'%s\'' % hostname) if ip_list != '': ip = ',' + ip ip_list += ip return ip_list if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument('--deployer', action='store_true') args = parser.parse_args() config_pointer_file = get_python_path() + '/config_pointer_file' if os.path.isfile(config_pointer_file): with open(config_pointer_file) as f: config_path = f.read() else: config_path = None inv = Inventory(config_path) cfg = Config(config_path) ip_list = _get_pxe_ips(inv) if args.deployer: ip_list += "," + cfg.get_depl_netw_cont_ip() print(ip_list)