nomade 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/nomade/http.rb +77 -188
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c6c3684724566430b8c0af280a5be1e03be57a5c7bcac2298c22d14d03098565
4
- data.tar.gz: ae59d84da529ab3e4041d52a9216f0cf4e68cd7cd39751d895907d05f52b19cf
3
+ metadata.gz: 57489957cb990a28ffbd23c94b9d655ec517a43cd4e8f2101d7e7cddb17bfcf7
4
+ data.tar.gz: 305ac1c39551b472ad16c3553fb4f5965b4eebfb08ea51b2557ff26cd8153af8
5
5
  SHA512:
6
- metadata.gz: 1fb1d40654d23c00694ca29e9423050e3e829dd543dd293b5655c7def2885fa0c73593bc06a35a7f3c7f53869b3f4afc470077d9e9613cb8b0d113d1328ee49f
7
- data.tar.gz: be3a100801b40b10a38f162095a86f3f36e386b9ff17e46ee7597e7ab37c373e059c8472acbfa6262bd56d37743252d5bb7780925812cfef6227a7f18c63d54f
6
+ metadata.gz: 17a0f1d1cb4e15de6ec6fedea6f5d0d4675e72628596f0fba951c3143699ac79eaeed36138d7b6d1f895f2689b3f187c839e67c782a6c201f7ce57ba34dfbd53
7
+ data.tar.gz: 5ca41d3f6f53e5e670512575b256ef06688d3ce54303303b0075ab2ba9e1088fbe0f3fe856fe3e2568d958e6938323e0333195ee7eab54de7df4557d37b96a99
data/lib/nomade/http.rb CHANGED
@@ -13,92 +13,34 @@ module Nomade
13
13
  else
14
14
  ""
15
15
  end
16
- uri = URI("#{@nomad_endpoint}/v1/jobs#{search_prefix}")
17
-
18
- http = Net::HTTP.new(uri.host, uri.port)
19
- if @nomad_endpoint.include?("https://")
20
- http.use_ssl = true
21
- http.verify_mode = OpenSSL::SSL::VERIFY_PEER
22
- end
23
-
24
- req = Net::HTTP::Get.new(uri)
25
- req.add_field "Content-Type", "application/json"
26
-
27
- res = http.request(req)
28
-
29
- raise if res.code != "200"
30
- raise if res.content_type != "application/json"
31
-
32
- return JSON.parse(res.body)
16
+ path = "/v1/jobs#{search_prefix}"
17
+ res_body = _request(:get, path, total_retries: 3)
18
+ return JSON.parse(res_body)
33
19
  rescue StandardError => e
34
20
  Nomade.logger.fatal "HTTP Request failed (#{e.message})"
35
21
  raise
36
22
  end
37
23
 
38
24
  def evaluation_request(evaluation_id)
39
- uri = URI("#{@nomad_endpoint}/v1/evaluation/#{evaluation_id}")
40
-
41
- http = Net::HTTP.new(uri.host, uri.port)
42
- if @nomad_endpoint.include?("https://")
43
- http.use_ssl = true
44
- http.verify_mode = OpenSSL::SSL::VERIFY_PEER
45
- end
46
-
47
- req = Net::HTTP::Get.new(uri)
48
- req.add_field "Content-Type", "application/json"
49
-
50
- res = http.request(req)
51
-
52
- raise if res.code != "200"
53
- raise if res.content_type != "application/json"
54
-
55
- return JSON.parse(res.body)
25
+ res_body = _request(:get, "/v1/evaluation/#{evaluation_id}", total_retries: 3)
26
+ return JSON.parse(res_body)
56
27
  rescue StandardError => e
57
28
  Nomade.logger.fatal "HTTP Request failed (#{e.message})"
58
29
  raise
59
30
  end
60
31
 
61
32
  def allocations_from_evaluation_request(evaluation_id)
