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
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/nomade/http.rb', line 61 def allocations_from_evaluation_request(evaluation_id) uri = URI("#{@nomad_endpoint}/v1/evaluation/#{evaluation_id}/allocations") http = Net::HTTP.new(uri.host, uri.port) if @nomad_endpoint.include?("https://") http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_PEER end 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.})" raise end |
#check_if_job_exists?(nomad_job) ⇒ Boolean
107 108 109 110 |
# File 'lib/nomade/http.rb', line 107 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
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/nomade/http.rb', line 112 def create_job(nomad_job) uri = URI("#{@nomad_endpoint}/v1/jobs") http = Net::HTTP.new(uri.host, uri.port) if @nomad_endpoint.include?("https://") http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_PEER end 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.})" raise end |
#deployment_request(deployment_id) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/nomade/http.rb', line 84 def deployment_request(deployment_id) uri = URI("#{@nomad_endpoint}/v1/deployment/#{deployment_id}") http = Net::HTTP.new(uri.host, uri.port) if @nomad_endpoint.include?("https://") http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_PEER end 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.})" raise end |
#evaluation_request(evaluation_id) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/nomade/http.rb', line 38 def evaluation_request(evaluation_id) uri = URI("#{@nomad_endpoint}/v1/evaluation/#{evaluation_id}") http = Net::HTTP.new(uri.host, uri.port) if @nomad_endpoint.include?("https://") http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_PEER end 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.})" raise end |
#fail_deployment(deployment_id) ⇒ Object
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/nomade/http.rb', line 184 def fail_deployment(deployment_id) uri = URI("#{@nomad_endpoint}/v1/deployment/fail/#{deployment_id}") http = Net::HTTP.new(uri.host, uri.port) if @nomad_endpoint.include?("https://") http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_PEER end 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.})" raise end |
#get_allocation_logs(allocation_id, task_name, logtype) ⇒ Object
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
# File 'lib/nomade/http.rb', line 206 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) if @nomad_endpoint.include?("https://") http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_PEER end 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.})" 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 35 36 |
# 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) if @nomad_endpoint.include?("https://") http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_PEER end 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.})" raise end |
#plan_job(nomad_job) ⇒ Object
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
# File 'lib/nomade/http.rb', line 225 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
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/nomade/http.rb', line 158 def promote_deployment(deployment_id) uri = URI("#{@nomad_endpoint}/v1/deployment/promote/#{deployment_id}") http = Net::HTTP.new(uri.host, uri.port) if @nomad_endpoint.include?("https://") http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_PEER end 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.})" raise end |
#update_job(nomad_job) ⇒ Object
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/nomade/http.rb', line 135 def update_job(nomad_job) uri = URI("#{@nomad_endpoint}/v1/job/#{nomad_job.job_name}") http = Net::HTTP.new(uri.host, uri.port) if @nomad_endpoint.include?("https://") http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_PEER end 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.})" raise end |