def read_dynamic_args(): """Prompt the user for dynamic arguments An empty key name ends the prompt""" dynamic_args = {} while True: argname = cooked_input( colorize("Argument name: ", color="yellow")) # An empty argument name means we're done if argname == "": break else: argvalue = cooked_input( colorize("Argument value: ", color="yellow")) # Properly add integer values try: argvalue = int(argvalue) except ValueError: # That was not an int, leave it alone pass dynamic_args[argname] = argvalue return dynamic_args
def cmds(): # People just need a little reminder... print "" print colorize(" re-client is readline enabled!", color="lgreen") print colorize(" Use up/down keys to go through history", color="lgreen") print """
def download_playbook(self, save_path, project, pb_id): (pb, path) = self._get_playbook(project, pb_id) print "Playbook fetched" print "Saving playbook to: %s" % save_path reclient.utils.save_playbook(pb, save_path, self.format) print colorize( "Success: Playbook %s saved to %s" % ( pb_id, save_path), color="green")
def upload_playbook(self, source_path, project): with open(source_path, 'r') as _source: result = self._send_playbook(project, _source) _result = reclient.utils.deserialize(result.content, self.format) _id = colorize(str(_result['id']), color="yellow") print colorize( "Success: Playbook uploaded. ID: %s" % ( _id), color="green")
def cli(): """Entry point from the /bin/re-client command Parses/updates client configuration via config_reclient, parses command line options. Launches the REPL. """ parser = argparse.ArgumentParser( description='Release Engine Client Utility') parser.add_argument('-d', '--debug', action='store_true', default=False, help='Enable REST debugging') parser.add_argument('-p', '--project', required=False, default=None, help='Set default project') parser.add_argument('-i', '--id', required=False, default=None, help='Set default playbook ID') parser.add_argument('-f', '--format', required=False, default='yaml', choices=('yaml', 'json'), help='Set playbook format (Default: yaml)') args = parser.parse_args() out = logging.getLogger('reclient') if args.debug: _level = 'DEBUG' else: _level = 'INFO' out.setLevel(_level) handler = logging.StreamHandler() handler.setFormatter(logging.Formatter("%(message)s")) out.addHandler(handler) out.debug("Logging set to: %s" % _level) config_reclient(out) atexit.register(readline.write_history_file, histfile) banner = """ _ __ ___ ___| (_) ___ _ __ | |_ | '__/ _ \_____ / __| | |/ _ \ '_ \| __| | | | __/_____| (__| | | __/ | | | |_ |_| \___| \___|_|_|\___|_| |_|\__| """ print colorize(banner, color="green") repl(args)
def edit_playbook(self, project, pb_id): try: (pb, path) = self._get_playbook(project, pb_id) except ReClientGETError, rcge: response_msg = reclient.utils.deserialize(rcge.args[0].content, 'json') print colorize("Error while fetching playbooks for %s:" % project, color="red", background="lightgray") print colorize( "%s - %s" % ( str(rcge), response_msg['message']), color="red", background="lightgray") return False
def start_deployment(self, project, pb_id): suffix = "%s/playbook/%s/deployment/" % ( project, pb_id) print colorize("Dynamic Arguments. Finish/skip by entering an empty key name", color="green") dargs = reclient.utils.read_dynamic_args() if dargs != {}: print colorize("Going to begin deployment with the following dynamic args", color="green") print reclient.utils.dynamic_args_table(dargs) if reclient.utils.user_prompt_yes_no("Run deployment? "): result = self.connector.put(suffix, data=json.dumps(dargs)) else: return False try: _status = reclient.utils.deserialize(result.content, 'json').get('status') if _status == 'error': raise ReClientDeployError(result) except ReClientDeployError, rcde: response_msg = reclient.utils.deserialize(rcde.args[0].content, 'json') print colorize("Error while fetching playbooks for %s:" % project, color="red", background="lightgray") print colorize( "%s - %s" % ( str(rcde), response_msg['message']), color="red", background="lightgray") return False
def view_file(self, project, pb_id): """ Open playbook with id `pd_id` in `project` with the /bin/less command """ try: (pb, path) = self._get_playbook(project, pb_id) except ReClientGETError: print colorize(( "Error while attempting to find '%s' for project '%s'\n" "Are you sure it exists?") % ( pb_id, project), color="red", background="lightgray") else: reclient.utils.less_file(path.name)
def get_all_playbooks(self, project): """ Get all playbooks that match `project` """ try: (path, pb_fp) = self._get_playbook(project) except ReClientGETError, e: response_msg = reclient.utils.deserialize(e.args[0], self.format) print colorize("Error while fetching playbooks for %s:" % project, color="red", background="lightgray") print colorize( "%s - %s" % ( str(e), response_msg['message']), color="red", background="lightgray")
def repl(args): """Read. Evaluate. Print. Loop""" rclient = RC(debug=args.debug, format=args.format) while True: cmds() try: action = int(raw_input(colorize("command>> ", color="yellow"))) except KeyboardInterrupt: raise SystemExit except ValueError, e: bad_input = e.args[0].split(':')[1].replace("'", '').strip() if bad_input.lower() == 'q': raise SystemExit else: print colorize("Invalid option. Try again.", color="red") continue except:
def repl(args): """Read. Evaluate. Print. Loop""" rclient = RC(debug=args.debug, format=args.format) while True: cmds() try: action = int(raw_input( colorize("command>> ", color="yellow"))) except KeyboardInterrupt: raise SystemExit except ValueError, e: bad_input = e.args[0].split(':')[1].replace("'", '').strip() if bad_input.lower() == 'q': raise SystemExit else: print colorize("Invalid option. Try again.", color="red") continue except:
def get_all_playbooks_ever(self): """Get ALL THE PLAYBOOKS""" suffix = "playbooks/" result = self.connector.get(suffix) try: response_msg = reclient.utils.deserialize(result.content, self.format) if response_msg['status'] == 'error': print colorize( "Error while fetching all playbooks", color="red", background="lightgray") print colorize( "%s - %s" % (str(result), response_msg), color="red", background="lightgray") raise ReClientGETError(result) except Exception: return False else: view_file = reclient.utils.temp_blob(result, self.format) reclient.utils.less_file(view_file.name)
def start_deployment(self, project, pb_id): suffix = "%s/playbook/%s/deployment/" % ( project, pb_id) result = self.connector.put(suffix) try: _status = reclient.utils.deserialize(result.content, 'json').get('status') if _status == 'error': raise ReClientDeployError(result) except ReClientDeployError, rcde: response_msg = reclient.utils.deserialize(rcde.args[0].content, 'json') print colorize("Error while fetching playbooks for %s:" % project, color="red", background="lightgray") print colorize( "%s - %s" % ( str(rcde), response_msg['message']), color="red", background="lightgray") return False
def read_dynamic_args(): """Prompt the user for dynamic arguments An empty key name ends the prompt""" dynamic_args = {} while True: argname = cooked_input(colorize("Argument name: ", color="yellow")) # An empty argument name means we're done if argname == "": break else: argvalue = cooked_input( colorize("Argument value: ", color="yellow")) # Properly add integer values try: argvalue = int(argvalue) except ValueError: # That was not an int, leave it alone pass dynamic_args[argname] = argvalue return dynamic_args
color="red", background="lightgray") return False pb_fp = reclient.utils.edit_playbook(path, self.format) send_back = reclient.utils.user_prompt_yes_no("Upload?") if send_back: try: result = self._send_playbook(project, pb_fp, pb_id) except IOError, ioe: raise ioe except ReClientSendError, rcse: print "Error while sending updated playbook: %s" % ( str(rcse)) else: print colorize("Updated playbook for %s:" % project, color="green") return result else: print colorize("Not sending back. Playbook will be saved in %s until this program is closed." % ( pb_fp.name), color="yellow") return def download_playbook(self, save_path, project, pb_id): (pb, path) = self._get_playbook(project, pb_id) print "Playbook fetched" print "Saving playbook to: %s" % save_path reclient.utils.save_playbook(pb, save_path, self.format) print colorize( "Success: Playbook %s saved to %s" % ( pb_id, save_path),