def run(self, parser, namespace, values, dest, option_string=None):
        """
        Execution of the action.

        :name parser: The argument parser in use.
        :type parser: argparse.ArgumentParser
        :name namespace: The namespace for parsed args.
        :type namespace: argparse.Namespace
        :name values: Values for the action.
        :type values: mixed
        :name option_string: Option string.
        :type option_string: str or None
        :raises: subprocess.CalledProcessError
        """
        hostfs_dirs = []
        add_files = {}
        for item in namespace.add_file:
            local, host = item.split('=')
            hostfs_dirs.append(os.path.dirname(host))
            add_files[local] = host

        output = util.mkdir(namespace.output)
        with open(os.path.sep.join([output, 'Dockerfile']), 'w') as dockerfile:
            loader = jinja2.PackageLoader('system_buildah')
            rendered = loader.load(
                jinja2.Environment(), 'Dockerfile.j2').render(
                    from_base=namespace.from_base, name=values,
                    maintainer=namespace.maintainer,
                    license_name=namespace.license, summary=namespace.summary,
                    version=namespace.version, help_text=namespace.help_text,
                    architecture=namespace.architecture, scope=namespace.scope,
                    add_files=add_files, hostfs_dirs=set(hostfs_dirs))
            dockerfile.write(rendered)
예제 #2
0
    def tar(self, namespace, output):
        """
        Exports a specific image to a tar file.

        :param namespace: Namespace passed in via CLI.
        :type namespace: argparse.Namespace
        :param output: The name of the file to output.
        :type output: str
        :raises: subprocess.CalledProcessError
        """
        logging.debug('buildah tar will be used')
        logging.warn(
            'The tar result may not be usable '
            'until this manager is stablized!')
        out = self._normalize_filename(output)
        util.mkdir(out)  # Ensure the directory exists
        # NOTE: We _expand_path on out as buildah requires a full path
        full_path = util._expand_path(out)
        command = ['buildah', 'push', output, 'dir:/{}'.format(full_path)]
        # Export the layers
        subprocess.check_call(command)
        # Create a tar file from the layers
        subprocess.check_call(['tar', '-cf', '{}.tar'.format(out), out])
예제 #3
0
    def __call__(self, parser, namespace, values, dest, option_string=None):
        """
        Execution of the action.

        :name parser: The argument parser in use.
        :type parser: argparse.ArgumentParser
        :name namespace: The namespace for parsed args.
        :type namespace: argparse.Namespace
        :name values: Values for the action.
        :type values: mixed
        :name option_string: Option string.
        :type option_string: str or None
        :raises: subprocess.CalledProcessError
        """
        output = util.mkdir(values)
        manifest_struct = {
            "version": "1.0",
            "defaultValues": {},
        }
        for item in namespace.default:
            try:
                k, v = item.split('=')
                manifest_struct['defaultValues'][k] = v
            except ValueError as error:
                parser._print_message(
                    '{} not in a=b format. Skipping...'.format(item))

        # Generate the manifest.json
        manifest_out = os.path.sep.join([output, 'manifest.json'])
        with open(manifest_out, 'w') as manifest:
            json.dump(manifest_struct, manifest, indent='    ')

        # Generate the service.template
        service_out = os.path.sep.join([output, 'service.template'])
        with open(service_out, 'w') as service:
            loader = jinja2.PackageLoader('system_buildah')
            rendered = loader.load(jinja2.Environment(),
                                   'service.template.j2').render(
                                       description=namespace.description)
            service.write(rendered)

        # Generate config.json using ocitools
        temp_dir = tempfile.mkdtemp()
        with util.pushd(temp_dir):
            try:
                ocitools_cmd = ['ocitools', 'generate', "--read-only"]
                for item in namespace.config.split(' '):
                    try:
                        ocitools_cmd = ocitools_cmd + item.split('=')
                    except ValueError as error:
                        parser._print_message(
                            '{} not in a=b format. Skipping...'.format(item))
                subprocess.check_call(ocitools_cmd)
                config_out = os.path.sep.join([output, 'config.json.template'])
                try:
                    with open('config.json', 'r') as config_file:
                        configuration = json.load(config_file)
                        configuration['process']['terminal'] = False
                    with open(config_out, 'w') as dest:
                        json.dump(configuration,
                                  dest,
                                  indent=8,
                                  sort_keys=True)
                finally:
                    os.unlink('config.json')
            finally:
                shutil.rmtree(temp_dir)
예제 #4
0
def test_mkdir(tmpdir):
    """Verify mkdir creates directories"""
    path = tmpdir.mkdir('mkdir').dirname
    assert util.mkdir(path) == path  # New directory
    assert util.mkdir(path) == path  # Already exists