def build_inside(input, input_args=None, substitutions=None): """ use requested input plugin to load configuration and then initiate build """ def process_keyvals(keyvals): """ ["key=val", "x=y"] -> {"key": "val", "x": "y"} """ keyvals = keyvals or [] processed_keyvals = {} for arg in keyvals: key, value = arg.split("=", 1) processed_keyvals[key] = value return processed_keyvals if not input: raise RuntimeError("No input method specified!") else: logger.debug("getting build json from input %s", input) cleaned_input_args = process_keyvals(input_args) cleaned_subs = process_keyvals(substitutions) cleaned_input_args['substitutions'] = cleaned_subs input_runner = InputPluginsRunner([{'name': input, 'args': cleaned_input_args}]) build_json = input_runner.run()[input] logger.debug("build json: %s", build_json) if not build_json: raise RuntimeError("No valid build json!") # TODO: validate json dbw = DockerBuildWorkflow(**build_json) build_result = dbw.build_docker_image() if not build_result or build_result.is_failed(): raise RuntimeError("no image built") else: logger.info("build has finished successfully \o/")
def test_substitution_on_plugins(self, tmpdir): tmpdir_path = str(tmpdir) build_json_path = os.path.join(tmpdir_path, "build.json") with open(build_json_path, 'w') as fp: json.dump( { "image": "some-image", "prebuild_plugins": [{ 'name': 'asd', 'args': { 'key': 'value1' } }] }, fp) changed_value = "value-123" runner = InputPluginsRunner([{ "name": "path", "args": { "path": build_json_path, "substitutions": { "prebuild_plugins.asd.key": changed_value } } }]) results = runner.run() assert results['path']['prebuild_plugins'][0]['args'][ 'key'] == changed_value
def test_autoinput_more_autousable(self): # mock env vars checked by both env and osv3 input plugins flexmock(os, environ={'BUILD': 'a', 'SOURCE_URI': 'b', 'OUTPUT_IMAGE': 'c', 'BUILD_JSON': 'd'}) runner = InputPluginsRunner([{'name': 'auto', 'args': {}}]) with pytest.raises(PluginFailedException) as e: runner.run() assert 'More than one usable plugin with "auto" input' in str(e) assert 'osv3, env' in str(e) or 'env, osv3' in str(e)
def build_inside(input_method, input_args=None, substitutions=None): """ use requested input plugin to load configuration and then initiate build """ def process_keyvals(keyvals): """ ["key=val", "x=y"] -> {"key": "val", "x": "y"} """ keyvals = keyvals or [] processed_keyvals = {} for arg in keyvals: key, value = arg.split("=", 1) processed_keyvals[key] = value return processed_keyvals main = __name__.split('.', 1)[0] log_encoding = get_logging_encoding(main) logger.info("log encoding: %s", log_encoding) if not input_method: raise RuntimeError("No input method specified!") logger.debug("getting build json from input %s", input_method) cleaned_input_args = process_keyvals(input_args) cleaned_input_args['substitutions'] = process_keyvals(substitutions) input_runner = InputPluginsRunner([{ 'name': input_method, 'args': cleaned_input_args }]) build_json = input_runner.run()[input_method] if isinstance(build_json, Exception): raise RuntimeError( "Input plugin raised exception: {}".format(build_json)) logger.debug("build json: %s", build_json) if not build_json: raise RuntimeError("No valid build json!") if not isinstance(build_json, dict): raise RuntimeError( "Input plugin did not return valid build json: {}".format( build_json)) dbw = DockerBuildWorkflow(**build_json) try: build_result = dbw.build_docker_image() except Exception as e: if dbw.builder: logger.info("Dockerfile used for build:\n%s", dbw.builder.original_df) logger.error('image build failed: %s', e) raise else: logger.info("Dockerfile used for build:\n%s", dbw.builder.original_df) if not build_result or build_result.is_failed(): raise RuntimeError("no image built") else: logger.info("build has finished successfully \\o/")
def test_autoinput_one_autousable(self): # mock env var for env input plugin flexmock(os, environ={'BUILD_JSON': json.dumps({'image': 'some-image'})}) runner = InputPluginsRunner([{ 'name': 'auto', 'args': { 'substitutions': {} } }]) results = runner.run() assert results == {'auto': {'image': 'some-image'}}
def test_substitution(self, tmpdir): tmpdir_path = str(tmpdir) build_json_path = os.path.join(tmpdir_path, "build.json") with open(build_json_path, 'w') as fp: json.dump({ "image": "some-image" }, fp) changed_image_name = "changed-image-name" runner = InputPluginsRunner([{"name": "path", "args": { "path": build_json_path, "substitutions": { "image": changed_image_name}}}]) results = runner.run() assert results['path']['image'] == changed_image_name
def build_inside(input_method, input_args=None, substitutions=None): """ use requested input plugin to load configuration and then initiate build """ def process_keyvals(keyvals): """ ["key=val", "x=y"] -> {"key": "val", "x": "y"} """ keyvals = keyvals or [] processed_keyvals = {} for arg in keyvals: key, value = arg.split("=", 1) processed_keyvals[key] = value return processed_keyvals main = __name__.split('.', 1)[0] log_encoding = get_logging_encoding(main) logger.info("log encoding: %s", log_encoding) if not input_method: raise RuntimeError("No input method specified!") logger.debug("getting build json from input %s", input_method) cleaned_input_args = process_keyvals(input_args) cleaned_input_args['substitutions'] = process_keyvals(substitutions) input_runner = InputPluginsRunner([{'name': input_method, 'args': cleaned_input_args}]) build_json = input_runner.run()[input_method] if isinstance(build_json, Exception): raise RuntimeError("Input plugin raised exception: {}".format(build_json)) logger.debug("build json: %s", build_json) if not build_json: raise RuntimeError("No valid build json!") if not isinstance(build_json, dict): raise RuntimeError("Input plugin did not return valid build json: {}".format(build_json)) dbw = DockerBuildWorkflow(**build_json) try: build_result = dbw.build_docker_image() except Exception as e: logger.error('image build failed: %s', e) raise else: if not build_result or build_result.is_failed(): raise RuntimeError("no image built") else: logger.info("build has finished successfully \\o/")
def build_inside(input_method, input_args=None, substitutions=None): """ use requested input plugin to load configuration and then initiate build """ def process_keyvals(keyvals): """ ["key=val", "x=y"] -> {"key": "val", "x": "y"} """ keyvals = keyvals or [] processed_keyvals = {} for arg in keyvals: key, value = arg.split("=", 1) processed_keyvals[key] = value return processed_keyvals main = __name__.split('.', 1)[0] log_encoding = get_logging_encoding(main) logger.info("log encoding: %s", log_encoding) if not input_method: raise RuntimeError("No input method specified!") else: logger.debug("getting build json from input %s", input_method) cleaned_input_args = process_keyvals(input_args) cleaned_subs = process_keyvals(substitutions) cleaned_input_args['substitutions'] = cleaned_subs input_runner = InputPluginsRunner([{ 'name': input_method, 'args': cleaned_input_args }]) build_json = input_runner.run()[input_method] logger.debug("build json: %s", build_json) if not build_json: raise RuntimeError("No valid build json!") # TODO: validate json dbw = DockerBuildWorkflow(**build_json) build_result = dbw.build_docker_image() if not build_result or build_result.is_failed(): raise RuntimeError("no image built") else: logger.info("build has finished successfully \o/")
def test_substitution_on_plugins(self, tmpdir): tmpdir_path = str(tmpdir) build_json_path = os.path.join(tmpdir_path, "build.json") with open(build_json_path, 'w') as fp: json.dump({ "image": "some-image", "prebuild_plugins": [{ 'name': 'asd', 'args': { 'key': 'value1' } }] }, fp) changed_value = "value-123" runner = InputPluginsRunner([{"name": "path", "args": {"path": build_json_path, "substitutions": { "prebuild_plugins.asd.key": changed_value}}}]) results = runner.run() assert results['path']['prebuild_plugins'][0]['args']['key'] == changed_value
def test_autoinput_one_autousable(self): # mock env var for env input plugin flexmock(os, environ={'BUILD_JSON': json.dumps({'image': 'some-image'})}) runner = InputPluginsRunner([{'name': 'auto', 'args': {'substitutions': {}}}]) results = runner.run() assert results == {'auto': {'image': 'some-image'}}
def test_autoinput_no_autousable(self): flexmock(os, environ={}) runner = InputPluginsRunner([{'name': 'auto', 'args': {}}]) with pytest.raises(PluginFailedException) as e: runner.run() assert 'No autousable input plugin' in str(e)