capistrano-nomad 0.9.2 → 0.10.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: '064875b4478852c18387c8084ff4b0e5c2cb4e9377a6854724f46119b416ecf5'
4
- data.tar.gz: 15069bfd33db0a2b497a7e6a1adf816669df6ba0b508364fb8a26728e46ae329
3
+ metadata.gz: 76af53f1a2764deb4b91363674b6627d7b6017a30609e067d42b66cdbd5c9fc0
4
+ data.tar.gz: 64825bb8a9bdccf833f23cb36aeb10cba6f516bbac8e6690193eb1674339fb94
5
5
  SHA512:
6
- metadata.gz: eff94039fa9b7c506fbe139670639d326423c409813c4a10c97c15e79256f4bc5f65a181a2d8608af9bdd0aa51aeaeb999ad27e02836b2af8815b961999f8470
7
- data.tar.gz: 255c76c93ba40a4fd35206b31d511a7a42e2954c4e4f072154effe68643e66a938fd0aacd3f029e7e5cfa37f13f82cb924606ab4bc2fc61be92234c5e3d7428d
6
+ metadata.gz: d359a15febd84d1303360f8d9c51faf026fa6388cf6a8b97edc7fa99cee59007c4999d040935bad0351ad1fa3bc1aca5908819b1cd1fc302d711b09a7d3a42e6
7
+ data.tar.gz: 5bf77756a9d1dc2bc28ded978f0523fff65059932154c1930545dd4b06930012d45ed646108f593802e606559b2aa4c2cae867f2012bef83245308b30d1a52dd
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- capistrano-nomad (0.9.2)
4
+ capistrano-nomad (0.10.1)
5
5
  activesupport (<= 7.0.8)
6
6
  byebug
7
7
  capistrano (~> 3.0)
data/README.md CHANGED
@@ -134,15 +134,15 @@ cap production nomad:analytics:grafana:restart
134
134
  cap production nomad:postgres:status
135
135
  ```
136
136
 
137
- Tasks can go by namespace or `all` namespaces, and can also be filtered by tags
137
+ Tasks can apply across all namespaces or be filtered by namespaces or tags
138
138
 
139
139
  ```shell
140
140
  cap production nomad:analytics:deploy
141
141
  cap production nomad:analytics:upload_run
142
- cap production nomad:all:deploy
143
- cap production nomad:all:deploy TAG=admin
144
- cap production nomad:all:upload_run
145
- cap production nomad:all:upload_run TAGS=admin,redis
142
+ cap production nomad:deploy
143
+ cap production nomad:deploy TAG=admin
144
+ cap production nomad:upload_run
145
+ cap production nomad:upload_run TAGS=admin,redis
146
146
  ```
147
147
 
148
148
  Open console
@@ -171,7 +171,7 @@ cap production nomad:app:ui
171
171
  Create missing and delete unused namespaces
172
172
 
173
173
  ```shell