62
- uri = URI("#{@nomad_endpoint}/v1/evaluation/#{evaluation_id}/allocations")
63
-
64
- http = Net::HTTP.new(uri.host, uri.port)
65
- if @nomad_endpoint.include?("https://")
66
- http.use_ssl = true
67
- http.verify_mode = OpenSSL::SSL::VERIFY_PEER
68
- end
69
-
70
- req = Net::HTTP::Get.new(uri)
71
- req.add_field "Content-Type", "application/json"
72
-
73
- res = http.request(req)
74
-
75
- raise if res.code != "200"
76
- raise if res.content_type != "application/json"
77
-
78
- return JSON.parse(res.body)
33
+ res_body = _request(:get, "/v1/evaluation/#{evaluation_id}/allocations", total_retries: 3)
34
+ return JSON.parse(res_body)
79
35
  rescue StandardError => e
80
36
  Nomade.logger.fatal "HTTP Request failed (#{e.message})"
81
37
  raise
82
38
  end
83
39
 
84
40
  def deployment_request(deployment_id)
85
- uri = URI("#{@nomad_endpoint}/v1/deployment/#{deployment_id}")
41
+ res_body = _request(:get, "/v1/deployment/#{deployment_id}", total_retries: 3)
86
42
 
87
- http = Net::HTTP.new(uri.host, uri.port)
88
- if @nomad_endpoint.include?("https://")
89
- http.use_ssl = true
90
- http.verify_mode = OpenSSL::SSL::VERIFY_PEER
91
- end
92
-
93
- req = Net::HTTP::Get.new(uri)
94
- req.add_field "Content-Type", "application/json"
95
-
96
- res = http.request(req)
97
-
98
- raise if res.code != "200"
99
- raise if res.content_type != "application/json"
100
-
101
- return JSON.parse(res.body)
43
+ return JSON.parse(res_body)
102
44
  rescue StandardError => e
103
45
  Nomade.logger.fatal "HTTP Request failed (#{e.message})"
104
46
  raise
@@ -110,98 +52,45 @@ module Nomade
110
52
  end
111
53
 
112
54
  def create_job(nomad_job)
113
- uri = URI("#{@nomad_endpoint}/v1/jobs")
114
-
115
- http = Net::HTTP.new(uri.host, uri.port)
116
- if @nomad_endpoint.include?("https://")
117
- http.use_ssl = true
118
- http.verify_mode = OpenSSL::SSL::VERIFY_PEER
119
- end
120
-
121
- req = Net::HTTP::Post.new(uri)
122
- req.add_field "Content-Type", "application/json"
123
- req.body = JSON.generate({"Job" => nomad_job.configuration(:hash)})
124
-
125
- res = http.request(req)
55
+ req_body = JSON.generate({"Job" => nomad_job.configuration(:hash)})
56
+ res_body = _request(:post, "/v1/jobs", body: req_body)
126
57
 
127
- raise if res.code != "200"
128
- raise if res.content_type != "application/json"
129
-
130
- return JSON.parse(res.body)["EvalID"]
58
+ return JSON.parse(res_body)["EvalID"]
131
59
  rescue StandardError => e
132
60
  Nomade.logger.fatal "HTTP Request failed (#{e.message})"
133
61
  raise
134
62
  end
135
63
 
136
64
  def update_job(nomad_job)
137
- uri = URI("#{@nomad_endpoint}/v1/job/#{nomad_job.job_name}")
138
-
139
- http = Net::HTTP.new(uri.host, uri.port)
140
- if @nomad_endpoint.include?("https://")
141
- http.use_ssl = true
142
- http.verify_mode = OpenSSL::SSL::VERIFY_PEER
143
- end
144
-
145
- req = Net::HTTP::Post.new(uri)
146
- req.add_field "Content-Type", "application/json"
147
- req.body = JSON.generate({"Job" => nomad_job.configuration(:hash)})
148
-
149
- res = http.request(req)
150
-
151
- raise if res.code != "200"
152
- raise if res.content_type != "application/json"
65
+ req_body = JSON.generate({"Job" => nomad_job.configuration(:hash)})
66
+ res_body = _request(:post, "/v1/job/#{nomad_job.job_name}", body: req_body)
153
67
 
154
- return JSON.parse(res.body)["EvalID"]
68
+ return JSON.parse(res_body)["EvalID"]
155
69
  rescue StandardError => e
156
70
  Nomade.logger.fatal "HTTP Request failed (#{e.message})"
157
71
  raise
158
72
  end
159
73
 
160
74
  def stop_job(nomad_job, purge = false)
