Example #1
0
    def test_resolve(self):
        resolve_default_ip_mock = MagicMock(return_value='resolve_result')
        with patch('conductr_cli.host.resolve_default_ip',
                   resolve_default_ip_mock):
            result = host.resolve_default_host()
            self.assertEqual(result, 'resolve_result')

        resolve_default_ip_mock.assert_called_with()
Example #2
0
    def test_resolve(self):
        getenv_mock = MagicMock(return_value="test")
        resolve_default_ip_mock = MagicMock(return_value="resolve_result")
        with patch("os.getenv", getenv_mock), patch("conductr_cli.host.resolve_default_ip", resolve_default_ip_mock):
            result = host.resolve_default_host()
            self.assertEqual(result, "test")

        getenv_mock.assert_called_with("CONDUCTR_HOST", "resolve_result")
Example #3
0
def get_cli_parameters(args):
    parameters = ['']
    arg = vars(args).get('scheme')
    if not args.dcos_mode and arg and arg != DEFAULT_SCHEME:
        parameters.append('--scheme {}'.format(args.scheme))
    arg = vars(args).get('host')
    if not args.dcos_mode and arg and arg != host.resolve_default_host():
        parameters.append('--host {}'.format(args.host))
    arg = vars(args).get('ip')
    if not args.dcos_mode and arg and arg != host.resolve_default_ip():
        parameters.append('--ip {}'.format(args.ip))
    arg = vars(args).get('port', int(DEFAULT_PORT))
    if not args.dcos_mode and arg and arg != int(DEFAULT_PORT):
        parameters.append('--port {}'.format(args.port))
    arg = vars(args).get('base_path', DEFAULT_BASE_PATH)
    if not args.dcos_mode and arg and arg != DEFAULT_BASE_PATH:
        parameters.append('--base-path {}'.format(args.base_path))
    arg = vars(args).get('api_version', DEFAULT_API_VERSION)
    if arg and arg != DEFAULT_API_VERSION:
        parameters.append('--api-version {}'.format(args.api_version))
    return ' '.join(parameters)
Example #4
0
def get_cli_parameters(args):
    parameters = ['']
    arg = vars(args).get('scheme')
    if not args.dcos_mode and arg and arg != DEFAULT_SCHEME:
        parameters.append('--scheme {}'.format(args.scheme))
    arg = vars(args).get('host')
    if not args.dcos_mode and arg and arg != host.resolve_default_host():
        parameters.append('--host {}'.format(args.host))
    arg = vars(args).get('ip')
    if not args.dcos_mode and arg and arg != host.resolve_default_ip():
        parameters.append('--ip {}'.format(args.ip))
    arg = vars(args).get('port', int(DEFAULT_PORT))
    if not args.dcos_mode and arg and arg != int(DEFAULT_PORT):
        parameters.append('--port {}'.format(args.port))
    arg = vars(args).get('base_path', DEFAULT_BASE_PATH)
    if not args.dcos_mode and arg and arg != DEFAULT_BASE_PATH:
        parameters.append('--base-path {}'.format(args.base_path))
    arg = vars(args).get('api_version', DEFAULT_API_VERSION)
    if arg and arg != DEFAULT_API_VERSION:
        parameters.append('--api-version {}'.format(args.api_version))
    return ' '.join(parameters)
Example #5
0
def run(_args=[], configure_logging=True):
    # If we're being invoked via DC/OS then route our http
    # calls via its extension to the requests library. In
    # addition remove the 'conduct-dcos' and 'conduct' arg so that the conduct
    # sub-commands are positioned correctly, along with their
    # arguments.
    if sys.argv and Path(
            sys.argv[0]).name == constants.DCOS_COMMAND_PREFIX + 'conduct':
        dcos_mode = True
        _args = sys.argv[2:]
    else:
        dcos_mode = False
        if not _args:
            # Remove the 'conduct' arg so that we start with the sub command directly
            _args = sys.argv[1:]
    # Parse arguments
    parser = build_parser(dcos_mode)
    argcomplete.autocomplete(parser)
    args = parser.parse_args(_args)
    args.dcos_mode = dcos_mode
    if not vars(args).get('func'):
        if vars(args).get('dcos_info'):
            print(
                'Lightbend ConductR sub commands. Type \'dcos conduct\' to see more.'
            )
            exit(0)
        else:
            parser.print_help()
    else:
        # Offline functions are the functions which do not require network to run, e.g. `conduct version` or
        # `conduct setup-dcos`.
        offline_functions = ['version', 'setup']

        # Only setup network related args (i.e. host, bundle resolvers, basic auth, etc) for functions which requires
        # connectivity to ConductR.
        current_function = vars(args).get('func').__name__
        if current_function not in offline_functions:
            # Add custom plugin dir to import path
            custom_plugins_dir = vars(args).get('custom_plugins_dir')
            if custom_plugins_dir:
                sys.path.append(custom_plugins_dir)

            # DC/OS provides the location of ConductR...
            if dcos_mode:
                args.command = 'dcos conduct'
                dcos_url = urlparse(config.get_config_val('core.dcos_url'))
                args.scheme = dcos_url.scheme
                args.ip = dcos_url.hostname
                default_http_port = 80 if dcos_url.scheme == 'http' else 443
                args.port = dcos_url.port if dcos_url.port else default_http_port
                dcos_url_path = dcos_url.path if dcos_url.path else '/'
                args.base_path = dcos_url_path + 'service/{}/'.format(
                    DEFAULT_DCOS_SERVICE)
            else:
                args.command = 'conduct'

            # Set ConductR host is --host or --ip argument not set
            # Also set the local_connection argument accordingly
            host_from_args = conduct_url.conductr_host(args)
            if not host_from_args:
                host_from_env = host.resolve_host_from_env()
                if host_from_env:
                    args.host = host_from_env
                    args.local_connection = False
                else:
                    args.host = host.resolve_default_host()
            else:
                args.local_connection = False

            args.cli_parameters = get_cli_parameters(args)
            args.custom_settings = custom_settings.load_from_file(args)

            args.conductr_auth = custom_settings.load_conductr_credentials(
                args)

            # Ensure HTTPS is used if authentication is configured
            if args.conductr_auth and args.scheme != 'https':
                args.scheme = 'https'

            args.server_verification_file = custom_settings.load_server_ssl_verification_file(
                args)
            # Ensure verification file exists if specified
            if args.server_verification_file \
                    and not os.path.exists(args.server_verification_file):
                # Configure logging so error message can be logged properly before exiting with failure
                logging_setup.configure_logging(args)
                log = logging.getLogger(__name__)
                log.error(
                    'Ensure server SSL verification file exists: {}'.format(
                        args.server_verification_file))
                exit(1)

            if not args.dcos_mode and args.scheme == 'https':
                disable_urllib3_warnings()

        if configure_logging:
            logging_setup.configure_logging(args)

        is_completed_without_error = args.func(args)
        if not is_completed_without_error:
            exit(1)
