def load_v2(args): log = logging.getLogger(__name__) log.info('Retrieving bundle...') custom_settings = args.custom_settings resolve_cache_dir = args.resolve_cache_dir bundle_name, bundle_file = resolver.resolve_bundle(custom_settings, resolve_cache_dir, args.bundle) bundle_conf = bundle_utils.zip_entry('bundle.conf', bundle_file) if bundle_conf is None: raise MalformedBundleError('Unable to find bundle.conf within the bundle file') else: configuration_name, configuration_file, bundle_conf_overlay = (None, None, None) if args.configuration is not None: log.info('Retrieving configuration...') configuration_name, configuration_file = resolver.resolve_bundle(custom_settings, resolve_cache_dir, args.configuration) bundle_conf_overlay = bundle_utils.zip_entry('bundle.conf', configuration_file) files = [('bundleConf', ('bundle.conf', bundle_conf))] if bundle_conf_overlay is not None: files.append(('bundleConfOverlay', ('bundle.conf', bundle_conf_overlay))) files.append(('bundle', (bundle_name, open(bundle_file, 'rb')))) if configuration_file is not None: files.append(('configuration', (configuration_name, open(configuration_file, 'rb')))) url = conduct_url.url('bundles', args) log.info('Loading bundle to ConductR...') response = requests.post(url, files=files, timeout=LOAD_HTTP_TIMEOUT) validation.raise_for_status_inc_3xx(response) if log.is_verbose_enabled(): log.verbose(validation.pretty_json(response.text)) response_json = json.loads(response.text) bundle_id = response_json['bundleId'] if args.long_ids else bundle_utils.short_id(response_json['bundleId']) if not args.no_wait: bundle_installation.wait_for_installation(response_json['bundleId'], args) log.info('Bundle loaded.') log.info('Start bundle with: conduct run{} {}'.format(args.cli_parameters, bundle_id)) log.info('Unload bundle with: conduct unload{} {}'.format(args.cli_parameters, bundle_id)) log.info('Print ConductR info with: conduct info{}'.format(args.cli_parameters)) if not log.is_info_enabled() and log.is_quiet_enabled(): log.quiet(response_json['bundleId']) return True
def test_resolve_bundle_success(self): custom_settings = Mock() first_resolver_mock = Mock() first_resolver_mock.load_bundle_from_cache = MagicMock(return_value=(False, None, None)) first_resolver_mock.resolve_bundle = MagicMock(return_value=(False, None, None)) second_resolver_mock = Mock() second_resolver_mock.load_bundle_from_cache = MagicMock(return_value=(False, None, None)) second_resolver_mock.resolve_bundle = MagicMock(return_value=(True, 'bundle_name', 'mock bundle_file')) resolver_chain_mock = MagicMock(return_value=[first_resolver_mock, second_resolver_mock]) with patch('conductr_cli.resolver.resolver_chain', resolver_chain_mock): bundle_name, bundle_file = resolver.resolve_bundle(custom_settings, '/some-cache-dir', '/some-bundle-path') self.assertEqual('bundle_name', bundle_name) self.assertEqual('mock bundle_file', bundle_file) resolver_chain_mock.assert_called_with(custom_settings) first_resolver_mock.load_from_cache('/some-cache-dir', '/some-bundle-path') first_resolver_mock.resolve_bundle.assert_called_with('/some-cache-dir', '/some-bundle-path') second_resolver_mock.load_from_cache('/some-cache-dir', '/some-bundle-path') second_resolver_mock.resolve_bundle.assert_called_with('/some-cache-dir', '/some-bundle-path')
def load_v1(args): log = logging.getLogger(__name__) log.info('Retrieving bundle...') custom_settings = args.custom_settings resolve_cache_dir = args.resolve_cache_dir bundle_name, bundle_file = resolver.resolve_bundle(custom_settings, resolve_cache_dir, args.bundle) configuration_name, configuration_file = (None, None) if args.configuration is not None: log.info('Retrieving configuration...') configuration_name, configuration_file = resolver.resolve_bundle(custom_settings, resolve_cache_dir, args.configuration) bundle_conf = ConfigFactory.parse_string(bundle_utils.conf(bundle_file)) overlay_bundle_conf = None if configuration_file is None else \ ConfigFactory.parse_string(bundle_utils.conf(configuration_file)) with_bundle_configurations = partial(apply_to_configurations, bundle_conf, overlay_bundle_conf) url = conduct_url.url('bundles', args) files = get_payload(bundle_name, bundle_file, with_bundle_configurations) if configuration_file is not None: files.append(('configuration', (configuration_name, open(configuration_file, 'rb')))) log.info('Loading bundle to ConductR...') response = requests.post(url, files=files, timeout=LOAD_HTTP_TIMEOUT) validation.raise_for_status_inc_3xx(response) if log.is_verbose_enabled(): log.verbose(validation.pretty_json(response.text)) response_json = json.loads(response.text) bundle_id = response_json['bundleId'] if args.long_ids else bundle_utils.short_id(response_json['bundleId']) if not args.no_wait: bundle_installation.wait_for_installation(response_json['bundleId'], args) log.info('Bundle loaded.') log.info('Start bundle with: conduct run{} {}'.format(args.cli_parameters, bundle_id)) log.info('Unload bundle with: conduct unload{} {}'.format(args.cli_parameters, bundle_id)) log.info('Print ConductR info with: conduct info{}'.format(args.cli_parameters)) if not log.is_info_enabled() and log.is_quiet_enabled(): log.quiet(response_json['bundleId']) return True
def test_resolve_bundle_success(self): custom_settings = Mock() first_resolver_mock = Mock() first_resolver_mock.load_bundle_from_cache = MagicMock( return_value=(False, None, None)) first_resolver_mock.resolve_bundle = MagicMock(return_value=(False, None, None)) second_resolver_mock = Mock() second_resolver_mock.load_bundle_from_cache = MagicMock( return_value=(False, None, None)) second_resolver_mock.resolve_bundle = MagicMock( return_value=(True, 'bundle_name', 'mock bundle_file')) resolver_chain_mock = MagicMock( return_value=[first_resolver_mock, second_resolver_mock]) with patch('conductr_cli.resolver.resolver_chain', resolver_chain_mock): bundle_name, bundle_file = resolver.resolve_bundle( custom_settings, '/some-cache-dir', '/some-bundle-path') self.assertEqual('bundle_name', bundle_name) self.assertEqual('mock bundle_file', bundle_file) resolver_chain_mock.assert_called_with(custom_settings, False) first_resolver_mock.load_from_cache('/some-cache-dir', '/some-bundle-path') first_resolver_mock.resolve_bundle.assert_called_with( '/some-cache-dir', '/some-bundle-path') second_resolver_mock.load_from_cache('/some-cache-dir', '/some-bundle-path') second_resolver_mock.resolve_bundle.assert_called_with( '/some-cache-dir', '/some-bundle-path')
def load_v1(args): log = logging.getLogger(__name__) log.info('Retrieving bundle...') custom_settings = args.custom_settings resolve_cache_dir = args.resolve_cache_dir bundle_name, bundle_file = resolver.resolve_bundle(custom_settings, resolve_cache_dir, args.bundle) configuration_name, configuration_file = (None, None) if args.configuration is not None: log.info('Retrieving configuration...') configuration_name, configuration_file = resolver.resolve_bundle( custom_settings, resolve_cache_dir, args.configuration) bundle_conf = ConfigFactory.parse_string(bundle_utils.conf(bundle_file)) overlay_bundle_conf = None if configuration_file is None else \ ConfigFactory.parse_string(bundle_utils.conf(configuration_file)) with_bundle_configurations = partial(apply_to_configurations, bundle_conf, overlay_bundle_conf) url = conduct_url.url('bundles', args) files = get_payload(bundle_name, bundle_file, with_bundle_configurations) if configuration_file is not None: files.append(('configuration', (configuration_name, open(configuration_file, 'rb')))) log.info('Loading bundle to ConductR...') # At the time when this comment is being written, we need to pass the Host header when making HTTP request due to # a bug with requests python library not working properly when IPv6 address is supplied: # https://github.com/kennethreitz/requests/issues/3002 # The workaround for this problem is to explicitly set the Host header when making HTTP request. # This fix is benign and backward compatible as the library would do this when making HTTP request anyway. response = requests.post(url, files=files, timeout=LOAD_HTTP_TIMEOUT, headers=conduct_url.request_headers(args)) validation.raise_for_status_inc_3xx(response) if log.is_verbose_enabled(): log.verbose(validation.pretty_json(response.text)) response_json = json.loads(response.text) bundle_id = response_json[ 'bundleId'] if args.long_ids else bundle_utils.short_id( response_json['bundleId']) if not args.no_wait: bundle_installation.wait_for_installation(response_json['bundleId'], args) log.info('Bundle loaded.') log.info('Start bundle with: conduct run{} {}'.format( args.cli_parameters, bundle_id)) log.info('Unload bundle with: conduct unload{} {}'.format( args.cli_parameters, bundle_id)) log.info('Print ConductR info with: conduct info{}'.format( args.cli_parameters)) if not log.is_info_enabled() and log.is_quiet_enabled(): log.quiet(response_json['bundleId']) return True
def load_v1(args): log = logging.getLogger(__name__) log.info('Retrieving bundle..') custom_settings = args.custom_settings bundle_resolve_cache_dir = args.bundle_resolve_cache_dir configuration_cache_dir = args.configuration_resolve_cache_dir validate_cache_dir_permissions(bundle_resolve_cache_dir, configuration_cache_dir, log) initial_bundle_file_name, bundle_file = resolver.resolve_bundle( custom_settings, bundle_resolve_cache_dir, args.bundle, args.offline_mode) configuration_file_name, configuration_file = (None, None) if args.configuration is not None: log.info('Retrieving configuration..') configuration_file_name, configuration_file = \ resolver.resolve_bundle_configuration(custom_settings, configuration_cache_dir, args.configuration, args.offline_mode) bundle_conf_text = bundle_utils.conf(bundle_file) bundle_conf = ConfigFactory.parse_string(bundle_conf_text) bundle_file_name, bundle_open_file = open_bundle(initial_bundle_file_name, bundle_file, bundle_conf_text) overlay_bundle_conf = None if configuration_file is None else \ ConfigFactory.parse_string(bundle_utils.conf(configuration_file)) with_bundle_configurations = partial(apply_to_configurations, bundle_conf, overlay_bundle_conf) url = conduct_url.url('bundles', args) files = get_payload(bundle_file_name, bundle_open_file, with_bundle_configurations) if configuration_file is not None: open_configuration_file, config_digest = bundle_utils.digest_extract_and_open( configuration_file) files.append(('configuration', (configuration_file_name, open_configuration_file))) # TODO: Delete the bundle configuration file. # Currently, this results into a permission error on Windows. # Therefore, the deletion is disabled for now. # Issue: https://github.com/typesafehub/conductr-cli/issues/175 # if configuration_file and os.path.exists(configuration_file): # os.remove(configuration_file) log.info('Loading bundle to ConductR..') multipart = create_multipart(log, files) response = conduct_request.post( args.dcos_mode, conductr_host(args), url, data=multipart, auth=args.conductr_auth, verify=args.server_verification_file, headers={'Content-Type': multipart.content_type}) validation.raise_for_status_inc_3xx(response) if log.is_verbose_enabled(): log.verbose(validation.pretty_json(response.text)) response_json = json.loads(response.text) bundle_id = response_json[ 'bundleId'] if args.long_ids else bundle_utils.short_id( response_json['bundleId']) if not args.no_wait: bundle_installation.wait_for_installation(response_json['bundleId'], args) cleanup_old_bundles(bundle_resolve_cache_dir, bundle_file_name, excluded=bundle_file) log.info('Bundle loaded.') if not args.disable_instructions: log.info('Start bundle with: {} run{} {}'.format( args.command, args.cli_parameters, bundle_id)) log.info('Unload bundle with: {} unload{} {}'.format( args.command, args.cli_parameters, bundle_id)) log.info('Print ConductR info with: {} info{}'.format( args.command, args.cli_parameters)) log.info('Print bundle info with: {} info{} {}'.format( args.command, args.cli_parameters, bundle_id)) if not log.is_info_enabled() and log.is_quiet_enabled(): log.quiet(response_json['bundleId']) return True
def load_v1(args): log = logging.getLogger(__name__) log.info('Retrieving bundle..') custom_settings = args.custom_settings resolve_cache_dir = args.resolve_cache_dir validate_cache_dir_permissions(resolve_cache_dir, log) bundle_file_name, bundle_file = resolver.resolve_bundle(custom_settings, resolve_cache_dir, args.bundle) configuration_file_name, configuration_file = (None, None) if args.configuration is not None: log.info('Retrieving configuration..') configuration_file_name, configuration_file = resolver.resolve_bundle_configuration(custom_settings, resolve_cache_dir, args.configuration) bundle_conf = ConfigFactory.parse_string(bundle_utils.conf(bundle_file)) overlay_bundle_conf = None if configuration_file is None else \ ConfigFactory.parse_string(bundle_utils.conf(configuration_file)) with_bundle_configurations = partial(apply_to_configurations, bundle_conf, overlay_bundle_conf) url = conduct_url.url('bundles', args) files = get_payload(bundle_file_name, bundle_file, with_bundle_configurations) if configuration_file is not None: files.append(('configuration', (configuration_file_name, open(configuration_file, 'rb')))) # TODO: Delete the bundle configuration file. # Currently, this results into a permission error on Windows. # Therefore, the deletion is disabled for now. # Issue: https://github.com/typesafehub/conductr-cli/issues/175 # if configuration_file and os.path.exists(configuration_file): # os.remove(configuration_file) log.info('Loading bundle to ConductR..') multipart = create_multipart(log, files) response = conduct_request.post(args.dcos_mode, conductr_host(args), url, data=multipart, auth=args.conductr_auth, verify=args.server_verification_file, headers={'Content-Type': multipart.content_type}) validation.raise_for_status_inc_3xx(response) if log.is_verbose_enabled(): log.verbose(validation.pretty_json(response.text)) response_json = json.loads(response.text) bundle_id = response_json['bundleId'] if args.long_ids else bundle_utils.short_id(response_json['bundleId']) if not args.no_wait: bundle_installation.wait_for_installation(response_json['bundleId'], args) cleanup_old_bundles(resolve_cache_dir, bundle_file_name, excluded=bundle_file) log.info('Bundle loaded.') if not args.disable_instructions: log.info('Start bundle with: {} run{} {}'.format(args.command, args.cli_parameters, bundle_id)) log.info('Unload bundle with: {} unload{} {}'.format(args.command, args.cli_parameters, bundle_id)) log.info('Print ConductR info with: {} info{}'.format(args.command, args.cli_parameters)) if not log.is_info_enabled() and log.is_quiet_enabled(): log.quiet(response_json['bundleId']) return True