def load_container_info(self): """get the list of containers on the host""" hostname = self.hostname err_msg, output = \ self.inventory.run_ansible_command('-a "docker ps -a"', hostname) if err_msg: msg = 'Error accessing host %s : %s %s' % (hostname, err_msg, output) raise FailedOperation(msg) if not output: msg = ('Host %s is not accessible.' % hostname) raise FailedOperation(msg) else: if '>>' not in output: msg = ('Host: %s. Invalid ansible return data: [%s].' % (hostname, output)) raise FailedOperation(msg) if 'NAMES' not in output: msg = ('Host: %s. Invalid docker ps return data: [%s].' % (hostname, output)) raise FailedOperation(msg) ansible_properties = AnsibleProperties() base_distro = \ ansible_properties.get_property_value('kolla_base_distro') install_type = \ ansible_properties.get_property_value('kolla_install_type') # typically this prefix will be "ol-openstack-" container_prefix = base_distro + '-' + install_type + '-' # process ps output containers = {} # the ps output is after the '>>' output = output.split('>>', 1)[1] LOG.info('docker ps -a on host: %s:\n%s' % (hostname, output)) lines = output.split('\n') for line in lines: tokens = line.split() if len(tokens) < 2: continue cid = tokens[0] image = tokens[1] if container_prefix not in image: # skip non-kolla containers continue name = image.split(container_prefix)[1] name = name.split(':')[0] containers[cid] = name self.container_info = containers
def set_password_sshkey(pwd_key, private_key, public_key): cmd = '%s -k %s -r "%s" -u "%s"' % (_get_cmd_prefix(), pwd_key, private_key, public_key) err_msg, output = utils.run_cmd(cmd, print_output=False) if err_msg: raise FailedOperation( u._('Password ssh key set failed. {error} {message}').format( error=err_msg, message=output))
def clear_password(pwd_key): """clear a password if the password exists, it will be removed from the passwords file """ cmd = '%s -k %s -c' % (_get_cmd_prefix(), pwd_key) err_msg, output = utils.run_cmd(cmd, print_output=False) if err_msg: raise FailedOperation('%s %s' % (err_msg, output))
def get_password_names(): """return a list of password names""" cmd = '%s -l' % (_get_cmd_prefix()) err_msg, output = utils.run_cmd(cmd, print_output=False) if err_msg: raise FailedOperation('%s %s' % (err_msg, output)) pwd_names = [] if output and ',' in output: pwd_names = output.strip().split(',') return pwd_names
def get_log(self, container_id): """read the container log""" hostname = self.hostname cmd = '-a "docker logs %s"' % container_id err_msg, output = self.inventory.run_ansible_command(cmd, hostname) if err_msg: msg = 'Error accessing host %s : %s ' % (hostname, err_msg) raise FailedOperation(msg) if not output: msg = ('Host %s is not accessible.' % hostname) raise FailedOperation(msg) if '>>' not in output: msg = ('Host: %s. Invalid ansible return data: [%s].' % (hostname, output)) raise FailedOperation(msg) # the log info is after the '>>' output = output.split('>>', 1)[1] return output
def config_reset(): """Config Reset. Resets the kolla-ansible configuration to its release defaults. """ actions_path = utils.get_kolla_actions_path() cmd = ('%s config_reset' % actions_path) err_msg, output = utils.run_cmd(cmd, print_output=False) if err_msg: raise FailedOperation( u._('Configuration reset failed. {error} {message}').format( error=err_msg, message=output))
def get_empty_password_values(): cmd = '%s -e' % (_get_cmd_prefix()) err_msg, output = utils.run_cmd(cmd, print_output=False) # output of this command is a comma separated string of password keys # that have empty values. if err_msg: raise FailedOperation('%s %s' % (err_msg, output)) empty_keys = [] if output: # password keys exist that have no values empty_keys = output.strip().split(',') return empty_keys
def save(inventory): """Save the inventory in a pickle file""" inventory_path = os.path.join(get_kolla_cli_etc(), INVENTORY_PATH) try: # multiple trips thru json to render a readable inventory file data = jsonpickle.encode(inventory) data_str = json.loads(data) pretty_data = json.dumps(data_str, indent=4) sync_write_file(inventory_path, pretty_data) except Exception as e: raise FailedOperation( u._('Saving inventory failed. : {error}').format(error=str(e)))
def set_password(pwd_key, pwd_value): """set a password value If the password name exists, it will be changed. If it doesn't exist, a new password will be added. """ value_switch = '-v' if not pwd_value: pwd_value = '' value_switch = '' cmd = '%s -k %s %s %s' % (_get_cmd_prefix(), pwd_key, value_switch, pwd_value) err_msg, output = utils.run_cmd(cmd, print_output=False) if err_msg: raise FailedOperation( u._('Password set failed. {error} {message}').format( error=err_msg, message=output))
def load(): """load the inventory from a pickle file""" inventory_path = os.path.join(get_kolla_cli_etc(), INVENTORY_PATH) data = '' try: if os.path.exists(inventory_path): data = sync_read_file(inventory_path) if data.strip(): inventory = jsonpickle.decode(data) # upgrade version handling if inventory.version != inventory.class_version: inventory.upgrade() else: inventory = Inventory() Inventory.save(inventory) except Exception: raise FailedOperation( u._('Loading inventory failed. : {error}').format( error=traceback.format_exc())) return inventory
def init_passwords(): # init empty passwords & ssh keys to auto-gen'd values cmd = '%s -i' % (_get_cmd_prefix()) err_msg, output = utils.run_cmd(cmd, print_output=False) if err_msg: raise FailedOperation('%s %s' % (err_msg, output))