How to use parsed_args method in Slash

Best Python code snippet using slash

createcluster.py

Source:createcluster.py Github

copy

Full Screen

1# Copyright 2014 Amazon.com, Inc. or its affiliates. All Rights Reserved.2#3# Licensed under the Apache License, Version 2.0 (the "License"). You4# may not use this file except in compliance with the License. A copy of5# the License is located at6#7# http://aws.amazon.com/apache2.0/8#9# or in the "license" file accompanying this file. This file is10# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF11# ANY KIND, either express or implied. See the License for the specific12# language governing permissions and limitations under the License.13import re14from awscli.customizations.commands import BasicCommand15from awscli.customizations.emr import applicationutils16from awscli.customizations.emr import argumentschema17from awscli.customizations.emr import constants18from awscli.customizations.emr import emrfsutils19from awscli.customizations.emr import emrutils20from awscli.customizations.emr import exceptions21from awscli.customizations.emr import hbaseutils22from awscli.customizations.emr import helptext23from awscli.customizations.emr import instancegroupsutils24from awscli.customizations.emr import instancefleetsutils25from awscli.customizations.emr import steputils26from awscli.customizations.emr.command import Command27from awscli.customizations.emr.constants import EC2_ROLE_NAME28from awscli.customizations.emr.constants import EMR_ROLE_NAME29from botocore.compat import json30class CreateCluster(Command):31 NAME = 'create-cluster'32 DESCRIPTION = helptext.CREATE_CLUSTER_DESCRIPTION33 ARG_TABLE = [34 {'name': 'release-label',35 'help_text': helptext.RELEASE_LABEL},36 {'name': 'ami-version',37 'help_text': helptext.AMI_VERSION},38 {'name': 'instance-groups',39 'schema': argumentschema.INSTANCE_GROUPS_SCHEMA,40 'help_text': helptext.INSTANCE_GROUPS},41 {'name': 'instance-type',42 'help_text': helptext.INSTANCE_TYPE},43 {'name': 'instance-count',44 'help_text': helptext.INSTANCE_COUNT},45 {'name': 'auto-terminate', 'action': 'store_true',46 'group_name': 'auto_terminate',47 'help_text': helptext.AUTO_TERMINATE},48 {'name': 'no-auto-terminate', 'action': 'store_true',49 'group_name': 'auto_terminate'},50 {'name': 'instance-fleets',51 'schema': argumentschema.INSTANCE_FLEETS_SCHEMA,52 'help_text': helptext.INSTANCE_FLEETS},53 {'name': 'name',54 'default': 'Development Cluster',55 'help_text': helptext.CLUSTER_NAME},56 {'name': 'log-uri',57 'help_text': helptext.LOG_URI},58 {'name': 'service-role',59 'help_text': helptext.SERVICE_ROLE},60 {'name': 'auto-scaling-role',61 'help_text': helptext.AUTOSCALING_ROLE},62 {'name': 'use-default-roles', 'action': 'store_true',63 'help_text': helptext.USE_DEFAULT_ROLES},64 {'name': 'configurations',65 'help_text': helptext.CONFIGURATIONS},66 {'name': 'ec2-attributes',67 'help_text': helptext.EC2_ATTRIBUTES,68 'schema': argumentschema.EC2_ATTRIBUTES_SCHEMA},69 {'name': 'termination-protected', 'action': 'store_true',70 'group_name': 'termination_protected',71 'help_text': helptext.TERMINATION_PROTECTED},72 {'name': 'no-termination-protected', 'action': 'store_true',73 'group_name': 'termination_protected'},74 {'name': 'scale-down-behavior',75 'help_text': helptext.SCALE_DOWN_BEHAVIOR},76 {'name': 'visible-to-all-users', 'action': 'store_true',77 'group_name': 'visibility',78 'help_text': helptext.VISIBILITY},79 {'name': 'no-visible-to-all-users', 'action': 'store_true',80 'group_name': 'visibility'},81 {'name': 'enable-debugging', 'action': 'store_true',82 'group_name': 'debug',83 'help_text': helptext.DEBUGGING},84 {'name': 'no-enable-debugging', 'action': 'store_true',85 'group_name': 'debug'},86 {'name': 'tags', 'nargs': '+',87 'help_text': helptext.TAGS,88 'schema': argumentschema.TAGS_SCHEMA},89 {'name': 'bootstrap-actions',90 'help_text': helptext.BOOTSTRAP_ACTIONS,91 'schema': argumentschema.BOOTSTRAP_ACTIONS_SCHEMA},92 {'name': 'applications',93 'help_text': helptext.APPLICATIONS,94 'schema': argumentschema.APPLICATIONS_SCHEMA},95 {'name': 'emrfs',96 'help_text': helptext.EMR_FS,97 'schema': argumentschema.EMR_FS_SCHEMA},98 {'name': 'steps',99 'schema': argumentschema.STEPS_SCHEMA,100 'help_text': helptext.STEPS},101 {'name': 'additional-info',102 'help_text': helptext.ADDITIONAL_INFO},103 {'name': 'restore-from-hbase-backup',104 'schema': argumentschema.HBASE_RESTORE_FROM_BACKUP_SCHEMA,105 'help_text': helptext.RESTORE_FROM_HBASE},106 {'name': 'security-configuration',107 'help_text': helptext.SECURITY_CONFIG},108 {'name': 'custom-ami-id',109 'help_text' : helptext.CUSTOM_AMI_ID},110 {'name': 'ebs-root-volume-size',111 'help_text' : helptext.EBS_ROOT_VOLUME_SIZE},112 {'name': 'repo-upgrade-on-boot',113 'help_text' : helptext.REPO_UPGRADE_ON_BOOT},114 {'name': 'kerberos-attributes',115 'schema': argumentschema.KERBEROS_ATTRIBUTES_SCHEMA,116 'help_text': helptext.KERBEROS_ATTRIBUTES}117 ]118 SYNOPSIS = BasicCommand.FROM_FILE('emr', 'create-cluster-synopsis.rst')119 EXAMPLES = BasicCommand.FROM_FILE('emr', 'create-cluster-examples.rst')120 def _run_main_command(self, parsed_args, parsed_globals):121 params = {}122 params['Name'] = parsed_args.name123 self._validate_release_label_ami_version(parsed_args)124 service_role_validation_message = (125 " Either choose --use-default-roles or use both --service-role "126 "<roleName> and --ec2-attributes InstanceProfile=<profileName>.")127 if parsed_args.use_default_roles is True and \128 parsed_args.service_role is not None:129 raise exceptions.MutualExclusiveOptionError(130 option1="--use-default-roles",131 option2="--service-role",132 message=service_role_validation_message)133 if parsed_args.use_default_roles is True and \134 parsed_args.ec2_attributes is not None and \135 'InstanceProfile' in parsed_args.ec2_attributes:136 raise exceptions.MutualExclusiveOptionError(137 option1="--use-default-roles",138 option2="--ec2-attributes InstanceProfile",139 message=service_role_validation_message)140 if parsed_args.instance_groups is not None and \141 parsed_args.instance_fleets is not None:142 raise exceptions.MutualExclusiveOptionError(143 option1="--instance-groups",144 option2="--instance-fleets")145 instances_config = {}146 if parsed_args.instance_fleets is not None:147 instances_config['InstanceFleets'] = \148 instancefleetsutils.validate_and_build_instance_fleets(149 parsed_args.instance_fleets)150 else:151 instances_config['InstanceGroups'] = \152 instancegroupsutils.validate_and_build_instance_groups(153 instance_groups=parsed_args.instance_groups,154 instance_type=parsed_args.instance_type,155 instance_count=parsed_args.instance_count)156 if parsed_args.release_label is not None:157 params["ReleaseLabel"] = parsed_args.release_label158 if parsed_args.configurations is not None:159 try:160 params["Configurations"] = json.loads(161 parsed_args.configurations)162 except ValueError:163 raise ValueError('aws: error: invalid json argument for '164 'option --configurations')165 if (parsed_args.release_label is None and166 parsed_args.ami_version is not None):167 is_valid_ami_version = re.match('\d?\..*', parsed_args.ami_version)168 if is_valid_ami_version is None:169 raise exceptions.InvalidAmiVersionError(170 ami_version=parsed_args.ami_version)171 params['AmiVersion'] = parsed_args.ami_version172 emrutils.apply_dict(173 params, 'AdditionalInfo', parsed_args.additional_info)174 emrutils.apply_dict(params, 'LogUri', parsed_args.log_uri)175 if parsed_args.use_default_roles is True:176 parsed_args.service_role = EMR_ROLE_NAME177 if parsed_args.ec2_attributes is None:178 parsed_args.ec2_attributes = {}179 parsed_args.ec2_attributes['InstanceProfile'] = EC2_ROLE_NAME180 emrutils.apply_dict(params, 'ServiceRole', parsed_args.service_role)181 if parsed_args.instance_groups is not None:182 for instance_group in instances_config['InstanceGroups']:183 if 'AutoScalingPolicy' in instance_group.keys():184 if parsed_args.auto_scaling_role is None:185 raise exceptions.MissingAutoScalingRoleError()186 emrutils.apply_dict(params, 'AutoScalingRole', parsed_args.auto_scaling_role)187 if parsed_args.scale_down_behavior is not None:188 emrutils.apply_dict(params, 'ScaleDownBehavior', parsed_args.scale_down_behavior)189 if (190 parsed_args.no_auto_terminate is False and191 parsed_args.auto_terminate is False):192 parsed_args.no_auto_terminate = True193 instances_config['KeepJobFlowAliveWhenNoSteps'] = \194 emrutils.apply_boolean_options(195 parsed_args.no_auto_terminate,196 '--no-auto-terminate',197 parsed_args.auto_terminate,198 '--auto-terminate')199 instances_config['TerminationProtected'] = \200 emrutils.apply_boolean_options(201 parsed_args.termination_protected,202 '--termination-protected',203 parsed_args.no_termination_protected,204 '--no-termination-protected')205 if (parsed_args.visible_to_all_users is False and206 parsed_args.no_visible_to_all_users is False):207 parsed_args.visible_to_all_users = True208 params['VisibleToAllUsers'] = \209 emrutils.apply_boolean_options(210 parsed_args.visible_to_all_users,211 '--visible-to-all-users',212 parsed_args.no_visible_to_all_users,213 '--no-visible-to-all-users')214 params['Tags'] = emrutils.parse_tags(parsed_args.tags)215 params['Instances'] = instances_config216 if parsed_args.ec2_attributes is not None:217 self._build_ec2_attributes(218 cluster=params, parsed_attrs=parsed_args.ec2_attributes)219 debugging_enabled = emrutils.apply_boolean_options(220 parsed_args.enable_debugging,221 '--enable-debugging',222 parsed_args.no_enable_debugging,223 '--no-enable-debugging')224 if parsed_args.log_uri is None and debugging_enabled is True:225 raise exceptions.LogUriError226 if debugging_enabled is True:227 self._update_cluster_dict(228 cluster=params,229 key='Steps',230 value=[231 self._build_enable_debugging(parsed_args, parsed_globals)])232 if parsed_args.applications is not None:233 if parsed_args.release_label is None:234 app_list, ba_list, step_list = \235 applicationutils.build_applications(236 region=self.region,237 parsed_applications=parsed_args.applications,238 ami_version=params['AmiVersion'])239 self._update_cluster_dict(240 params, 'NewSupportedProducts', app_list)241 self._update_cluster_dict(242 params, 'BootstrapActions', ba_list)243 self._update_cluster_dict(244 params, 'Steps', step_list)245 else:246 params["Applications"] = []247 for application in parsed_args.applications:248 params["Applications"].append(application)249 hbase_restore_config = parsed_args.restore_from_hbase_backup250 if hbase_restore_config is not None:251 args = hbaseutils.build_hbase_restore_from_backup_args(252 dir=hbase_restore_config.get('Dir'),253 backup_version=hbase_restore_config.get('BackupVersion'))254 step_config = emrutils.build_step(255 jar=constants.HBASE_JAR_PATH,256 name=constants.HBASE_RESTORE_STEP_NAME,257 action_on_failure=constants.CANCEL_AND_WAIT,258 args=args)259 self._update_cluster_dict(260 params, 'Steps', [step_config])261 if parsed_args.bootstrap_actions is not None:262 self._build_bootstrap_actions(263 cluster=params,264 parsed_boostrap_actions=parsed_args.bootstrap_actions)265 if parsed_args.emrfs is not None:266 self._handle_emrfs_parameters(267 cluster=params,268 emrfs_args=parsed_args.emrfs,269 release_label=parsed_args.release_label)270 if parsed_args.steps is not None:271 steps_list = steputils.build_step_config_list(272 parsed_step_list=parsed_args.steps,273 region=self.region,274 release_label=parsed_args.release_label)275 self._update_cluster_dict(276 cluster=params, key='Steps', value=steps_list)277 if parsed_args.security_configuration is not None:278 emrutils.apply_dict(279 params, 'SecurityConfiguration', parsed_args.security_configuration)280 if parsed_args.custom_ami_id is not None:281 emrutils.apply_dict(282 params, 'CustomAmiId', parsed_args.custom_ami_id283 )284 if parsed_args.ebs_root_volume_size is not None:285 emrutils.apply_dict(286 params, 'EbsRootVolumeSize', int(parsed_args.ebs_root_volume_size)287 )288 if parsed_args.repo_upgrade_on_boot is not None:289 emrutils.apply_dict(290 params, 'RepoUpgradeOnBoot', parsed_args.repo_upgrade_on_boot291 )292 if parsed_args.kerberos_attributes is not None:293 emrutils.apply_dict(294 params, 'KerberosAttributes', parsed_args.kerberos_attributes)295 self._validate_required_applications(parsed_args)296 run_job_flow_response = emrutils.call(297 self._session, 'run_job_flow', params, self.region,298 parsed_globals.endpoint_url, parsed_globals.verify_ssl)299 constructed_result = self._construct_result(run_job_flow_response)300 emrutils.display_response(self._session, 'run_job_flow',301 constructed_result, parsed_globals)302 return 0303 def _construct_result(self, run_job_flow_result):304 jobFlowId = None305 if run_job_flow_result is not None:306 jobFlowId = run_job_flow_result.get('JobFlowId')307 if jobFlowId is not None:308 return {'ClusterId': jobFlowId}309 else:310 return {}311 def _build_ec2_attributes(self, cluster, parsed_attrs):312 keys = parsed_attrs.keys()313 instances = cluster['Instances']314 if ('SubnetId' in keys and 'SubnetIds' in keys):315 raise exceptions.MutualExclusiveOptionError(316 option1="SubnetId",317 option2="SubnetIds")318 if ('AvailabilityZone' in keys and 'AvailabilityZones' in keys):319 raise exceptions.MutualExclusiveOptionError(320 option1="AvailabilityZone",321 option2="AvailabilityZones")322 if ('SubnetId' in keys or 'SubnetIds' in keys) \323 and ('AvailabilityZone' in keys or 'AvailabilityZones' in keys):324 raise exceptions.SubnetAndAzValidationError325 emrutils.apply_params(326 src_params=parsed_attrs, src_key='KeyName',327 dest_params=instances, dest_key='Ec2KeyName')328 emrutils.apply_params(329 src_params=parsed_attrs, src_key='SubnetId',330 dest_params=instances, dest_key='Ec2SubnetId')331 emrutils.apply_params(332 src_params=parsed_attrs, src_key='SubnetIds',333 dest_params=instances, dest_key='Ec2SubnetIds')334 if 'AvailabilityZone' in keys:335 instances['Placement'] = dict()336 emrutils.apply_params(337 src_params=parsed_attrs, src_key='AvailabilityZone',338 dest_params=instances['Placement'],339 dest_key='AvailabilityZone')340 if 'AvailabilityZones' in keys:341 instances['Placement'] = dict()342 emrutils.apply_params(343 src_params=parsed_attrs, src_key='AvailabilityZones',344 dest_params=instances['Placement'],345 dest_key='AvailabilityZones')346 emrutils.apply_params(347 src_params=parsed_attrs, src_key='InstanceProfile',348 dest_params=cluster, dest_key='JobFlowRole')349 emrutils.apply_params(350 src_params=parsed_attrs, src_key='EmrManagedMasterSecurityGroup',351 dest_params=instances, dest_key='EmrManagedMasterSecurityGroup')352 emrutils.apply_params(353 src_params=parsed_attrs, src_key='EmrManagedSlaveSecurityGroup',354 dest_params=instances, dest_key='EmrManagedSlaveSecurityGroup')355 emrutils.apply_params(356 src_params=parsed_attrs, src_key='ServiceAccessSecurityGroup',357 dest_params=instances, dest_key='ServiceAccessSecurityGroup')358 emrutils.apply_params(359 src_params=parsed_attrs, src_key='AdditionalMasterSecurityGroups',360 dest_params=instances, dest_key='AdditionalMasterSecurityGroups')361 emrutils.apply_params(362 src_params=parsed_attrs, src_key='AdditionalSlaveSecurityGroups',363 dest_params=instances, dest_key='AdditionalSlaveSecurityGroups')364 emrutils.apply(params=cluster, key='Instances', value=instances)365 return cluster366 def _build_bootstrap_actions(367 self, cluster, parsed_boostrap_actions):368 cluster_ba_list = cluster.get('BootstrapActions')369 if cluster_ba_list is None:370 cluster_ba_list = []371 bootstrap_actions = []372 if len(cluster_ba_list) + len(parsed_boostrap_actions) \373 > constants.MAX_BOOTSTRAP_ACTION_NUMBER:374 raise ValueError('aws: error: maximum number of '375 'bootstrap actions for a cluster exceeded.')376 for ba in parsed_boostrap_actions:377 ba_config = {}378 if ba.get('Name') is not None:379 ba_config['Name'] = ba.get('Name')380 else:381 ba_config['Name'] = constants.BOOTSTRAP_ACTION_NAME382 script_arg_config = {}383 emrutils.apply_params(384 src_params=ba, src_key='Path',385 dest_params=script_arg_config, dest_key='Path')386 emrutils.apply_params(387 src_params=ba, src_key='Args',388 dest_params=script_arg_config, dest_key='Args')389 emrutils.apply(390 params=ba_config,391 key='ScriptBootstrapAction',392 value=script_arg_config)393 bootstrap_actions.append(ba_config)394 result = cluster_ba_list + bootstrap_actions395 if len(result) > 0:396 cluster['BootstrapActions'] = result397 return cluster398 def _build_enable_debugging(self, parsed_args, parsed_globals):399 if parsed_args.release_label:400 jar = constants.COMMAND_RUNNER401 args = [constants.DEBUGGING_COMMAND]402 else:403 jar = emrutils.get_script_runner(self.region)404 args = [emrutils.build_s3_link(405 relative_path=constants.DEBUGGING_PATH,406 region=self.region)]407 return emrutils.build_step(408 name=constants.DEBUGGING_NAME,409 action_on_failure=constants.TERMINATE_CLUSTER,410 jar=jar,411 args=args)412 def _update_cluster_dict(self, cluster, key, value):413 if key in cluster.keys():414 cluster[key] += value415 elif value is not None and len(value) > 0:416 cluster[key] = value417 return cluster418 def _validate_release_label_ami_version(self, parsed_args):419 if parsed_args.ami_version is not None and \420 parsed_args.release_label is not None:421 raise exceptions.MutualExclusiveOptionError(422 option1="--ami-version",423 option2="--release-label")424 if parsed_args.ami_version is None and \425 parsed_args.release_label is None:426 raise exceptions.RequiredOptionsError(427 option1="--ami-version",428 option2="--release-label")429 # Checks if the applications required by steps are specified430 # using the --applications option.431 def _validate_required_applications(self, parsed_args):432 specified_apps = set([])433 if parsed_args.applications is not None:434 specified_apps = \435 set([app['Name'].lower() for app in parsed_args.applications])436 missing_apps = self._get_missing_applications_for_steps(specified_apps,437 parsed_args)438 # Check for HBase.439 if parsed_args.restore_from_hbase_backup is not None:440 if constants.HBASE not in specified_apps:441 missing_apps.add(constants.HBASE.title())442 if len(missing_apps) != 0:443 raise exceptions.MissingApplicationsError(444 applications=missing_apps)445 def _get_missing_applications_for_steps(self, specified_apps, parsed_args):446 allowed_app_steps = set([constants.HIVE, constants.PIG,447 constants.IMPALA])448 missing_apps = set([])449 if parsed_args.steps is not None:450 for step in parsed_args.steps:451 if len(missing_apps) == len(allowed_app_steps):452 break453 step_type = step.get('Type')454 if step_type is not None:455 step_type = step_type.lower()456 if step_type in allowed_app_steps and \457 step_type not in specified_apps:458 missing_apps.add(step['Type'].title())459 return missing_apps460 def _filter_configurations_in_special_cases(self, configurations,461 parsed_args, parsed_configs):462 if parsed_args.use_default_roles:463 configurations = [x for x in configurations464 if x.name is not 'service_role' and465 x.name is not 'instance_profile']466 return configurations467 def _handle_emrfs_parameters(self, cluster, emrfs_args, release_label):468 if release_label:469 self.validate_no_emrfs_configuration(cluster)470 emrfs_configuration = emrfsutils.build_emrfs_confiuration(471 emrfs_args)472 self._update_cluster_dict(473 cluster=cluster, key='Configurations',474 value=[emrfs_configuration])475 else:476 emrfs_ba_config_list = emrfsutils.build_bootstrap_action_configs(477 self.region, emrfs_args)478 self._update_cluster_dict(479 cluster=cluster, key='BootstrapActions',480 value=emrfs_ba_config_list)481 def validate_no_emrfs_configuration(self, cluster):482 if 'Configurations' in cluster:483 for config in cluster['Configurations']:484 if config is not None and \485 config.get('Classification') == constants.EMRFS_SITE:...

