Class: Cnvrg::Helpers::Executer

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project: nil, job_type: nil, job_id: nil, image: nil) ⇒ Executer

Returns a new instance of Executer.



2
3
4
5
6
7
8
9
10
11
# File 'lib/cnvrg/helpers/executer.rb', line 2

def initialize(project: nil, job_type: nil, job_id: nil, image: nil)
  @image = image
  @project = project || Cnvrg::Project.new(owner: ENV['CNVRG_OWNER'], slug: ENV['CNVRG_PROJECT'])
  @job_type = job_type || ENV['CNVRG_JOB_TYPE']
  @job_id = job_id || ENV['CNVRG_JOB_ID']
  if @job_id.blank?
    Cnvrg::CLI.log_message("Cant find job, exiting.", 'red')
    exit(1)
  end
end

Class Method Details

.get_executerObject



14
15
16
17
18
19
# File 'lib/cnvrg/helpers/executer.rb', line 14

def self.get_executer
  if ENV["CNVRG_JOB_ID"].blank?
    return
  end
  self.new
end

Instance Method Details

#decrypt_commands(text, key, iv) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/cnvrg/helpers/executer.rb', line 27

def decrypt_commands(text,key,iv)
  text, key, iv = [text,key,iv].map{|x| x.unpack('m')[0]}
  decipher = OpenSSL::Cipher::AES256.new :CBC
  decipher.decrypt
  decipher.key = key
  decipher.iv = iv
  commands = decipher.update(text) + decipher.final
  JSON.parse(commands)
end

#execute(cmd) ⇒ Object



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
# File 'lib/cnvrg/helpers/executer.rb', line 37

def execute(cmd)
  ## execute the command for running
  # cmd will have to following fields
  #
  # :command => the command to execute
  # :type => the command type, 'notify' or nil
  # :timeout => the timeout for the command in seconds (default is 60 hours)
  # :retries => integer, default 1
  #
  # when type == 'file_exists'
  #  'file' => string => file to check (fullpath)
  #  'exists_commands' => list of commands in case file exists
  #   'non_exists_commands' => list of commands in case file doesnt exists
  # when type == 'notify'
  # :before_execute_log => log to be logged before execution
  # :logs => boolean => add the execution logs to the job logs
  # :title => command title, can replace the on_error, on_success fields
  # :on_error_log => log to be logged on exit_code != 0
  # :on_success_log => log to be logged on exit_code == 0
  #
  retries = cmd[:retries] || 1
  resp = []
  retries.times.each do
    resp = execute_helper(cmd)
    exit_status, _, _, _, _ = resp
    return resp if exit_status == 0
  end
  return resp
end

#execute_cmds(cmds) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/cnvrg/helpers/executer.rb', line 67

def execute_cmds(cmds)
  cmds.each do |command|
    puts "===================="
    if command[:type] == "file_exists"
      puts "Looking for file #{command[:file]}"
    else
      puts "Execute #{command[:command]}" unless command[:no_stdout]
    end
    execute(command)
  end
end

#fetch_commands(block: nil, key: nil) ⇒ Object



21
22
23
24
25
# File 'lib/cnvrg/helpers/executer.rb', line 21

def fetch_commands(block: nil, key: nil)
  resp = Cnvrg::API.request("#{base_url}/commands", "GET", {block: block})
  commands = decrypt_commands(resp["commands"], resp["key"], resp["iv"])
  commands.map{|k| k.with_indifferent_access}
end

#get_requirements_commandsObject



79
80
81
82
83
# File 'lib/cnvrg/helpers/executer.rb', line 79

def get_requirements_commands
  resp = Cnvrg::API.request("#{base_url}/requirements", "GET")
  commands = resp["commands"]
  commands.map{|k| k.with_indifferent_access}
end

#set_dataset_status(dataset: nil, status: nil) ⇒ Object



92
93
94
# File 'lib/cnvrg/helpers/executer.rb', line 92

def set_dataset_status(dataset: nil, status: nil)
  Cnvrg::API.request("#{base_url}/datasets/#{dataset}", "PUT", {status: status})
end

#update_git_commitObject



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

def update_git_commit
  git_commit = `git rev-parse --verify HEAD`
  return if git_commit.blank?
  Cnvrg::API.request("#{base_url}/update_git_commit", "POST", {git_commit: git_commit.strip!})
end