def edit_config_file(conf_str, config_field, object_name): "edit the configuration on the server" descriptor, file_name = tempfile.mkstemp(suffix=".yml", text=True) handle = os.fdopen(descriptor, 'w+b') handle.write(conf_str) handle.close() os.system("%s %s" % (system_state.editor, file_name)) post_data = yaml.load(open(file_name).read()) submit = libUi.ask_yes_no("Commit changes to server", libUi.YES) if submit: output = config_field.post_data(object_name, post_data) os.unlink(file_name) return output["command_status"], output["command_output"] else: msg = "Discarded changes. Edits can be found here: %s" % file_name return OK, [msg]
def cmd(self, command_line): """ command_line -- all of the keywords passed in the command string, parsed """ status = OK output = [] if len(command_line) < 2: raise CommandError("Incomplete command.") user_name = self.check_user_name(command_line) if len(command_line) == 2: system_state.push_prompt(["user", user_name]) else: command = command_line[2].lower() if command == "edit": status, output = Edit.User().cmd(["edit", "user", user_name], 0) elif command == "set-password": password = libUi.pwd_input("password for %s: " % user_name) try: status, output = system_state.cnm_connector.set_password(user_name, password) except MachineTraceback, m_err: libUi.process_traceback(m_err) status = FAIL elif command == "delete": if system_state.username == user_name: status = FAIL output = ["Cannot delete your own user object."] else: prompt = 'Delete user "%s" -- are you sure' % user_name if libUi.ask_yes_no(prompt, libUi.NO) == libUi.NO: status = FAIL output = ["Aborted"] try: return system_state.cnm_connector.delete_user(user_name) except MachineTraceback, m_err: libUi.process_traceback(m_err) status = FAIL
def cmd(self, command_line): '''Create a thing that the user is interested in''' if command_line.no_flag: return FAIL, [] if len(command_line) < 3: return FAIL, ["Incomplete command."] conf_str = self.config_field.get_default_data() #conf_str = yaml.dump(current_dict, default_flow_style=False) file_descriptor, file_name = tempfile.mkstemp(suffix=".yml", text=True) file_handle = os.fdopen(file_descriptor, 'w+b') file_handle.write(conf_str) file_handle.close() os.system("vim %s" % file_name) post_data = yaml.load(open(file_name).read()) submit = libUi.ask_yes_no("Commit changes to server", libUi.YES) if submit: _status, output = self.config_field.post_specific_data(command_line, 2, post_data) os.unlink(file_name) return OK, [output] else: msg = "Discarded changes. Edits can be found here: %s" % file_name return OK, [msg]
def get_comments(form_text): "Create a form that the user can edit and pull out relevant data" _fd, file_name = tempfile.mkstemp(suffix=".txt", text=True) file_handle = os.fdopen(_fd, 'w+b') file_handle.write(form_text) file_handle.close() os.system("%s %s" % (system_state.editor, file_name)) form_data = open(file_name).read() publish, job_names, comments = process_form_data(form_data) delete_temp_file = False submit = False if job_names and comments: submit = libUi.ask_yes_no("Submit comments to server", libUi.YES) if submit: delete_temp_file = True else: msg = "%%%% Discarded changes. Edits can be found here: %s\n" print msg % file_name else: delete_temp_file = True if delete_temp_file: os.unlink(file_name) return submit, publish, job_names, '\n'.join(comments)
def watch_jobs(self, job_names): "Given a list of jobs, watch their progress." summary_output = [] summary_status = OK jobs = set(job_names) try: while job_names: jobs = jobs.union(job_names) time.sleep(0.25) url = "json/job/poll/" post_data = {"yaml": yaml.dump({"job_names": job_names})} output = self.service_yaml_request(url, post_data=post_data) if type(output) != type({}): msg = "Request to %s returned and invalid type (%s)" libUi.error(msg % (url, output)) return FAIL, output for job_name in output: job_output = output[job_name] if "traceback" in job_output: raise MachineTraceback(url, job_output["traceback"]) new_output = job_output.get("new_output", '') if new_output: libUi.process_cnm(new_output) alive = job_output.get("alive") if alive == None: if job_output.get("command_status") == "QUEUED": pending_job = job_output.get("pending_job") msg = "%s is pending %s." % (job_name, pending_job) libUi.warning(msg) libUi.info("Watching %s..." % pending_job) job_names = [pending_job] break if alive == False: out = system_state.cnm_connector.join_job(job_name) summary_output.append(out.get("command_output")) status = out.get("command_status", OK) if status != OK: summary_status = FAIL job_names += out.get("children", []) job_names.remove(job_name) comments = out.get("jobs_requiring_comment", 0) if comments: system_state.comment_counter = len(comments) except exceptions.KeyboardInterrupt: term = libUi.ask_yes_no("Terminate job", libUi.YES) if term == libUi.NO: msg = ["Disconnected from %s" % job_names, "To re-connect, use 'job view'"] return OK, msg else: matcher = re.compile("(\S+)@(\S+)\-(\d+)") machine_names = set() for job_name in job_names: _user, machine_name, _counter = matcher.findall(job_name)[0] machine_names.update([machine_name]) output = {} for machine_name in machine_names: _status, output = self.stop_jobs(machine_name) print ">>> Halting all jobs on %s..." % machine_name libUi.user_output(output, FAIL) raise exceptions.KeyboardInterrupt libUi.info("Finishing processing of %d jobs..." % len(jobs)) return summary_status, summary_output