Class: Cnvrg::Image

Inherits:
Object
  • Object
show all
Defined in:
lib/cnvrg/image.rb

Instance Method Summary collapse

Constructor Details

#initialize(image_id) ⇒ Image

Returns a new instance of Image.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/cnvrg/image.rb', line 6

def initialize(image_id)
  begin
    @cli = Cnvrg::CLI.new
    home_dir = File.expand_path('~')
    config = YAML.load_file(home_dir+"/.cnvrg/config.yml")
    @owner = config.to_h[:owner]
    @username =  config.to_h[:username]
    @image_id = image_id
  rescue => e
    @owner = ""
    @username =  ""
    @cli.log_message("cnvrg is not configured")
  end
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
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

#get_image_dataObject



86
87
88
89
90
# File 'lib/cnvrg/image.rb', line 86

def get_image_data
  response = Cnvrg::API.request("users/#{@owner}/images/#{@image_id}/image_start_build", 'GET')
  CLI.is_response_success(response)
  return response["image"]
end

#job_log(logs, level: 'info', step: nil, job_type: "image", job_id: @image_id) ⇒ Object



99
100
101
102
103
104
105
# File 'lib/cnvrg/image.rb', line 99

def job_log(logs, level: 'info', step: nil, job_type: "image", job_id: @image_id)
  logs = [logs].flatten
  logs.each_slice(10).each do |temp_logs|
    Cnvrg::API.request("users/#{@owner}/images/#{@image_id}/log", "POST", {job_type: job_type, job_id: job_id, logs: temp_logs, log_level: level, step: step, timestamp: Time.now})
    sleep(1)
  end
end

#join_logs(output, errors) ⇒ Object



108
109
110
# File 'lib/cnvrg/image.rb', line 108

def join_logs(output, errors)
  output.map{ |o| o[:logs]}.join(" ") + " " + errors.map{ |o| o[:logs]}.join(" ")
end

#post_build_update(success, message = "") ⇒ Object



92
93
94
95
96
# File 'lib/cnvrg/image.rb', line 92

def post_build_update(success, message = "")
  response = Cnvrg::API.request("users/#{@owner}/images/#{@image_id}/image_end_build", 'POST', {success: success, message: message})
  CLI.is_response_success(response)
  return response["image"]
end