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
- #check_if_job_exists?(nomad_job) ⇒ Boolean
- #create_job(nomad_job) ⇒ Object
- #deployment_request(deployment_id) ⇒ 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
- #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
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/nomade/http.rb', line 57 def allocations_from_evaluation_request(evaluation_id) uri = URI("#{@nomad_endpoint}/v1/evaluation/#{evaluation_id}/allocations") http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_PEER req = Net::HTTP::Get.new(uri) req.add_field "Content-Type", "application/json" res = http.request(req) raise if res.code != "200" raise if res.content_type != "application/json" return JSON.parse(res.body) rescue StandardError => e Nomade.logger.fatal "HTTP Request failed (#{e.message})" raise end |
#check_if_job_exists?(nomad_job) ⇒ Boolean
99 100 101 102 |
# File 'lib/nomade/http.rb', line 99 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 |
#create_job(nomad_job) ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/nomade/http.rb', line 104 def create_job(nomad_job) uri = URI("#{@nomad_endpoint}/v1/jobs") http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_PEER req = Net::HTTP::Post.new(uri) req.add_field "Content-Type", "application/json" req.body = nomad_job.configuration(:json) res = http.request(req) raise if res.code != "200" raise if res.content_type != "application/json" return JSON.parse(res.body)["EvalID"] rescue StandardError => e Nomade.logger.fatal "HTTP Request failed (#{e.message})" raise end |
#deployment_request(deployment_id) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/nomade/http.rb', line 78 def deployment_request(deployment_id) uri = URI("#{@nomad_endpoint}/v1/deployment/#{deployment_id}") http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_PEER req = Net::HTTP::Get.new(uri) req.add_field "Content-Type", "application/json" res = http.request(req) raise if res.code != "200" raise if res.content_type != "application/json" return JSON.parse(res.body) rescue StandardError => e Nomade.logger.fatal "HTTP Request failed (#{e.message})" raise end |
#evaluation_request(evaluation_id) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/nomade/http.rb', line 36 def evaluation_request(evaluation_id) uri = URI("#{@nomad_endpoint}/v1/evaluation/#{evaluation_id}") http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_PEER req = Net::HTTP::Get.new(uri) req.add_field "Content-Type", "application/json" res = http.request(req) raise if res.code != "200" raise if res.content_type != "application/json" return JSON.parse(res.body) rescue StandardError => e Nomade.logger.fatal "HTTP Request failed (#{e.message})" raise end |
#fail_deployment(deployment_id) ⇒ Object
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/nomade/http.rb', line 170 def fail_deployment(deployment_id) uri = URI("#{@nomad_endpoint}/v1/deployment/fail/#{deployment_id}") http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_PEER req = Net::HTTP::Post.new(uri) req.add_field "Content-Type", "application/json" res = http.request(req) raise if res.code != "200" raise if res.content_type != "application/json" return true rescue StandardError => e Nomade.logger.fatal "HTTP Request failed (#{e.message})" raise end |
#get_allocation_logs(allocation_id, task_name, logtype) ⇒ Object
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/nomade/http.rb', line 190 def get_allocation_logs(allocation_id, task_name, logtype) uri = URI("#{@nomad_endpoint}/v1/client/fs/logs/#{allocation_id}?task=#{task_name}&type=#{logtype}&plain=true&origin=end") http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_PEER req = Net::HTTP::Get.new(uri) res = http.request(req) raise if res.code != "200" return res.body.gsub(/\e\[\d+m/, '') rescue StandardError => e Nomade.logger.fatal "HTTP Request failed (#{e.message})" raise end |
#job_index_request(search_prefix = nil) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/nomade/http.rb', line 10 def job_index_request(search_prefix = nil) search_prefix = if search_prefix "?prefix=#{search_prefix}" else "" end uri = URI("#{@nomad_endpoint}/v1/jobs#{search_prefix}") http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_PEER req = Net::HTTP::Get.new(uri) req.add_field "Content-Type", "application/json" res = http.request(req) raise if res.code != "200" raise if res.content_type != "application/json" return JSON.parse(res.body) rescue StandardError => e Nomade.logger.fatal "HTTP Request failed (#{e.message})" raise end |
#plan_job(nomad_job) ⇒ Object
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
# File 'lib/nomade/http.rb', line 207 def plan_job(nomad_job) rendered_template = nomad_job.configuration(:hcl) # 0: No allocations created or destroyed. Nothing to do. # 1: Allocations created or destroyed. # 255: Error determining plan results. Nothing to do. allowed_exit_codes = [0, 1, 255] exit_status, stdout, stderr = Shell.exec("NOMAD_ADDR=#{@nomad_endpoint} nomad job plan -diff -verbose -no-color -", rendered_template, allowed_exit_codes) case exit_status when 0 raise Nomade::NoModificationsError.new when 1 # no-op when 255 raise Nomade::PlanningError.new end true end |
#promote_deployment(deployment_id) ⇒ Object
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/nomade/http.rb', line 146 def promote_deployment(deployment_id) uri = URI("#{@nomad_endpoint}/v1/deployment/promote/#{deployment_id}") http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_PEER req = Net::HTTP::Post.new(uri) req.add_field "Content-Type", "application/json" req.body = { "DeploymentID" => deployment_id, "All" => true, }.to_json res = http.request(req) raise if res.code != "200" raise if res.content_type != "application/json" return true rescue StandardError => e Nomade.logger.fatal "HTTP Request failed (#{e.message})" raise end |
#update_job(nomad_job) ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/nomade/http.rb', line 125 def update_job(nomad_job) uri = URI("#{@nomad_endpoint}/v1/job/#{nomad_job.job_name}") http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_PEER req = Net::HTTP::Post.new(uri) req.add_field "Content-Type", "application/json" req.body = nomad_job.configuration(:json) res = http.request(req) raise if res.code != "200" raise if res.content_type != "application/json" return JSON.parse(res.body)["EvalID"] rescue StandardError => e Nomade.logger.fatal "HTTP Request failed (#{e.message})" raise end |