Class: Cnvrg::Helpers::Executer
- Inherits:
-
Object
- Object
- Cnvrg::Helpers::Executer
- Defined in:
- lib/cnvrg/helpers/executer.rb
Defined Under Namespace
Modules: CommandsStatus
Class Method Summary collapse
Instance Method Summary collapse
- #decrypt_commands(text, key, iv) ⇒ Object
- #execute(cmd) ⇒ Object
- #execute_cmds(cmds) ⇒ Object
- #fetch_commands(block: nil, key: nil) ⇒ Object
- #get_command(command_slug) ⇒ Object
- #get_commands ⇒ Object
- #get_requirements_commands ⇒ Object
-
#initialize(project: nil, job_type: nil, job_id: nil, image: nil) ⇒ Executer
constructor
A new instance of Executer.
- #monitor_command(command, command_slug) ⇒ Object
- #set_dataset_status(dataset: nil, status: nil) ⇒ Object
- #update_command(status, context, command_slug) ⇒ Object
- #update_git_commit ⇒ Object
Constructor Details
#initialize(project: nil, job_type: nil, job_id: nil, image: nil) ⇒ Executer
Returns a new instance of Executer.
8 9 10 11 12 13 14 15 16 17 |
# File 'lib/cnvrg/helpers/executer.rb', line 8 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.("Cant find job, exiting.", 'red') exit(1) end end |
Class Method Details
.get_executer ⇒ Object
20 21 22 23 24 25 |
# File 'lib/cnvrg/helpers/executer.rb', line 20 def self.get_executer if ENV["CNVRG_JOB_ID"].blank? return end self.new end |
Instance Method Details
#decrypt_commands(text, key, iv) ⇒ Object
33 34 35 36 37 38 39 40 41 |
# File 'lib/cnvrg/helpers/executer.rb', line 33 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
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 |
# File 'lib/cnvrg/helpers/executer.rb', line 43 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 # when type == 'notify_command' # notify to commands api about command progress # when type == 'spawn' # run in another process and detach from it # 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
77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/cnvrg/helpers/executer.rb', line 77 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
27 28 29 30 31 |
# File 'lib/cnvrg/helpers/executer.rb', line 27 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_command(command_slug) ⇒ Object
101 102 103 104 105 |
# File 'lib/cnvrg/helpers/executer.rb', line 101 def get_command(command_slug) resp = Cnvrg::API.request("#{base_url}/commands/#{command_slug}", "GET") command = resp["command"] command.with_indifferent_access end |
#get_commands ⇒ Object
95 96 97 98 99 |
# File 'lib/cnvrg/helpers/executer.rb', line 95 def get_commands resp = Cnvrg::API.request("#{base_url}/commands", "GET") commands = resp["commands"] commands.map{|k| k.with_indifferent_access} end |
#get_requirements_commands ⇒ Object
89 90 91 92 93 |
# File 'lib/cnvrg/helpers/executer.rb', line 89 def get_requirements_commands resp = Cnvrg::API.request("#{base_url}/requirements", "GET") commands = resp["commands"] commands.map{|k| k.with_indifferent_access} end |
#monitor_command(command, command_slug) ⇒ Object
122 123 124 |
# File 'lib/cnvrg/helpers/executer.rb', line 122 def monitor_command(command, command_slug) monitor_single_command(command, command_slug) end |
#set_dataset_status(dataset: nil, status: nil) ⇒ Object
118 119 120 |
# File 'lib/cnvrg/helpers/executer.rb', line 118 def set_dataset_status(dataset: nil, status: nil) Cnvrg::API.request("#{base_url}/datasets/#{dataset}", "PUT", {status: status}) end |
#update_command(status, context, command_slug) ⇒ Object
107 108 109 |
# File 'lib/cnvrg/helpers/executer.rb', line 107 def update_command(status, context, command_slug) Cnvrg::API.request("#{base_url}/commands/#{command_slug}", "PUT", {status: status, context: context, timestamp: Time.now}) end |
#update_git_commit ⇒ Object
112 113 114 115 116 |
# File 'lib/cnvrg/helpers/executer.rb', line 112 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 |