Class: Ufo::Docker::Builder

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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils::Pretty

#pretty_home, #pretty_path, #pretty_time

Methods included from Utils::Logging

#logger

Methods included from Utils::Execute

#execute, #user_params

Constructor Details

#initialize(options = {}) ⇒ Builder

Returns a new instance of Builder.



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

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

Class Method Details

.build(options = {}) ⇒ Object



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

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



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

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 = execute(command, log: log)
  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



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

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

#check_dockerfile_existsObject



96
97
98
99
100
101
# File 'lib/ufo/docker/builder.rb', line 96

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



87
88
89
90
91
92
93
94
# File 'lib/ufo/docker/builder.rb', line 87

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


111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/ufo/docker/builder.rb', line 111

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



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

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



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

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

#from_image_names(path) ⇒ Object



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

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



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

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

#git_shaObject



153
154
155
156
157
158
159
160
# File 'lib/ufo/docker/builder.rb', line 153

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



104
105
106
# File 'lib/ufo/docker/builder.rb', line 104

def image_name
  Ufo.config.docker.repo
end

#pusherObject



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

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.



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

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



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

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.



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

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



163
164
165
166
167
168
169
170
# File 'lib/ufo/docker/builder.rb', line 163

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