174
- cap production nomad:all:modify_namespaces
174
+ cap production nomad:modify_namespaces
175
175
  ```
176
176
 
177
177
  ## Development
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |spec|
4
4
  spec.name = "capistrano-nomad"
5
- spec.version = "0.9.2"
5
+ spec.version = "0.10.1"
6
6
  spec.authors = ["James Hu"]
7
7
 
8
8
  spec.summary = "Capistrano plugin for deploying and managing Nomad jobs"
@@ -36,6 +36,9 @@ def nomad_namespace(namespace, **options, &block)
36
36
  end
37
37
 
38
38
  def nomad_job(name, attributes = {})
39
+ # This is the namespace when there's no namespace defined in Nomad too
40
+ @nomad_namespace ||= :default
41
+
39
42
  attributes[:tags] ||= []
40
43
 
41
44
  # Tags added to namespace should be added to all jobs within
@@ -29,7 +29,7 @@ def capistrano_nomad_ensure_absolute_path(path)
29
29
  path[0] == "/" ? path : "/#{path}"
30
30
  end
31
31
 
32
- def capistrano_nomad_build_file_path(parent_path, basename, kind: nil, namespace: nil)
32
+ def capistrano_nomad_build_file_path(parent_path, basename, kind: nil, namespace: :default)
33
33
  segments = [parent_path]
34
34
 
35
35
  if namespace
@@ -132,7 +132,7 @@ def capistrano_nomad_capture_nomad_command(*args)
132
132
  output
133
133
  end
134
134
 
135
- def capistrano_nomad_find_job_task_details(name, namespace: nil, task: nil)
135
+ def capistrano_nomad_find_job_task_details(name, namespace: :default, task: nil)
136
136
  task = task.presence || name
137
137
 
138
138
  # Find alloc id that contains task
@@ -170,7 +170,7 @@ def capistrano_nomad_find_job_task_details(name, namespace: nil, task: nil)
170
170
  }
171
171
  end
172
172
 
173
- def capistrano_nomad_exec_within_job(name, command, namespace: nil, task: nil)
173
+ def capistrano_nomad_exec_within_job(name, command, namespace: :default, task: nil)
174
174
  capistrano_nomad_run_remotely do
175
175
  if (task_details = capistrano_nomad_find_job_task_details(name, namespace: namespace, task: task))
176
176
  capistrano_nomad_execute_nomad_command(
@@ -246,7 +246,7 @@ def capistrano_nomad_fetch_namespace_options(namespace)
246
246
  fetch(:nomad_namespaces)&.dig(namespace)
247
247
  end
248
248
 
249
- def capistrano_nomad_fetch_job_options(name, *args, namespace: nil)
249
+ def capistrano_nomad_fetch_job_options(name, *args, namespace: :default)
250
250
  fetch(:nomad_jobs).dig(namespace, name.to_sym, *args)
251
251
  end
252
252
 
@@ -254,7 +254,7 @@ def capistrano_nomad_fetch_job_var_files(name, *args)
254
254
  capistrano_nomad_fetch_job_options(name, :var_files, *args) || []
255
255
  end
256
256
 
257
- def capistrano_nomad_fetch_jobs_names_by_namespace(namespace: :all)
257
+ def capistrano_nomad_fetch_jobs_names_by_namespace(namespace: :default)
258
258
  # Can pass tags via command line (e.g. TAG=foo or TAGS=foo,bar)
259
259
  tags =
260
260
  [ENV["TAG"], ENV["TAGS"]].map do |tag_args|
@@ -266,7 +266,7 @@ def capistrano_nomad_fetch_jobs_names_by_namespace(namespace: :all)
266
266
  .compact
267
267
 
268
268
  fetch(:nomad_jobs).each_with_object({}) do |(jobs_namespace, jobs_options), hash|
269
- next if namespace != :all && namespace != jobs_namespace
269
+ next if !namespace.nil? && namespace != jobs_namespace
270
270
 
271
271
  hash[jobs_namespace] = jobs_options.each_with_object([]) do |(job_name, job_options), collection|
272
272
  # Filter jobs by tags if specified
@@ -277,89 +277,97 @@ def capistrano_nomad_fetch_jobs_names_by_namespace(namespace: :all)
277
277
  end
278
278
  end
279
279
 
280
- def capistrano_nomad_fetch_jobs_docker_image_types(names, namespace: nil)
280
+ def capistrano_nomad_fetch_jobs_docker_image_types(names, namespace: :default)
281
281
  names.map { |n| fetch(:nomad_jobs).dig(namespace, n.to_sym, :docker_image_types) }.flatten.compact.uniq
282
282
  end
283
283
 
284
284
  def capistrano_nomad_define_group_tasks(namespace:)
285
- namespace(namespace) do
286
- desc "Build #{namespace} job Docker images"
285
+ define_tasks = lambda do |nomad_namespace: nil|
286
+ desc "Build #{nomad_namespace} job Docker images"
287
287
  task :build do
288
- capistrano_nomad_fetch_jobs_names_by_namespace(namespace: namespace).each do |namespace_by, names|
289
- capistrano_nomad_push_jobs_docker_images(names, namespace: namespace_by)
288
+ capistrano_nomad_fetch_jobs_names_by_namespace(namespace: nomad_namespace).each do |jobs_namespace, names|
289
+ capistrano_nomad_push_jobs_docker_images(names, namespace: jobs_namespace)
290
290
  end
291
291
  end
292
292
 
293
- desc "Push #{namespace} job Docker images"
293
+ desc "Push #{nomad_namespace} job Docker images"
294
294
  task :push do
295
- capistrano_nomad_fetch_jobs_names_by_namespace(namespace: namespace).each do |namespace_by, names|
296
- capistrano_nomad_push_jobs_docker_images(names, namespace: namespace_by)
295
+ capistrano_nomad_fetch_jobs_names_by_namespace(namespace: nomad_namespace).each do |jobs_namespace, names|
296
+ capistrano_nomad_push_jobs_docker_images(names, namespace: jobs_namespace)
297
297
  end
298
298
  end
299
299
 
300
- desc "Build and push #{namespace} job Docker images"
300
+ desc "Build and push #{nomad_namespace} job Docker images"
301
301
  task :assemble do
302
- capistrano_nomad_fetch_jobs_names_by_namespace(namespace: namespace).each do |namespace_by, names|
303
- capistrano_nomad_assemble_jobs_docker_images(names, namespace: namespace_by)
302
+ capistrano_nomad_fetch_jobs_names_by_namespace(namespace: nomad_namespace).each do |jobs_namespace, names|
303
+ capistrano_nomad_assemble_jobs_docker_images(names, namespace: jobs_namespace)
304
304
  end
305
305
  end
306
306
 
307
- desc "Upload #{namespace} jobs"
307
+ desc "Upload #{nomad_namespace} jobs"
308
308
  task :upload do
309
- capistrano_nomad_fetch_jobs_names_by_namespace(namespace: namespace).each do |namespace_by, names|
310
- capistrano_nomad_upload_jobs(names, namespace: namespace_by)
309
+ capistrano_nomad_fetch_jobs_names_by_namespace(namespace: nomad_namespace).each do |jobs_namespace, names|
310
+ capistrano_nomad_upload_jobs(names, namespace: jobs_namespace)
311
311
  end
312
312
  end
313
313
 
314
- desc "Run #{namespace} jobs"
314
+ desc "Run #{nomad_namespace} jobs"
315
315
  task :run do
316
- capistrano_nomad_fetch_jobs_names_by_namespace(namespace: namespace).each do |namespace_by, names|
317
- capistrano_nomad_run_jobs(names, namespace: namespace_by)
316
+ capistrano_nomad_fetch_jobs_names_by_namespace(namespace: nomad_namespace).each do |jobs_namespace, names|
317
+ capistrano_nomad_run_jobs(names, namespace: jobs_namespace)
318
318
  end
319
319
  end
320
320
 
321
- desc "Upload and run #{namespace} jobs"
321
+ desc "Upload and run #{nomad_namespace} jobs"
322
322
  task :upload_run do
323
- capistrano_nomad_fetch_jobs_names_by_namespace(namespace: namespace).each do |namespace_by, names|
324
- capistrano_nomad_upload_run_jobs(names, namespace: namespace_by)
323
+ capistrano_nomad_fetch_jobs_names_by_namespace(namespace: nomad_namespace).each do |jobs_namespace, names|
324
+ capistrano_nomad_upload_run_jobs(names, namespace: jobs_namespace)
325
325
  end
326
326
  end
327
327
 
328
- desc "Deploy #{namespace} jobs"
328
+ desc "Deploy #{nomad_namespace} jobs"
329
329
  task :deploy do
330
- capistrano_nomad_fetch_jobs_names_by_namespace(namespace: namespace).each do |namespace_by, names|
331
- capistrano_nomad_deploy_jobs(names, namespace: namespace_by)
330
+ capistrano_nomad_fetch_jobs_names_by_namespace(namespace: nomad_namespace).each do |jobs_namespace, names|
331
+ capistrano_nomad_deploy_jobs(names, namespace: jobs_namespace)
332
332
  end
333
333
  end
334
334
 
335
- desc "Rerun #{namespace} jobs"
335
+ desc "Rerun #{nomad_namespace} jobs"
336
336
  task :rerun do
337
- capistrano_nomad_fetch_jobs_names_by_namespace(namespace: namespace).each do |namespace_by, names|
338
- capistrano_nomad_rerun_jobs(names, namespace: namespace_by)
337
+ capistrano_nomad_fetch_jobs_names_by_namespace(namespace: nomad_namespace).each do |jobs_namespace, names|
338
+ capistrano_nomad_rerun_jobs(names, namespace: jobs_namespace)
339
339
  end
340
340
  end
341
341
 
342
- desc "Restart #{namespace} jobs"
342
+ desc "Restart #{nomad_namespace} jobs"
343
343
  task :restart do
344
- capistrano_nomad_fetch_jobs_names_by_namespace(namespace: namespace).each do |namespace_by, names|
345
- capistrano_nomad_restart_jobs(names, namespace: namespace_by)
344
+ capistrano_nomad_fetch_jobs_names_by_namespace(namespace: nomad_namespace).each do |jobs_namespace, names|
345
+ capistrano_nomad_restart_jobs(names, namespace: jobs_namespace)
346
346
  end
347
347
  end
348
348
 
349
- desc "Stop #{namespace} jobs"
349
+ desc "Stop #{nomad_namespace} jobs"
350
350
  task :stop do
351
- capistrano_nomad_fetch_jobs_names_by_namespace(namespace: namespace).each do |namespace_by, names|
352
- capistrano_nomad_stop_jobs(names, namespace: namespace_by)
351
+ capistrano_nomad_fetch_jobs_names_by_namespace(namespace: nomad_namespace).each do |jobs_namespace, names|
352
+ capistrano_nomad_stop_jobs(names, namespace: jobs_namespace)
353
353
  end
354
354
  end
355
355
 
356
- desc "Purge #{namespace} jobs"
356
+ desc "Purge #{nomad_namespace} jobs"
357
357
  task :purge do
358
- capistrano_nomad_fetch_jobs_names_by_namespace(namespace: namespace).each do |namespace_by, names|
359
- capistrano_nomad_purge_jobs(names, namespace: namespace_by)
358
+ capistrano_nomad_fetch_jobs_names_by_namespace(namespace: nomad_namespace).each do |jobs_namespace, names|
359
+ capistrano_nomad_purge_jobs(names, namespace: jobs_namespace)
360
360
  end
361
361
  end
362
362
  end
363
+
364
+ if namespace
365
+ namespace(namespace) do
366
+ define_tasks.call(nomad_namespace: namespace)
367
+ end
368
+ else
369
+ define_tasks.call
370
+ end
363
371
  end
364
372
 
365
373
  def capistrano_nomad_build_jobs_docker_images(names, *args)
@@ -421,7 +429,7 @@ def capistrano_nomad_plan_jobs(names, *args)
421
429
  end
422
430
  end
423
431
 
424
- def capistrano_nomad_run_jobs(names, namespace: nil, is_detached: true)
432
+ def capistrano_nomad_run_jobs(names, namespace: :default, is_detached: true)
425
433
  names.each do |name|
426
434
  run_options = {
427
435
  namespace: namespace,
@@ -493,7 +501,7 @@ def capistrano_nomad_stop_jobs(names, **options)
493
501
  end
494
502
  end
495
503
 
496
- def capistrano_nomad_purge_jobs(names, namespace: nil, is_detached: true)
504
+ def capistrano_nomad_purge_jobs(names, namespace: :default, is_detached: true)
497
505
  names.each do |name|
498
506
  capistrano_nomad_execute_nomad_command(:stop, { namespace: namespace, purge: true, detach: is_detached }, name)
499
507
  end
@@ -503,7 +511,7 @@ def capistrano_nomad_display_job_status(name, **options)
503
511
  capistrano_nomad_execute_nomad_command(:status, options, name)
504
512
  end
505
513
 
506
- def capistrano_nomad_display_job_logs(name, namespace: nil, **options)
514
+ def capistrano_nomad_display_job_logs(name, namespace: :default, **options)
507
515
  if (task_details = capistrano_nomad_find_job_task_details(name, namespace: namespace, task: ENV["TASK"]))
508
516
  capistrano_nomad_execute_nomad_command(
509
517
  :alloc,
@@ -526,7 +534,7 @@ def capistrano_nomad_tail_job_logs(*args, **options)
526
534
  capistrano_nomad_display_job_logs(*args, **options.merge(tail: true, n: 50))
527
535
  end
528
536
 
529
- def capistrano_nomad_open_job_ui(name, namespace: nil)
537
+ def capistrano_nomad_open_job_ui(name, namespace: :default)
530
538
  run_locally do
531
539
  url = "#{fetch(:nomad_ui_url)}/ui/jobs/#{name}"
532
540
  url += "@#{namespace}" if namespace
@@ -1,46 +1,37 @@
1
1
  namespace :nomad do
2
- desc "Show version"
3
- task :version do
4
- capistrano_nomad_execute_nomad_command(:version)
5
- end
6
-
7
2
  # Define tasks for all jobs
8
- capistrano_nomad_define_group_tasks(namespace: :all)
3
+ capistrano_nomad_define_group_tasks(namespace: nil)
9
4
 
10
- namespace :all do
11
- desc "Create missing and remove unused namespaces"
12
- task :modify_namespaces do
13
- output = capistrano_nomad_capture_nomad_command(:namespace, :list, t: "'{{range .}}{{ .Name }}|{{end}}'")
14
- current_namespaces = output.split("|").compact.map(&:to_sym)
5
+ desc "Create missing and remove unused namespaces"
6
+ task :modify_namespaces do
7
+ output = capistrano_nomad_capture_nomad_command(:namespace, :list, t: "'{{range .}}{{ .Name }}|{{end}}'")
8
+ current_namespaces = output.split("|").compact.map(&:to_sym)
15
9
 
16
- # If key is nil then it actually belongs to the default namespace
17
- desired_namespaces = fetch(:nomad_jobs).keys.map { |n| n.nil? ? :default : n.to_sym }
10
+ # If key is nil then it actually belongs to the default namespace
11
+ desired_namespaces = fetch(:nomad_jobs).keys.map { |n| n.nil? ? :default : n.to_sym }
18
12
 
19
- missing_namespaces = desired_namespaces - current_namespaces
20
- unused_namespaces = current_namespaces - desired_namespaces
13
+ missing_namespaces = desired_namespaces - current_namespaces
14
+ unused_namespaces = current_namespaces - desired_namespaces
21
15
 
22
- # Remove unused namespaces
23
- unused_namespaces.each do |namespace|
24
- capistrano_nomad_execute_nomad_command(:namespace, :delete, namespace)
25
- end
16
+ # Remove unused namespaces
17
+ unused_namespaces.each do |namespace|
18
+ capistrano_nomad_execute_nomad_command(:namespace, :delete, namespace)
19
+ end
26
20
 
27
- # Create missing namespaces
28
- missing_namespaces.each do |namespace|
29
- capistrano_nomad_execute_nomad_command(:namespace, :apply, namespace)
30
- end
21
+ # Create missing namespaces
22
+ missing_namespaces.each do |namespace|
23
+ capistrano_nomad_execute_nomad_command(:namespace, :apply, namespace)
31
24
  end
32
25
  end
33
26
 
34
- namespace :docker_images do
35
- desc "Used for adding hooks before or after pushing Docker images"
36
- task :push
27
+ desc "Run garbage collector tasks"
28
+ task :gc do
29
+ capistrano_nomad_execute_nomad_command(:system, :gc)
30
+ capistrano_nomad_execute_nomad_command(:system, :reconcile, :summaries)
37
31
  end
38
32
 
39
- namespace :system do
40
- desc "Clean up Nomad"
41
- task :clean do
42
- capistrano_nomad_execute_nomad_command(:system, :gc)
43
- capistrano_nomad_execute_nomad_command(:system, :reconcile, :summaries)
44
- end
33
+ desc "Show version"
34
+ task :version do
35
+ capistrano_nomad_execute_nomad_command(:version)
45
36
  end
46
37
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: capistrano-nomad
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.2
4
+ version: 0.10.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Hu