21
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
# File 'lib/cnvrg/image.rb', line 21
def build
image_data = get_image_data
file_name = "Dockerfile-#{@image_id}"
File.new(file_name, "w+")
if image_data["reqs_file_path"].present? and image_data["from_image_name"]
File.open(file_name, "w+") do |i|
i.write("FROM #{image_data["from_image_name"]}")
i.write("ADD requirements.txt requirements.txt")
i.write("RUN pip3 install -r requirements.txt")
end
else
open(file_name, 'wb') do |file|
file << open(image_data["docker_file_path"]).read
end
end
docker_url = "#{image_data["docker_name"]}"
command = {:type=>"notify",
:title=>"docker build",
:logs=>true,
:before_execute_log=>"Building docker image",
:timeout=>3600,
:command=>"sudo docker build . -t #{docker_url} -f #{file_name}"}
@executer = Helpers::Executer.new(project: @project, job_type: "image", job_id: @image_id, image: self)
exit_status, output, errors, _, _ = @executer.execute(command)
all_logs = join_logs(output, errors)
if exit_status != 0
raise StandardError.new(all_logs)
end
if ENV["CNVRG_IMAGE_BUILD_USERNAME"].present? and ENV["CNVRG_IMAGE_BUILD_PASSWORD"].present?
if ENV["CNVRG_IMAGE_BUILD_REGISTRY"].present?
command = {:type=>"notify",
:no_stdout => true,
:title=>"docker login",
:logs=>true,
:command=>"sudo docker login #{ENV["CNVRG_IMAGE_BUILD_REGISTRY"]} --username=#{ENV["CNVRG_IMAGE_BUILD_USERNAME"]} --password=\"#{ENV["CNVRG_IMAGE_BUILD_PASSWORD"]}\""}
else
command = {:type=>"notify",
:no_stdout => true,
:title=>"docker login",
:logs=>true,
:command=>"sudo docker login --username=#{ENV["CNVRG_IMAGE_BUILD_USERNAME"]} --password=\"#{ENV["CNVRG_IMAGE_BUILD_PASSWORD"]}\""}
end
exit_status, output, errors, _, _ = @executer.execute(command)
all_logs = join_logs(output, errors)
if exit_status != 0
raise StandardError.new(all_logs)
end
end
command = {:type=>"notify",
:title=>"docker push",
:logs=>true,
:before_execute_log=>"Pushing docker image",
:timeout=>3600,
:command=>"sudo docker push #{docker_url}"}
exit_status, output, errors, _, _ = @executer.execute(command)
all_logs = join_logs(output, errors)
if exit_status != 0
raise StandardError.new(all_logs)
end
post_build_update(true)
rescue => e
@cli.log_message("Image Build failed")
post_build_update(false, e.message)
end
|