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: []) ⇒ 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 ⇒ Object
Push an image to hub.docker.com.
-
#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: []) ⇒ Object
Build a docker image from a directory
32 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 |
# File 'lib/puppet_docker_tools/runner.rb', line 32 def build(no_cache: false, version: nil, build_args: []) 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 # 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}" puts "Building #{path}:latest" # 't' in the build_options sets the tag for the image we're building = { 't' => "#{path}:latest", 'dockerfile' => dockerfile, 'buildargs' => "#{build_args_hash.to_json}"} if no_cache puts "Ignoring cache for #{path}" ['nocache'] = true end Docker::Image.build_from_dir(directory, ) 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.
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/puppet_docker_tools/runner.rb', line 85 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.
103 104 105 106 |
# File 'lib/puppet_docker_tools/runner.rb', line 103 def local_lint output, status = Open3.capture2e(PuppetDockerTools::Utilities.get_hadolint_command("#{directory}/#{dockerfile}")) fail output unless status == 0 end |
#push ⇒ Object
Push an image to hub.docker.com
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/puppet_docker_tools/runner.rb', line 110 def push image_name = File.basename(directory) path = "#{repository}/#{image_name}" version = PuppetDockerTools::Utilities.get_value_from_label(path, value: 'version', namespace: namespace) # We always want to push a versioned label in addition to the latest label unless version fail "No version specified in #{dockerfile} for #{path}" end puts "Pushing #{path}:#{version} to Docker Hub" exitstatus, _ = PuppetDockerTools::Utilities.push_to_dockerhub("#{path}:#{version}") unless exitstatus == 0 fail "Pushing #{path}:#{version} to dockerhub failed!" end puts "Pushing #{path}:latest to Docker Hub" exitstatus, _ = PuppetDockerTools::Utilities.push_to_dockerhub("#{path}:latest") unless exitstatus == 0 fail "Pushing #{path}:latest to dockerhub failed!" end end |
#rev_labels ⇒ Object
Update vcs-ref and build-date labels in the Dockerfile
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/puppet_docker_tools/runner.rb', line 135 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
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/puppet_docker_tools/runner.rb', line 155 def spec tests = Dir.glob("#{directory}/spec/*_spec.rb") test_files = tests.map { |test| File.basename(test, '.rb') } puts "Running RSpec tests from #{File.expand_path("#{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
176 177 178 |
# File 'lib/puppet_docker_tools/runner.rb', line 176 def version puts PuppetDockerTools::Utilities.get_value_from_env('version', namespace: namespace, directory: directory, dockerfile: dockerfile) end |