def merge_review(ctx, work, review_id=None, message=None): get_tryton_connection() Review = Model.get('project.work.codereview') Task = Model.get('project.work') tasks = Task.find([('code', '=', work)]) if not tasks: print(t.red('Error: Task %s was not found.' % work), file=sys.stderr) sys.exit(1) w = tasks[0] reviews = Review.find([('work', '=', w.id), ('state', '=', 'opened')]) for review in reviews: if review_id and str(review.id) != review_id: print(review_id, review.id) continue show_review(review) if not review_id: continue confirm = choice.Binary('Are you sure you want to merge?', False).ask() if confirm: owner, repo, request_id = get_request_info(review.url) res = pullrequests.merge(owner, repo, request_id, message) if res and res['state'] == 'MERGED': review.state = 'closed' review.save()
def greetings(): user_greeting_response = choice.Menu(['Login', 'Create new profile']).ask() if user_greeting_response == 'Login': username = choice.Input('Enter your username', str).ask() password = choice.Input('Enter your password', str).ask() user_data = get_user_data(username, password) if user_data: click.echo('You are logged in! What would you like to do?') return user_data else: click.echo('Incorrect password or username') greetings() else: click.echo('creating a new user') first_name = choice.Input('What is your first name?', str).ask() last_name = choice.Input('What is your last name?', str).ask() username = choice.Input('Pick a username?', str).ask() password = choice.Input('Enter a password', str).ask() email = choice.Input('Enter your email address', str).ask() # can be null add_addresses = choice.Binary( 'Would you like to add addresses to your profile?', True).ask() insert_into_profile(first_name, last_name, username, password, email, add_addresses) if add_addresses: user_id = get_user_data(username, password)['user_id'] add_addresses_prompt(user_id)
def paasta_restart(args): pargs = apply_args_filters(args) soa_dir = args.soa_dir affected_flinks = [] affected_non_flinks = [] for cluster, service_instances in pargs.items(): for service, instances in service_instances.items(): for instance in instances.keys(): service_config = get_instance_config( service=service, cluster=cluster, instance=instance, soa_dir=soa_dir, load_deployments=False, ) if isinstance(service_config, FlinkDeploymentConfig): affected_flinks.append(service_config) else: affected_non_flinks.append(service_config) if affected_flinks: flinks_info = ", ".join( [f"{f.service}.{f.instance}" for f in affected_flinks]) print( f"WARN: paasta restart is currently unsupported for Flink instances ({flinks_info})." ) print("To restart, please run:", end="\n\n") for flink in affected_flinks: print( f"paasta stop -s {flink.service} -i {flink.instance} -c {flink.cluster}" ) print( f"paasta start -s {flink.service} -i {flink.instance} -c {flink.cluster}", end="\n\n", ) if not affected_non_flinks: return 1 non_flinks_info = ", ".join( [f"{f.service}.{f.instance}" for f in affected_non_flinks]) proceed = choice.Binary( f"Would you like to restart the other instances ({non_flinks_info}) anyway?", False, ).ask() if not proceed: return 1 return paasta_start(args)
def confirm_to_continue(cluster_service_instances, desired_state): paasta_print(f'You are about to {desired_state} the following instances:') paasta_print( "Either --instances or --clusters not specified. Asking for confirmation." ) i_count = 0 for cluster, services_instances in cluster_service_instances: for service, instances in services_instances.items(): for instance in instances.keys(): paasta_print(f'cluster = {cluster}, instance = {instance}') i_count += 1 if sys.stdin.isatty(): return choice.Binary( f'Are you sure you want to {desired_state} these {i_count} instances?', False).ask() return True
def create(ctx, path, module, summary, description, bug, group='NaN'): """ Create or update review """ diff, base_diff = module_diff(ctx, path, module, show=False) root = get_root() review_id = review_file(path) if review_id: upgrade_review = choice.Binary( 'Do you like upgrade the review ' '%s (%s)?' % (review_id, module), True).ask() if not upgrade_review: review_id = None if review_id is None: review_request = root.get_review_requests().create( repository=get_repository()) create_review_file(path, review_request.id) else: review_request = root.get_review_request( review_request_id=review_file(path)) review_request.get_diffs().upload_diff(diff.encode('utf-8'), base_diff.encode('utf-8')) draft = review_request.get_draft() review_description = description or summary or '' draft.update( summary=summary.encode('utf-8'), description=review_description.encode('utf-8'), bugs_closed=bug, ) user = root.get_session().get_user() draft = draft.update(target_people=user.username, target_groups=group) draft.update(public=True) return review_request['id']
def paasta_start_or_stop(args, desired_state): """Requests a change of state to start or stop given branches of a service.""" soa_dir = args.soa_dir pargs = apply_args_filters(args) if len(pargs) == 0: return 1 affected_services = {s for service_list in pargs.values() for s in service_list.keys()} if len(affected_services) > 1: paasta_print(PaastaColors.red("Warning: trying to start/stop/restart multiple services:")) for cluster, services_instances in pargs.items(): paasta_print("Cluster %s:" % cluster) for service, instances in services_instances.items(): paasta_print(" Service %s:" % service) paasta_print(" Instances %s" % ",".join(instances)) if sys.stdin.isatty(): confirm = choice.Binary('Are you sure you want to continue?', False).ask() else: confirm = False if not confirm: paasta_print() paasta_print("exiting") return 1 invalid_deploy_groups = [] marathon_message_printed, chronos_message_printed = False, False for cluster, services_instances in pargs.items(): for service, instances in services_instances.items(): try: remote_refs = remote_git.list_remote_refs(utils.get_git_url(service, soa_dir)) except remote_git.LSRemoteException as e: msg = ( "Error talking to the git server: %s\n" "This PaaSTA command requires access to the git server to operate.\n" "The git server may be down or not reachable from here.\n" "Try again from somewhere where the git server can be reached, " "like your developer environment." ) % str(e) paasta_print(msg) return 1 for instance in instances: service_config = get_instance_config( service=service, cluster=cluster, instance=instance, soa_dir=soa_dir, load_deployments=False, ) deploy_group = service_config.get_deploy_group() (deploy_tag, _) = get_latest_deployment_tag(remote_refs, deploy_group) if deploy_tag not in remote_refs: invalid_deploy_groups.append(deploy_group) else: force_bounce = utils.format_timestamp(datetime.datetime.utcnow()) if isinstance(service_config, MarathonServiceConfig) and not marathon_message_printed: print_marathon_message(desired_state) marathon_message_printed = True elif isinstance(service_config, ChronosJobConfig) and not chronos_message_printed: print_chronos_message(desired_state) chronos_message_printed = True issue_state_change_for_service( service_config=service_config, force_bounce=force_bounce, desired_state=desired_state, ) return_val = 0 if invalid_deploy_groups: paasta_print("No branches found for %s in %s." % (", ".join(invalid_deploy_groups), remote_refs)) paasta_print("Has %s been deployed there yet?" % service) return_val = 1 return return_val
import choice # Get a yes or no response (default is no) confirm = choice.Binary('Are you sure you want to delete?', False).ask() if confirm: deleteIt() # Input an arbitrary value, check for correctness howmany = choice.Input('How many pies?', int).ask() print("You ordered {} pies".format(howmany)) # Choose from a set of options entree = choice.Menu(['steak', 'potatoes', 'eggplant']).ask() print("You choice {}".format(entree)) posts = ['post {}'.format(num) for num in range(15)] resp = choice.Menu(posts, ['edit', 'delete', 'publish'], ['newpost', 'exit']).ask() print(resp) resp = choice.Input('Enter an integer', int).ask() resp = choice.Input('Enter a string with "a" in it', choice.validate(lambda s: "a" in s)).ask() resp = choice.Binary('Yes or no?', True).ask() resp = choice.Binary('yes or No?', False).ask() resp = choice.Binary('yes or no?').ask()
def paasta_start_or_stop(args, desired_state): """Requests a change of state to start or stop given branches of a service.""" soa_dir = args.soa_dir pargs = apply_args_filters(args) if len(pargs) == 0: return 1 affected_services = { s for service_list in pargs.values() for s in service_list.keys() } if len(affected_services) > 1: print( PaastaColors.red( "Warning: trying to start/stop/restart multiple services:")) for cluster, services_instances in pargs.items(): print("Cluster %s:" % cluster) for service, instances in services_instances.items(): print(" Service %s:" % service) print(" Instances %s" % ",".join(instances.keys())) if sys.stdin.isatty(): confirm = choice.Binary("Are you sure you want to continue?", False).ask() else: confirm = False if not confirm: print() print("exiting") return 1 invalid_deploy_groups = [] marathon_message_printed = False affected_flinks = [] if args.clusters is None or args.instances is None: if confirm_to_continue(pargs.items(), desired_state) is False: print() print("exiting") return 1 for cluster, services_instances in pargs.items(): for service, instances in services_instances.items(): for instance in instances.keys(): service_config = get_instance_config( service=service, cluster=cluster, instance=instance, soa_dir=soa_dir, load_deployments=False, ) if isinstance(service_config, FlinkDeploymentConfig): affected_flinks.append(service_config) continue try: remote_refs = get_remote_refs(service, soa_dir) except remote_git.LSRemoteException as e: msg = ( "Error talking to the git server: %s\n" "This PaaSTA command requires access to the git server to operate.\n" "The git server may be down or not reachable from here.\n" "Try again from somewhere where the git server can be reached, " "like your developer environment.") % str(e) print(msg) return 1 deploy_group = service_config.get_deploy_group() (deploy_tag, _) = get_latest_deployment_tag(remote_refs, deploy_group) if deploy_tag not in remote_refs: invalid_deploy_groups.append(deploy_group) else: force_bounce = utils.format_timestamp( datetime.datetime.utcnow()) if (isinstance(service_config, MarathonServiceConfig) and not marathon_message_printed): print_marathon_message(desired_state) marathon_message_printed = True issue_state_change_for_service( service_config=service_config, force_bounce=force_bounce, desired_state=desired_state, ) return_val = 0 # TODO: Refactor to discover if set_state is available for given # instance_type in API if affected_flinks: print_flink_message(desired_state) csi = defaultdict(lambda: defaultdict(list)) for service_config in affected_flinks: csi[service_config.cluster][service_config.service].append( service_config.instance) system_paasta_config = load_system_paasta_config() for cluster, services_instances in csi.items(): client = get_paasta_api_client(cluster, system_paasta_config) if not client: print("Cannot get a paasta-api client") exit(1) for service, instances in services_instances.items(): for instance in instances: try: client.service.instance_set_state( service=service, instance=instance, desired_state=desired_state, ).result() except HTTPError as exc: print(exc.response.text) return exc.status_code return_val = 0 if invalid_deploy_groups: print(f"No deploy tags found for {', '.join(invalid_deploy_groups)}.") print(f"Has {service} been deployed there yet?") return_val = 1 return return_val
#!/usr/bin/python import choice import mc_solr confirm = choice.Binary( "This will delete all documents in the Solr database\n" + 'Are you sure you want to do this?', False).ask() if confirm: print mc_solr.delete_all_documents()
def paasta_start_or_stop(args, desired_state): """Requests a change of state to start or stop given branches of a service.""" soa_dir = args.soa_dir pargs = apply_args_filters(args) if len(pargs) == 0: return 1 affected_services = { s for service_list in pargs.values() for s in service_list.keys() } if len(affected_services) > 1: paasta_print( PaastaColors.red( "Warning: trying to start/stop/restart multiple services:")) for cluster, services_instances in pargs.items(): paasta_print("Cluster %s:" % cluster) for service, instances in services_instances.items(): paasta_print(" Service %s:" % service) paasta_print(" Instances %s" % ",".join(instances.keys())) if sys.stdin.isatty(): confirm = choice.Binary('Are you sure you want to continue?', False).ask() else: confirm = False if not confirm: paasta_print() paasta_print("exiting") return 1 invalid_deploy_groups = [] marathon_message_printed = False chronos_message_printed = False affected_flinkclusters = [] if args.clusters is None or args.instances is None: if confirm_to_continue(pargs.items(), desired_state) is False: paasta_print() paasta_print("exiting") return 1 for cluster, services_instances in pargs.items(): for service, instances in services_instances.items(): for instance in instances.keys(): service_config = get_instance_config( service=service, cluster=cluster, instance=instance, soa_dir=soa_dir, load_deployments=False, ) if isinstance(service_config, FlinkClusterConfig): affected_flinkclusters.append(service_config) continue try: remote_refs = get_remote_refs(service, soa_dir) except remote_git.LSRemoteException as e: msg = ( "Error talking to the git server: %s\n" "This PaaSTA command requires access to the git server to operate.\n" "The git server may be down or not reachable from here.\n" "Try again from somewhere where the git server can be reached, " "like your developer environment.") % str(e) paasta_print(msg) return 1 deploy_group = service_config.get_deploy_group() (deploy_tag, _) = get_latest_deployment_tag(remote_refs, deploy_group) if deploy_tag not in remote_refs: invalid_deploy_groups.append(deploy_group) else: force_bounce = utils.format_timestamp( datetime.datetime.utcnow()) if isinstance(service_config, MarathonServiceConfig ) and not marathon_message_printed: print_marathon_message(desired_state) marathon_message_printed = True elif isinstance( service_config, ChronosJobConfig) and not chronos_message_printed: print_chronos_message(desired_state) chronos_message_printed = True issue_state_change_for_service( service_config=service_config, force_bounce=force_bounce, desired_state=desired_state, ) return_val = 0 if affected_flinkclusters: if os.environ.get('ON_PAASTA_MASTER'): print_flinkcluster_message(desired_state) kube_client = KubeClient() for service_config in affected_flinkclusters: set_flinkcluster_desired_state( kube_client=kube_client, service=service_config.service, instance=service_config.instance, desired_state=dict(start='running', stop='stopped')[desired_state], ) else: csi = defaultdict(lambda: defaultdict(list)) for service_config in affected_flinkclusters: csi[service_config.cluster][service_config.service].append( service_config.instance) system_paasta_config = load_system_paasta_config() for cluster, services_instances in csi.items(): for service, instances in services_instances.items(): cmd_parts = [ 'ON_PAASTA_MASTER=1', 'paasta', desired_state, '-c', cluster, '-s', service, '-i', ','.join(instances), ] return_val, _ = run_on_master( cluster=cluster, system_paasta_config=system_paasta_config, cmd_parts=cmd_parts, graceful_exit=True, ) if invalid_deploy_groups: paasta_print("No branches found for %s in %s." % (", ".join(invalid_deploy_groups), remote_refs)) paasta_print("Has %s been deployed there yet?" % service) return_val = 1 return return_val
#!/usr/bin/python import choice import mc_solr confirm = choice.Binary('Warning: A full import will delete all data from solr and then reimport from Postgresql\n' + 'Are you sure you want to do this?', False).ask() if confirm: print mc_solr.dataimport_full_import()
def main(args=None): try: arguments = docopt(__doc__, version=__version__) if not arguments['--no-update']: update_self() # WARNING: port does is not supported by remoto atm ssh_username = arguments['SSH_USERNAME'] if arguments['api']: sys.exit(do_api(arguments['PATH'], arguments['ACCOUNT_ID'])) host = arguments['--host'] port = arguments['--port'] ssh_conn_str = "{id}@{host}".format(id=ssh_username, host=host, port=port) # Init if arguments['--password']: password = getpass.getpass() else: password = None pkey = arguments['--pkey'] rpc = None while rpc is None: try: rpc = SshJsonRpc(ssh_username, password, pkey, host=host, port=int(port)) password = None except SshRpcKeyEncrypted: if pkey is not None: password = getpass.getpass("Password for pubkey:") else: fail("Encrypted keys require the -i and -P flags.") except SshRpcKeyNoAuthMethod: password = getpass.getpass("No keys, log in with password:"******"Authentication failed!") try: is_assembly = rpc.do_assert_is_assembly() if not is_assembly: fail("Container is not an assembly") except SshRpcError as e: fail(e) res = rpc.do_check_init() if res['needs_init']: confirm = choice.Binary('Assembly is not initialized, would you like to do it now?', False).ask() if not confirm: stop("You choose not to init the assembly, exiting...") rpc.do_init() lock_content_json = { "hostname": platform.node(), "unix_epoch": int(time.time()), } lock_content = json.dumps(lock_content_json) rpc.do_lock_session(lock_content) rpc.do_sync() console = Console(ssh_username, rpc, ssh_conn_str) if arguments['--non-interactive'] is None: # Print status on login print_status(ssh_username, rpc.do_status(), rpc.do_env()) # Start console prompt console.cmdloop_with_keyboard_interrupt("Welcome to jsc!") else: def parse_noninteractive_cmds(line): cmd_line = line.replace(";", " ; ") cmd_arr = shlex.split(cmd_line) cmds = [] while True: if not len(cmd_arr): break cmd = cmd_arr[0] cmd_arr = cmd_arr[1:] params = [] while len(cmd_arr) and cmd_arr[0] != ";": params.append(cmd_arr[0]) cmd_arr = cmd_arr[1:] cmd_arr = cmd_arr[1:] cmds.append({"cmd": cmd, "params": " ".join(params)}) return cmds for cmd in parse_noninteractive_cmds(arguments['--non-interactive']): try: f = getattr(console, "do_{cmd}".format(cmd=cmd["cmd"])) f(cmd["params"]) except AttributeError: fail("Invalid command [{cmd}]".format(cmd="{} {}".format(cmd["cmd"], cmd["params"]))) except KeyboardInterrupt: os._exit(1) os._exit(0)