This repository has been archived by the owner on May 4, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Created plugin for getting artifacts from local dir #159
Open
vbusch
wants to merge
2
commits into
jboss-dockerfiles:develop
Choose a base branch
from
vbusch:master
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import glob | ||
import os | ||
import shutil | ||
|
||
from dogen.plugin import Plugin | ||
|
||
class Artifact(Plugin): | ||
@staticmethod | ||
def info(): | ||
return "artifact", "Support overriding the sources from an artifact directory" | ||
|
||
@staticmethod | ||
def inject_args(parser): | ||
parser.add_argument('--artifact-local-enable', action='store_true', help='Uses local artifacts from the provided artifacts directory') | ||
parser.add_argument('--artifact-local-dir', default='artifacts', help='Provides path to local artifacts directory') | ||
|
||
return parser | ||
|
||
def __init__(self, dogen, args): | ||
super(Artifact, self).__init__(dogen, args) | ||
self.artifact_files = [] | ||
|
||
def before_sources(self): | ||
|
||
if not self.artifact_files: | ||
return | ||
|
||
if not os.path.exists(self.output): | ||
self.log.warn("build directory does not exist") | ||
return | ||
self.log.info("Copying local artifact files from '%s'" % self.args.artifact_local_dir) | ||
|
||
for f in sorted(self.artifact_files): | ||
self.log.debug("Copying local artifact %s" % os.path.basename(f)) | ||
shutil.copy2(f, self.output) | ||
|
||
self.log.debug("Done.") | ||
|
||
def prepare(self, cfg): | ||
|
||
if not self.args.artifact_local_enable: | ||
return | ||
|
||
if not self.args.artifact_local_dir: | ||
self.log.debug("No directory with local artifact files specified, skipping artifact plugin") | ||
return | ||
|
||
if not os.path.isdir(self.args.artifact_local_dir): | ||
self.log.debug("Provided path to directory with local artifact files does not exists or is not a directory") | ||
return | ||
|
||
self.artifact_files = glob.glob(os.path.join(self.args.artifact_local_dir, "*")) | ||
|
||
if not self.artifact_files: | ||
self.log.debug("No local artifacts found") | ||
return |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import argparse | ||
import mock | ||
import os | ||
import tempfile | ||
import unittest | ||
import shutil | ||
import logging | ||
import sys | ||
|
||
from dogen.plugins.artifact import Artifact | ||
from dogen.generator import Generator | ||
|
||
class MockDogen(): | ||
def __init__(self, log, cfg={}): | ||
self.log = log | ||
self.descriptor = 0 | ||
self.output = "" | ||
self.cfg = cfg | ||
|
||
class TestArtifactPlugin(unittest.TestCase): | ||
def setUp(self): | ||
self.workdir = tempfile.mkdtemp(prefix='test_artifact_plugin') | ||
self.descriptor = tempfile.NamedTemporaryFile(delete=False) | ||
self.target_dir = os.path.join(self.workdir, "target") | ||
if not os.path.exists(self.target_dir): | ||
os.makedirs(self.target_dir) | ||
self.artifact_dir = os.path.join(self.workdir, "artifact") | ||
if not os.path.exists(self.artifact_dir): | ||
os.makedirs(self.artifact_dir) | ||
|
||
self.log = mock.Mock() | ||
|
||
def teardown(self): | ||
shutil.rmtree(self.workdir) | ||
|
||
def write_config(self, config): | ||
with self.descriptor as f: | ||
f.write(config.encode()) | ||
|
||
def prepare_dogen(self, artifact_local_dir=None, output_dir=None): | ||
self.write_config("version: '1'\ncmd:\nfrom: scratch\nname: someimage\n") | ||
args = argparse.Namespace(path=self.descriptor.name, output=output_dir, without_sources=None, | ||
template=None, scripts_path=None, additional_script=None, | ||
skip_ssl_verification=None, repo_files_dir=None, artifact_local_enable=True, artifact_local_dir=artifact_local_dir) | ||
self.dogen = Generator(self.log, args, [Artifact]) | ||
|
||
def test_should_skip_plugin_if_no_path_to_local_dir_is_provided(self): | ||
self.prepare_dogen(None, self.target_dir) | ||
self.dogen.run() | ||
|
||
self.log.debug.assert_any_call("No directory with local artifact files specified, skipping artifact plugin") | ||
|
||
def test_should_skip_plugin_if_local_dir_does_not_exist(self): | ||
self.prepare_dogen("non_existing_local_artifact_dir", self.target_dir) | ||
self.dogen.run() | ||
|
||
self.log.debug.assert_any_call("Provided path to directory with local artifact files does not exists or is not a directory") | ||
|
||
def test_should_skip_plugin_if_path_to_local_artifact_dir_is_provided_but_there_are_no_files(self): | ||
self.prepare_dogen(self.artifact_dir, self.target_dir) | ||
self.dogen.run() | ||
self.log.debug.assert_any_call("No local artifacts found") | ||
|
||
def test_local_artifact_files_should_be_copied_to_target(self): | ||
open(os.path.join(self.artifact_dir, "artifact1"), 'a').close() | ||
open(os.path.join(self.artifact_dir, "artifact2"), 'a').close() | ||
|
||
self.prepare_dogen(self.artifact_dir, self.target_dir) | ||
self.dogen.run() | ||
|
||
self.assertTrue(os.path.exists(os.path.join(self.target_dir, "artifact1"))) | ||
self.assertTrue(os.path.exists(os.path.join(self.target_dir, "artifact2"))) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how this handle target?
https://github.com/jboss-dockerfiles/dogen/blob/develop/dogen/generator.py#L289
artifacts in dogen can be copied anywhere into the subtree of output directory - this functionality is crucial to CCT plugin. This probably needs to be updated. @goldmann opinion?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The target name could be used in the artifacts directory. The sources would then find it and pick it up.
I'll need to update to handle subdirectories. If the artifacts directory contains the subdirectories and target names, they can be copied over.