Class: Ufo::Docker::Builder

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/ufo/docker/builder.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util

#default_cluster, #default_params, #display_params, #execute, #pretty_time, #settings

Constructor Details

#initialize(options = {}) ⇒ Builder

Returns a new instance of Builder.



16
17
18
19
20
# File 'lib/ufo/docker/builder.rb', line 16

def initialize(options={})
  @options = options
  @dockerfile = options[:dockerfile] || 'Dockerfile'
  @image_namespace = options[:image_namespace] || 'ufo'
end

Class Method Details

.build(options) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/ufo/docker/builder.rb', line 8

def self.build(options)
  builder = Builder.new(options) # outside if because it need builder.full_image_name
  builder.build
  pusher = Pusher.new(nil, options)
  pusher.push
  builder
end

Instance Method Details

#buildObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/ufo/docker/builder.rb', line 22

def build
  start_time = Time.now
  store_full_image_name

  update_auth_token

  command = "docker build #{build_options}-t #{full_image_name} -f #{@dockerfile} ."
  say "Building docker image with:".green
  say "  #{command}".green
  check_dockerfile_exists
  command = "cd #{Ufo.root} && #{command}"
  success = execute(command, use_system: true)
  unless success
    docker_version_success = system("docker version > /dev/null 2>&1")
    unless docker_version_success
      docker_version_message = "  Are you sure the docker daemon is available?  Try running: docker version."
    end
    puts "ERROR: The docker image fail to build.#{docker_version_message}".colorize(:red)
    exit 1
  end

  took = Time.now - start_time
  say "Docker image #{full_image_name} built.  " + "Took #{pretty_time(took)}.".green
end

#build_optionsObject



47
48
49
50
51
# File 'lib/ufo/docker/builder.rb', line 47

def build_options
  options = ENV['UFO_DOCKER_BUILD_OPTIONS']
  options += " " if options
  options
end

#check_dockerfile_existsObject



83
84
85
86
87
88
# File 'lib/ufo/docker/builder.rb', line 83

def check_dockerfile_exists
  unless File.exist?("#{Ufo.root}/#{@dockerfile}")
    puts "#{@dockerfile} does not exist.  Are you sure it exists?"
    exit 1
  end
end

#docker_name_pathObject



124
125
126
127
# File 'lib/ufo/docker/builder.rb', line 124

def docker_name_path
  # output gets entirely wiped by tasks builder so dotn use that folder
  "#{Ufo.root}/.ufo/data/docker_image_name_#{@image_namespace}.txt"
end

#ecr_image_names(path) ⇒ Object



66
67
68
# File 'lib/ufo/docker/builder.rb', line 66

def ecr_image_names(path)
  from_image_names(path).select { |i| i =~ /\.amazonaws\.com/ }
end

#from_image_names(path) ⇒ Object



70
71
72
73
74
75
76
77
# File 'lib/ufo/docker/builder.rb', line 70

def from_image_names(path)
  lines = IO.readlines(path)
  froms = lines.select { |l| l =~ /^FROM/ }
  froms.map do |l|
    md = l.match(/^FROM (.*)/)
    md[1]
  end.compact
end

#full_image_nameObject

full_image - Includes the tag. Examples:

123456789.dkr.ecr.us-west-2.amazonaws.com/myapp:ufo-2018-04-20T09-29-08-b7d51df
tongueroo/hi:ufo-2018-04-20T09-29-08-b7d51df


98
99
100
101
102
103
104
105
106
107
# File 'lib/ufo/docker/builder.rb', line 98

def full_image_name
  return generate_name if @options[:generate]
  return "tongueroo/hi:ufo-12345678" if ENV['TEST']

  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_nameObject



120
121
122
# File 'lib/ufo/docker/builder.rb', line 120

def generate_name
  "#{image_name}:#{@image_namespace}-#{timestamp}-#{git_sha}"
end

#git_shaObject



133
134
135
136
137
138
# File 'lib/ufo/docker/builder.rb', line 133

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 #{Ufo.root} && git rev-parse --short HEAD`
  @git_sha.strip!
end

#image_nameObject

full_image - does not include the tag



91
92
93
# File 'lib/ufo/docker/builder.rb', line 91

def image_name
  settings["image"]
end

#pusherObject



79
80
81
# File 'lib/ufo/docker/builder.rb', line 79

def pusher
  @pusher ||= Pusher.new(full_image_name, @options)
end

#say(msg) ⇒ Object



145
146
147
# File 'lib/ufo/docker/builder.rb', line 145

def say(msg)
  puts msg unless @options[:mute]
end

#store_full_image_nameObject

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.



113
114
115
116
117
118
# File 'lib/ufo/docker/builder.rb', line 113

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)
end

#timestampObject



129
130
131
# File 'lib/ufo/docker/builder.rb', line 129

def timestamp
  @timestamp ||= Time.now.strftime('%Y-%m-%dT%H-%M-%S')
end

#update_auth_tokenObject

Parse Dockerfile for FROM instruction. If the starting image is from an ECR repository, it’s likely an private image so we authorize ECR for pulling.



55
56
57
58
59
60
61
62
63
64
# File 'lib/ufo/docker/builder.rb', line 55

def update_auth_token
  ecr_image_names = ecr_image_names("#{Ufo.root}/#{@dockerfile}")
  return if ecr_image_names.empty?

  ecr_image_names.each do |ecr_image_name|
    auth = Ufo::Ecr::Auth.new(ecr_image_name)
    # wont update auth token unless the image being pushed in the ECR image format
    auth.update
  end
end

#update_dockerfileObject



140
141
142
143
# File 'lib/ufo/docker/builder.rb', line 140

def update_dockerfile
  dockerfile = Dockerfile.new(full_image_name, @options)
  dockerfile.update
end