def add(context, name, bucket, region, security_group_id, vswitch_id, access_key, access_secret): """ Add a Aliyun account in the user name space on the MASH server. """ config_data = get_config(context.obj) with handle_errors(config_data['log_level'], config_data['no_color']): data = { 'account_name': name, 'bucket': bucket, 'credentials': { 'access_key': access_key, 'access_secret': access_secret }, 'region': region } if security_group_id: data['security_group_id'] = security_group_id if vswitch_id: data['vswitch_id'] = vswitch_id result = handle_request_with_token(config_data, '/v1/accounts/aliyun/', data) echo_dict(result, config_data['no_color'])
def reset_password(context, email): """ Initialize password reset for user. """ config_data = get_config(context.obj) if not email: email = config_data.get('email') if not email: echo_style('No --email parameter and no email in config.', config_data['no_color'], fg='red') sys.exit(1) with handle_errors(config_data['log_level'], config_data['no_color']): if click.confirm('Are you sure you want to reset password?'): result = handle_request(config_data, '/v1/user/password', job_data={'email': email}, action='post') echo_style(result['msg'], config_data['no_color']) else: echo_style('Aborted', config_data['no_color'], fg='red') sys.exit(1)
def update(context, name, bucket, zone, testing_account, credentials): """ Update a GCE account in the user name space on the MASH server. """ config_data = get_config(context.obj) with handle_errors(config_data['log_level'], config_data['no_color']): data = {} if credentials: with open(credentials) as credentials_file: creds = json.load(credentials_file) data['credentials'] = creds if bucket: data['bucket'] = bucket if zone: data['region'] = zone if testing_account: data['testing_account'] = testing_account if not data: echo_style('Nothing to update', config_data['no_color'], fg='red') sys.exit(1) result = handle_request_with_token( config_data, '/v1/accounts/gce/{name}'.format(name=name), data) echo_dict(result, config_data['no_color'])
def wait(context, wait_time, job_id): """ Wait for job to arrive at finished or failed status. By default waits 5 minutes between status queries. """ config_data = get_config(context.obj) state = 'undefined' while True: with handle_errors(config_data['log_level'], config_data['no_color']): result = handle_request_with_token( config_data, '/v1/jobs/{0}'.format(job_id), action='get' ) state = result['state'] if state not in ('running', 'undefined'): break time.sleep(wait_time) click.echo(state)
def add(context, name, bucket, region, availability_domain, compartment_id, oci_user_id, tenancy, signing_key_file): """ Add a OCI account in the user name space on the MASH server. """ config_data = get_config(context.obj) with handle_errors(config_data['log_level'], config_data['no_color']): with open(signing_key_file) as key_file: signing_key = key_file.read() data = { 'account_name': name, 'bucket': bucket, 'region': region, 'availability_domain': availability_domain, 'compartment_id': compartment_id, 'oci_user_id': oci_user_id, 'tenancy': tenancy, 'signing_key': signing_key } result = handle_request_with_token(config_data, '/v1/accounts/oci/', data) echo_dict(result, config_data['no_color'])
def add(context, name, bucket, zone, testing_account, is_publishing_account, credentials): """ Add a GCE account in the user name space on the MASH server. """ config_data = get_config(context.obj) with handle_errors(config_data['log_level'], config_data['no_color']): with open(credentials) as credentials_file: creds = json.load(credentials_file) data = { 'account_name': name, 'bucket': bucket, 'credentials': creds, 'region': zone } if testing_account: data['testing_account'] = testing_account if is_publishing_account: data['is_publishing_account'] = True result = handle_request_with_token(config_data, '/v1/accounts/gce/', data) echo_dict(result, config_data['no_color'])
def delete(context, job_id): """ Delete the job with the given ID from the MASH server pipeline. """ config_data = get_config(context.obj) with handle_errors(config_data['log_level'], config_data['no_color']): result = delete_job(config_data, job_id) echo_style(result['msg'], config_data['no_color'])
def status(context, job_id): """ Get basic status for a job in the MASH server pipeline. """ config_data = get_config(context.obj) with handle_errors(config_data['log_level'], config_data['no_color']): status_info = get_job_status(config_data, job_id) echo_dict(status_info, config_data['no_color'])
def get_schema(context, output_style): """ Get the an annotated json dictionary for a Azure job. """ config_data = get_config(context.obj) with handle_errors(config_data['log_level'], config_data['no_color']): result = get_job_schema_by_cloud(config_data, output_style, 'azure') echo_dict(result, config_data['no_color'])
def logout(context): """ Handle mash user logout. Deletes the current refresh token. """ config_data = get_config(context.obj) with handle_errors(config_data['log_level'], config_data['no_color']): result = logout_session(config_data) echo_style(result['msg'], config_data['no_color'])
def get(context, jti): """ Return information for token matching jti. """ config_data = get_config(context.obj) with handle_errors(config_data['log_level'], config_data['no_color']): result = handle_request_with_token( config_data, '/v1/auth/token/{jti}'.format(jti=jti), action='get') echo_dict(result, config_data['no_color'])
def refresh(context): """ Handle token refresh. Get a new access token using current refresh token. """ config_data = get_config(context.obj) with handle_errors(config_data['log_level'], config_data['no_color']): refresh_token(config_data) echo_style('Token refreshed.', config_data['no_color'])
def get_user(context): """ Get mash user info. """ config_data = get_config(context.obj) with handle_errors(config_data['log_level'], config_data['no_color']): result = handle_request_with_token(config_data, '/v1/user/', action='get') echo_dict(result, config_data['no_color'])
def list_aliyun_accounts(context): """ Get a list of all Aliyun accounts. """ config_data = get_config(context.obj) with handle_errors(config_data['log_level'], config_data['no_color']): result = handle_request_with_token(config_data, '/v1/accounts/aliyun/', action='get') echo_dict(result, config_data['no_color'])
def token_list(context): """ Return a list of JWT tokens for user. """ config_data = get_config(context.obj) with handle_errors(config_data['log_level'], config_data['no_color']): result = handle_request_with_token(config_data, '/v1/auth/token', action='get') echo_dict(result, config_data['no_color'])
def update( context, additional_regions, group, name, region, subnet, access_key_id, secret_access_key ): """ Update an EC2 account in the user name space on the MASH server. """ config_data = get_config(context.obj) with handle_errors(config_data['log_level'], config_data['no_color']): data = {} if all([access_key_id, secret_access_key]): data['credentials'] = { 'access_key_id': access_key_id, 'secret_access_key': secret_access_key } elif any([access_key_id, secret_access_key]): echo_style( 'Both secret_access_key and access_key_id are required ' 'when updating credentials.', config_data['no_color'], fg='red' ) sys.exit(1) if additional_regions: regions = additional_regions_repl() if regions: data['additional_regions'] = regions if group: data['group'] = group if region: data['region'] = region if subnet: data['subnet'] = subnet if not data: echo_style('Nothing to update', config_data['no_color'], fg='red') sys.exit(1) result = handle_request_with_token( config_data, '/v1/accounts/ec2/{name}'.format(name=name), data ) echo_dict(result, config_data['no_color'])
def show_config(context): """ Prints a dictionary from the client config file based on profile. """ config_data = get_config(context.obj) config_file_path = os.path.join(config_data['config_dir'], config_data['profile'] + '.yaml') with handle_errors(config_data['log_level'], config_data['no_color']): with open(config_file_path, 'r') as config_file: config_values = yaml.safe_load(config_file) echo_dict(config_values, config_data['no_color'])
def get(context, name): """ Get info for a Aliyun account. """ config_data = get_config(context.obj) with handle_errors(config_data['log_level'], config_data['no_color']): result = handle_request_with_token( config_data, '/v1/accounts/aliyun/{name}'.format(name=name), action='get') echo_dict(result, config_data['no_color'])
def delete(context, name): """ Delete an Aliyun account in the user name space on the MASH server. """ config_data = get_config(context.obj) with handle_errors(config_data['log_level'], config_data['no_color']): result = handle_request_with_token( config_data, '/v1/accounts/aliyun/{name}'.format(name=name), action='delete') echo_style(result['msg'], config_data['no_color'])
def get(context, job_id, show_data): """ Get info for a job in the MASH server pipeline. """ config_data = get_config(context.obj) with handle_errors(config_data['log_level'], config_data['no_color']): result = get_job(config_data, job_id) if not show_data: with suppress(KeyError): del result['data'] echo_dict(result, config_data['no_color'])
def delete_user(context): """ Handle mash user deletion requests. """ config_data = get_config(context.obj) with handle_errors(config_data['log_level'], config_data['no_color']): if click.confirm('Are you sure you want to delete user?'): result = handle_request_with_token(config_data, '/v1/user/', action='delete') echo_style(result['msg'], config_data['no_color']) else: echo_style('Aborted', config_data['no_color'], fg='red') sys.exit(1)
def list_jobs(context, api_version, per_page, page): """ List all jobs in the MASH server pipeline. """ config_data = get_config(context.obj) with handle_errors(config_data['log_level'], config_data['no_color']): kwargs = {} if page: kwargs['page'] = page if per_page: kwargs['per_page'] = per_page if api_version: kwargs['api_version'] = api_version result = list_user_jobs(config_data, **kwargs) echo_dict(result, config_data['no_color'])
def oidc(context): """ Handle mash OpenID Connect authentication. """ config_data = get_config(context.obj) with handle_errors(config_data['log_level'], config_data['no_color']): result = handle_request(config_data, '/v1/auth/oauth2', action='get') redirect_port = get_free_port(result['redirect_ports']) if not redirect_port: raise MashClientException('No redirect port available') auth_url = '{}&redirect_uri=http%3A%2F%2Flocalhost%3A{}'.format( result['auth_url'], redirect_port) # display authentication message echo_style('{}: {}'.format(result['msg'], auth_url), config_data['no_color']) click.launch(auth_url) auth_code = get_oauth2_code(redirect_port) if not auth_code: echo_style('Failed login: No authentication code received.', config_data['no_color'], fg='red') sys.exit(1) job_data = { 'auth_code': auth_code, 'state': result['state'], 'redirect_port': redirect_port } tokens = handle_request(config_data, '/v1/auth/oauth2', job_data=job_data, action='post') tokens_file = get_tokens_file(config_data['config_dir'], config_data['profile']) save_tokens_to_file(tokens_file, tokens) echo_style('Login successful.', config_data['no_color'])
def login(context, email): """ Handle mash user login. """ config_data = get_config(context.obj) if not email: email = config_data.get('email') if not email: echo_style('No --email parameter and no email in config.', config_data['no_color'], fg='red') sys.exit(1) with handle_errors(config_data['log_level'], config_data['no_color']): password = click.prompt('Enter password', type=str, hide_input=True) result = login_with_pass(config_data, email, password) echo_style(result['msg'], config_data['no_color'])
def update(context, name, bucket, region, security_group_id, vswitch_id, access_key, access_secret): """ Update a Aliyun account in the user name space on the MASH server. """ config_data = get_config(context.obj) with handle_errors(config_data['log_level'], config_data['no_color']): data = {} if all([access_key, access_secret]): data['credentials'] = { 'access_key': access_key, 'access_secret': access_secret } elif any([access_key, access_secret]): echo_style( 'Both access_secret and access_key are required ' 'when updating credentials.', config_data['no_color'], fg='red') sys.exit(1) if bucket: data['bucket'] = bucket if region: data['region'] = region if security_group_id: data['security_group_id'] = security_group_id if vswitch_id: data['vswitch_id'] = vswitch_id if not data: echo_style('Nothing to update', config_data['no_color'], fg='red') sys.exit(1) result = handle_request_with_token( config_data, '/v1/accounts/aliyun/{name}'.format(name=name), data) echo_dict(result, config_data['no_color'])
def update(context, name, bucket, region, availability_domain, compartment_id, oci_user_id, tenancy, signing_key_file): """ Update a OCI account in the user name space on the MASH server. """ config_data = get_config(context.obj) with handle_errors(config_data['log_level'], config_data['no_color']): data = {} if signing_key_file: with open(signing_key_file) as key_file: signing_key = key_file.read() data['signing_key'] = signing_key if bucket: data['bucket'] = bucket if region: data['region'] = region if availability_domain: data['availability_domain'] = availability_domain if compartment_id: data['compartment_id'] = compartment_id if oci_user_id: data['oci_user_id'] = oci_user_id if tenancy: data['tenancy'] = tenancy if not data: echo_style('Nothing to update', config_data['no_color'], fg='red') sys.exit(1) result = handle_request_with_token( config_data, '/v1/accounts/oci/{name}'.format(name=name), data) echo_dict(result, config_data['no_color'])
def setup_config(config_dir, profile, email, host, port, log_level, color, verify): """ Create a configuration file for the mash command line tool For details see man 5 mash_client.conf """ no_color = not color # Value is stored as no_color in config file if verify: path_to_cert = click.prompt('Enter path to cert (optional)', type=str, default='', show_default=True) if path_to_cert: verify = path_to_cert config_values = { 'host': host, 'log_level': log_level, 'no_color': no_color, 'verify': verify } if email: config_values['email'] = email if port: config_values['port'] = port config_dir = os.path.expanduser(config_dir) if not os.path.isdir(config_dir): os.makedirs(config_dir) config_file_path = os.path.join(config_dir, profile + '.yaml') with handle_errors(log_level, no_color): with open(config_file_path, 'w') as config_file: yaml.dump(config_values, config_file, default_flow_style=False) echo_dict(config_values, no_color)
def change_password(context, email): """ Change password for user. """ config_data = get_config(context.obj) if not email: email = config_data.get('email') if not email: echo_style('No --email parameter and no email in config.', config_data['no_color'], fg='red') sys.exit(1) with handle_errors(config_data['log_level'], config_data['no_color']): current_pass = click.prompt('Enter current password', type=str, hide_input=True) new_pass1 = click.prompt('Enter new password', type=str, hide_input=True) new_pass2 = click.prompt('Confirm new password', type=str, hide_input=True) if new_pass1 != new_pass2: raise MashClientException('New passwords do not match!') job_data = { 'email': email, 'current_password': current_pass, 'new_password': new_pass1 } result = handle_request(config_data, '/v1/user/password', job_data=job_data, action='put') echo_style(result['msg'], config_data['no_color'])
def delete(context, jti): """ If no jti is provided delete all tokens. Otherwise delete token matching the jti. """ config_data = get_config(context.obj) with handle_errors(config_data['log_level'], config_data['no_color']): if jti: result = handle_request_with_token( config_data, '/v1/auth/token/{jti}'.format(jti=jti), action='delete') elif click.confirm('Are you sure you want to delete all tokens?'): result = handle_request_with_token(config_data, '/v1/auth/token', action='delete') else: echo_style('No tokens deleted', config_data['no_color'], fg='red') sys.exit(1) echo_style(result['msg'], config_data['no_color'])
def add(context, dry_run, document, api_version): """ Send add azure job request to mash server based on provided json document. """ config_data = get_config(context.obj) with handle_errors(config_data['log_level'], config_data['no_color']): with open(document) as job_file: job_data = json.load(job_file) if dry_run: job_data['dry_run'] = True kwargs = {} if api_version: kwargs['api_version'] = api_version result = add_job(config_data, job_data, 'azure', **kwargs) if 'msg' in result: echo_style(result['msg'], config_data['no_color']) else: echo_dict(result, config_data['no_color'])