def test_load_object_from_string(self): tests = ( ("string.Template", string.Template), ("os.path.basename", os.path.basename), ("string.letters", string.letters) ) for t in tests: self.assertIs(load_object_from_string(t[0]), t[1])
def test_load_object_from_string(self): tests = ( ("string.Template", string.Template), ("os.path.basename", os.path.basename), ("string.letters", string.letters) ) for t in tests: self.assertIs(load_object_from_string(t[0]), t[1])
def __call__(self): # Use the context property of the baseclass, if present. # If not, default to a basic context. try: ctx = self.context except AttributeError: ctx = Context(config=self.config, environment={'environment': 'test'}) configvars = self.stack.variables or {} variables = [Variable(k, v) for k, v in configvars.iteritems()] blueprint_class = load_object_from_string( self.stack.class_path) blueprint = blueprint_class(self.stack.name, ctx) blueprint.resolve_variables(variables or []) blueprint.setup_parameters() blueprint.create_template() self.assertRenderedBlueprint(blueprint)
def setup_resource(self): """ Setting Up Resource """ template = self.template variables = self.get_variables() tclass = variables['Class'] tprops = variables['Properties'] output = variables['Output'] klass = load_object_from_string('troposphere.' + tclass) instance = klass.from_dict('ResourceRefName', tprops) template.add_resource(instance) template.add_output(Output( output, Description="A reference to the object created in this blueprint", Value=Ref(instance) ))
def handle_hooks(stage, hooks, provider, context): """ Used to handle pre/post_build hooks. These are pieces of code that we want to run before/after the builder builds the stacks. Args: stage (string): The current stage (pre_run, post_run, etc). hooks (list): A list of :class:`stacker.config.Hook` containing the hooks to execute. provider (:class:`stacker.provider.base.BaseProvider`): The provider the current stack is using. context (:class:`stacker.context.Context`): The current stacker context. """ if not hooks: logger.debug("No %s hooks defined.", stage) return hook_paths = [] for i, h in enumerate(hooks): try: hook_paths.append(h.path) except KeyError: raise ValueError("%s hook #%d missing path." % (stage, i)) logger.info("Executing %s hooks: %s", stage, ", ".join(hook_paths)) for hook in hooks: data_key = hook.data_key required = hook.required kwargs = hook.args or {} enabled = hook.enabled if not enabled: logger.debug("hook with method %s is disabled, skipping", hook.path) continue try: method = load_object_from_string(hook.path) except (AttributeError, ImportError): logger.exception("Unable to load method at %s:", hook.path) if required: raise continue try: result = method(context=context, provider=provider, **kwargs) except Exception: logger.exception("Method %s threw an exception:", hook.path) if required: raise continue if not result: if required: logger.error("Required hook %s failed. Return value: %s", hook.path, result) sys.exit(1) logger.warning("Non-required hook %s failed. Return value: %s", hook.path, result) else: if isinstance(result, collections.Mapping): if data_key: logger.debug( "Adding result for hook %s to context in " "data_key %s.", hook.path, data_key) context.set_hook_data(data_key, result) else: logger.debug( "Hook %s returned result data, but no data " "key set, so ignoring.", hook.path)