Example #1
0
def parse_context(name, registry: Registry, description=""):
    parser = argparse.ArgumentParser(description=description)
    parser.add_argument("--mode", "-m",
                        choices=["interactive", "background"],
                        dest="mode",
                        default="interactive",
                        help="one of [ interactive | background ]. "
                             "Note that some actions cannot be executed in non-interactive mode")
    parser.add_argument("--dry-run",
                        default=False,
                        dest="dryrun",
                        action="store_true",
                        help="runs in dry run mode. In that mode actions that modify your environment will not be "
                             "executed")
    parser.add_argument("--plan", "-p",
                        default=False,
                        dest="plan",
                        action="store_true",
                        help="prints out an execution plan (takes into account your platform and program flags)")
    parser.add_argument("--debug", "-d",
                        default=False,
                        dest="debug",
                        action="store_true",
                        help="logs debug information to the console")
    parser.add_argument("--experimental", "-e",
                        default=False,
                        dest="experimental",
                        action="store_true",
                        help="turns on experimental features")
    parser.add_argument("--log-file",
                        dest="log_file",
                        help="absolute path to optional log file")
    parser.add_argument("--config",
                        dest="config_file",
                        help="optional JSON config file path")
    parser.add_argument("--components",
                        default=None,
                        dest="components",
                        help="optional comma separated list of component names. Supported components are: {}"
                        .format(list(registry.component_ids())))

    args = parser.parse_args()

    if args.components is not None:
        components = [comp.strip() for comp in args.components.split(",")]
    else:
        components = None

    return Context(
        name=name,
        config_file=args.config_file,
        registry=registry,
        mode=Mode.from_str(args.mode),
        debug=args.debug,
        log_file=args.log_file,
        plan=args.plan,
        dryrun=args.dryrun,
        experimental=args.experimental,
        components=components
    )
Example #2
0
    def test_prompt_with_non_existing_user_input(self, patched_input):
        context = Context(name="test",
                          registry=Registry(),
                          mode=Mode.INTERACTIVE)
        key = uuid4()

        self.assertEqual(random_user_input,
                         context.get_or_request_user_input(key, "some prompt"))
Example #3
0
    def test_user_input_fails_in_non_interactive_mode(self):
        context = Context(name="test",
                          registry=Registry(),
                          mode=Mode.BACKGROUND)

        self.assertRaises(
            Exception, lambda *args: context.get_or_request_user_input(
                "any", "some prompt"))
Example #4
0
    def test_existing_user_input(self):
        context = Context(name="test",
                          registry=Registry(),
                          mode=Mode.INTERACTIVE)
        key = uuid4()
        value = uuid4()

        context._user_inputs[key] = value

        self.assertEqual(value,
                         context.get_or_request_user_input(key, "some prompt"))
Example #5
0
    def run(self):
        registry = Registry()
        self._register_components(registry)

        ctx = self.parse_context(name=self.name,
                                 registry=registry,
                                 description=self.description)

        ctx.logger.info("Starting {}.".format(self.name))
        _print_header(ctx)

        if ctx.flags.plan:
            _run_safe_execution_plan(ctx)
        else:
            run_safe(ctx, self._do_run)

        ctx.logger.info("{} finished.".format(self.name.capitalize()))
Example #6
0
def _parse_context():
    parser = argparse.ArgumentParser(
        description=
        "Collects environment information and packs it all into a tar archive for support "
        "purposes.")
    parser.add_argument("--debug",
                        "-d",
                        default=False,
                        dest="debug",
                        action="store_true",
                        help="logs debug information to the console")
    parser.add_argument("--experimental",
                        "-e",
                        default=False,
                        dest="experimental",
                        action="store_true",
                        help="turns on experimental features")
    parser.add_argument("--log-file",
                        dest="log_file",
                        help="absolute path to optional log file")
    parser.add_argument("--config",
                        dest="config_file",
                        help="optional JSON config file path")
    parser.add_argument("--output",
                        "-o",
                        default=None,
                        dest="out_file",
                        help="absolute path to the output tar archive")

    args = parser.parse_args()

    context = Context(
        name="dump",
        config_file=args.config_file,
        registry=Registry(),
        debug=args.debug,
        log_file=args.log_file,
        experimental=args.experimental,
    )

    context.flags.out_file = resolve_output_file_path(args)

    return context
Example #7
0
def test_context(mode=None) -> Context:
    resolved_mode = mode
    if resolved_mode is None:
        resolved_mode = Mode.from_str(os.environ.get('INSPECTOR_TEST_MODE', str(Mode.BACKGROUND)))

    return Context(name="test", mode=resolved_mode, dryrun=True, registry=Registry())
Example #8
0
def _inspection_context():
    registry = Registry()
    inspector.register_components(registry)
    ctx = parse_context(name="installer", registry=registry)

    return ctx
