From ccc1c20fef0cf1ca204a6623b4575d98d38f6a97 Mon Sep 17 00:00:00 2001 From: Dominika Hodovska Date: Jul 18 2017 08:41:04 +0000 Subject: Implement module2dockerfile --- diff --git a/README.md b/README.md index 2f7413b..1e04f70 100644 --- a/README.md +++ b/README.md @@ -26,13 +26,30 @@ Example usage: /path/to/Dockerfile OpenShift template is generated here: /tmp/tmpM0teUC/openshift-template.yml ``` -### How to run tests + +## modtools module2dockerfile + +The tool exists to ease creation of module related Dockerfiles. It pre-filles +the information from modulemd file, but be aware, the generated Dockerfile +needs to be manualy extended by other info (configuration, volumes, etc.). + +### How to use modtools module2dockerfile +Run the command with following parameters: +```bash +./modtools module2dockerfile --template MODULEMD_FILE +``` + where parameters mean: + * template - base for Dockerfile, if you don't want otherwise use this file: https://github.com/container-images/container-image-template/blob/master/Dockerfile.template + * modulemd.yaml - path to the modulemd file + + +## How to run tests In order to run tests, run command: `py.test-2.7 tests/` -### Modulemd creation +## Modulemd creation modtools rpm2module script creates modulemd file from package names. Output is written in modulemd-output.yaml file (multiple packages as input) or in file named after package name (single package as input). diff --git a/modularity/cli.py b/modularity/cli.py index 4bcd9a9..bc749d8 100644 --- a/modularity/cli.py +++ b/modularity/cli.py @@ -3,6 +3,7 @@ import argparse from modularity.module_generator import ModuleGenerator from modularity.oc_template import OpenShiftTemplateGenerator +from modularity.mod2dockerfile import ModulemdDockerfileGenerator class ModtoolsCLI(object): @@ -39,6 +40,29 @@ class ModtoolsCLI(object): help="Specify Dockerfile name. Default is Dockerfile." ) + parser_module2dockerfile = subparsers.add_parser( + 'module2dockerfile', help="Generates dockerfile from modulemd file", + description="Creates Dockerfile with suggestions and pre-filled values" + ) + + parser_module2dockerfile.add_argument( + "modulemd_file", + metavar='MODULEMD_FILE', + help='Specify path to modulemd file' + ) + + parser_module2dockerfile.add_argument( + "--output", + nargs='?', + help='Specify custom output file.' + ) + + parser_module2dockerfile.add_argument( + "--template", + help='Specify custom Dockerfile template', + required=True + ) + return parser def __init__(self, args=None): @@ -65,6 +89,11 @@ class ModtoolsCLIHelper(object): if cli.args.cmd_name == 'docker2openshift': otg = OpenShiftTemplateGenerator(cli.args) otg.run() + + if cli.args.cmd_name == 'module2dockerfile': + mtd = ModulemdDockerfileGenerator(cli.args) + mtd.run() + except KeyboardInterrupt: print('\nInterrupted by user') except Exception as e: diff --git a/modularity/mod2dockerfile.py b/modularity/mod2dockerfile.py new file mode 100644 index 0000000..dafee67 --- /dev/null +++ b/modularity/mod2dockerfile.py @@ -0,0 +1,49 @@ +import modulemd +import os +import tempfile +import shutil +from string import Template + + +class PercentTemplate(Template): + delimiter = '%%' + + +class ModulemdDockerfileGenerator(object): + + def __init__(self, args=None): + self.dockerfile_path = args.output + self.modulemd_path = args.modulemd_file + self.template_path=args.template + + def load_modulemd(self): + self.mmd = modulemd.ModuleMetadata() + self.mmd.load(self.modulemd_path) + + self.values = {'NAME': self.mmd.name,'VERSION': self.mmd.version, + 'DESCRIPTION': self.mmd.description, 'SUMMARY': self.mmd.summary, + 'API_PACKAGES': ' '.join(self.mmd.api.rpms)} + + def generate_dockerfile(self): + tmplFile = open(self.template_path) + tmpl = PercentTemplate(tmplFile.read()) + self.result = tmpl.safe_substitute(self.values) + + def save_result(self): + if self.dockerfile_path is not None: + output_file = self.dockerfile_path + else: + tmp_dir = tempfile.mkdtemp() + if os.path.isdir(tmp_dir): + shutil.rmtree(tmp_dir) + os.makedirs(tmp_dir) + output_file = os.path.join(tmp_dir, os.path.basename('Dockerfile')) + + with open(output_file, 'w') as f: + f.write(self.result) + print("Modulemd template is generated here: %s" % (output_file)) + + def run(self): + self.load_modulemd() + self.generate_dockerfile() + self.save_result()