nomade 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/nomade/http.rb +77 -188
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 57489957cb990a28ffbd23c94b9d655ec517a43cd4e8f2101d7e7cddb17bfcf7
|
4
|
+
data.tar.gz: 305ac1c39551b472ad16c3553fb4f5965b4eebfb08ea51b2557ff26cd8153af8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
17
|
-
|
18
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
41
|
+
res_body = _request(:get, "/v1/deployment/#{deployment_id}", total_retries: 3)
|
86
42
|
|
87
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
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(
|
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
|
-
|
162
|
-
|
75
|
+
path = if purge
|
76
|
+
"/v1/job/#{nomad_job.job_name}?purge=true"
|
163
77
|
else
|
164
|
-
|
78
|
+
"/v1/job/#{nomad_job.job_name}"
|
165
79
|
end
|
166
80
|
|
167
|
-
|
168
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
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
|
-
|
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
|
-
|
286
|
-
|
287
|
-
|
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
|
-
|
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
|
-
|
296
|
-
|
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 =
|
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 =
|
307
|
-
|
308
|
-
res =
|
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
|
-
|
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
|
-
|
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
|