Class: Ufo::Docker::Builder

Inherits:
Object
  • Object
show all
Extended by:
Memoist
Includes:
Concerns, Hooks::Concern
Defined in:
lib/ufo/docker/builder.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Hooks::Concern

#run_hooks

Methods included from Concerns

#deploy, #info, #ps

Methods included from Concerns::Names

#names

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.docker_image
  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
46
47
48
# File 'lib/ufo/docker/builder.rb', line 22

def build
  start_time = Time.now
  store_docker_image

  logger.info "Building Docker Image"
  compile_dockerfile_erb
  check_dockerfile_exists
  update_auth_token
  command = "docker build #{build_options}-t #{docker_image} -f #{@dockerfile} ."
  log = ".ufo/log/docker.log" if @options[:quiet]
  success = nil
  run_hooks(name: "build", file: "docker.rb") do
    success = execute(command, log: log)
  end
  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
    logger.info "ERROR: Fail to build Docker image.#{docker_version_message}".color(:red)
    exit 1
  end

  took = Time.now - start_time
  logger.info "Docker Image built: #{docker_image}"
  logger.info "Took #{pretty_time(took)}"
end

#build_optionsObject



50
51
52
53
54
# File 'lib/ufo/docker/builder.rb', line 50

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

#check_dockerfile_existsObject



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

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

#compileObject



91
92
93
94
95
96
97
98
# File 'lib/ufo/docker/builder.rb', line 91

def compile
  erb_path = "#{Ufo.root}/#{@dockerfile}.erb"
  if File.exist?(erb_path)
    compile_dockerfile_erb
  else
    logger.info "File #{erb_path.color(:green)} does not exist. Cannot compile it if it doesnt exist"
  end
end

#docker_imageObject

full_image - Includes the tag. Examples:

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


115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/ufo/docker/builder.rb', line 115

def docker_image
  return generate_name if @options[:generate]

  unless File.exist?(docker_name_path)
    logger.info <<~EOL.color(:yellow)
      WARN: Unable to find: #{pretty_path(docker_name_path)}
      This contains the Docker image name that the build process uses.
      Please first run:

          ufo docker build

    EOL
    return "docker image not yet built"
  end
  IO.read(docker_name_path).strip
end

#docker_name_pathObject



147
148
149
150
# File 'lib/ufo/docker/builder.rb', line 147

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

#ecr_image_names(path) ⇒ Object



69
70
71
# File 'lib/ufo/docker/builder.rb', line 69

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

#from_image_names(path) ⇒ Object



73
74
75
76
77
78
79
80
# File 'lib/ufo/docker/builder.rb', line 73

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

#generate_nameObject



143
144
145
# File 'lib/ufo/docker/builder.rb', line 143

def generate_name
  ["#{image_name}:#{@image_namespace}", Ufo.role, Ufo.env, timestamp, git_sha].compact.join('-') # compact in case git_sha is unavailable
end

#git_shaObject



157
158
159
160
161
162
163
164
# File 'lib/ufo/docker/builder.rb', line 157

def git_sha
  sha = if File.exist?('.git')
    `git rev-parse --short HEAD`
  elsif ENV['CODEBUILD_RESOLVED_SOURCE_VERSION'] # AWS codebuild
    ENV['CODEBUILD_RESOLVED_SOURCE_VERSION'][0..6] # first 7 chars
  end
  sha.strip if sha
end

#image_nameObject

full_image - does not include the tag



108
109
110
# File 'lib/ufo/docker/builder.rb', line 108

def image_name
  Ufo.config.docker.repo
end

#pusherObject



82
83
84
# File 'lib/ufo/docker/builder.rb', line 82

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

#store_docker_imageObject

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.



136
137
138
139
140
141
# File 'lib/ufo/docker/builder.rb', line 136

def store_docker_image
  dirname = File.dirname(docker_name_path)
  FileUtils.mkdir_p(dirname) unless File.exist?(dirname)
  docker_image = generate_name
  IO.write(docker_name_path, docker_image)
end

#timestampObject



152
153
154
# File 'lib/ufo/docker/builder.rb', line 152

def 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.



58
59
60
61
62
63
64
65
66
67
# File 'lib/ufo/docker/builder.rb', line 58

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



167
168
169
170
171
172
173
174
# File 'lib/ufo/docker/builder.rb', line 167

def update_dockerfile
  updater = if File.exist?("#{Ufo.root}/Dockerfile.erb") # dont use @dockerfile on purpose
    State.new(@options.merge(base_image: docker_image))
  else
    Dockerfile.new(docker_image, @options)
  end
  updater.update
end