Full Screen

Full Screen

role.py

Source:role.py Github

copy

Full Screen

1# Copyright 2012-2013 OpenStack Foundation2#3# Licensed under the Apache License, Version 2.0 (the "License"); you may4# not use this file except in compliance with the License. You may obtain5# a copy of the License at6#7# http://www.apache.org/licenses/LICENSE-2.08#9# Unless required by applicable law or agreed to in writing, software10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the12# License for the specific language governing permissions and limitations13# under the License.14#15"""Identity v3 Role action implementations"""16import logging17import six18import sys19from cliff import command20from cliff import lister21from cliff import show22from openstackclient.common import utils23class AddRole(command.Command):24 """Adds a role to a user or group on a domain or project"""25 log = logging.getLogger(__name__ + '.AddRole')26 def get_parser(self, prog_name):27 parser = super(AddRole, self).get_parser(prog_name)28 parser.add_argument(29 'role',30 metavar='<role>',31 help='Name or ID of role to add',32 )33 user_or_group = parser.add_mutually_exclusive_group()34 user_or_group.add_argument(35 '--user',36 metavar='<user>',37 help='Name or ID of user to add a role',38 )39 user_or_group.add_argument(40 '--group',41 metavar='<group>',42 help='Name or ID of group to add a role',43 )44 domain_or_project_or_sid_or_sip = parser.add_mutually_exclusive_group()45 domain_or_project_or_sid_or_sip.add_argument(46 '--domain',47 metavar='<domain>',48 help='Name or ID of domain associated with user or group',49 )50 domain_or_project_or_sid_or_sip.add_argument(51 '--project',52 metavar='<project>',53 help='Name or ID of project associated with user or group',54 )55 domain_or_project_or_sid_or_sip.add_argument(56 '--sid',57 metavar='<sid>',58 help='Name or ID of sid associated with user or group',59 )60 domain_or_project_or_sid_or_sip.add_argument(61 '--sip',62 metavar='<sip>',63 help='Name or ID of sip associated with user or group',64 )65 return parser66 def take_action(self, parsed_args):67 self.log.debug('take_action(%s)' % parsed_args)68 identity_client = self.app.client_manager.identity69 if (not parsed_args.user and not parsed_args.domain70 and not parsed_args.group and not parsed_args.project71 and not parsed_args.sid and not parsed_args.sip):72 return73 role = utils.find_resource(74 identity_client.roles,75 parsed_args.role,76 )77 if parsed_args.user and parsed_args.domain:78 user = utils.find_resource(79 identity_client.users,80 parsed_args.user,81 )82 domain = utils.find_resource(83 identity_client.domains,84 parsed_args.domain,85 )86 identity_client.roles.grant(87 role.id,88 user=user.id,89 domain=domain.id,90 )91 elif parsed_args.user and parsed_args.project:92 user = utils.find_resource(93 identity_client.users,94 parsed_args.user,95 )96 project = utils.find_resource(97 identity_client.projects,98 parsed_args.project,99 )100 identity_client.roles.grant(101 role.id,102 user=user.id,103 project=project.id,104 )105 elif parsed_args.group and parsed_args.domain:106 group = utils.find_resource(107 identity_client.groups,108 parsed_args.group,109 )110 domain = utils.find_resource(111 identity_client.domains,112 parsed_args.domain,113 )114 identity_client.roles.grant(115 role.id,116 group=group.id,117 domain=domain.id,118 )119 elif parsed_args.group and parsed_args.project:120 group = utils.find_resource(121 identity_client.groups,122 parsed_args.group,123 )124 project = utils.find_resource(125 identity_client.projects,126 parsed_args.project,127 )128 identity_client.roles.grant(129 role.id,130 group=group.id,131 project=project.id,132 )133 elif parsed_args.user and parsed_args.sid:134 user = utils.find_resource(135 identity_client.users,136 parsed_args.user,137 )138 sid = utils.find_resource(139 identity_client.domains,140 parsed_args.sid,141 )142 identity_client.roles.grant_sid(143 role.id,144 user=user.id,145 sid=sid.id,146 )147 elif parsed_args.user and parsed_args.sip:148 user = utils.find_resource(149 identity_client.users,150 parsed_args.user,151 )152 sip = utils.find_resource(153 identity_client.projects,154 parsed_args.sip,155 )156 identity_client.roles.grant_sid(157 role.id,158 user=user.id,159 sip=sip.id,160 )161 else:162 sys.stderr.write("Role not added, incorrect set of arguments \163 provided. See openstack --help for more details\n")164 return165class CreateRole(show.ShowOne):166 """Create new role"""167 log = logging.getLogger(__name__ + '.CreateRole')168 def get_parser(self, prog_name):169 parser = super(CreateRole, self).get_parser(prog_name)170 parser.add_argument(171 'name',172 metavar='<role-name>',173 help='New role name',174 )175 return parser176 def take_action(self, parsed_args):177 self.log.debug('take_action(%s)' % parsed_args)178 identity_client = self.app.client_manager.identity179 role = identity_client.roles.create(name=parsed_args.name)180 return zip(*sorted(six.iteritems(role._info)))181class DeleteRole(command.Command):182 """Delete existing role"""183 log = logging.getLogger(__name__ + '.DeleteRole')184 def get_parser(self, prog_name):185 parser = super(DeleteRole, self).get_parser(prog_name)186 parser.add_argument(187 'role',188 metavar='<role>',189 help='Name or ID of role to delete',190 )191 return parser192 def take_action(self, parsed_args):193 self.log.debug('take_action(%s)' % parsed_args)194 identity_client = self.app.client_manager.identity195 role = utils.find_resource(196 identity_client.roles,197 parsed_args.role,198 )199 identity_client.roles.delete(role.id)200 return201class ListRole(lister.Lister):202 """List roles"""203 log = logging.getLogger(__name__ + '.ListRole')204 def take_action(self, parsed_args):205 self.log.debug('take_action(%s)' % parsed_args)206 columns = ('ID', 'Name')207 data = self.app.client_manager.identity.roles.list()208 return (columns,209 (utils.get_item_properties(210 s, columns,211 formatters={},212 ) for s in data))213class RemoveRole(command.Command):214 """Remove role command"""215 log = logging.getLogger(__name__ + '.RemoveRole')216 def get_parser(self, prog_name):217 parser = super(RemoveRole, self).get_parser(prog_name)218 parser.add_argument(219 'role',220 metavar='<role>',221 help='Name or ID of role to remove',222 )223 user_or_group = parser.add_mutually_exclusive_group()224 user_or_group.add_argument(225 '--user',226 metavar='<user>',227 help='Name or ID of user to remove a role',228 )229 user_or_group.add_argument(230 '--group',231 metavar='<group>',232 help='Name or ID of group to remove a role',233 )234 domain_or_project = parser.add_mutually_exclusive_group()235 domain_or_project.add_argument(236 '--domain',237 metavar='<domain>',238 help='Name or ID of domain associated with user or group',239 )240 domain_or_project.add_argument(241 '--project',242 metavar='<project>',243 help='Name or ID of project associated with user or group',244 )245 return parser246 def take_action(self, parsed_args):247 self.log.debug('take_action(%s)' % parsed_args)248 identity_client = self.app.client_manager.identity249 if (not parsed_args.user and not parsed_args.domain250 and not parsed_args.group and not parsed_args.project):251 return252 role = utils.find_resource(253 identity_client.roles,254 parsed_args.role,255 )256 if parsed_args.user and parsed_args.domain:257 user = utils.find_resource(258 identity_client.users,259 parsed_args.user,260 )261 domain = utils.find_resource(262 identity_client.domains,263 parsed_args.domain,264 )265 identity_client.roles.revoke(266 role.id,267 user=user.id,268 domain=domain.id,269 )270 elif parsed_args.user and parsed_args.project:271 user = utils.find_resource(272 identity_client.users,273 parsed_args.user,274 )275 project = utils.find_resource(276 identity_client.projects,277 parsed_args.project,278 )279 identity_client.roles.revoke(280 role.id,281 user=user.id,282 project=project.id,283 )284 elif parsed_args.group and parsed_args.domain:285 group = utils.find_resource(286 identity_client.groups,287 parsed_args.group,288 )289 domain = utils.find_resource(290 identity_client.domains,291 parsed_args.domain,292 )293 identity_client.roles.revoke(294 role.id,295 group=group.id,296 domain=domain.id,297 )298 elif parsed_args.group and parsed_args.project:299 group = utils.find_resource(300 identity_client.groups,301 parsed_args.group,302 )303 project = utils.find_resource(304 identity_client.projects,305 parsed_args.project,306 )307 identity_client.roles.revoke(308 role.id,309 group=group.id,310 project=project.id,311 )312 else:313 sys.stderr.write("Role not removed, incorrect set of arguments \314 provided. See openstack --help for more details\n")315 return316class SetRole(command.Command):317 """Set role command"""318 log = logging.getLogger(__name__ + '.SetRole')319 def get_parser(self, prog_name):320 parser = super(SetRole, self).get_parser(prog_name)321 parser.add_argument(322 'role',323 metavar='<role>',324 help='Name or ID of role to update',325 )326 parser.add_argument(327 '--name',328 metavar='<new-role-name>',329 help='New role name',330 )331 return parser332 def take_action(self, parsed_args):333 self.log.debug('take_action(%s)' % parsed_args)334 identity_client = self.app.client_manager.identity335 if not parsed_args.name:336 return337 role = utils.find_resource(338 identity_client.roles,339 parsed_args.role,340 )341 identity_client.roles.update(role.id, name=parsed_args.name)342 return343class ShowRole(show.ShowOne):344 """Show single role"""345 log = logging.getLogger(__name__ + '.ShowRole')346 def get_parser(self, prog_name):347 parser = super(ShowRole, self).get_parser(prog_name)348 parser.add_argument(349 'role',350 metavar='<role>',351 help='Name or ID of role to display',352 )353 return parser354 def take_action(self, parsed_args):355 self.log.debug('take_action(%s)' % parsed_args)356 identity_client = self.app.client_manager.identity357 role = utils.find_resource(358 identity_client.roles,359 parsed_args.role,360 )...

