Class: Nomade::Http
- Inherits:
-
Object
- Object
- Nomade::Http
- Defined in:
- lib/nomade/http.rb
Instance Method Summary collapse
- #allocations_from_evaluation_request(evaluation_id) ⇒ Object
- #capacity_plan_job(nomad_job) ⇒ Object
- #check_if_job_exists?(nomad_job) ⇒ Boolean
- #convert_hcl_to_json(job_hcl) ⇒ Object
- #create_job(nomad_job) ⇒ Object
- #deployment_request(deployment_id) ⇒ Object
- #dispatch_job(nomad_job, payload_data: nil, payload_metadata: nil) ⇒ Object
- #evaluation_request(evaluation_id) ⇒ Object
- #fail_deployment(deployment_id) ⇒ Object
- #get_allocation_logs(allocation_id, task_name, logtype) ⇒ Object
-
#initialize(nomad_endpoint) ⇒ Http
constructor
A new instance of Http.
- #job_index_request(search_prefix = nil) ⇒ Object
- #plan_job(nomad_job) ⇒ Object
- #promote_deployment(deployment_id) ⇒ Object
- #stop_job(nomad_job, purge = false) ⇒ Object
- #update_job(nomad_job) ⇒ Object
Constructor Details
#initialize(nomad_endpoint) ⇒ Http
Returns a new instance of Http.
6 7 8 |
# File 'lib/nomade/http.rb', line 6 def initialize(nomad_endpoint) @nomad_endpoint = nomad_endpoint end |
Instance Method Details
#allocations_from_evaluation_request(evaluation_id) ⇒ Object
32 33 34 35 36 37 38 |
# File 'lib/nomade/http.rb', line 32 def allocations_from_evaluation_request(evaluation_id) res_body = _request(:get, "/v1/evaluation/#{evaluation_id}/allocations", total_retries: 3) return JSON.parse(res_body) rescue StandardError => e Nomade.logger.fatal "HTTP Request failed (#{e.})" raise end |
#capacity_plan_job(nomad_job) ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/nomade/http.rb', line 120 def capacity_plan_job(nomad_job) plan_output = plan_job(nomad_job) if plan_output["FailedTGAllocs"] raise Nomade::FailedTaskGroupPlan.new("Failed to plan groups: #{plan_output["FailedTGAllocs"].keys.join(",")}") end true rescue Nomade::FailedTaskGroupPlan raise rescue StandardError => e Nomade.logger.fatal "HTTP Request failed (#{e.})" raise end |
#check_if_job_exists?(nomad_job) ⇒ Boolean
49 50 51 52 |
# File 'lib/nomade/http.rb', line 49 def check_if_job_exists?(nomad_job) jobs = job_index_request(nomad_job.job_name) jobs.map{|job| job["ID"]}.include?(nomad_job.job_name) end |
#convert_hcl_to_json(job_hcl) ⇒ Object
135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/nomade/http.rb', line 135 def convert_hcl_to_json(job_hcl) req_body = JSON.generate({ "JobHCL": job_hcl, "Canonicalize": false, }) res_body = _request(:post, "/v1/jobs/parse", body: req_body, total_retries: 3) res_body rescue StandardError => e Nomade.logger.fatal "HTTP Request failed (#{e.})" raise end |
#create_job(nomad_job) ⇒ Object
54 55 56 57 58 59 60 61 62 |
# File 'lib/nomade/http.rb', line 54 def create_job(nomad_job) req_body = JSON.generate({"Job" => nomad_job.configuration(:hash)}) res_body = _request(:post, "/v1/jobs", body: req_body) return JSON.parse(res_body)["EvalID"] rescue StandardError => e Nomade.logger.fatal "HTTP Request failed (#{e.})" raise end |
#deployment_request(deployment_id) ⇒ Object
40 41 42 43 44 45 46 47 |
# File 'lib/nomade/http.rb', line 40 def deployment_request(deployment_id) res_body = _request(:get, "/v1/deployment/#{deployment_id}", total_retries: 3) return JSON.parse(res_body) rescue StandardError => e Nomade.logger.fatal "HTTP Request failed (#{e.})" raise end |
#dispatch_job(nomad_job, payload_data: nil, payload_metadata: nil) ⇒ Object
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/nomade/http.rb', line 157 def dispatch_job(nomad_job, payload_data: nil, payload_metadata: nil) if .class != Hash raise DispatchMetaDataFormattingError.new("Expected #{} to be a Hash, but received #{.class}") end req_body = JSON.generate({ "Payload": payload_data, "Meta": , }.delete_if { |_k, v| v.nil? }) res_body = _request(:post, "/v1/job/#{nomad_job.job_name}/dispatch", body: req_body) JSON.parse(res_body) rescue StandardError => e Nomade.logger.fatal "HTTP Request failed (#{e.})" raise end |
#evaluation_request(evaluation_id) ⇒ Object
24 25 26 27 28 29 30 |
# File 'lib/nomade/http.rb', line 24 def evaluation_request(evaluation_id) res_body = _request(:get, "/v1/evaluation/#{evaluation_id}", total_retries: 3) return JSON.parse(res_body) rescue StandardError => e Nomade.logger.fatal "HTTP Request failed (#{e.})" raise end |
#fail_deployment(deployment_id) ⇒ Object
101 102 103 104 105 106 107 |
# File 'lib/nomade/http.rb', line 101 def fail_deployment(deployment_id) _request(:post, "/v1/deployment/fail/#{deployment_id}") return true rescue StandardError => e Nomade.logger.fatal "HTTP Request failed (#{e.})" raise end |
#get_allocation_logs(allocation_id, task_name, logtype) ⇒ Object
109 110 111 112 113 114 115 116 117 118 |
# File 'lib/nomade/http.rb', line 109 def get_allocation_logs(allocation_id, task_name, logtype) res_body = _request(:get, "/v1/client/fs/logs/#{allocation_id}?task=#{task_name}&type=#{logtype}&plain=true&origin=end", total_retries: 3, expected_content_type: "text/plain", ) return res_body.gsub(/\e\[\d+m/, '') rescue StandardError => e Nomade.logger.fatal "HTTP Request failed (#{e.})" raise end |
#job_index_request(search_prefix = nil) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/nomade/http.rb', line 10 def job_index_request(search_prefix = nil) search_prefix = if search_prefix "?prefix=#{search_prefix}" else "" end path = "/v1/jobs#{search_prefix}" res_body = _request(:get, path, total_retries: 3) return JSON.parse(res_body) rescue StandardError => e Nomade.logger.fatal "HTTP Request failed (#{e.})" raise end |
#plan_job(nomad_job) ⇒ Object
147 148 149 150 151 152 153 154 155 |
# File 'lib/nomade/http.rb', line 147 def plan_job(nomad_job) req_body = JSON.generate({"Job" => nomad_job.configuration(:hash)}) res_body = _request(:post, "/v1/job/#{nomad_job.job_name}/plan", body: req_body) JSON.parse(res_body) rescue StandardError => e Nomade.logger.fatal "HTTP Request failed (#{e.})" raise end |
#promote_deployment(deployment_id) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/nomade/http.rb', line 88 def promote_deployment(deployment_id) req_body = { "DeploymentID" => deployment_id, "All" => true, }.to_json _request(:post, "/v1/deployment/promote/#{deployment_id}", body: req_body) return true rescue StandardError => e Nomade.logger.fatal "HTTP Request failed (#{e.})" raise end |
#stop_job(nomad_job, purge = false) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/nomade/http.rb', line 74 def stop_job(nomad_job, purge = false) path = if purge "/v1/job/#{nomad_job.job_name}?purge=true" else "/v1/job/#{nomad_job.job_name}" end res_body = _request(:delete, path) return JSON.parse(res_body)["EvalID"] rescue StandardError => e Nomade.logger.fatal "HTTP Request failed (#{e.})" raise end |
#update_job(nomad_job) ⇒ Object
64 65 66 67 68 69 70 71 72 |
# File 'lib/nomade/http.rb', line 64 def update_job(nomad_job) req_body = JSON.generate({"Job" => nomad_job.configuration(:hash)}) res_body = _request(:post, "/v1/job/#{nomad_job.job_name}", body: req_body) return JSON.parse(res_body)["EvalID"] rescue StandardError => e Nomade.logger.fatal "HTTP Request failed (#{e.})" raise end |