def post_license(args, license_file): """ Post license file to ConductR. :param args: input args obtained from argparse :param license_file: the path to license file """ url = conduct_url.url('license', args) response = conduct_request.post(args.dcos_mode, conductr_host(args), url, data=open(license_file, 'rb'), auth=args.conductr_auth, verify=args.server_verification_file) if response.status_code == 503: return False else: validation.raise_for_status_inc_3xx(response) return True
def test_post(self): enriched_args = {'enriched': 'args'} enrich_args_mock = MagicMock(return_value=enriched_args) dcos_http_response = 'dcos_http_response' dcos_http_mock = MagicMock(return_value=dcos_http_response) requests_http_response = 'requests_http_response' requests_http_mock = MagicMock(return_value=requests_http_response) with patch('conductr_cli.conduct_request.enrich_args', enrich_args_mock), \ patch('dcos.http.post', dcos_http_mock), \ patch('requests.post', requests_http_mock): result = conduct_request.post(self.dcos_mode, self.host, self.url, **self.kwargs) self.assertEqual(requests_http_response, result) enrich_args_mock.assert_called_with(self.host, **self.kwargs) requests_http_mock.assert_called_with(self.url, **enriched_args) dcos_http_mock.assert_not_called()
def test_post(self): enriched_args = {'enriched': 'args'} enrich_args_mock = MagicMock(return_value=enriched_args) dcos_http_response = 'dcos_http_response' dcos_http_mock = MagicMock(return_value=dcos_http_response) requests_http_response = 'requests_http_response' requests_http_mock = MagicMock(return_value=requests_http_response) with patch('conductr_cli.conduct_request.enrich_args', enrich_args_mock), \ patch('dcos.http.post', dcos_http_mock), \ patch('requests.post', requests_http_mock): result = conduct_request.post(self.dcos_mode, self.host, self.url, **self.kwargs) self.assertEqual(requests_http_response, result) enrich_args_mock.assert_called_with(self.host, **self.kwargs) requests_http_mock.assert_called_with(self.url, **enriched_args) dcos_http_mock.assert_not_called()
def deploy(args): """`conduct deploy` command""" log = logging.getLogger(__name__) bintray_webhook_secret = custom_settings.load_bintray_webhook_secret(args) if not bintray_webhook_secret: log.error( 'The deploy command requires bintray webhook secret to be configured' ) log.error('Add the following configuration to the ' 'custom settings file {}'.format( vars(args).get('custom_settings_file'))) log.error( ' conductr.continuous-delivery.bintray-webhook-secret = "configured-continuous-delivery-secret"' ) return resolved_version = resolver.resolve_bundle_version(args.custom_settings, args.bundle) if not resolved_version: log.error('Unable to resolve bundle {}'.format(args.bundle)) return # Build Continuous Delivery URL using our resolver mechanism deploy_uri = resolver.continuous_delivery_uri(args.custom_settings, resolved_version) if not deploy_uri: log.error('Unable to form Continuous Delivery uri for {}'.format( args.bundle)) return # Confirm with the user unless auto deploy is enabled accepted = True if args.auto_deploy else request_deploy_confirmation( resolved_version, args) if not accepted: log.info('Abort') return url = conduct_url.url(deploy_uri, args) # JSON Payload for deployment request payload = { 'package': resolved_version['package_name'], 'version': '{}-{}'.format(resolved_version['compatibility_version'], resolved_version['digest']) } # HTTP headers required for deployment request hmac_digest = bundle_deploy.generate_hmac_signature( bintray_webhook_secret, resolved_version['package_name']) headers = {'X-Bintray-WebHook-Hmac': hmac_digest} response = conduct_request.post(args.dcos_mode, conductr_host(args), url, json=payload, headers=headers, auth=args.conductr_auth, verify=args.server_verification_file) validation.raise_for_status_inc_3xx(response) deployment_id = response.text log.info('Deployment request sent.') log.info('Deployment id {}'.format(deployment_id)) if not args.no_wait: bundle_deploy.wait_for_deployment_complete(deployment_id, resolved_version, args) 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 deploy(args): """`conduct deploy` command""" log = logging.getLogger(__name__) bintray_webhook_secret = custom_settings.load_bintray_webhook_secret(args) if not bintray_webhook_secret: log.error('The deploy command requires bintray webhook secret to be configured') log.error('Add the following configuration to the ' 'custom settings file {}'.format(vars(args).get('custom_settings_file'))) log.error(' conductr.continuous-delivery.bintray-webhook-secret = "configured-continuous-delivery-secret"') return resolved_version = resolver.resolve_bundle_version(args.custom_settings, args.bundle) if not resolved_version: log.error('Unable to resolve bundle {}'.format(args.bundle)) return # Build Continuous Delivery URL using our resolver mechanism deploy_uri = resolver.continuous_delivery_uri(args.custom_settings, resolved_version) if not deploy_uri: log.error('Unable to form Continuous Delivery uri for {}'.format(args.bundle)) return # Confirm with the user unless auto deploy is enabled accepted = True if args.auto_deploy else request_deploy_confirmation(resolved_version, args) if not accepted: log.info('Abort') return url = conduct_url.url(deploy_uri, args) # JSON Payload for deployment request payload = { 'package': resolved_version['package_name'], 'version': '{}-{}'.format(resolved_version['compatibility_version'], resolved_version['digest']) } # HTTP headers required for deployment request hmac_digest = bundle_deploy.generate_hmac_signature(bintray_webhook_secret, resolved_version['package_name']) headers = { 'X-Bintray-WebHook-Hmac': hmac_digest } response = conduct_request.post(args.dcos_mode, conductr_host(args), url, json=payload, headers=headers, auth=args.conductr_auth, verify=args.server_verification_file) validation.raise_for_status_inc_3xx(response) deployment_id = response.text log.info('Deployment request sent.') log.info('Deployment id {}'.format(deployment_id)) if not args.no_wait: bundle_deploy.wait_for_deployment_complete(deployment_id, resolved_version, args) 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