def create_mock_environment(source_config=None, target_config=None): """ Create and return a mock environment instance """ environment = Environment() environment.source = (None if not source_config else Manifest(StringIO(source_config), namespace="test")) environment.target = (None if not target_config else Manifest(StringIO(target_config), namespace="test")) return environment
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 test_global_config(self): """ Global config should accept a file-like object, or default to ROOT/.sprinter/.global/config.cfg """ temp_dir = tempfile.mkdtemp() os.makedirs(os.path.join(temp_dir, ".global")) with open(os.path.join(temp_dir, ".global", "config.cfg"), 'w+') as fh: fh.write("""[shell] bash = true [global] env_source_rc = False """) try: env = Environment(root=temp_dir) assert env.global_config.get('shell', 'bash') == "true" finally: shutil.rmtree(temp_dir)
def test_utilssh_file_written(self): """ The latest utilssh file should be written at the end of an install """ temp_dir = tempfile.mkdtemp() try: with patch('sprinter.lib.prompt') as prompt: prompt.return_value = "1,2" env = Environment(root=temp_dir) env.formula_dict['sprinter.formulabase'] = Mock(return_value=create_mock_formulabase()) env.target = StringIO(test_target) env.warmup() env.injections.commit = Mock() env.install() global_path = os.path.join(temp_dir, '.global') assert os.path.exists(os.path.join(global_path, 'utils.sh')) finally: shutil.rmtree(temp_dir)
def create_mock_environment(source_config=None, target_config=None, installed=False, global_config=MOCK_GLOBAL_CONFIGURATION, root=None, mock_injections=True, mock_global_injections=True, mock_system=True, mock_directory=True): """ Create and return a mock environment instance """ environment = Environment(global_config=global_config, write_files=False, root=root) environment.source = (None if not source_config else Manifest(StringIO(source_config), namespace="test")) environment.target = (None if not target_config else Manifest(StringIO(target_config), namespace="test")) # mocking directory if mock_directory: environment.directory = Mock(spec=environment.directory) environment.directory.bin_path.return_value = "dummy" environment.directory.install_directory.return_value = "/tmp/" environment.directory.new = not installed # mocking injections if mock_injections: environment.injections = Mock(spec=Injections) # mocking global injections if mock_global_injections: environment.global_injections = Mock(spec=Injections) # mocking system (we will always test as if the system is debian, unless explicitly specified) if mock_system: environment.system = Mock(spec=environment.system) environment.system.isDebianBased.return_value = True environment.system.isOSX.return_value = False environment.system.isFedoraBased.return_value = False environment.write_manifest = Mock() return environment
def parse_args(argv, Environment=Environment): options = docopt( __doc__, argv=argv, version=pkg_resources.get_distribution('sprinter').version) 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 = manifest.load_manifest( target, username=options['<username>'], password=options['<password>'], verify_certificate=( not options['--allow-bad-certificate'])) else: target = manifest.load_manifest( target, 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 = manifest.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 = manifest.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 = manifest.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 = manifest.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 = manifest.load_manifest( env.directory.manifest_path, namespace=options['<environment_name>'], do_inherit=False) env.activate() elif options['list']: for _env in os.listdir(env.root): if _env != ".global": print(_env) elif options['validate']: if options['--username'] or options['--auth']: options = get_credentials(options, parse_domain(target)) target = manifest.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." elif options['globals']: if options['--reconfigure']: configure_config(env.global_config, reconfigure=True) write_config(env.global_config, env.global_config_path) else: print_global_config(env.global_config) 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(""" ===================================================================== the sprinter action 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()) env.logger.info(""" ===================================================================== """.strip()) raise
def parse_args(argv, Environment=Environment): options, args = parser.parse_args(argv) if len(args) == 0: error("Please enter a sprinter action: %s" % str(VALID_COMMANDS)) command = args[0].lower() if command not in VALID_COMMANDS: error("Please enter a valid sprinter action: %s" % ",".join(VALID_COMMANDS)) target = args[1] if len(args) > 1 else None logging_level = logging.DEBUG if options.verbose else logging.INFO # start processing commands env = Environment(logging_level=logging_level) try: if command == "install": 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 = Manifest(target, username=options.username, password=options.password, verify_certificate=(not options.allow_bad_certificate)) env.target = target env.namespace = options.namespace env.install() elif command == "update": env.directory = Directory(target) env.source = Manifest(env.directory.manifest_path) if options.username or options.auth: options = get_credentials(options, target) env.target = Manifest(env.source.source(), username=options.username, password=options.password, verify_certificate=(not options.allow_bad_certificate)) env.update(reconfigure=options.reconfigure) elif command in ["remove", "deactivate", "activate", "reconfigure"]: env.directory = Directory(target) env.source = Manifest(env.directory.manifest_path, namespace=target) getattr(env, command)() elif command == "environments": SPRINTER_ROOT = os.path.expanduser(os.path.join("~", ".sprinter")) for env in os.listdir(SPRINTER_ROOT): print "%s" % env elif command == "validate": if options.username or options.auth: options = get_credentials(parse_domain(target)) errors = env.validate_manifest(target, username=options.username, password=options.password) if len(errors) > 0: print "Manifest is invalid!" print "\n".join(errors) else: print "Manifest is valid!" except BadCredentialsException, e: raise e
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 = 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>'] env.install() elif options['update']: target = options['<environment_name>'] env.directory = Directory(target, sprinter_root=env.root, shell_util_path=env.shell_util_path) env.source = Manifest(env.directory.manifest_path) use_auth = options['--username'] or options['--auth'] if use_auth: options = get_credentials(options, target) env.target = 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(options['<environment_name>'], sprinter_root=env.root, shell_util_path=env.shell_util_path) env.source = Manifest(env.directory.manifest_path, namespace=options['<environment_name>']) env.remove() elif options['deactivate']: env.directory = Directory(options['<environment_name>'], sprinter_root=env.root, shell_util_path=env.shell_util_path) env.source = Manifest(env.directory.manifest_path, namespace=options['<environment_name>']) env.deactivate() elif options['activate']: env.directory = Directory(options['<environment_name>'], sprinter_root=env.root, shell_util_path=env.shell_util_path) env.source = Manifest(env.directory.manifest_path, namespace=options['<environment_name>']) 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 = 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] print(str(e)) print("Could not find Manifest!") 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())
def parse_args(argv, Environment=Environment): options = docopt(__doc__, argv=argv, version= pkg_resources.get_distribution('sprinter').version) 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 = manifest.load_manifest( target, username=options['<username>'], password=options['<password>'], verify_certificate=(not options['--allow-bad-certificate']) ) else: target = manifest.load_manifest( target, 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 = manifest.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 = manifest.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 = manifest.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 = manifest.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 = manifest.load_manifest( env.directory.manifest_path, namespace=options['<environment_name>'], do_inherit=False ) env.activate() elif options['list']: for _env in os.listdir(env.root): if _env != ".global": print(_env) elif options['validate']: if options['--username'] or options['--auth']: options = get_credentials(options, parse_domain(target)) target = manifest.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." elif options['globals']: if options['--reconfigure']: configure_config(env.global_config, reconfigure=True) write_config(env.global_config, env.global_config_path) else: print_global_config(env.global_config) 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(""" ===================================================================== the sprinter action 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()) env.logger.info(""" ===================================================================== """.strip()) raise