def create_mock_environment(source_config=None, target_config=None, global_config=None, mock_formulabase=None): temp_directory = tempfile.mkdtemp() environment = Environment(root=temp_directory, sprinter_namespace='test', global_config=(global_config or create_default_config())) environment.namespace = "test" if source_config: environment.source = load_manifest(StringIO(source_config), namespace="test") if target_config: environment.target = load_manifest(StringIO(target_config), namespace="test") environment.warmup() # TODO: implement sandboxing so no need to mock these environment.injections.commit = Mock() environment.global_injections.commit = Mock() environment.write_manifest = Mock() if mock_formulabase: formula_dict = {'sprinter.formula.base': mock_formulabase} environment.features = FeatureDict(environment, environment.source, environment.target, environment.global_path, formula_dict=formula_dict) return environment, temp_directory
def warmup(self): """ initialize variables necessary to perform a sprinter action """ self.logger.debug("Warming up...") try: if not isinstance(self.source, Manifest) and self.source: self.source = load_manifest(self.source) if not isinstance(self.target, Manifest) and self.target: self.target = load_manifest(self.target) self.main_manifest = self.target or self.source except lib.BadCredentialsException: e = sys.exc_info()[1] self.logger.error(str(e)) raise SprinterException( "Fatal error! Bad credentials to grab manifest!") if not getattr(self, 'namespace', None): if self.target: self.namespace = self.target.namespace elif not self.namespace and self.source: self.namespace = self.source.namespace else: raise SprinterException( "No environment name has been specified!") self.directory_root = self.custom_directory_root if not self.directory: if not self.directory_root: self.directory_root = os.path.join(self.root, self.namespace) self.directory = Directory(self.directory_root, shell_util_path=self.shell_util_path) if not self.injections: self.injections = Injections( wrapper="%s_%s" % (self.sprinter_namespace.upper(), self.namespace), override="SPRINTER_OVERRIDES") if not self.global_injections: self.global_injections = Injections( wrapper="%s" % self.sprinter_namespace.upper() + "GLOBALS", override="SPRINTER_OVERRIDES") # append the bin, in the case sandboxes are necessary to # execute commands further down the sprinter lifecycle os.environ['PATH'] = self.directory.bin_path( ) + ":" + os.environ['PATH'] self.warmed_up = True
def install(self): """ Install the environment """ self.phase = PHASE.INSTALL if not self.directory.new: self.logger.info("Namespace %s directory already exists!" % self.namespace) self.source = load_manifest(self.directory.manifest_path) return self.update() try: self.logger.info("Installing environment %s..." % self.namespace) self.directory.initialize() self.install_sandboxes() self.instantiate_features() self.grab_inputs() self._specialize() for feature in self.features.run_order: self.run_action(feature, 'sync') self.inject_environment_config() self._finalize() except Exception: self.logger.debug("", exc_info=sys.exc_info()) self.logger.info("An error occured during installation!") if not self.ignore_errors: self.clear_all() self.logger.info("Removing installation %s..." % self.namespace) self.directory.remove() et, ei, tb = sys.exc_info() reraise(et, ei, tb)
def warmup(self): """ initialize variables necessary to perform a sprinter action """ self.logger.debug("Warming up...") try: if not isinstance(self.source, Manifest) and self.source: self.source = load_manifest(self.source) if not isinstance(self.target, Manifest) and self.target: self.target = load_manifest(self.target) self.main_manifest = self.target or self.source except lib.BadCredentialsException: e = sys.exc_info()[1] self.logger.error(str(e)) raise SprinterException("Fatal error! Bad credentials to grab manifest!") if not getattr(self, 'namespace', None): if self.target: self.namespace = self.target.namespace elif not self.namespace and self.source: self.namespace = self.source.namespace else: raise SprinterException("No environment name has been specified!") self.directory_root = self.custom_directory_root if not self.directory: if not self.directory_root: self.directory_root = os.path.join(self.root, self.namespace) self.directory = Directory(self.directory_root, shell_util_path=self.shell_util_path) if not self.injections: self.injections = Injections(wrapper="%s_%s" % (self.sprinter_namespace.upper(), self.namespace), override="SPRINTER_OVERRIDES") if not self.global_injections: self.global_injections = Injections(wrapper="%s" % self.sprinter_namespace.upper() + "GLOBALS", override="SPRINTER_OVERRIDES") # append the bin, in the case sandboxes are necessary to # execute commands further down the sprinter lifecycle os.environ['PATH'] = self.directory.bin_path() + ":" + os.environ['PATH'] self.warmed_up = True
def parse_args(argv, Environment=Environment): options = docopt(__doc__, argv=argv, version="Sprinter 1.0") logging_level = logging.DEBUG if options['--verbose'] else logging.INFO # start processing commands env = Environment(logging_level=logging_level, ignore_errors=options['--ignore-errors']) try: if options['install']: target = options['<environment_source>'] def handle_install_shutdown(signal, frame): if env.phase == PHASE.INSTALL: print("Removing install...") env.directory.remove() env.clear_all() signal_handler(signal, frame) signal.signal(signal.SIGINT, handle_install_shutdown) if options['--username'] or options['--auth']: options = get_credentials(options, parse_domain(target)) target = load_manifest(target, username=options['<username>'], password=options['<password>'], verify_certificate=(not options['--allow-bad-certificate'])) env.target = target if options['--namespace']: env.namespace = options['<namespace>'] if options['--local']: env.do_inject_environment_config = False env.custom_directory_root = os.path.abspath(os.path.expanduser(options['--local'])) env.install() elif options['update']: target = options['<environment_name>'] env.directory = Directory(os.path.join(env.root, target), shell_util_path=env.shell_util_path) env.source = load_manifest(env.directory.manifest_path, do_inherit=False) use_auth = options['--username'] or options['--auth'] if use_auth: options = get_credentials(options, target) env.target = load_manifest(env.source.source(), username=options['<username>'] if use_auth else None, password=options['<password>'] if use_auth else None, verify_certificate=(not options['--allow-bad-certificate'])) env.update(reconfigure=options['--reconfigure']) elif options["remove"]: env.directory = Directory(os.path.join(env.root, options['<environment_name>']), shell_util_path=env.shell_util_path) env.source = load_manifest(env.directory.manifest_path, namespace=options['<environment_name>'], do_inherit=False) env.remove() elif options['deactivate']: env.directory = Directory(os.path.join(env.root, options['<environment_name>']), shell_util_path=env.shell_util_path) env.source = load_manifest(env.directory.manifest_path, namespace=options['<environment_name>'], do_inherit=False) env.deactivate() elif options['activate']: env.directory = Directory(os.path.join(env.root, options['<environment_name>']), shell_util_path=env.shell_util_path) env.source = load_manifest(env.directory.manifest_path, namespace=options['<environment_name>'], do_inherit=False) env.activate() elif options['environments']: SPRINTER_ROOT = os.path.expanduser(os.path.join("~", ".sprinter")) for env in os.listdir(SPRINTER_ROOT): if env != ".global": print(env) elif options['validate']: if options['--username'] or options['--auth']: options = get_credentials(options, parse_domain(target)) target = load_manifest(options['<environment_source>'], username=options['<username>'], password=options['<password>'], verify_certificate=(not options['--allow-bad-certificate'])) env.target = options['<environment_source>'] env.validate() if not env.error_occured: print("No errors! Manifest is valid!") else: "Manifest is invalid! Please see errors above." except BadCredentialsException: e = sys.exc_info()[1] raise e except ManifestException: e = sys.exc_info()[1] env.log_error(str(e)) env.logger.info("Error occured when attempting to load manifest!") env.logger.info("Writing debug output to /tmp/sprinter.log") env.write_debug_log("/tmp/sprinter.log") except Exception: e = sys.exc_info()[1] env.log_error(str(e)) env.logger.info("failed! Writing debug output to /tmp/sprinter.log") env.write_debug_log("/tmp/sprinter.log") if env.message_failure(): env.logger.info(env.message_failure())