Example #6
0
def run(_args=[], configure_logging=True):
    # If we're being invoked via DC/OS then route our http
    # calls via its extension to the requests library. In
    # addition remove the 'conduct-dcos' and 'conduct' arg so that the conduct
    # sub-commands are positioned correctly, along with their
    # arguments.
    if sys.argv and Path(sys.argv[0]).name == constants.DCOS_COMMAND_PREFIX + 'conduct':
        dcos_mode = True
        _args = sys.argv[2:]
    else:
        dcos_mode = False
        if not _args:
            # Remove the 'conduct' arg so that we start with the sub command directly
            _args = sys.argv[1:]
    # Parse arguments
    parser = build_parser(dcos_mode)
    argcomplete.autocomplete(parser)
    args = parser.parse_args(_args)
    args.dcos_mode = dcos_mode
    if not vars(args).get('func'):
        if vars(args).get('dcos_info'):
            print('Lightbend ConductR sub commands. Type \'dcos conduct\' to see more.')
            exit(0)
        else:
            parser.print_help()
    else:
        # Offline functions are the functions which do not require network to run, e.g. `conduct version` or
        # `conduct setup-dcos`.
        offline_functions = ['version', 'setup']

        # Only setup network related args (i.e. host, bundle resolvers, basic auth, etc) for functions which requires
        # connectivity to ConductR.
        current_function = vars(args).get('func').__name__
        if current_function not in offline_functions:
            # Add custom plugin dir to import path
            custom_plugins_dir = vars(args).get('custom_plugins_dir')
            if custom_plugins_dir:
                sys.path.append(custom_plugins_dir)

            # DC/OS provides the location of ConductR...
            if dcos_mode:
                args.command = 'dcos conduct'
                dcos_url = urlparse(config.get_config_val('core.dcos_url'))
                args.scheme = dcos_url.scheme
                args.ip = dcos_url.hostname
                default_http_port = 80 if dcos_url.scheme == 'http' else 443
                args.port = dcos_url.port if dcos_url.port else default_http_port
                dcos_url_path = dcos_url.path if dcos_url.path else '/'
                args.base_path = dcos_url_path + 'service/{}/'.format(DEFAULT_DCOS_SERVICE)
            else:
                args.command = 'conduct'

            # Ensure ConductR host is not empty
            host_from_args = conduct_url.conductr_host(args)
            if not host_from_args:
                host_from_env = host.resolve_default_host()
                if host_from_env:
                    args.host = host_from_env
                else:
                    # Configure logging so error message can be logged properly before exiting with failure
                    logging_setup.configure_logging(args)
                    log = logging.getLogger(__name__)
                    log.error('ConductR host address is not specified')
                    log.error('Please ensure either `{}` environment is specified,'
                              ' or specify the ConductR host using `--host` argument'.format(CONDUCTR_HOST))
                    exit(1)
            else:
                args.local_connection = False

            args.cli_parameters = get_cli_parameters(args)
            args.custom_settings = custom_settings.load_from_file(args)

            args.conductr_auth = custom_settings.load_conductr_credentials(args)

            # Ensure HTTPS is used if authentication is configured
            if args.conductr_auth and not args.scheme == 'https':
                # Configure logging so error message can be logged properly before exiting with failure
                logging_setup.configure_logging(args)
                log = logging.getLogger(__name__)
                log.error('Unable to use Basic Auth over {}'.format(args.scheme))
                log.error('Please ensure either `{}` environment is set to `https`,'
                          ' or specify https using `--scheme https` argument'.format(CONDUCTR_SCHEME))
                exit(1)

            args.server_verification_file = custom_settings.load_server_ssl_verification_file(args)
            # Ensure verification file exists if specified
            if args.server_verification_file \
                    and not os.path.exists(args.server_verification_file):
                # Configure logging so error message can be logged properly before exiting with failure
                logging_setup.configure_logging(args)
                log = logging.getLogger(__name__)
                log.error('Ensure server SSL verification file exists: {}'.format(args.server_verification_file))
                exit(1)

            if not args.dcos_mode and args.scheme == 'https':
                disable_urllib3_warnings()

        if configure_logging:
            logging_setup.configure_logging(args)

        is_completed_without_error = args.func(args)
        if not is_completed_without_error:
            exit(1)