Class: Ufo::Docker::Builder
- Inherits:
-
Object
- Object
- Ufo::Docker::Builder
- Includes:
- Util
- Defined in:
- lib/ufo/docker/builder.rb
Class Method Summary collapse
Instance Method Summary collapse
- #build ⇒ Object
- #check_dockerfile_exists ⇒ Object
- #docker_name_path ⇒ Object
- #ecr_image? ⇒ Boolean
-
#full_image_name ⇒ Object
full_image - includes the tag.
- #generate_name ⇒ Object
- #git_sha ⇒ Object
-
#image_name ⇒ Object
full_image - does not include the tag.
-
#initialize(options = {}) ⇒ Builder
constructor
A new instance of Builder.
- #push ⇒ Object
- #say(msg) ⇒ Object
- #settings ⇒ Object
-
#store_full_image_name ⇒ Object
Store this in a file because this name gets reference in other tasks later and we want the image name to stay the same when the commands are run separate in different processes.
- #timestamp ⇒ Object
- #update_auth_token ⇒ Object
- #update_dockerfile ⇒ Object
Methods included from Util
Constructor Details
#initialize(options = {}) ⇒ Builder
Returns a new instance of Builder.
14 15 16 17 18 19 |
# File 'lib/ufo/docker/builder.rb', line 14 def initialize(={}) @options = @project_root = [:project_root] || '.' @dockerfile = [:dockerfile] || 'Dockerfile' @image_namespace = [:image_namespace] || 'ufo' end |
Class Method Details
Instance Method Details
#build ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/ufo/docker/builder.rb', line 21 def build start_time = Time.now store_full_image_name update_auth_token # call after store_full_image_name command = "docker build -t #{full_image_name} -f #{@dockerfile} ." say "Building docker image with:".green say " #{command}".green check_dockerfile_exists command = "cd #{@project_root} && #{command}" success = execute(command, use_system: true) unless success puts "ERROR: The docker image fail to build. Are you sure the docker daemon is available? Try running: docker version".colorize(:red) exit 1 end took = Time.now - start_time say "Docker image #{full_image_name} built. " + "Took #{pretty_time(took)}.".green end |
#check_dockerfile_exists ⇒ Object
61 62 63 64 65 66 |
# File 'lib/ufo/docker/builder.rb', line 61 def check_dockerfile_exists unless File.exist?("#{@project_root}/#{@dockerfile}") puts "#{@dockerfile} does not exist. Are you sure it exists?" exit 1 end end |
#docker_name_path ⇒ Object
113 114 115 116 |
# File 'lib/ufo/docker/builder.rb', line 113 def docker_name_path # output gets entirely wiped by tasks builder so dotn use that folder "#{@project_root}/ufo/docker_image_name_#{@image_namespace}.txt" end |
#ecr_image? ⇒ Boolean
75 76 77 |
# File 'lib/ufo/docker/builder.rb', line 75 def ecr_image? full_image_name =~ /\.amazonaws\.com/ end |
#full_image_name ⇒ Object
full_image - includes the tag
85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/ufo/docker/builder.rb', line 85 def full_image_name if @options[:generate] return generate_name # name already has a newline end unless File.exist?(docker_name_path) puts "Unable to find #{docker_name_path} which contains the last docker image name that was used as a part of `ufo docker build`. Please run `ufo docker build` first." exit 1 end IO.read(docker_name_path).strip end |
#generate_name ⇒ Object
109 110 111 |
# File 'lib/ufo/docker/builder.rb', line 109 def generate_name "#{image_name}:#{@image_namespace}-#{}-#{git_sha}" end |
#git_sha ⇒ Object
122 123 124 125 126 127 |
# File 'lib/ufo/docker/builder.rb', line 122 def git_sha return @git_sha if @git_sha # always call this and dont use the execute method because of the noop option @git_sha = `cd #{@project_root} && git rev-parse --short HEAD` @git_sha.strip! end |
#image_name ⇒ Object
full_image - does not include the tag
80 81 82 |
# File 'lib/ufo/docker/builder.rb', line 80 def image_name settings.data["image"] end |
#push ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/ufo/docker/builder.rb', line 41 def push update_auth_token start_time = Time.now = "Pushed #{full_image_name} docker image." if @options[:noop] = "NOOP #{}" else command = "docker push #{full_image_name}" puts "=> #{command}".colorize(:green) success = execute(command, use_system: true) unless success puts "ERROR: The docker image fail to push.".colorize(:red) exit 1 end end took = Time.now - start_time << " Took #{pretty_time(took)}.".green puts unless @options[:mute] end |
#say(msg) ⇒ Object
138 139 140 |
# File 'lib/ufo/docker/builder.rb', line 138 def say(msg) puts msg unless @options[:mute] end |
#settings ⇒ Object
129 130 131 |
# File 'lib/ufo/docker/builder.rb', line 129 def settings @settings ||= Settings.new(@project_root) end |
#store_full_image_name ⇒ Object
Store this in a file because this name gets reference in other tasks later and we want the image name to stay the same when the commands are run separate in different processes. So we store the state in a file. Only when a new docker build command gets run will the image name state be updated.
101 102 103 104 105 106 107 |
# File 'lib/ufo/docker/builder.rb', line 101 def store_full_image_name dirname = File.dirname(docker_name_path) FileUtils.mkdir_p(dirname) unless File.exist?(dirname) full_image_name = generate_name IO.write(docker_name_path, full_image_name) IO.write("#{@project_root}/ufo/version", full_image_name) end |
#timestamp ⇒ Object
118 119 120 |
# File 'lib/ufo/docker/builder.rb', line 118 def @timestamp ||= Time.now.strftime('%Y-%m-%dT%H-%M-%S') end |
#update_auth_token ⇒ Object
68 69 70 71 72 73 |
# File 'lib/ufo/docker/builder.rb', line 68 def update_auth_token return unless ecr_image? repo_domain = "https://#{image_name.split('/').first}" auth = Ecr::Auth.new(repo_domain) auth.update end |
#update_dockerfile ⇒ Object
133 134 135 136 |
# File 'lib/ufo/docker/builder.rb', line 133 def update_dockerfile dockerfile = Docker::Dockerfile.new(full_image_name, @options) dockerfile.update end |