def test_format_parameters(self): p = utils.format_parameters('InstanceType=m1.large;DBUsername=wp;' 'DBPassword=verybadpassword;KeyName=heat_key;' 'LinuxDistribution=F17') self.assertEqual({'InstanceType': 'm1.large', 'DBUsername': '******', 'DBPassword': '******', 'KeyName': 'heat_key', 'LinuxDistribution': 'F17' }, p) self.assertEqual({}, utils.format_parameters(None))
def launch_test_deployment(hc, template, overrides, test, keep_failed, sleeper): pattern = re.compile('[\W]') stack_name = pattern.sub('_', "%s-%s" % (test['name'], time())) data = {"stack_name": stack_name, "template": yaml.safe_dump(template)} timeout = get_create_value(test, 'timeout') parameters = get_create_value(test, 'parameters') if overrides: if parameters: parameters = dict(parameters.items() + utils.format_parameters(overrides).items()) else: parameters = utils.format_parameters(overrides) # retries = get_create_value(test, 'retries') # TODO: Implement retries if timeout: data["timeout_mins"] = timeout timeout_value = timeout * 60 signal.signal(signal.SIGALRM, hot.utils.timeout.handler) signal.alarm(timeout_value) if parameters: data.update({"parameters": parameters}) print("Launching: %s" % stack_name) stack = hc.stacks.create(**data) if timeout_value: print(" Timeout set to %s seconds." % timeout_value) try: monitor_stack(hc, stack['stack']['id'], sleeper) if timeout_value: signal.alarm(0) except Exception as exc: print exc if "Script exited with code 1" not in str(exc): print("Infrastructure failure. Skipping tests.") else: print("Automation scripts failed. Running tests anyway:") try: run_resource_tests(hc, stack['stack']['id'], test) print(" Test Passed!") except: exctype, value = sys.exc_info()[:2] print("Test Failed! {0}: {1}".format(exctype, value)) delete_test_deployment(hc, stack, keep_failed) sys.exit("Stack failed to deploy") return stack
def launch_test_deployment(hc, template, overrides, test, keep_failed, sleeper): pattern = re.compile('[\W]') stack_name = pattern.sub('_', "%s-%s" % (test['name'], time())) data = {"stack_name": stack_name, "template": yaml.safe_dump(template)} timeout = get_create_value(test, 'timeout') parameters = get_create_value(test, 'parameters') if overrides: if parameters: parameters = dict(parameters.items() + utils.format_parameters(overrides).items()) else: parameters = utils.format_parameters(overrides) retries = get_create_value(test, 'retries') # TODO: Implement retries if timeout: timeout_value = timeout * 60 signal.signal(signal.SIGALRM, hot.utils.timeout.handler) signal.alarm(timeout_value) if parameters: data.update({"parameters": parameters}) print("Launching: %s" % stack_name) stack = hc.stacks.create(**data) if timeout_value: print(" Timeout set to %s seconds." % timeout_value) try: monitor_stack(hc, stack['stack']['id'], sleeper) if timeout_value: signal.alarm(0) except Exception as exc: print exc if "Script exited with code 1" not in str(exc): print("Infrastructure failure. Skipping tests.") else: print("Automation scripts failed. Running tests anyway:") try: run_resource_tests(hc, stack['stack']['id'], test) print(" Test Passed!") except: exctype, value = sys.exc_info()[:2] print("Test Failed! {0}: {1}".format(exctype, value)) delete_test_deployment(hc, stack, keep_failed) sys.exit("Stack failed to deploy") return stack
def do_deployment_create(hc, args): try: config = hc.software_configs.get(config_id=args.config) except exc.HTTPNotFound: raise exc.CommandError(_('Configuration not found: %s') % args.id) derrived_params = deployment_utils.build_derived_config_params( action=args.action, source=config, name=args.name, input_values=utils.format_parameters(args.input_value, False), server_id=args.server, signal_transport=args.signal_transport, signal_id=deployment_utils.build_signal_id(hc, args) ) derived_config = hc.software_configs.create(**derrived_params) sd = hc.software_deployments.create( tenant_id='asdf', config_id=derived_config.id, server_id=args.server, action=args.action, status='IN_PROGRESS' ) print(jsonutils.dumps(sd.to_dict(), indent=2))
def do_stack_adopt(hc, args): '''Adopt a stack.''' env_files, env = template_utils.process_environment_and_files( env_path=args.environment_file) if not args.adopt_file: raise exc.CommandError(_('Need to specify %(arg)s') % {'arg': '--adopt-file'}) adopt_url = utils.normalise_file_path_to_url(args.adopt_file) adopt_data = request.urlopen(adopt_url).read() if args.create_timeout: logger.warning(_LW('%(arg1)s is deprecated, ' 'please use %(arg2)s instead'), { 'arg1': '-c/--create-timeout', 'arg2': '-t/--timeout' }) fields = { 'stack_name': args.name, 'disable_rollback': not(args.enable_rollback), 'adopt_stack_data': adopt_data, 'parameters': utils.format_parameters(args.parameters), 'files': dict(list(env_files.items())), 'environment': env } timeout = args.timeout or args.create_timeout if timeout: fields['timeout_mins'] = timeout hc.stacks.create(**fields) do_stack_list(hc)
def test_format_parameters_multiple_values_per_pamaters(self): p = utils.format_parameters([ 'status=COMPLETE', 'status=FAILED']) self.assertIn('status', p) self.assertIn('COMPLETE', p['status']) self.assertIn('FAILED', p['status'])
def take_action(self, parsed_args): self.log.debug('take_action(%s)', parsed_args) client = self.app.client_manager.orchestration fields = { 'nested_depth': parsed_args.nested_depth, 'with_detail': parsed_args.long, 'filters': heat_utils.format_parameters(parsed_args.filter), } try: resources = client.resources.list(parsed_args.stack, **fields) except heat_exc.HTTPNotFound: msg = _('Stack not found: %s') % parsed_args.stack raise exc.CommandError(msg) if parsed_args.formatter == 'dot': return [], resources columns = ['physical_resource_id', 'resource_type', 'resource_status', 'updated_time'] if len(resources) >= 1 and not hasattr(resources[0], 'resource_name'): columns.insert(0, 'logical_resource_id') else: columns.insert(0, 'resource_name') if parsed_args.nested_depth or parsed_args.long: columns.append('stack_name') return ( columns, (utils.get_item_properties(r, columns) for r in resources) )
def take_action(self, parsed_args): self.log.debug('take_action(%s)', parsed_args) client = self.app.client_manager.orchestration fields = { 'nested_depth': parsed_args.nested_depth, 'with_detail': parsed_args.long, 'filters': heat_utils.format_parameters(parsed_args.filter), } try: resources = client.resources.list(parsed_args.stack, **fields) except heat_exc.HTTPNotFound: msg = _('Stack not found: %s') % parsed_args.stack raise exc.CommandError(msg) columns = ['physical_resource_id', 'resource_type', 'resource_status', 'updated_time'] if len(resources) >= 1 and not hasattr(resources[0], 'resource_name'): columns.insert(0, 'logical_resource_id') else: columns.insert(0, 'resource_name') if parsed_args.nested_depth or parsed_args.long: columns.append('stack_name') return ( columns, (utils.get_item_properties(r, columns) for r in resources) )
def _validate(heat_client, args): tpl_files, template = template_utils.process_template_path( args.template, object_request=http.authenticated_fetcher(heat_client)) env_files_list = [] env_files, env = template_utils.process_multiple_environments_and_files( env_paths=args.environment, env_list_tracker=env_files_list) fields = { 'template': template, 'parameters': heat_utils.format_parameters(args.parameter), 'files': dict(list(tpl_files.items()) + list(env_files.items())), 'environment': env, } if args.ignore_errors: fields['ignore_errors'] = args.ignore_errors # If one or more environments is found, pass the listing to the server if env_files_list: fields['environment_files'] = env_files_list if args.show_nested: fields['show_nested'] = args.show_nested validation = heat_client.stacks.validate(**fields) data = list(six.itervalues(validation)) columns = list(six.iterkeys(validation)) return columns, data
def do_event_list(hc, args): '''List events for a stack.''' fields = { 'stack_id': args.id, 'resource_name': args.resource, 'limit': args.limit, 'marker': args.marker, 'filters': utils.format_parameters(args.filters) } try: events = hc.events.list(**fields) except exc.HTTPNotFound as ex: # it could be the stack or resource that is not found # just use the message that the server sent us. raise exc.CommandError(str(ex)) else: fields = [ 'id', 'resource_status_reason', 'resource_status', 'event_time' ] if len(events) >= 1: if hasattr(events[0], 'resource_name'): fields.insert(0, 'resource_name') else: fields.insert(0, 'logical_resource_id') utils.print_list(events, fields, sortby_index=None)
def take_action(self, parsed_args): self.log.debug('take_action(%s)', parsed_args) client = self.app.client_manager.orchestration env_files, env = ( template_utils.process_multiple_environments_and_files( env_paths=parsed_args.environment)) adopt_url = heat_utils.normalise_file_path_to_url( parsed_args.adopt_file) adopt_data = request.urlopen(adopt_url).read().decode('utf-8') fields = { 'stack_name': parsed_args.name, 'disable_rollback': not parsed_args.enable_rollback, 'adopt_stack_data': adopt_data, 'parameters': heat_utils.format_parameters(parsed_args.parameter), 'files': dict(list(env_files.items())), 'environment': env, 'timeout': parsed_args.timeout } stack = client.stacks.create(**fields)['stack'] if parsed_args.wait: stack_status, msg = event_utils.poll_for_events( client, parsed_args.name, action='ADOPT') if stack_status == 'ADOPT_FAILED': raise exc.CommandError(msg) return _show_stack(client, stack['id'], format='table', short=True)
def do_stack_preview(hc, args): """Preview the stack.""" tpl_files, template = template_utils.get_template_contents( args.template_file, args.template_url, args.template_object, hc.http_client.raw_request ) env_files, env = template_utils.process_multiple_environments_and_files(env_paths=args.environment_file) fields = { "stack_name": args.name, "disable_rollback": not (args.enable_rollback), "timeout_mins": args.timeout, "parameters": utils.format_parameters(args.parameters), "template": template, "files": dict(list(tpl_files.items()) + list(env_files.items())), "environment": env, } stack = hc.stacks.preview(**fields) formatters = { "description": utils.text_wrap_formatter, "template_description": utils.text_wrap_formatter, "stack_status_reason": utils.text_wrap_formatter, "parameters": utils.json_formatter, "outputs": utils.json_formatter, "resources": utils.json_formatter, "links": utils.link_formatter, } utils.print_dict(stack.to_dict(), formatters=formatters)
def do_stack_create(hc, args): '''Create the stack.''' tpl_files, template = template_utils.get_template_contents( args.template_file, args.template_url, args.template_object, hc.http_client.raw_request) env_files, env = template_utils.process_multiple_environments_and_files( env_paths=args.environment_file) if args.create_timeout: logger.warning('-c/--create-timeout is deprecated, ' 'please use -t/--timeout instead') fields = { 'stack_name': args.name, 'disable_rollback': not (args.enable_rollback), 'parameters': utils.format_parameters(args.parameters), 'template': template, 'files': dict(list(tpl_files.items()) + list(env_files.items())), 'environment': env } timeout = args.timeout or args.create_timeout if timeout: fields['timeout_mins'] = timeout hc.stacks.create(**fields) do_stack_list(hc)
def do_stack_adopt(hc, args): '''Adopt a stack.''' env_files, env = template_utils.process_environment_and_files( env_path=args.environment_file) if not args.adopt_file: raise exc.CommandError('Need to specify --adopt-file') adopt_url = template_utils.normalise_file_path_to_url(args.adopt_file) adopt_data = request.urlopen(adopt_url).read() if args.create_timeout: logger.warning('-c/--create-timeout is deprecated, ' 'please use -t/--timeout instead') fields = { 'stack_name': args.name, 'disable_rollback': not (args.enable_rollback), 'adopt_stack_data': adopt_data, 'parameters': utils.format_parameters(args.parameters), 'files': dict(list(env_files.items())), 'environment': env } timeout = args.timeout or args.create_timeout if timeout: fields['timeout_mins'] = timeout hc.stacks.create(**fields) do_stack_list(hc)
def _list(client, args=None): kwargs = {} columns = [ 'ID', 'Stack Name', 'Stack Status', 'Creation Time', 'Updated Time', ] if args: kwargs = { 'limit': args.limit, 'marker': args.marker, 'filters': heat_utils.format_parameters(args.properties), 'tags': None, 'tags_any': None, 'not_tags': None, 'not_tags_any': None, 'global_tenant': args.all_projects or args.long, 'show_deleted': args.deleted, 'show_hidden': args.hidden } if args.tags: if args.tag_mode: if args.tag_mode == 'any': kwargs['tags_any'] = args.tags elif args.tag_mode == 'not': kwargs['not_tags'] = args.tags elif args.tag_mode == 'not-any': kwargs['not_tags_any'] = args.tags else: err = _('tag mode must be one of "any", "not", "not-any"') raise exc.CommandError(err) else: kwargs['tags'] = args.tags if args.short: columns.pop() columns.pop() if args.long: columns.insert(2, 'Stack Owner') if args.nested: columns.append('Parent') kwargs['show_nested'] = True if args.deleted: columns.append('Deletion Time') data = client.stacks.list(**kwargs) data = list(data) for stk in data: if hasattr(stk, 'project'): columns.insert(2, 'Project') break data = utils.sort_items(data, args.sort if args else None) return (columns, (utils.get_item_properties(s, columns) for s in data))
def take_action(self, parsed_args): self.log.debug('take_action(%s)', parsed_args) client = self.app.client_manager.orchestration config = {} if parsed_args.config: try: config = client.software_configs.get(parsed_args.config) except heat_exc.HTTPNotFound: msg = (_('Software configuration not found: %s') % parsed_args.config) raise exc.CommandError(msg) derived_params = deployment_utils.build_derived_config_params( parsed_args.action, config, parsed_args.name, heat_utils.format_parameters(parsed_args.input_value, False), parsed_args.server, parsed_args.signal_transport, signal_id=deployment_utils.build_signal_id(client, parsed_args) ) derived_config = client.software_configs.create(**derived_params) sd = client.software_deployments.create( config_id=derived_config.id, server_id=parsed_args.server, action=parsed_args.action, status='IN_PROGRESS' ) return zip(*sorted(sd.to_dict().items()))
def do_template_validate(hc, args): '''Validate a template with parameters''' fields = {'parameters': utils.format_parameters(args.parameters)} _set_template_fields(hc, args, fields) validation = hc.stacks.validate(**fields) print json.dumps(validation, indent=2)
def take_action(self, parsed_args): self.log.debug('take_action(%s)', parsed_args) client = self.app.client_manager.orchestration env_files, env = ( template_utils.process_multiple_environments_and_files( env_paths=parsed_args.environment)) adopt_url = heat_utils.normalise_file_path_to_url( parsed_args.adopt_file) adopt_data = request.urlopen(adopt_url).read().decode('utf-8') yaml_adopt_data = yaml.safe_load(adopt_data) or {} files = yaml_adopt_data.get('files', {}) files.update(env_files) fields = { 'stack_name': parsed_args.name, 'disable_rollback': not parsed_args.enable_rollback, 'adopt_stack_data': adopt_data, 'parameters': heat_utils.format_parameters(parsed_args.parameter), 'files': files, 'environment': env, 'timeout': parsed_args.timeout } stack = client.stacks.create(**fields)['stack'] if parsed_args.wait: stack_status, msg = event_utils.poll_for_events(client, parsed_args.name, action='ADOPT') if stack_status == 'ADOPT_FAILED': raise exc.CommandError(msg) return _show_stack(client, stack['id'], format='table', short=True)
def do_stack_create(hc, args): """Create the stack.""" tpl_files, template = template_utils.get_template_contents( args.template_file, args.template_url, args.template_object, hc.http_client.raw_request ) env_files, env = template_utils.process_multiple_environments_and_files(env_paths=args.environment_file) if args.create_timeout: logger.warning("-c/--create-timeout is deprecated, " "please use -t/--timeout instead") fields = { "stack_name": args.name, "disable_rollback": not (args.enable_rollback), "parameters": utils.format_parameters(args.parameters), "template": template, "files": dict(list(tpl_files.items()) + list(env_files.items())), "environment": env, } timeout = args.timeout or args.create_timeout if timeout: fields["timeout_mins"] = timeout hc.stacks.create(**fields) do_stack_list(hc)
def do_stack_update(hc, args): '''Update the stack.''' tpl_files, template = template_utils.get_template_contents( args.template_file, args.template_url, args.template_object, hc.http_client.raw_request) env_files, env = template_utils.process_environment_and_files( env_path=args.environment_file) fields = { 'stack_id': args.id, 'parameters': utils.format_parameters(args.parameters), 'template': template, 'files': dict(list(tpl_files.items()) + list(env_files.items())), 'environment': env } if args.timeout: fields['timeout_mins'] = args.timeout hc.stacks.update(**fields) do_stack_list(hc)
def do_stack_adopt(hc, args): """Adopt a stack.""" env_files, env = template_utils.process_environment_and_files(env_path=args.environment_file) if not args.adopt_file: raise exc.CommandError("Need to specify --adopt-file") adopt_url = template_utils.normalise_file_path_to_url(args.adopt_file) adopt_data = request.urlopen(adopt_url).read() if args.create_timeout: logger.warning("-c/--create-timeout is deprecated, " "please use -t/--timeout instead") fields = { "stack_name": args.name, "disable_rollback": not (args.enable_rollback), "adopt_stack_data": adopt_data, "parameters": utils.format_parameters(args.parameters), "files": dict(list(env_files.items())), "environment": env, } timeout = args.timeout or args.create_timeout if timeout: fields["timeout_mins"] = timeout hc.stacks.create(**fields) do_stack_list(hc)
def take_action(self, parsed_args): self.log.debug('take_action(%s)', parsed_args) client = self.app.client_manager.orchestration config = {} if parsed_args.config: try: config = client.software_configs.get(parsed_args.config) except heat_exc.HTTPNotFound: msg = (_('Software configuration not found: %s') % parsed_args.config) raise exc.CommandError(msg) derived_params = deployment_utils.build_derived_config_params( parsed_args.action, config, parsed_args.name, heat_utils.format_parameters(parsed_args.input_value, False), parsed_args.server, parsed_args.signal_transport, signal_id=deployment_utils.build_signal_id(client, parsed_args)) derived_config = client.software_configs.create(**derived_params) sd = client.software_deployments.create(config_id=derived_config.id, server_id=parsed_args.server, action=parsed_args.action, status='IN_PROGRESS') return zip(*sorted(sd.to_dict().items()))
def do_stack_preview(hc, args): '''Preview the stack.''' tpl_files, template = template_utils.get_template_contents( args.template_file, args.template_url, args.template_object, hc.http_client.raw_request) env_files, env = template_utils.process_environment_and_files( env_path=args.environment_file) fields = { 'stack_name': args.name, 'parameters': utils.format_parameters(args.parameters), 'template': template, 'files': dict(list(tpl_files.items()) + list(env_files.items())), 'environment': env } stack = hc.stacks.preview(**fields) formatters = { 'description': utils.text_wrap_formatter, 'template_description': utils.text_wrap_formatter, 'stack_status_reason': utils.text_wrap_formatter, 'parameters': utils.json_formatter, 'outputs': utils.json_formatter, 'resources': utils.json_formatter, 'links': utils.link_formatter, } utils.print_dict(stack.to_dict(), formatters=formatters)
def do_stack_preview(hc, args): '''Preview the stack.''' tpl_files, template = template_utils.get_template_contents( args.template_file, args.template_url, args.template_object, hc.http_client.raw_request) env_files, env = template_utils.process_multiple_environments_and_files( env_paths=args.environment_file) fields = { 'stack_name': args.name, 'disable_rollback': not (args.enable_rollback), 'timeout_mins': args.timeout, 'parameters': utils.format_parameters(args.parameters), 'template': template, 'files': dict(list(tpl_files.items()) + list(env_files.items())), 'environment': env } stack = hc.stacks.preview(**fields) formatters = { 'description': utils.text_wrap_formatter, 'template_description': utils.text_wrap_formatter, 'stack_status_reason': utils.text_wrap_formatter, 'parameters': utils.json_formatter, 'outputs': utils.json_formatter, 'resources': utils.json_formatter, 'links': utils.link_formatter, } utils.print_dict(stack.to_dict(), formatters=formatters)
def do_stack_create(hc, args): '''Create the stack.''' tpl_files, template = template_utils.get_template_contents( args.template_file, args.template_url, args.template_object, hc.http_client.raw_request) env_files, env = template_utils.process_environment_and_files( env_path=args.environment_file) if args.create_timeout: logger.warning('-c/--create-timeout is deprecated, ' 'please use -t/--timeout instead') fields = { 'stack_name': args.name, 'disable_rollback': not(args.enable_rollback), 'parameters': utils.format_parameters(args.parameters), 'template': template, 'files': dict(list(tpl_files.items()) + list(env_files.items())), 'environment': env } timeout = args.timeout or args.create_timeout if timeout: fields['timeout_mins'] = timeout hc.stacks.create(**fields) do_stack_list(hc)
def do_stack_adopt(hc, args): '''Adopt a stack.''' tpl_files, template = template_utils.get_template_contents( args.template_file, args.template_url, args.template_object, hc.http_client.raw_request) env_files, env = template_utils.process_environment_and_files( env_path=args.environment_file) if not args.adopt_file: raise exc.CommandError('Need to specify --adopt-file') adopt_url = template_utils.normalise_file_path_to_url(args.adopt_file) adopt_data = request.urlopen(adopt_url).read() if args.create_timeout: logger.warning('-c/--create-timeout is deprecated, ' 'please use -t/--timeout instead') fields = { 'stack_name': args.name, 'timeout_mins': args.timeout or args.create_timeout, 'disable_rollback': not(args.enable_rollback), 'adopt_stack_data': adopt_data, 'parameters': utils.format_parameters(args.parameters), 'template': template, 'files': dict(list(tpl_files.items()) + list(env_files.items())), 'environment': env } hc.stacks.create(**fields) do_stack_list(hc)
def do_template_validate(hc, args): '''Validate a template with parameters.''' fields = {'parameters': utils.format_parameters(args.parameters)} _set_template_fields(hc, args, fields) _process_environment_and_files(args, fields) validation = hc.stacks.validate(**fields) print(jsonutils.dumps(validation, indent=2))
def do_stack_update(hc, args): '''Update the stack''' fields = {'stack_id': args.id, 'parameters': utils.format_parameters(args.parameters)} _set_template_fields(hc, args, fields) hc.stacks.update(**fields) do_list(hc)
def do_stack_create(hc, args): '''Create the stack''' fields = {'stack_name': args.name, 'timeoutmins': args.create_timeout, 'parameters': utils.format_parameters(args.parameters)} _set_template_fields(hc, args, fields) hc.stacks.create(**fields) do_stack_list(hc)
def test_format_parameters_multiple_semicolon_values(self): p = utils.format_parameters([ 'KeyName=heat_key', 'DnsSecKey=hsgx1m31;PbaNF4WEcHlwj;IlCGgfOdoB;58/ww7a4oAO;NQ/fD==', 'UpstreamDNS=8.8.8.8']) self.assertEqual({'KeyName': 'heat_key', 'DnsSecKey': 'hsgx1m31;PbaNF4WEcHlwj;IlCGgfOdoB;58/' 'ww7a4oAO;NQ/fD==', 'UpstreamDNS': '8.8.8.8'}, p)
def do_stack_update(hc, args): '''Update the stack.''' fields = {'stack_id': args.id, 'parameters': utils.format_parameters(args.parameters)} _set_template_fields(hc, args, fields) _process_environment_and_files(args, fields) hc.stacks.update(**fields) do_list(hc)
def test_format_parameters_multiple(self): p = utils.format_parameters([ 'KeyName=heat_key', 'DnsSecKey=hsgx1m31PbamNF4WEcHlwjIlCGgifOdoB58/wwC7a4oAONQ/fDV5ct' 'qrYBoLlKHhTfkyQEw9iVScKYZbbMtMNg==', 'UpstreamDNS=8.8.8.8']) self.assertEqual({'KeyName': 'heat_key', 'DnsSecKey': 'hsgx1m31PbamNF4WEcHlwjIlCGgifOdoB58/ww' 'C7a4oAONQ/fDV5ctqrYBoLlKHhTfkyQEw9iVScKYZbbMtMNg==', 'UpstreamDNS': '8.8.8.8'}, p)
def _list(client, args=None): kwargs = {} columns = [ 'ID', 'Stack Name', 'Stack Status', 'Creation Time', 'Updated Time', ] if args: kwargs = {'limit': args.limit, 'marker': args.marker, 'filters': heat_utils.format_parameters(args.properties), 'tags': None, 'tags_any': None, 'not_tags': None, 'not_tags_any': None, 'global_tenant': args.all_projects or args.long, 'show_deleted': args.deleted, 'show_hidden': args.hidden} if args.tags: if args.tag_mode: if args.tag_mode == 'any': kwargs['tags_any'] = args.tags elif args.tag_mode == 'not': kwargs['not_tags'] = args.tags elif args.tag_mode == 'not-any': kwargs['not_tags_any'] = args.tags else: err = _('tag mode must be one of "any", "not", "not-any"') raise exc.CommandError(err) else: kwargs['tags'] = args.tags if args.short: columns.pop() columns.pop() if args.long: columns.insert(2, 'Stack Owner') if args.long or args.all_projects: columns.insert(2, 'Project') if args.nested: columns.append('Parent') kwargs['show_nested'] = True data = client.stacks.list(**kwargs) data = utils.sort_items(data, args.sort if args else None) return ( columns, (utils.get_item_properties(s, columns) for s in data) )
def _list_resourcetypes(heat_client, parsed_args): resource_types = heat_client.resource_types.list( filters=heat_utils.format_parameters(parsed_args.filter), with_description=parsed_args.long) if parsed_args.long: columns = ['Resource Type', 'Description'] rows = sorted([r.resource_type, r.description] for r in resource_types) else: columns = ['Resource Type'] rows = sorted([r.resource_type] for r in resource_types) return columns, rows
def do_stack_create(hc, args): '''Create the stack.''' fields = {'stack_name': args.name, 'timeout_mins': args.create_timeout, 'disable_rollback': not(args.enable_rollback), 'parameters': utils.format_parameters(args.parameters)} _set_template_fields(hc, args, fields) _process_environment_and_files(args, fields) hc.stacks.create(**fields) do_stack_list(hc)
def do_stack_list(hc, args=None): '''List the user's stacks.''' kwargs = {} if args: kwargs = {'limit': args.limit, 'marker': args.marker, 'filters': utils.format_parameters(args.filters)} stacks = hc.stacks.list(**kwargs) fields = ['id', 'stack_name', 'stack_status', 'creation_time'] utils.print_list(stacks, fields, sortby_index=3)
def test_format_parameters(self): p = utils.format_parameters([ 'InstanceType=m1.large;DBUsername=wp;' 'DBPassword=verybadpassword;KeyName=heat_key;' 'LinuxDistribution=F17']) self.assertEqual({'InstanceType': 'm1.large', 'DBUsername': '******', 'DBPassword': '******', 'KeyName': 'heat_key', 'LinuxDistribution': 'F17' }, p)
def list(self, args=None): kwargs = {} if args: kwargs = {'limit': args.get['limit'], 'marker': args.get['marker'], 'filters': utils.format_parameters(args.get['filters']), 'global_tenant': args.get['global_tenant'], 'show_deleted': args.get['show_deleted']} stacks = self.client.stacks.list(**kwargs) fields = ['id', 'stack_name', 'stack_status', 'creation_time'] #utils.print_list(stacks, fields) return stacks
def _list_resourcetypes(heat_client, parsed_args): resource_types = heat_client.resource_types.list( filters=heat_utils.format_parameters(parsed_args.filter), with_description=parsed_args.long ) if parsed_args.long: columns = ['Resource Type', 'Description'] rows = sorted([r.resource_type, r.description] for r in resource_types) else: columns = ['Resource Type'] rows = sorted([r.resource_type] for r in resource_types) return columns, rows
def do_stack_list(hc, args=None): '''List the user's stacks.''' kwargs = {} if args: kwargs = {'limit': args.limit, 'marker': args.marker, 'filters': utils.format_parameters(args.filters), 'global_tenant': args.global_tenant} stacks = hc.stacks.list(**kwargs) fields = ['id', 'stack_name', 'stack_status', 'creation_time'] utils.print_list(stacks, fields, sortby_index=3)
def update(self, stack_id, template, parameters=None, environment=None, disable_rollback=None, timeout=None): fields = { "stack_id": stack_id, "parameters": utils.format_parameters(parameters), #'existing': args.existing, "template": template, #'files': dict(list(tpl_files.items()) + list(env_files.items())), "environment": environment, } try: response = self.client.stacks.update(**fields) except Exception: raise return response
def list(self, args=None): kwargs = {} if args: kwargs = { 'limit': args.get['limit'], 'marker': args.get['marker'], 'filters': utils.format_parameters(args.get['filters']), 'global_tenant': args.get['global_tenant'], 'show_deleted': args.get['show_deleted'] } stacks = self.client.stacks.list(**kwargs) fields = ['id', 'stack_name', 'stack_status', 'creation_time'] #utils.print_list(stacks, fields) return stacks
def create_stack(self): Tcreate_stack_start=time.time() if self.stack_manager is None: self.__init__(self.auth_token) tpl_files, template = template_utils.get_template_contents(template_file) stack = None stack_number = 0 stack_created = False while stack_created == False: stack_number += 1 try: stack_name_full = stack_name + str(stack_number) print "stack name: ", stack_name_full kwargs = None float_ip = self. get_floating_ip() if float_ip: params = ['influxdb_floating_ip_old=' + float_ip] kwargs = { 'stack_name': stack_name_full, 'template': template, 'parameters': utils.format_parameters(params) } else: kwargs = { 'stack_name': stack_name_full, 'template': template } self.stack_manager.validate(**kwargs) stack=self.stack_manager.create(**kwargs) print "stack: ", stack uid=stack['stack']['id'] self.stack_id = uid stack=self.stack_manager.get(stack_id=uid).to_dict() while stack['stack_status'] == 'CREATE_IN_PROGRESS': print "Stack in state: {}".format(stack['stack_status']) stack = self.stack_manager.get(stack_id=uid).to_dict() time.sleep(5) if stack['stack_status'] == 'CREATE_COMPLETE': print "Stack succesfully created." else: raise Exception("Stack fall to unknow status: {}".format(stack)) stack_created = True except HTTPConflict: print "Stack ", stack_name_full, " already exists" Tcreate_stack=time.time()-Tcreate_stack_start print "Time to create the new stack: ", Tcreate_stack, "s" return stack
def do_template_validate(hc, args): '''Validate a template with parameters.''' tpl_files, template = template_utils.get_template_contents( args.template_file, args.template_url, args.template_object, hc.http_client.raw_request) env_files, env = template_utils.process_environment_and_files( env_path=args.environment_file) fields = { 'parameters': utils.format_parameters(args.parameters), 'template': template, 'files': dict(list(tpl_files.items()) + list(env_files.items())), 'environment': env } validation = hc.stacks.validate(**fields) print(jsonutils.dumps(validation, indent=2))
def update(self, stack_id, template, parameters=None, environment=None, disable_rollback=None, timeout=None): fields = { 'stack_id': stack_id, 'parameters': utils.format_parameters(parameters), #'existing': args.existing, 'template': template, #'files': dict(list(tpl_files.items()) + list(env_files.items())), 'environment': environment } try: response = self.client.stacks.update(**fields) except Exception: raise return response
def do_stack_update(hc, args): '''Update the stack.''' tpl_files, template = template_utils.get_template_contents( args.template_file, args.template_url, args.template_object, hc.http_client.raw_request) env_files, env = template_utils.process_multiple_environments_and_files( env_paths=args.environment_file) fields = { 'stack_id': args.id, 'parameters': utils.format_parameters(args.parameters), 'existing': args.existing, 'template': template, 'files': dict(list(tpl_files.items()) + list(env_files.items())), 'environment': env } if args.timeout: fields['timeout_mins'] = args.timeout if args.clear_parameter: fields['clear_parameters'] = list(args.clear_parameter) if args.rollback is not None: try: rollback = strutils.bool_from_string(args.rollback, strict=True) except ValueError as ex: raise exc.CommandError(six.text_type(ex)) else: fields['disable_rollback'] = not (rollback) # TODO(pshchelo): remove the following 'else' clause after deprecation # period of --enable-rollback switch and assign -r shortcut to --rollback else: if args.enable_rollback: fields['disable_rollback'] = False hc.stacks.update(**fields) do_stack_list(hc)
def do_stack_list(hc, args=None): '''List the user's stacks.''' kwargs = {} fields = ['id', 'stack_name', 'stack_status', 'creation_time'] if args: kwargs = { 'limit': args.limit, 'marker': args.marker, 'filters': utils.format_parameters(args.filters), 'global_tenant': args.global_tenant, 'show_deleted': args.show_deleted } if args.show_nested: fields.append('parent') kwargs['show_nested'] = True if args.global_tenant or args.show_owner: fields.insert(2, 'stack_owner') if args.global_tenant: fields.insert(2, 'project') stacks = hc.stacks.list(**kwargs) utils.print_list(stacks, fields, sortby_index=3)
def deploy(self, name, template, parameters=None, environment=None, disable_rollback=True, timeout=None): kcargs = { 'stack_name': name, 'disable_rollback': disable_rollback, 'parameters': utils.format_parameters(parameters), 'template': template, #'files' : dict(list(tpl_files.items()) + list(env_files.items())), #'files' : dict(list(kwargs.get('environment_file').items())), 'environment': environment } #print json.dumps(kcargs, indent=2) if timeout: kcargs['timeout_mins'] = timeout try: stack_details = self.client.stacks.create(**kcargs) except Exception: raise return stack_details
def take_action(self, parsed_args): self.log.debug('take_action(%s)', parsed_args) client = self.app.client_manager.orchestration columns = ['id', 'resource_status', 'resource_status_reason', 'event_time', 'physical_resource_id'] kwargs = { 'resource_name': parsed_args.resource, 'filters': heat_utils.format_parameters(parsed_args.filter), 'sort_dir': 'asc' } if parsed_args.resource and parsed_args.nested_depth: msg = _('--nested-depth cannot be specified with --resource') raise exc.CommandError(msg) if parsed_args.nested_depth: columns.append('stack_name') nested_depth = parsed_args.nested_depth else: nested_depth = 0 if parsed_args.sort: sorts = [] sort_keys = [] for sort in parsed_args.sort: if sort.startswith(":"): sorts.append(":".join(["event_time", sort.lstrip(":")])) else: sorts.append(sort) sort_keys.append(sort.split(":")[0]) kwargs['sort_keys'] = sort_keys if ":" in parsed_args.sort[0]: kwargs['sort_dir'] = parsed_args.sort[0].split(":")[1] if parsed_args.follow: if parsed_args.formatter != 'log': msg = _('--follow can only be specified with --format log') raise exc.CommandError(msg) marker = parsed_args.marker try: event_log_context = heat_utils.EventLogContext() while True: events = event_utils.get_events( client, stack_id=parsed_args.stack, event_args=kwargs, nested_depth=nested_depth, marker=marker) if events: marker = getattr(events[-1], 'id', None) events_log = heat_utils.event_log_formatter( events, event_log_context) self.app.stdout.write(events_log) self.app.stdout.write('\n') time.sleep(5) # this loop never exits except (KeyboardInterrupt, EOFError): # ctrl-c, ctrl-d return [], [] events = event_utils.get_events( client, stack_id=parsed_args.stack, event_args=kwargs, nested_depth=nested_depth, marker=parsed_args.marker, limit=parsed_args.limit) if parsed_args.sort: events = utils.sort_items(events, ','.join(sorts)) if parsed_args.formatter == 'log': return [], events if len(events): if hasattr(events[0], 'resource_name'): columns.insert(0, 'resource_name') columns.append('logical_resource_id') else: columns.insert(0, 'logical_resource_id') return ( columns, (utils.get_item_properties(s, columns) for s in events) )
def test_format_parameter_none(self): self.assertEqual({}, utils.format_parameters(None))
def test_format_parameters_parse_semicolon_false(self): p = utils.format_parameters( ['KeyName=heat_key;UpstreamDNS=8.8.8.8;a=b'], parse_semicolon=False) self.assertEqual({'KeyName': 'heat_key;UpstreamDNS=8.8.8.8;a=b'}, p)
def _list_resourcetypes(heat_client, parsed_args): resource_types = heat_client.resource_types.list( filters=heat_utils.format_parameters(parsed_args.filter)) columns = ['Resource Type'] rows = sorted([r.resource_type] for r in resource_types) return (columns, rows)
def take_action(self, parsed_args): self.log.debug('take_action(%s)', parsed_args) client = self.app.client_manager.orchestration columns = [ 'id', 'resource_status', 'resource_status_reason', 'event_time', 'physical_resource_id' ] kwargs = { 'resource_name': parsed_args.resource, 'limit': parsed_args.limit, 'marker': parsed_args.marker, 'filters': heat_utils.format_parameters(parsed_args.filter), 'sort_dir': 'asc' } if parsed_args.resource and parsed_args.nested_depth: msg = _('--nested-depth cannot be specified with --resource') raise exc.CommandError(msg) if parsed_args.nested_depth: # Until the API supports recursive event listing we'll have to do # the marker/limit filtering client-side del kwargs['marker'] del kwargs['limit'] columns.append('stack_name') nested_depth = parsed_args.nested_depth else: nested_depth = 0 if parsed_args.follow: if parsed_args.formatter != 'value': msg = _('--follow can only be specified with --format value') raise exc.CommandError(msg) marker = parsed_args.marker try: while True: kwargs['marker'] = marker events = event_utils.get_events(client, stack_id=parsed_args.stack, event_args=kwargs, nested_depth=nested_depth, marker=marker) if events: marker = getattr(events[-1], 'id', None) events_log = heat_utils.event_log_formatter(events) self.app.stdout.write(events_log) self.app.stdout.write('\n') time.sleep(5) # this loop never exits except (KeyboardInterrupt, EOFError): # ctrl-c, ctrl-d return [], [] events = event_utils.get_events(client, stack_id=parsed_args.stack, event_args=kwargs, nested_depth=nested_depth, marker=parsed_args.marker, limit=parsed_args.limit) if parsed_args.sort: events = utils.sort_items(events, ','.join(parsed_args.sort)) if parsed_args.formatter == 'value': events = heat_utils.event_log_formatter(events).split('\n') return [], [e.split(' ') for e in events] if len(events): if hasattr(events[0], 'resource_name'): columns.insert(0, 'resource_name') columns.append('logical_resource_id') else: columns.insert(0, 'logical_resource_id') return (columns, (utils.get_item_properties(s, columns) for s in events))
def _run_heat(args, hot): try: from heatclient.common import utils from heatclient.client import Client as Heat_Client from keystoneclient.v2_0 import Client as Keystone_Client except ImportError as e: LOG.error("You must have python-heatclient in your python path") raise Exception(e) CREDS = { 'username': os.environ.get('OS_USERNAME'), 'password': os.environ.get('OS_PASSWORD'), 'tenant_name': os.environ.get( 'OS_TENANT_NAME', os.environ.get('OS_PROJECT_NAME') ), 'auth_url': os.environ.get('OS_AUTH_URL'), } ex_msg = ( "%s, ensure your environment (probably the stackrc file) is properly " "configured with OpenStack credentials" ) # Get name of CREDS key with a value of None and raise an exception # because we're missing some obviously important creds data if not all(CREDS.values()): name = CREDS.keys()[CREDS.values().index(None)] namestr = "%s is missing" % name raise OpenStackConfigurationError(ex_msg % namestr) if args.heat_stack_name: stack_name = args.heat_stack_name else: stack_name = os.path.basename(args.environment) STACK = { 'stack_name': stack_name, 'template': hot } if args.heat_parameters: STACK['parameters'] = utils.format_parameters(args.heat_parameters) LOG.debug("Logging into heat") ks_client = Keystone_Client(**CREDS) heat_endpoint = ks_client.service_catalog.url_for( service_type='orchestration', endpoint_type='publicURL') heatclient = Heat_Client('1', heat_endpoint, token=ks_client.auth_token) try: LOG.debug("Checking for existence of heat stack: %s" % stack_name) heatclient.stacks.get(stack_name) stack_exists = True LOG.debug("Already exists") except Exception as e: if e.code == 404: stack_exists = False else: raise Exception(e) stack_action = 'create' if stack_exists and args.heat_stack_update: stack_action = 'update' LOG.debug("Updating stack") heatclient.stacks.update(stack_name, **STACK) time.sleep(5) elif not stack_exists: LOG.debug("Creating stack") heatclient.stacks.create(**STACK) time.sleep(5) while heatclient.stacks.get(stack_name).status == 'IN_PROGRESS': LOG.debug("Waiting on stack...") time.sleep(5) stack = heatclient.stacks.get(stack_name) if stack.status != 'COMPLETE': raise Exception("stack %s returned an unexpected status (%s)" % (stack_name, stack.status)) LOG.debug("Stack %sd!" % stack_action) servers = {} floating_ip = None private_key = None for output in stack.outputs: if output['output_key'] == "floating_ip": floating_ip = output['output_value'] LOG.debug("floating_ip : %s" % floating_ip) elif output['output_key'] == "private_key": private_key = output['output_value'] else: servers[output['output_key']] = output['output_value'] LOG.debug("server : %s (%s) " % (output['output_key'], output['output_value'])) ssh_config = """ Host * User ubuntu ForwardAgent yes UserKnownHostsFile /dev/null StrictHostKeyChecking no PasswordAuthentication no """ # write out private key if using one generated by heat if private_key: ssh_config += " IdentityFile tmp/ssh_key" LOG.debug("writing ssh_key to /tmp/ssh_key") with open("tmp/ssh_key", "w") as text_file: text_file.write(private_key) os.chmod("tmp/ssh_key", 0600) _ssh_add("tmp/ssh_key") with open("tmp/ssh_config", "w") as text_file: text_file.write(ssh_config) if floating_ip: ssh_config_pre = """ Host floating_ip Hostname {floating_ip} """ ssh_config = ssh_config_pre.format(floating_ip=floating_ip) with open("tmp/ssh_config", "a") as text_file: text_file.write(ssh_config) for server, ip in servers.iteritems(): test_ip = ip ssh_config_pre = """ Host {server} Hostname {ip} """ if floating_ip: ssh_config_pre += (" ProxyCommand ssh -o StrictHostKeyChecking=no" " ubuntu@{floating_ip} nc %h %p\n\n") ssh_config = ssh_config_pre.format( server=server, ip=ip, floating_ip=floating_ip) with open("tmp/ssh_config", "a") as text_file: text_file.write(ssh_config) ansible_ssh_config_file = "tmp/ssh_config" if os.path.isfile(ansible_ssh_config_file): _append_envvar("ANSIBLE_SSH_ARGS", "-F %s" % ansible_ssh_config_file) LOG.debug("waiting for SSH connectivity...") if floating_ip: while not test_ssh(floating_ip): LOG.debug("waiting for SSH connectivity...") time.sleep(5) else: while not test_ssh(test_ip): LOG.debug("waiting for SSH connectivity...") time.sleep(5)