161
- uri = if purge
162
- URI("#{@nomad_endpoint}/v1/job/#{nomad_job.job_name}?purge=true")
75
+ path = if purge
76
+ "/v1/job/#{nomad_job.job_name}?purge=true"
163
77
  else
164
- URI("#{@nomad_endpoint}/v1/job/#{nomad_job.job_name}")
78
+ "/v1/job/#{nomad_job.job_name}"
165
79
  end
166
80
 
167
- http = Net::HTTP.new(uri.host, uri.port)
168
- if @nomad_endpoint.include?("https://")
169
- http.use_ssl = true
170
- http.verify_mode = OpenSSL::SSL::VERIFY_PEER
171
- end
172
-
173
- req = Net::HTTP::Delete.new(uri)
174
- req.add_field "Content-Type", "application/json"
175
-
176
- res = http.request(req)
177
- raise if res.code != "200"
178
- raise if res.content_type != "application/json"
179
-
180
- return JSON.parse(res.body)["EvalID"]
81
+ res_body = _request(:delete, path)
82
+ return JSON.parse(res_body)["EvalID"]
181
83
  rescue StandardError => e
182
84
  Nomade.logger.fatal "HTTP Request failed (#{e.message})"
183
85
  raise
184
86
  end
185
87
 
186
88
  def promote_deployment(deployment_id)
