Class: PuppetDockerTools::Runner
- Inherits:
-
Object
- Object
- PuppetDockerTools::Runner
- Defined in:
- lib/puppet_docker_tools/runner.rb
Instance Attribute Summary collapse
-
#directory ⇒ Object
Returns the value of attribute directory.
-
#dockerfile ⇒ Object
Returns the value of attribute dockerfile.
-
#namespace ⇒ Object
Returns the value of attribute namespace.
-
#repository ⇒ Object
Returns the value of attribute repository.
Instance Method Summary collapse
-
#build(no_cache: false, version: nil, build_args: [], latest: true, stream_output: true) ⇒ Object
Build a docker image from a directory.
-
#initialize(directory:, repository:, namespace:, dockerfile:) ⇒ Runner
constructor
A new instance of Runner.
-
#lint ⇒ Object
Run hadolint on the Dockerfile in the specified directory.
-
#local_lint ⇒ Object
Run hadolint Dockerfile linting using a local hadolint executable.
-
#push(latest: true, version: nil) ⇒ Object
Push an image to $repository.
-
#rev_labels ⇒ Object
Update vcs-ref and build-date labels in the Dockerfile.
-
#spec(image: nil) ⇒ Object
Run spec tests.
-
#version ⇒ Object
Get the version set in the Dockerfile in the specified directory.
Constructor Details
#initialize(directory:, repository:, namespace:, dockerfile:) ⇒ Runner
Returns a new instance of Runner.
12 13 14 15 16 17 18 19 20 |
# File 'lib/puppet_docker_tools/runner.rb', line 12 def initialize(directory: , repository: , namespace: , dockerfile: ) @directory = directory @repository = repository @namespace = namespace @dockerfile = dockerfile file = File.join(directory, dockerfile) fail "File #{file} doesn't exist!" unless File.exist? file end |
Instance Attribute Details
#directory ⇒ Object
Returns the value of attribute directory.
10 11 12 |
# File 'lib/puppet_docker_tools/runner.rb', line 10 def directory @directory end |
#dockerfile ⇒ Object
Returns the value of attribute dockerfile.
10 11 12 |
# File 'lib/puppet_docker_tools/runner.rb', line 10 def dockerfile @dockerfile end |
#namespace ⇒ Object
Returns the value of attribute namespace.
10 11 12 |
# File 'lib/puppet_docker_tools/runner.rb', line 10 def namespace @namespace end |
#repository ⇒ Object
Returns the value of attribute repository.
10 11 12 |
# File 'lib/puppet_docker_tools/runner.rb', line 10 def repository @repository end |
Instance Method Details
#build(no_cache: false, version: nil, build_args: [], latest: true, stream_output: true) ⇒ Object
Build a docker image from a directory
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/puppet_docker_tools/runner.rb', line 33 def build(no_cache: false, version: nil, build_args: [], latest: true, stream_output: true) image_name = File.basename(directory) build_args_hash = { 'vcs_ref' => PuppetDockerTools::Utilities.current_git_sha(directory), 'build_date' => Time.now.utc.iso8601 } # if version is passed in, add that into the build_args hash # **NOTE** if both `version` and `build_args` includes `version=something` # the value in `build_args` takes precedence build_args_hash['version'] = version unless version.nil? # Convert the build_args array to a hash, and merge it with the values # that have already been set if Array(build_args).any? build_args_hash.merge!(PuppetDockerTools::Utilities.parse_build_args(Array(build_args))) end build_args_hash = PuppetDockerTools::Utilities.filter_build_args(build_args: build_args_hash, dockerfile: File.join(directory, dockerfile)) # This variable is meant to be used for building the non-latest tagged build # If the version was set via `version` or `build_args`, use that. If not, # use `get_value_from_env` to parse that value from the dockerfile. # # If version hasn't been passed in via `version` or `build_args` there's # no need to add the version from `get_value_from_env` to the # build_args_hash, dockerfiles should not be using both hardcoded versions # and versions passed in to the dockerfile with an `ARG` version = build_args_hash['version'] || PuppetDockerTools::Utilities.get_value_from_env('version', namespace: namespace, directory: directory, dockerfile: dockerfile) path = File.join(repository, image_name) = [] if no_cache puts "Ignoring cache for #{path}" << '--no-cache' end if dockerfile != "Dockerfile" << ['--file', File.join(directory, dockerfile)] end = [] if latest << ['--tag', "#{path}:latest"] end if version << ['--tag', "#{path}:#{version}"] end if .empty? return nil end build_args = [] build_args_hash.map{ |k,v| "#{k}=#{v}" }.each do |val| build_args << ['--build-arg', val] end build_command = ['docker', 'build', build_args, , , directory].flatten Open3.popen2e(*build_command) do |stdin, output_stream, wait_thread| output='' output_stream.each_line do |line| stream_output ? (puts line) : (output += line) end exit_status = wait_thread.value.exitstatus puts output unless stream_output fail unless exit_status == 0 end end |
#lint ⇒ Object
Run hadolint on the Dockerfile in the specified directory. This will run hadolint inside of a container. To run a locally-installed hadolint binary see local_lint.
111 112 113 114 115 116 117 118 119 |
# File 'lib/puppet_docker_tools/runner.rb', line 111 def lint hadolint_container = 'hadolint/hadolint' # make sure we have the container locally PuppetDockerTools::Utilities.pull("#{hadolint_container}:latest") docker_run = ['docker', 'run', '--rm', '-v', "#{File.join(Dir.pwd, directory, dockerfile)}:/Dockerfile:ro", '-i', 'hadolint/hadolint', PuppetDockerTools::Utilities.get_hadolint_command('Dockerfile')].flatten output, status = Open3.capture2e(*docker_run) fail output unless status == 0 end |
#local_lint ⇒ Object
Run hadolint Dockerfile linting using a local hadolint executable. Executable found based on your path.
123 124 125 126 |
# File 'lib/puppet_docker_tools/runner.rb', line 123 def local_lint output, status = Open3.capture2e(*PuppetDockerTools::Utilities.get_hadolint_command(File.join(directory,dockerfile))) fail output unless status == 0 end |
#push(latest: true, version: nil) ⇒ Object
Push an image to $repository
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/puppet_docker_tools/runner.rb', line 132 def push(latest: true, version: nil) image_name = File.basename(directory) path = File.join(repository, image_name) # only check for version from the label if we didn't pass it in if version.nil? version = PuppetDockerTools::Utilities.get_value_from_label(path, value: 'version', namespace: namespace) end # We always want to push a versioned container unless version fail "No version specified in #{dockerfile} for #{path}" end puts "Pushing #{path}:#{version}" exitstatus, _ = PuppetDockerTools::Utilities.push_to_docker_repo("#{path}:#{version}") unless exitstatus == 0 fail "Pushing #{path}:#{version} failed!" end if latest puts "Pushing #{path}:latest" exitstatus, _ = PuppetDockerTools::Utilities.push_to_docker_repo("#{path}:latest") unless exitstatus == 0 fail "Pushing #{path}:latest failed!" end end end |
#rev_labels ⇒ Object
Update vcs-ref and build-date labels in the Dockerfile
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/puppet_docker_tools/runner.rb', line 163 def rev_labels file = File.join(directory, dockerfile) values_to_update = { "#{namespace}.vcs-ref" => PuppetDockerTools::Utilities.current_git_sha(directory), "#{namespace}.build-date" => Time.now.utc.iso8601 } text = File.read(file) values_to_update.each do |key, value| original = text.clone text = text.gsub(/#{key}=\"[a-z0-9A-Z\-:]*\"/, "#{key}=\"#{value}\"") puts "Updating #{key} in #{file}" unless original == text end File.open(file, 'w') { |f| f.puts text } end |
#spec(image: nil) ⇒ Object
Run spec tests
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/puppet_docker_tools/runner.rb', line 183 def spec(image: nil) if image fail 'Oh no! You have PUPPET_TEST_DOCKER_IMAGE set! Please unset!' if ENV['PUPPET_TEST_DOCKER_IMAGE'] ENV['PUPPET_TEST_DOCKER_IMAGE'] = image end tests = Dir.glob(File.join(directory,'spec','*_spec.rb')) test_files = tests.map { |test| File.basename(test, '.rb') } puts "Running RSpec tests from #{File.(File.join(directory,'spec'))} (#{test_files.join ","}), this may take some time" success = true tests.each do |test| Open3.popen2e('rspec', 'spec', test) do |stdin, output_stream, wait_thread| output_stream.each_line do |line| puts line end exit_status = wait_thread.value.exitstatus success = success && (exit_status == 0) end end if image ENV['PUPPET_TEST_DOCKER_IMAGE'] = nil end fail "Running RSpec tests for #{directory} failed!" unless success end |
#version ⇒ Object
Get the version set in the Dockerfile in the specified directory
213 214 215 |
# File 'lib/puppet_docker_tools/runner.rb', line 213 def version puts PuppetDockerTools::Utilities.get_value_from_env('version', namespace: namespace, directory: directory, dockerfile: dockerfile) end |