Class: Ufo::Docker::Builder
- Inherits:
-
Object
- Object
- Ufo::Docker::Builder
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) builder.build
pusher = Pusher.new(nil, options)
pusher.push
builder
end
|
Instance Method Details
#build ⇒ Object
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 22
def build
start_time = Time.now
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 #{Ufo.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
45
46
47
48
49
50
|
# File 'lib/ufo/docker/builder.rb', line 45
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_path ⇒ Object
84
85
86
87
|
# File 'lib/ufo/docker/builder.rb', line 84
def docker_name_path
"#{Ufo.root}/.ufo/data/docker_image_name_#{@image_namespace}.txt"
end
|
#full_image_name ⇒ Object
full_image - includes the tag
58
59
60
61
62
63
64
65
66
67
|
# File 'lib/ufo/docker/builder.rb', line 58
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_name ⇒ Object
80
81
82
|
# File 'lib/ufo/docker/builder.rb', line 80
def generate_name
"#{image_name}:#{@image_namespace}-#{timestamp}-#{git_sha}"
end
|
#git_sha ⇒ Object
93
94
95
96
97
98
|
# File 'lib/ufo/docker/builder.rb', line 93
def git_sha
return @git_sha if @git_sha
@git_sha = `cd #{Ufo.root} && git rev-parse --short HEAD`
@git_sha.strip!
end
|
#image_name ⇒ Object
full_image - does not include the tag
53
54
55
|
# File 'lib/ufo/docker/builder.rb', line 53
def image_name
settings["image"]
end
|
#pusher ⇒ Object
41
42
43
|
# File 'lib/ufo/docker/builder.rb', line 41
def pusher
@pusher ||= Pusher.new(full_image_name, @options)
end
|
#say(msg) ⇒ Object
105
106
107
|
# File 'lib/ufo/docker/builder.rb', line 105
def say(msg)
puts msg unless @options[:mute]
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.
73
74
75
76
77
78
|
# File 'lib/ufo/docker/builder.rb', line 73
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
|
#timestamp ⇒ Object
89
90
91
|
# File 'lib/ufo/docker/builder.rb', line 89
def timestamp
@timestamp ||= Time.now.strftime('%Y-%m-%dT%H-%M-%S')
end
|
#update_dockerfile ⇒ Object
100
101
102
103
|
# File 'lib/ufo/docker/builder.rb', line 100
def update_dockerfile
dockerfile = Dockerfile.new(full_image_name, @options)
dockerfile.update
end
|