Example #9
0
def register_components(registry: Registry):
    inspector.register_components(registry)

    registry.register_reactor(BREW_COMP_ID, HomebrewInstallReactor())
    registry.register_reactor(BAZEL_COMP_ID, BazelInstallReactor())
    registry.register_reactor(PYTHON_COMP_ID, PythonInstallReactor())
    registry.register_reactor(PYTHON3_COMP_ID, Python3InstallReactor())
    registry.register_reactor(XCODE_COMP_ID, XcodeInstallReactor())
    registry.register_reactor(GCLOUD_COMP_ID, GCloudInstallReactor())
    registry.register_reactor(GCLOUD_CONFIG_COMP_ID,
                              GCloudConfigInstallReactor())
    registry.register_reactor(inspector.DOCKER_COMP_ID, DockerInstallReactor())
Example #10
0
def test_context(mode=Mode.BACKGROUND) -> Context:
    mode = Mode.from_str(os.environ.get('INSPECTOR_TEST_MODE', str(mode)))

    return Context(name="test", registry=Registry(), mode=mode, dryrun=True)
Example #11
0
    def test_empty(self):
        registry = Registry()

        self.assertEqual(0, len(registry.component_ids()))
Example #12
0
    def test_basic_registry(self):
        registry = Registry()
        comp_id = "test"

        implied_compatible = DummyHandler()

        registry.register_collector(comp_id, implied_compatible)
        registry.register_validator(comp_id, implied_compatible)
        registry.register_reactor(comp_id, implied_compatible)

        self.assertEqual(implied_compatible, registry.find_collector(comp_id))
        self.assertEqual(implied_compatible, registry.find_validator(comp_id))
        self.assertEqual([implied_compatible], registry.find_reactors(comp_id))
Example #13
0
def register_components(registry: Registry):
    log_reactor = DebugReactor()

    registry.register_collector(NET_COMP_ID, UrlConnectivityInfoCollector())
    registry.register_validator(NET_COMP_ID, UrlConnectivityInfoValidator())
    registry.register_reactor(NET_COMP_ID, log_reactor)

    registry.register_collector(HARDWARE_COMP_ID, HardwareInfoCollector())
    registry.register_validator(HARDWARE_COMP_ID, HardwareInfoValidator())
    registry.register_reactor(HARDWARE_COMP_ID, log_reactor)

    registry.register_collector(DISK_COMP_ID, DiskInfoCollector())
    registry.register_validator(DISK_COMP_ID, DiskInfoValidator())
    registry.register_reactor(DISK_COMP_ID, log_reactor)

    registry.register_collector(BREW_COMP_ID,
                                HomebrewCommandCollectorValidator())
    registry.register_validator(BREW_COMP_ID,
                                HomebrewCommandCollectorValidator())
    registry.register_reactor(BREW_COMP_ID, log_reactor)

    registry.register_collector(XCODE_COMP_ID, XcodeInfoCollector())
    registry.register_validator(XCODE_COMP_ID, XcodeInfoValidator())
    registry.register_reactor(XCODE_COMP_ID, log_reactor)

    registry.register_collector(BAZEL_COMP_ID, BazelInfoCollector())
    registry.register_validator(BAZEL_COMP_ID, BazelInfoValidator())
    registry.register_reactor(BAZEL_COMP_ID, log_reactor)

    registry.register_collector(PYTHON_COMP_ID, PythonInfoCollector())
    registry.register_validator(
        PYTHON_COMP_ID,
        PythonInfoValidator(expected_ver=SemVer("2", "7", "0")))
    registry.register_reactor(PYTHON_COMP_ID, log_reactor)

    registry.register_collector(PYTHON3_COMP_ID,
                                PythonInfoCollector(binary_name="python3"))
    registry.register_validator(
        PYTHON3_COMP_ID,
        PythonInfoStrictValidator(expected_ver=SemVer("3", "6", "8")))
    registry.register_reactor(PYTHON3_COMP_ID, log_reactor)

    registry.register_collector(GCLOUD_COMP_ID,
                                GCloudCommandCollectorValidator())
    registry.register_validator(GCLOUD_COMP_ID,
                                GCloudCommandCollectorValidator())
    registry.register_reactor(GCLOUD_COMP_ID, log_reactor)

    registry.register_collector(GCLOUD_CONFIG_COMP_ID, GCloudConfigCollector())
    registry.register_validator(GCLOUD_CONFIG_COMP_ID, GCloudConfigValidator())
    registry.register_reactor(GCLOUD_CONFIG_COMP_ID, log_reactor)

    registry.register_collector(DOCKER_COMP_ID, DockerInfoCollector())
    registry.register_validator(DOCKER_COMP_ID, DockerInfoValidator())
    registry.register_reactor(DOCKER_COMP_ID, log_reactor)