def kill(self): """kill job in progress The process pid is owned by root, so that is not killable. Need to kill all its children. """ # the kill must be run as the kolla user so the # kolla_actions program must be used. try: actions_path = get_kolla_actions_path() cmd_prefix = ('%s job -t -p ' % actions_path) # kill the children from largest to smallest pids. child_pids = PidManager.get_child_pids(self._process.pid) for child_pid in sorted(child_pids, reverse=True): cmd = ''.join([cmd_prefix, child_pid]) err_msg, output = run_cmd(cmd, print_output=False) if err_msg: LOG.debug('kill failed: %s %s' % (err_msg, output)) else: LOG.debug('kill succeeded: %s' % child_pid) # record the name of user who killed the job cur_uid = os.getuid() self._kill_uname = pwd.getpwuid(cur_uid)[0] finally: self._cleanup()
def _add_cmd_info(tar): # run all the kollacli list commands cmds = [ 'kolla-cli --version', 'kolla-cli service listgroups', 'kolla-cli service list', 'kolla-cli group listservices', 'kolla-cli group listhosts', 'kolla-cli host list', 'kolla-cli property list', 'kolla-cli password list' ] # collect the json inventory output inventory = Inventory.load() inv_path = inventory.create_json_gen_file() cmds.append(inv_path) try: fd, path = tempfile.mkstemp(suffix='.tmp') os.close(fd) with open(path, 'w') as tmp_file: for cmd in cmds: err_msg, output = run_cmd(cmd, False) tmp_file.write('\n\n$ %s\n' % cmd) if err_msg: tmp_file.write('Error message: %s\n' % err_msg) for line in output.split('\n'): tmp_file.write(line + '\n') tar.add(path, arcname=os.path.join('kolla', 'cmds_output')) except Exception as e: raise e finally: if path: os.remove(path) inventory.remove_json_gen_file(inv_path) return
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 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 run_ansible_command(self, ansible_command, hostname): output = None command_string = '%s -vvv' % \ get_ansible_command() gen_file_path = self.create_json_gen_file() cmd = '%s %s -i %s %s' % (command_string, hostname, gen_file_path, ansible_command) try: err_msg, output = run_cmd(cmd, False) except Exception as e: err_msg = str(e) finally: self.remove_json_gen_file(gen_file_path) return err_msg, output
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 _command_exec(command): print('running - %s' % command) error, _ = utils.run_cmd(command) if error: print('error - %s' % error) sys.exit(1)
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))