#39 Implement module2dockerfile
Merged 6 years ago by phracek. Opened 6 years ago by dhodovsk.
modularity/ dhodovsk/modularity-tools module2dockerfile  into  master

file modified
+19 -2
@@ -26,13 +26,30 @@ 

  /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 <dockerfile_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).

file modified
+29
@@ -3,6 +3,7 @@ 

  

  from modularity.module_generator import ModuleGenerator

  from modularity.oc_template import OpenShiftTemplateGenerator

+ from modularity.mod2dockerfile import ModulemdDockerfileGenerator

  

  

  class ModtoolsCLI(object):
@@ -39,6 +40,29 @@ 

              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 @@ 

              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:

@@ -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()

no initial comment

@msuchy please check if this is what you had in mind.

The script works properly. As improvement I would hardcode github URL with raw string which downloads the latest template.

Pull-Request has been merged by phracek

6 years ago