Full Screen

Full Screen

connections.py

Source:connections.py Github

copy

Full Screen

1import logging2import subprocess3from contextlib import contextmanager4from os import path5logger = logging.getLogger('connections')6@contextmanager7def set_argument(parsed_args, name, value):8 old_value = getattr(parsed_args, name)9 setattr(parsed_args, name, value)10 try:11 yield parsed_args12 finally:13 setattr(parsed_args, name, old_value)14def check_output_as_list(command):15 logger.debug(command)16 output = subprocess.check_output([command], shell=True)17 logger.debug(output)18 lines = output.decode('utf-8').split('\n')19 lines = [line for line in lines if len(line) > 0]20 logger.debug(lines)21 return lines22class Connection(object):23 @classmethod24 def hosts(cls, parsed_args):25 """ Returns a list of hosts to connect to.26 Can return a host as '\n' to force tmux to make a new window.27 """28 raise NotImplementedError()29 @classmethod30 def copy(cls, host, parsed_args):31 raise NotImplementedError()32 @classmethod33 def command(cls, host, parsed_args):34 raise NotImplementedError()35 @classmethod36 def connect(cls, host, parsed_args):37 raise NotImplementedError()38class SSHConnection(Connection):39 @classmethod40 def hosts(cls, parsed_args):41 if len(parsed_args.hosts) == 0:42 raise ValueError("At least one host must be specified!\n")43 return parsed_args.hosts44 @classmethod45 def copy(cls, host, parsed_args, and_execute=True):46 script_path, script_name = path.split(parsed_args.script)47 copy = 'scp {} {} {}:/tmp'.format(parsed_args.ssh_options, parsed_args.script, host)48 connect = cls.connect(host, parsed_args)49 chmod = connect + ' chmod u+x /tmp/{}'.format(script_name)50 if and_execute:51 execute = connect + ' /tmp/{}'.format(script_name)52 return '{} && {} && {} && {}'.format(copy, chmod, execute, connect)53 return '{} && {}'.format(copy, chmod)54 @classmethod55 def command(cls, host, parsed_args):56 return '{} {} {} {}'.format(parsed_args.ssh_command, parsed_args.ssh_options, host, parsed_args.command)57 @classmethod58 def connect(cls, host, parsed_args):59 return '{} {} {}'.format(parsed_args.ssh_command, parsed_args.ssh_options, host)60class DockerConnection(Connection):61 @classmethod62 def hosts(cls, parsed_args, prepend_command=''):63 host_names = parsed_args.hosts64 matched_host_names = {}65 for name in host_names:66 matched_host_names[name] = False67 hosts = []68 names_and_ids = check_output_as_list(69 prepend_command + "docker ps --format '{{.Names}},{{.ID}}'")70 if len(host_names) == 0:71 hosts.extend(n_and_i.split(',')[1] for n_and_i in names_and_ids)72 else:73 for n_and_i in names_and_ids:74 n, i = n_and_i.split(',')75 if n in host_names:76 hosts.append(i)77 matched_host_names[n] = True78 elif i in host_names:79 hosts.append(i)80 matched_host_names[n] = True81 elif parsed_args.approximate and any(name in n for name in host_names):82 hosts.append(i)83 elif parsed_args.approximate and any(name in i for name in host_names):84 hosts.append(i)85 logger.debug("hosts = {0}".format(hosts))86 if len(hosts) == 0:87 raise ValueError("No docker containers detected to connect to!")88 if not parsed_args.approximate:89 for name in host_names:90 if not matched_host_names[name]:91 raise ValueError("No container named '{}' found!".format(name))92 return hosts93 @classmethod94 def _execute(cls, host, parsed_args, command, prepend_command=''):95 with set_argument(parsed_args, 'docker_command', 'exec -it {}'.format('{} ' + command)) as parsed_args:96 return cls.connect(host, parsed_args, prepend_command)97 @classmethod98 def copy(cls, host, parsed_args, prepend_command=''):99 script_path, script_name = path.split(parsed_args.script)100 copy = '{}docker cp {} {}:/tmp'.format(prepend_command, parsed_args.script, host)101 chmod = cls._execute(host, parsed_args, 'chmod u+x /tmp/' + script_name, prepend_command)102 execute = cls._execute(host, parsed_args, '/tmp/' + script_name, prepend_command)103 connect = cls.connect(host, parsed_args, prepend_command)104 return '{} && {} && {} && {}'.format(copy, chmod, execute, connect)105 @classmethod106 def command(cls, host, parsed_args, prepend_command=''):107 command = cls._execute(host, parsed_args, parsed_args.command, prepend_command)108 connect = cls.connect(host, parsed_args, prepend_command)109 return '{} && {}'.format(command, connect)110 @classmethod111 def connect(cls, host, parsed_args, prepend_command=''):112 if parsed_args.docker_command:113 if '{}' in parsed_args.docker_command:114 # Commands like:115 # -dc 'exec -it {} bash'116 return '{}docker {}'.format(prepend_command, parsed_args.docker_command.format(host))117 else:118 # Commands without {} get host appended to it, like:119 # -dc logs120 # -dc 'logs -f'121 return '{}docker {} {}'.format(prepend_command, parsed_args.docker_command, host)122 else:123 return '{}docker exec -it {} bash'.format(prepend_command, host)124class DockerComposeConnection(DockerConnection):125 @classmethod126 def hosts(cls, parsed_args):127 containers = check_output_as_list('docker-compose ps --filter="status=running" --services')128 logger.debug('containers = "{}"'.format(containers))129 filtered_hosts = []130 for container_name in containers:131 if container_name in parsed_args.hosts:132 filtered_hosts.append(container_name)133 elif parsed_args.approximate and any(h in container_name for h in parsed_args.hosts):134 filtered_hosts.append(container_name)135 logger.debug('filtered_hosts = "{}"'.format(filtered_hosts))136 if len(filtered_hosts) == 0:137 raise ValueError("No service found in {}".format(containers))138 hosts = []139 for name in containers:140 if len(filtered_hosts) > 0 and name not in filtered_hosts:141 continue142 container = check_output_as_list('docker-compose ps -q {}'.format(name))143 if len(container) == 1:144 hosts.append(container[0])145 if len(hosts) == 0:146 raise ValueError("No running docker containers detected to connect to!")147 return hosts148class SSHDockerConnection(DockerConnection):149 @classmethod150 def hosts(cls, parsed_args):151 if len(parsed_args.hosts) == 0:152 raise ValueError("At least one host must be specified!\n")153 ssh_hosts = parsed_args.hosts154 with set_argument(parsed_args, 'hosts', parsed_args.docker_containers.split(',')) as parsed_args:155 hosts = []156 for ssh_host in ssh_hosts:157 found_containers = super().hosts(parsed_args, 'ssh {} '.format(ssh_host))158 more_hosts = ['{},{}'.format(ssh_host, container) for container in found_containers]159 if len(hosts) > 0 and len(more_hosts) > 0:160 hosts.append('\n')161 hosts.extend(more_hosts)162 return hosts163 @classmethod164 def copy(cls, host, parsed_args):165 ssh_host, container = host.split(',')166 ssh_copy = SSHConnection.copy(ssh_host, parsed_args, and_execute=False)167 with set_argument(parsed_args, 'ssh_options', '-t ' + parsed_args.ssh_options) as parsed_args:168 ssh_command = SSHConnection.connect(ssh_host, parsed_args)169 with set_argument(parsed_args, 'script', '/tmp/' + parsed_args.script) as parsed_args:170 docker_copy = DockerConnection.copy(container, parsed_args, prepend_command=ssh_command + ' ')171 return '{} && {}'.format(ssh_copy, docker_copy)172 @classmethod173 def command(cls, host, parsed_args, prepend_command=''):174 ssh_host, container = host.split(',')175 with set_argument(parsed_args, 'ssh_options', '-t ' + parsed_args.ssh_options) as parsed_args:176 ssh_command = SSHConnection.connect(ssh_host, parsed_args)177 return DockerConnection.command(container, parsed_args, prepend_command=ssh_command + ' ')178 @classmethod179 def connect(cls, host, parsed_args):180 ssh_host, container = host.split(',')181 with set_argument(parsed_args, 'ssh_options', '-t ' + parsed_args.ssh_options) as parsed_args:182 ssh_command = SSHConnection.connect(ssh_host, parsed_args)...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Slash automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful