puppet_docker_tools 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2af7f0af2ac0fb8371c5358179ff8b3d26f10157
4
- data.tar.gz: 5c82a30a82d4dbaafbf3e8217a9c8f4b96cb4d5d
3
+ metadata.gz: 279a8c6d6a95b7d1c5f3f0dc4c045f2b67e363fd
4
+ data.tar.gz: fa7be0251b3e84ae7fe7be4942c795a578f116e9
5
5
  SHA512:
6
- metadata.gz: e3f350e40c4f26824845e9e06d820b3fb47d69e165602feb6a9049a3cf7a18f78514670a0f9a53dcae5d07bafc22e644173b93f2d5ec3e815c04ba7daff79c76
7
- data.tar.gz: f8092567055f4a50bcecd27682209ff6f3374d46a39806bc6a5a86143d3c3155b9ab5c06b9db87269a109f7e7ab25a1988f7e7e5019fa56f6e0c4c269afec1bd
6
+ metadata.gz: d1156f61257a15d2bb792ab555d146e6041d0782d072a2467a1766174562f9a10af9486d2b9d82f350ae45f18053467937cfa444664c70ebc0cfaec03c3d51b6
7
+ data.tar.gz: 5f2e135989bc479afca3e1116c45fff26cb6a43e21c2eacc904a1adf454ae627fb6e67e61d83a248d74fbea348c305f8933e090a2115e14edf2bfe21847629af
data/README.md CHANGED
@@ -12,8 +12,10 @@ You also need to have docker installed and running. See [these docs](https://doc
12
12
 
13
13
  ```
14
14
  $ puppet-docker help
15
+ Utilities for building and releasing Puppet docker images.
16
+
15
17
  Usage:
16
- puppet-docker build [DIRECTORY] [--dockerfile=<dockerfile>] [--repository=<repo>] [--namespace=<namespace>] [--no-cache]
18
+ puppet-docker build [DIRECTORY] [--dockerfile=<dockerfile>] [--repository=<repo>] [--namespace=<namespace>] [--no-cache] [--version=<version] [--build-arg=<buildarg> ...]
17
19
  puppet-docker lint [DIRECTORY] [--dockerfile=<dockerfile>]
18
20
  puppet-docker local-lint [DIRECTORY] [--dockerfile=<dockerfile>]
19
21
  puppet-docker pull [IMAGE] [--repository=<repo>]
@@ -35,6 +37,9 @@ Options:
35
37
  --no-cache Disable use of layer cache when building this image. Defaults to using the cache.
36
38
  --namespace=<namespace> Namespace for labels on the container [default: org.label-schema]
37
39
  --dockerfile=<dockerfile> File name for your dockerfile [default: Dockerfile]
40
+ --version=<version> Version to build. This field will be used to determine the label and will be passed as the version build arg.
41
+ **NOTE** `--build-arg version='<version>'` overrides `--version <version>`
42
+ --build-arg=<buildarg> Build arg to pass to container build, can be passed multiple times.
38
43
  ```
39
44
 
40
45
  ### `puppet-docker build`
@@ -7,7 +7,7 @@ doc = <<DOC
7
7
  Utilities for building and releasing Puppet docker images.
8
8
 
9
9
  Usage:
10
- puppet-docker build [DIRECTORY] [--dockerfile=<dockerfile>] [--repository=<repo>] [--namespace=<namespace>] [--no-cache]
10
+ puppet-docker build [DIRECTORY] [--dockerfile=<dockerfile>] [--repository=<repo>] [--namespace=<namespace>] [--no-cache] [--version=<version] [--build-arg=<buildarg> ...]
11
11
  puppet-docker lint [DIRECTORY] [--dockerfile=<dockerfile>]
12
12
  puppet-docker local-lint [DIRECTORY] [--dockerfile=<dockerfile>]
13
13
  puppet-docker pull [IMAGE] [--repository=<repo>]
@@ -29,6 +29,9 @@ Options:
29
29
  --no-cache Disable use of layer cache when building this image. Defaults to using the cache.
30
30
  --namespace=<namespace> Namespace for labels on the container [default: org.label-schema]
31
31
  --dockerfile=<dockerfile> File name for your dockerfile [default: Dockerfile]
32
+ --version=<version> Version to build. This field will be used to determine the label and will be passed as the version build arg.
33
+ **NOTE** `--build-arg version='<version>'` overrides `--version <version>`
34
+ --build-arg=<buildarg> Build arg to pass to container build, can be passed multiple times.
32
35
  DOC
33
36
 
34
37
  begin
@@ -44,6 +47,8 @@ defaults = {
44
47
  '--namespace' => 'org.label-schema',
45
48
  '--repository' => 'puppet',
46
49
  '--dockerfile' => 'Dockerfile',
50
+ '--version' => nil,
51
+ '--build-arg' => [],
47
52
  }
48
53
 
49
54
  options.merge!(defaults) do |key, option, default|
@@ -69,7 +74,7 @@ begin
69
74
  command_runner = PuppetDockerTools::Runner.new(directory: options['DIRECTORY'], repository: options['--repository'], namespace: options['--namespace'], dockerfile: options['--dockerfile'])
70
75
 
71
76
  if options['build']
72
- command_runner.build(no_cache: options['--no-cache'])
77
+ command_runner.build(no_cache: options['--no-cache'], version: options['--version'], build_args: options['--build-arg'])
73
78
  elsif options['lint']
74
79
  command_runner.lint
75
80
  elsif options['local-lint']
@@ -24,24 +24,50 @@ class PuppetDockerTools
24
24
  #
25
25
  # @param no_cache Whether or not to use existing layer caches when building
26
26
  # this image. Defaults to using the cache (no_cache = false).
27
- def build(no_cache: false)
27
+ # @param version Set the version for the container explicitly. Will get
28
+ # passed as the 'version' buildarg.
29
+ # @param build_args Pass arbitrary buildargs to the container build.
30
+ # Expected to be an array of strings, each string formatted like
31
+ # 'arg=value'.
32
+ def build(no_cache: false, version: nil, build_args: [])
28
33
  image_name = File.basename(directory)
29
- version = PuppetDockerTools::Utilities.get_value_from_env('version', namespace: namespace, directory: directory, dockerfile: dockerfile)
30
- path = "#{repository}/#{image_name}"
31
- puts "Building #{path}:latest"
32
-
33
- build_args = {
34
+ build_args_hash = {
34
35
  'vcs_ref' => PuppetDockerTools::Utilities.current_git_sha(directory),
35
36
  'build_date' => Time.now.utc.iso8601
36
37
  }
37
38
 
39
+ # if version is passed in, add that into the build_args hash
40
+ # **NOTE** if both `version` and `build_args` includes `version=something`
41
+ # the value in `build_args` takes precedence
42
+ build_args_hash['version'] = version unless version.nil?
43
+
44
+ # Convert the build_args array to a hash, and merge it with the values
45
+ # that have already been set
46
+ if Array(build_args).any?
47
+ build_args_hash.merge!(PuppetDockerTools::Utilities.parse_build_args(Array(build_args)))
48
+ end
49
+
50
+ # This variable is meant to be used for building the non-latest tagged build
51
+ # If the version was set via `version` or `build_args`, use that. If not,
52
+ # use `get_value_from_env` to parse that value from the dockerfile.
53
+ #
54
+ # If version hasn't been passed in via `version` or `build_args` there's
55
+ # no need to add the version from `get_value_from_env` to the
56
+ # build_args_hash, dockerfiles should not be using both hardcoded versions
57
+ # and versions passed in to the dockerfile with an `ARG`
58
+ version = build_args_hash['version'] || PuppetDockerTools::Utilities.get_value_from_env('version', namespace: namespace, directory: directory, dockerfile: dockerfile)
59
+
60
+ path = "#{repository}/#{image_name}"
61
+ puts "Building #{path}:latest"
62
+
38
63
  # 't' in the build_options sets the tag for the image we're building
39
- build_options = { 't' => "#{path}:latest", 'dockerfile' => dockerfile, 'buildargs' => "#{build_args.to_json}"}
64
+ build_options = { 't' => "#{path}:latest", 'dockerfile' => dockerfile, 'buildargs' => "#{build_args_hash.to_json}"}
40
65
 
41
66
  if no_cache
42
67
  puts "Ignoring cache for #{path}"
43
68
  build_options['nocache'] = true
44
69
  end
70
+
45
71
  Docker::Image.build_from_dir(directory, build_options)
46
72
 
47
73
  if version
@@ -26,6 +26,25 @@ class PuppetDockerTools
26
26
  end
27
27
  end
28
28
 
29
+ # parse build args into a hash for easier manipulation
30
+ #
31
+ # @param build_args array of build_args with each entry in the format 'arg=value'
32
+ def parse_build_args(build_args)
33
+ args_hash = {}
34
+
35
+ build_args.each do |arg|
36
+ fields = arg.split('=')
37
+ key = fields.first
38
+ # get rid of the key from the fields so we can get the value
39
+ fields.shift
40
+ # join the remaining fields with '=' in case the value had '=' in it
41
+ value = fields.join('=')
42
+ args_hash[key] = value
43
+ end
44
+
45
+ args_hash
46
+ end
47
+
29
48
  # Get a value from the labels on a docker image
30
49
  #
31
50
  # @param image The docker image you want to get a value from, e.g. 'puppet/puppetserver'
@@ -52,7 +71,12 @@ class PuppetDockerTools
52
71
 
53
72
  value = text.scan(/#{Regexp.escape(namespace)}\.(.+)=(.+) \\?/).to_h[label]
54
73
  # expand out environment variables
55
- value = get_value_from_variable(value, directory: directory, dockerfile: dockerfile, dockerfile_contents: text) if value.start_with?('$')
74
+ # This supports either label=$variable or label="$variable"
75
+ if value.start_with?('$') || value.start_with?('"$')
76
+ # if variable is quoted, get rid of leading and trailing quotes
77
+ value.gsub!(/\A"|"\Z/, '')
78
+ value = get_value_from_variable(value, directory: directory, dockerfile: dockerfile, dockerfile_contents: text)
79
+ end
56
80
  # check in higher-level image if we didn't find it defined in this docker file
57
81
  value = get_value_from_base_image(label, namespace: namespace, directory: directory, dockerfile: dockerfile) if value.nil?
58
82
  # This gets rid of leading or trailing quotes
@@ -12,6 +12,8 @@ describe PuppetDockerTools::Runner do
12
12
 
13
13
  let(:runner) { create_runner(directory: '/tmp/test-image', repository: 'test', namespace: 'org.label-schema', dockerfile: 'Dockerfile') }
14
14
  let(:buildargs) {{ 'vcs_ref' => 'b0c5ead01b6cabdb3f01871bce699be165c3288f', 'build_date' => '2018-06-08T17:18:13Z' }.to_json}
15
+ let(:buildargs_with_version) {{ 'vcs_ref' => 'b0c5ead01b6cabdb3f01871bce699be165c3288f', 'build_date' => '2018-06-08T17:18:13Z', 'version' => '1.2.3' }.to_json}
16
+ let(:extra_buildargs) {{ 'vcs_ref' => 'b0c5ead01b6cabdb3f01871bce699be165c3288f', 'build_date' => '2018-06-08T17:18:13Z', 'foo' => 'bar', 'baz' => 'test=with=equals' }.to_json}
15
17
 
16
18
  describe '#initialize' do
17
19
  it "should fail if the dockerfile doesn't exist" do
@@ -42,6 +44,24 @@ describe PuppetDockerTools::Runner do
42
44
  runner.build(no_cache: true)
43
45
  end
44
46
 
47
+ it 'passes the version when that parameter is set' do
48
+ expect(Docker::Image).to receive(:build_from_dir).with(runner.directory, { 't' => 'test/test-image:latest', 'dockerfile' => runner.dockerfile, 'buildargs' => buildargs_with_version }).and_return(image)
49
+ expect(Docker::Image).to receive(:build_from_dir).with(runner.directory, { 't' => 'test/test-image:1.2.3', 'dockerfile' => runner.dockerfile, 'buildargs' => buildargs_with_version }).and_return(image)
50
+ runner.build(version: '1.2.3')
51
+ end
52
+
53
+ it 'passes arbitrary build args' do
54
+ expect(PuppetDockerTools::Utilities).to receive(:get_value_from_env).with('version', namespace: runner.namespace, directory: runner.directory, dockerfile: runner.dockerfile).and_return(nil)
55
+ expect(Docker::Image).to receive(:build_from_dir).with(runner.directory, { 't' => 'test/test-image:latest', 'dockerfile' => runner.dockerfile, 'buildargs' => extra_buildargs }).and_return(image)
56
+ runner.build(build_args: ['foo=bar', 'baz=test=with=equals'])
57
+ end
58
+
59
+ it 'prioritizes version as a build arg over regular version' do
60
+ expect(Docker::Image).to receive(:build_from_dir).with(runner.directory, { 't' => 'test/test-image:latest', 'dockerfile' => runner.dockerfile, 'buildargs' => buildargs_with_version }).and_return(image)
61
+ expect(Docker::Image).to receive(:build_from_dir).with(runner.directory, { 't' => 'test/test-image:1.2.3', 'dockerfile' => runner.dockerfile, 'buildargs' => buildargs_with_version }).and_return(image)
62
+ runner.build(version: '1.2.4', build_args: ['version=1.2.3'])
63
+ end
64
+
45
65
  it 'uses a custom dockerfile if passed' do
46
66
  allow(File).to receive(:exist?).with('/tmp/test-image/Dockerfile.test').and_return(true)
47
67
  expect(PuppetDockerTools::Utilities).to receive(:get_value_from_env).with('version', namespace: 'org.label-schema', directory: '/tmp/test-image', dockerfile: 'Dockerfile.test').and_return(nil)
@@ -13,7 +13,7 @@ LABEL maintainer="Puppet Release Team <release@puppet.com>" \\
13
13
  org.label-schema.url="https://github.com/puppetlabs/puppet-in-docker" \\
14
14
  org.label-schema.name="Puppet Server (No PuppetDB)" \\
15
15
  org.label-schema.license="Apache-2.0" \\
16
- org.label-schema.version=$PUPPET_SERVER_VERSION \\
16
+ org.label-schema.version="$PUPPET_SERVER_VERSION" \\
17
17
  org.label-schema.vcs-url="https://github.com/puppetlabs/puppet-in-docker" \\
18
18
  org.label-schema.vcs-ref="b75674e1fbf52f7821f7900ab22a19f1a10cafdb" \\
19
19
  org.label-schema.build-date="2018-05-09T20:11:01Z" \\
@@ -204,4 +204,22 @@ HERE
204
204
  expect(PuppetDockerTools::Utilities.get_hadolint_command).to eq('hadolint --ignore DL3008 --ignore DL3018 --ignore DL4000 --ignore DL4001 -')
205
205
  end
206
206
  end
207
+
208
+ describe '#parse_build_args' do
209
+ let(:build_args) {
210
+ [
211
+ 'foo=bar',
212
+ 'test=a=string=with==equals'
213
+ ]
214
+ }
215
+ let(:build_args_hash) {
216
+ {
217
+ 'foo' => 'bar',
218
+ 'test' => 'a=string=with==equals'
219
+ }
220
+ }
221
+ it 'converts the array to a hash' do
222
+ expect(PuppetDockerTools::Utilities.parse_build_args(build_args)).to eq(build_args_hash)
223
+ end
224
+ end
207
225
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: puppet_docker_tools
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Puppet, Inc.
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-06-12 00:00:00.000000000 Z
11
+ date: 2018-06-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: docker-api