187
- uri = URI("#{@nomad_endpoint}/v1/deployment/promote/#{deployment_id}")
188
-
189
- http = Net::HTTP.new(uri.host, uri.port)
190
- if @nomad_endpoint.include?("https://")
191
- http.use_ssl = true
192
- http.verify_mode = OpenSSL::SSL::VERIFY_PEER
193
- end
194
-
195
- req = Net::HTTP::Post.new(uri)
196
- req.add_field "Content-Type", "application/json"
197
- req.body = {
89
+ req_body = {
198
90
  "DeploymentID" => deployment_id,
199
91
  "All" => true,
200
92
  }.to_json
201
-
202
- res = http.request(req)
203
- raise if res.code != "200"
204
- raise if res.content_type != "application/json"
93
+ res_body = _request(:post, "/v1/deployment/promote/#{deployment_id}", body: req_body)
205
94
 
206
95
  return true
207
96
  rescue StandardError => e
@@ -210,21 +99,7 @@ module Nomade
210
99
  end
211
100
 
212
101
  def fail_deployment(deployment_id)
213
- uri = URI("#{@nomad_endpoint}/v1/deployment/fail/#{deployment_id}")
214
-
215
- http = Net::HTTP.new(uri.host, uri.port)
216
- if @nomad_endpoint.include?("https://")
217
- http.use_ssl = true
218
- http.verify_mode = OpenSSL::SSL::VERIFY_PEER
219
- end
220
-
221
- req = Net::HTTP::Post.new(uri)
222
- req.add_field "Content-Type", "application/json"
223
-
224
- res = http.request(req)
225
- raise if res.code != "200"
226
- raise if res.content_type != "application/json"
227
-
102
+ res_body = _request(:post, "/v1/deployment/fail/#{deployment_id}")
228
103
  return true
229
104
  rescue StandardError => e
230
105
  Nomade.logger.fatal "HTTP Request failed (#{e.message})"
@@ -232,19 +107,11 @@ module Nomade
232
107
  end
233
108
 
234
109
  def get_allocation_logs(allocation_id, task_name, logtype)
235
- uri = URI("#{@nomad_endpoint}/v1/client/fs/logs/#{allocation_id}?task=#{task_name}&type=#{logtype}&plain=true&origin=end")
236
-
237
- http = Net::HTTP.new(uri.host, uri.port)
238
- if @nomad_endpoint.include?("https://")
239
- http.use_ssl = true
240
- http.verify_mode = OpenSSL::SSL::VERIFY_PEER
241
- end
242
-
243
- req = Net::HTTP::Get.new(uri)
244
- res = http.request(req)
245
- raise if res.code != "200"
246
-
247
- return res.body.gsub(/\e\[\d+m/, '')
110
+ res_body = _request(:get, "/v1/client/fs/logs/#{allocation_id}?task=#{task_name}&type=#{logtype}&plain=true&origin=end",
111
+ total_retries: 3,
112
+ expected_content_type: "text/plain",
113
+ )
114
+ return res_body.gsub(/\e\[\d+m/, '')
248
115
  rescue StandardError => e
249
116
  Nomade.logger.fatal "HTTP Request failed (#{e.message})"
250
117
  raise
@@ -266,34 +133,31 @@ module Nomade
266
133
  end
267
134
 
268
135
  def convert_hcl_to_json(job_hcl)
269
- uri = URI("#{@nomad_endpoint}/v1/jobs/parse")
270
-
271
- http = Net::HTTP.new(uri.host, uri.port)
272
- if @nomad_endpoint.include?("https://")
273
- http.use_ssl = true
274
- http.verify_mode = OpenSSL::SSL::VERIFY_PEER
275
- end
276
-
277
- req = Net::HTTP::Post.new(uri)
278
- req.add_field "Content-Type", "application/json"
279
-
280
- req.body = JSON.generate({
136
+ req_body = JSON.generate({
281
137
  "JobHCL": job_hcl,
282
138
  "Canonicalize": false,
283
139
  })
140
+ res_body = _request(:post, "/v1/jobs/parse", body: req_body, total_retries: 3)
141
+ res_body
142
+ rescue StandardError => e
143
+ Nomade.logger.fatal "HTTP Request failed (#{e.message})"
144
+ raise
145
+ end
284
146
 
285
- res = http.request(req)
286
- raise if res.code != "200"
287
- raise if res.content_type != "application/json"
147
+ def plan_job(nomad_job)
148
+ req_body = JSON.generate({"Job" => nomad_job.configuration(:hash)})
149
+ res_body = _request(:post, "/v1/job/#{nomad_job.job_name}/plan", body: req_body)
288
150
 
289
- res.body
151
+ JSON.parse(res_body)
290
152
  rescue StandardError => e
291
153
  Nomade.logger.fatal "HTTP Request failed (#{e.message})"
292
154
  raise
293
155
  end
294
156
 
295
- def plan_job(nomad_job)
296
- uri = URI("#{@nomad_endpoint}/v1/job/#{nomad_job.job_name}/plan")
157
+ private
158
+
159
+ def _request(request_type, path, body: nil, total_retries: 0, expected_content_type: "application/json")
160
+ uri = URI("#{@nomad_endpoint}#{path}")
297
161
 
298
162
  http = Net::HTTP.new(uri.host, uri.port)
299
163
  if @nomad_endpoint.include?("https://")
@@ -301,19 +165,44 @@ module Nomade
301
165
  http.verify_mode = OpenSSL::SSL::VERIFY_PEER
302
166
  end
303
167
 
304
- req = Net::HTTP::Post.new(uri)
168
+ req = case request_type
169
+ when :get
170
+ Net::HTTP::Get.new(uri)
171
+ when :post
172
+ Net::HTTP::Post.new(uri)
173
+ when :delete
174
+ Net::HTTP::Delete.new(uri)
175
+ else
176
+ raise "#{request_type} not supported"
177
+ end
305
178
  req.add_field "Content-Type", "application/json"
306
- req.body = JSON.generate({"Job" => nomad_job.configuration(:hash)})
307
-
308
- res = http.request(req)
179
+ req.body = body if body
180
+
181
+ res = begin
182
+ retries ||= 0
183
+ http.request(req)
184
+ rescue Timeout::Error, Errno::ETIMEDOUT, Errno::EINVAL, Errno::ECONNRESET, EOFError, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError, SocketError
185
+ if retries < total_retries
186
+ retries += 1
187
+ sleep 1
188
+ retry
189
+ else
190
+ raise
191
+ end
192
+ end
309
193
 
310
194
  raise if res.code != "200"
311
- raise if res.content_type != "application/json"
195
+ if res.content_type != expected_content_type
196
+ # Sometimes the log endpoint doesn't set content_type on no content
197
+ # https://github.com/hashicorp/nomad/issues/7264
198
+ if res.content_type == nil && expected_content_type == "text/plain"
199
+ # don't raise
200
+ else
201
+ raise
202
+ end
203
+ end
312
204
 
313
- JSON.parse(res.body)
314
- rescue StandardError => e
315
- Nomade.logger.fatal "HTTP Request failed (#{e.message})"
316
- raise
205
+ res.body
317
206
  end
318
207
 
319
208
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nomade
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kasper Grubbe