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) ⇒ 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 ⇒ 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.
13 14 15 16 17 18 19 20 21 |
# File 'lib/puppet_docker_tools/runner.rb', line 13 def initialize(directory: , repository: , namespace: , dockerfile: ) @directory = directory @repository = repository @namespace = namespace @dockerfile = dockerfile file = "#{directory}/#{dockerfile}" fail "File #{file} doesn't exist!" unless File.exist? file end |
Instance Attribute Details
#directory ⇒ Object
Returns the value of attribute directory.
11 12 13 |
# File 'lib/puppet_docker_tools/runner.rb', line 11 def directory @directory end |
#dockerfile ⇒ Object
Returns the value of attribute dockerfile.
11 12 13 |
# File 'lib/puppet_docker_tools/runner.rb', line 11 def dockerfile @dockerfile end |
#namespace ⇒ Object
Returns the value of attribute namespace.
11 12 13 |
# File 'lib/puppet_docker_tools/runner.rb', line 11 def namespace @namespace end |
#repository ⇒ Object
Returns the value of attribute repository.
11 12 13 |
# File 'lib/puppet_docker_tools/runner.rb', line 11 def repository @repository end |
Instance Method Details
#build(no_cache: false, version: nil, build_args: [], latest: true) ⇒ Object
Build a docker image from a directory
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 |
# File 'lib/puppet_docker_tools/runner.rb', line 34 def build(no_cache: false, version: nil, build_args: [], latest: 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: "#{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 = "#{repository}/#{image_name}" = {'dockerfile' => dockerfile, 'buildargs' => "#{build_args_hash.to_json}"} if no_cache puts "Ignoring cache for #{path}" ['nocache'] = true end if latest puts "Building #{path}:latest" # 't' in the build_options sets the tag for the image we're building ['t'] = "#{path}:latest" Docker::Image.build_from_dir(directory, ) end if version puts "Building #{path}:#{version}" ['t'] = "#{path}:#{version}" Docker::Image.build_from_dir(directory, ) 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.
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/puppet_docker_tools/runner.rb', line 94 def lint hadolint_container = 'hadolint/hadolint' # make sure we have the container locally PuppetDockerTools::Utilities.pull("#{hadolint_container}:latest") container = Docker::Container.create('Cmd' => ['/bin/sh', '-c', "#{PuppetDockerTools::Utilities.get_hadolint_command}"], 'Image' => hadolint_container, 'OpenStdin' => true, 'StdinOnce' => true) # This container.tap startes the container created above, and passes directory/Dockerfile to the container container.tap(&:start).attach(stdin: "#{directory}/#{dockerfile}") # Wait for the run to finish container.wait exit_status = container.json['State']['ExitCode'] unless exit_status == 0 fail container.logs(stdout: true, stderr: true) end end |
#local_lint ⇒ Object
Run hadolint Dockerfile linting using a local hadolint executable. Executable found based on your path.
112 113 114 115 |
# File 'lib/puppet_docker_tools/runner.rb', line 112 def local_lint output, status = Open3.capture2e(PuppetDockerTools::Utilities.get_hadolint_command("#{directory}/#{dockerfile}")) fail output unless status == 0 end |
#push(latest: true, version: nil) ⇒ Object
Push an image to $repository
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/puppet_docker_tools/runner.rb', line 121 def push(latest: true, version: nil) image_name = File.basename(directory) path = "#{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
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/puppet_docker_tools/runner.rb', line 152 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 ⇒ Object
Run spec tests
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/puppet_docker_tools/runner.rb', line 172 def spec tests = Dir.glob("#{directory}/spec/*_spec.rb") test_files = tests.map { |test| File.basename(test, '.rb') } puts "Running RSpec tests from #{File.("#{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| while line = output_stream.gets puts line end exit_status = wait_thread.value.exitstatus success = success && (exit_status == 0) end end fail "Running RSpec tests for #{directory} failed!" unless success end |
#version ⇒ Object
Get the version set in the Dockerfile in the specified directory
193 194 195 |
# File 'lib/puppet_docker_tools/runner.rb', line 193 def version puts PuppetDockerTools::Utilities.get_value_from_env('version', namespace: namespace, directory: directory, dockerfile: dockerfile) end |