本文整理汇总了Python中dockerfile_parse.DockerfileParser类的典型用法代码示例。如果您正苦于以下问题:Python DockerfileParser类的具体用法?Python DockerfileParser怎么用?Python DockerfileParser使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了DockerfileParser类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_adddockerfile_todest
def test_adddockerfile_todest(tmpdir):
df_content = """
FROM fedora
RUN yum install -y python-django
CMD blabla"""
df = DockerfileParser(str(tmpdir))
df.content = df_content
tasker = DockerTasker()
workflow = DockerBuildWorkflow(MOCK_SOURCE, 'test-image')
workflow.builder = X
workflow.builder.df_path = df.dockerfile_path
workflow.builder.df_dir = str(tmpdir)
runner = PreBuildPluginsRunner(
tasker,
workflow,
[{
'name': AddDockerfilePlugin.key,
'args': {'nvr': 'jboss-eap-6-docker-6.4-77',
'destdir': '/usr/share/doc/'}
}]
)
runner.run()
assert AddDockerfilePlugin.key is not None
expected_output = """
FROM fedora
RUN yum install -y python-django
ADD Dockerfile-jboss-eap-6-docker-6.4-77 /usr/share/doc/Dockerfile-jboss-eap-6-docker-6.4-77
CMD blabla"""
assert df.content == expected_output
示例2: __init__
def __init__(self, tasker, workflow, nvr=None, destdir="/root/buildinfo/",
use_final_dockerfile=False):
"""
constructor
:param tasker: DockerTasker instance
:param workflow: DockerBuildWorkflow instance
:param nvr: name-version-release, will be appended to Dockerfile-.
If not specified, try to get it from Name, Version, Release labels.
:param destdir: directory in the image to put Dockerfile-N-V-R into
:param use_final_dockerfile: bool, when set to True, uses final version of processed dockerfile,
when set to False, uses Dockerfile from time when this plugin was executed
"""
# call parent constructor
super(AddDockerfilePlugin, self).__init__(tasker, workflow)
self.use_final_dockerfile = use_final_dockerfile
if nvr is None:
labels = DockerfileParser(self.workflow.builder.df_path).labels
name = labels.get('Name')
version = labels.get('Version')
release = labels.get('Release')
if name is None or version is None or release is None:
raise ValueError("You have to specify either nvr arg or Name/Version/Release labels.")
nvr = "{0}-{1}-{2}".format(name, version, release)
nvr = nvr.replace("/", "-")
self.df_name = '{0}-{1}'.format(DOCKERFILE_FILENAME, nvr)
self.df_dir = destdir
self.df_path = os.path.join(self.df_dir, self.df_name)
# we are not using final dockerfile, so let's copy current snapshot
if not self.use_final_dockerfile:
local_df_path = os.path.join(self.workflow.builder.df_dir, self.df_name)
shutil.copy2(self.workflow.builder.df_path, local_df_path)
示例3: test_adddockerfile_nvr_from_labels2
def test_adddockerfile_nvr_from_labels2(tmpdir):
df_content = """
FROM fedora
RUN yum install -y python-django
CMD blabla"""
df = DockerfileParser(str(tmpdir))
df.content = df_content
tasker = DockerTasker()
workflow = DockerBuildWorkflow(MOCK_SOURCE, 'test-image')
flexmock(workflow, base_image_inspect={"Config": {"Labels": {}}})
workflow.builder = X
workflow.builder.df_path = df.dockerfile_path
workflow.builder.df_dir = str(tmpdir)
runner = PreBuildPluginsRunner(
tasker,
workflow,
[{
'name': AddLabelsPlugin.key,
'args': {'labels': {'Name': 'jboss-eap-6-docker',
'Version': '6.4',
'Release': '77'}}
},
{
'name': AddDockerfilePlugin.key
}]
)
runner.run()
assert AddDockerfilePlugin.key is not None
assert "ADD Dockerfile-jboss-eap-6-docker-6.4-77 /root/buildinfo/Dockerfile-jboss-eap-6-docker-6.4-77" in df.content
示例4: __init__
def __init__(self, filename):
DockerfileParser.__init__(self, cache_content=True)
if self.is_url(filename) is not None:
response = urllib.request.urlopen(filename)
if self.is_content_type_plain_text(response):
self.content = response.read().decode('utf-8')
App._dockerfile['is_remote'] = True
App._dockerfile['url'] = filename
else:
print('ERROR: file format not supported. Plain text expected\n')
exit(-1)
elif os.path.isfile(filename):
self.dockerfile_path = filename
self.content = open(filename, encoding='utf-8').read()
App._dockerfile['is_remote'] = False
elif self.is_github_repo(filename):
filename = 'https://raw.githubusercontent.com/' + filename + '/master/Dockerfile'
self.content = urllib.request.urlopen(filename).read().decode('utf-8')
App._dockerfile['is_remote'] = True
App._dockerfile['url'] = filename
else:
print('ERROR: file format not supported\n')
exit(-1)
self.commands = self.dict_to_command_object(self.structure)
示例5: test_adddockerfile_plugin
def test_adddockerfile_plugin(tmpdir):
df_content = """
FROM fedora
RUN yum install -y python-django
CMD blabla"""
df = DockerfileParser(str(tmpdir))
df.content = df_content
tasker = DockerTasker()
workflow = DockerBuildWorkflow(MOCK_SOURCE, 'test-image')
workflow.builder = X
workflow.builder.df_path = df.dockerfile_path
workflow.builder.df_dir = str(tmpdir)
runner = PreBuildPluginsRunner(
tasker,
workflow,
[{
'name': AddDockerfilePlugin.key,
'args': {'nvr': 'rhel-server-docker-7.1-20'}
}]
)
runner.run()
assert AddDockerfilePlugin.key is not None
expected_output = """
FROM fedora
RUN yum install -y python-django
ADD Dockerfile-rhel-server-docker-7.1-20 /root/buildinfo/Dockerfile-rhel-server-docker-7.1-20
CMD blabla"""
assert df.content == expected_output
示例6: test_add_labels_plugin
def test_add_labels_plugin(tmpdir, labels_conf_base, labels_conf, dont_overwrite, expected_output):
df = DockerfileParser(str(tmpdir))
df.content = DF_CONTENT
tasker = DockerTasker()
workflow = DockerBuildWorkflow(MOCK_SOURCE, 'test-image')
setattr(workflow, 'builder', X)
flexmock(workflow, base_image_inspect=labels_conf_base)
setattr(workflow.builder, 'df_path', df.dockerfile_path)
runner = PreBuildPluginsRunner(
tasker,
workflow,
[{
'name': AddLabelsPlugin.key,
'args': {'labels': labels_conf, "dont_overwrite": dont_overwrite}
}]
)
if isinstance(expected_output, RuntimeError):
with pytest.raises(RuntimeError):
runner.run()
else:
runner.run()
assert AddLabelsPlugin.key is not None
assert df.content in expected_output
示例7: run
def run(self):
dockerfile = DockerfileParser(self.workflow.builder.df_path)
image_name = ImageName.parse(dockerfile.baseimage)
if image_name.namespace != 'koji' or image_name.repo != 'image-build' :
self.log.info('Base image not supported: %s', dockerfile.baseimage)
return
image_build_conf = image_name.tag or 'image-build.conf'
self.session = create_koji_session(self.koji_hub, self.koji_auth_info)
task_id, filesystem_regex = self.build_filesystem(image_build_conf)
task = TaskWatcher(self.session, task_id, self.poll_interval)
task.wait()
if task.failed():
raise RuntimeError('Create filesystem task failed: {}'
.format(task_id))
filesystem = self.download_filesystem(task_id, filesystem_regex)
base_image = self.import_base_image(filesystem)
dockerfile.baseimage = base_image
return base_image
示例8: test_assertlabels_plugin
def test_assertlabels_plugin(tmpdir, df_content, req_labels, expected):
df = DockerfileParser(str(tmpdir))
df.content = df_content
tasker = DockerTasker()
workflow = DockerBuildWorkflow(MOCK_SOURCE, 'test-image')
workflow.builder = X
workflow.builder.df_path = df.dockerfile_path
workflow.builder.df_dir = str(tmpdir)
runner = PreBuildPluginsRunner(
tasker,
workflow,
[{
'name': AssertLabelsPlugin.key,
'args': {'required_labels': req_labels}
}]
)
assert AssertLabelsPlugin.key is not None
if isinstance(expected, PluginFailedException):
with pytest.raises(PluginFailedException):
runner.run()
else:
runner.run()
示例9: test_yuminject_plugin_notwrapped
def test_yuminject_plugin_notwrapped(tmpdir):
df_content = """\
FROM fedora
RUN yum install -y python-django
CMD blabla"""
df = DockerfileParser(str(tmpdir))
df.content = df_content
tasker, workflow = prepare(df.dockerfile_path)
metalink = 'https://mirrors.fedoraproject.org/metalink?repo=fedora-$releasever&arch=$basearch'
workflow.files[os.path.join(YUM_REPOS_DIR, DEFAULT_YUM_REPOFILE_NAME)] = render_yum_repo(OrderedDict(
(('name', 'my-repo'),
('metalink', metalink),
('enabled', 1),
('gpgcheck', 0)),
))
runner = PreBuildPluginsRunner(tasker, workflow, [{
'name': InjectYumRepoPlugin.key,
'args': {
"wrap_commands": False
}
}])
runner.run()
assert InjectYumRepoPlugin.key is not None
expected_output = r"""FROM fedora
ADD atomic-reactor-repos/* '/etc/yum.repos.d/'
RUN yum install -y python-django
CMD blabla
RUN rm -f '/etc/yum.repos.d/atomic-reactor-injected.repo'
"""
assert expected_output == df.content
示例10: test_add_labels_plugin_generated
def test_add_labels_plugin_generated(tmpdir, docker_tasker, auto_label, value_re_part):
df = DockerfileParser(str(tmpdir))
df.content = DF_CONTENT
if MOCK:
mock_docker()
workflow = DockerBuildWorkflow(MOCK_SOURCE, 'test-image')
setattr(workflow, 'builder', X)
flexmock(workflow, source=MockSource())
flexmock(workflow, base_image_inspect=LABELS_CONF_BASE)
setattr(workflow.builder, 'df_path', df.dockerfile_path)
runner = PreBuildPluginsRunner(
docker_tasker,
workflow,
[{
'name': AddLabelsPlugin.key,
'args': {'labels': {}, "dont_overwrite": [], "auto_labels": [auto_label],
'aliases': {'Build_Host': 'com.redhat.build-host'}}
}]
)
runner.run()
assert re.match(value_re_part, df.labels[auto_label])
示例11: test_add_labels_plugin
def test_add_labels_plugin(tmpdir, docker_tasker,
df_content, labels_conf_base, labels_conf, dont_overwrite, aliases,
expected_output, caplog):
df = DockerfileParser(str(tmpdir))
df.content = df_content
if MOCK:
mock_docker()
workflow = DockerBuildWorkflow(MOCK_SOURCE, 'test-image')
setattr(workflow, 'builder', X)
flexmock(workflow, base_image_inspect=labels_conf_base)
setattr(workflow.builder, 'df_path', df.dockerfile_path)
runner = PreBuildPluginsRunner(
docker_tasker,
workflow,
[{
'name': AddLabelsPlugin.key,
'args': {
'labels': labels_conf,
'dont_overwrite': dont_overwrite,
'auto_labels': [],
'aliases': aliases,
}
}]
)
runner.run()
if isinstance(expected_output, RuntimeError):
assert "plugin 'add_labels_in_dockerfile' raised an exception: RuntimeError" in caplog.text()
else:
assert AddLabelsPlugin.key is not None
assert df.content in expected_output
示例12: test_returndockerfile_plugin
def test_returndockerfile_plugin(tmpdir):
df_content = """
FROM fedora
RUN yum install -y python-django
CMD blabla"""
df = DockerfileParser(str(tmpdir))
df.content = df_content
tasker = DockerTasker()
workflow = DockerBuildWorkflow(MOCK_SOURCE, 'test-image')
workflow.builder = X
workflow.builder.df_path = df.dockerfile_path
workflow.builder.df_dir = str(tmpdir)
runner = PreBuildPluginsRunner(
tasker,
workflow,
[{
'name': CpDockerfilePlugin.key
}]
)
runner.run()
assert CpDockerfilePlugin.key is not None
assert workflow.prebuild_results.get(CpDockerfilePlugin.key, "") == df_content
示例13: run
def run(self):
"""
run the plugin
"""
# dict comprehension is syntax error on 2.6
yum_repos = {}
for key, value in self.workflow.files.items():
if key.startswith(YUM_REPOS_DIR):
yum_repos[key] = value
if self.wrap_commands:
wrap_yum_commands(yum_repos, self.workflow.builder.df_path)
else:
if not yum_repos:
return
# absolute path in containers -> relative path within context
repos_host_cont_mapping = {}
host_repos_path = os.path.join(self.workflow.builder.df_dir, RELATIVE_REPOS_PATH)
self.log.info("creating directory for yum repos: %s", host_repos_path)
os.mkdir(host_repos_path)
for repo, repo_content in self.workflow.files.items():
repo_basename = os.path.basename(repo)
repo_relative_path = os.path.join(RELATIVE_REPOS_PATH, repo_basename)
repo_host_path = os.path.join(host_repos_path, repo_basename)
self.log.info("writing repo to '%s'", repo_host_path)
with open(repo_host_path, "wb") as fp:
fp.write(repo_content.encode("utf-8"))
repos_host_cont_mapping[repo] = repo_relative_path
# Find out the USER inherited from the base image
inspect = self.workflow.builder.inspect_base_image()
inherited_user = inspect['Config'].get('User', '')
df = DockerfileParser(self.workflow.builder.df_path)
df.lines = add_yum_repos_to_dockerfile(repos_host_cont_mapping,
df, inherited_user)
示例14: test_constructor_cache
def test_constructor_cache(self, tmpdir):
tmpdir_path = str(tmpdir.realpath())
df1 = DockerfileParser(tmpdir_path)
df1.lines = ["From fedora:latest\n", "LABEL a b\n"]
df2 = DockerfileParser(tmpdir_path, True)
assert df2.cached_content
示例15: test_adddockerfile_nvr_from_labels
def test_adddockerfile_nvr_from_labels(tmpdir):
df_content = """
FROM fedora
RUN yum install -y python-django
LABEL Name="jboss-eap-6-docker" "Version"="6.4" "Release"=77
CMD blabla"""
df = DockerfileParser(str(tmpdir))
df.content = df_content
tasker = DockerTasker()
workflow = DockerBuildWorkflow(MOCK_SOURCE, 'test-image')
workflow.builder = X
workflow.builder.df_path = df.dockerfile_path
workflow.builder.df_dir = str(tmpdir)
runner = PreBuildPluginsRunner(
tasker,
workflow,
[{
'name': AddDockerfilePlugin.key
}]
)
runner.run()
assert AddDockerfilePlugin.key is not None
assert "ADD Dockerfile-jboss-eap-6-docker-6.4-77 /root/buildinfo/Dockerfile-jboss-eap-6-docker-6.4-77" in df.content