capistrano-nomad 0.9.1 → 0.10.0

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: e70ce696bab8b3ef8545e9f4eff59e12aa8c39a9705da8ba92a9ae0516b2534b
4
- data.tar.gz: 8053ced9f62a0d56046cc7162f5efd20ddd9ab20e0d22a23b9e237a84db5f605
3
+ metadata.gz: 9993fb04b78b68b03c055faa66454360543c92108f2e410f7156bedf7158f763
4
+ data.tar.gz: 20b4708abc11215f259bc46b8b4c7f4f0606fb0d2ff7e3c862c799b850ead5ee
5
5
  SHA512:
6
- metadata.gz: f861c87b67f797851738b1e0a29cab2286489b6067029f9314d55a2099e9e1751bd2e0f8df3ba3ba190545ddffecbe38e086515a2a520d7f39b5900299c783d3
7
- data.tar.gz: 62817162ccf5ed2e0c72464e5846bfcb3474964f9a3d3339565c66f58d154c53483341e955e067458398ac69c61aeeb8f698157b114c9802338468bf43d1c950
6
+ metadata.gz: 86d6750e7e181d120db5b56da6c5b7b083310901939e452aee8e7a6de12db22544f2c1accfe8fb9790c75c188644ed0108446a8281875566b0d6243ae8db6191
7
+ data.tar.gz: f80d0003e093e3ed5e0fefc69b6b89db158064145c7c21573944b8fc88f8beb40a1cb0b40fbbbb956856bbc637f217a72001c5a1de10ed992ea46e6fd45b8809
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- capistrano-nomad (0.9.1)
4
+ capistrano-nomad (0.10.0)
5
5
  activesupport (<= 7.0.8)
6
6
  byebug
7
7
  capistrano (~> 3.0)
data/README.md CHANGED
@@ -102,11 +102,14 @@ nomad_job :backend, docker_image_types: [:backend], var_files: [:rails]
102
102
  nomad_job :frontend
103
103
  nomad_job :postgres, docker_image_types: [:postgres]
104
104
  nomad_job :redis, docker_image_types: [:redis], tags: [:redis]
105
- nomad_job :"traefik-default", template: :traefik, erb_vars: { role: :default }, tags: [:traefik]
106
- nomad_job :"traefik-secondary", template: :traefik, erb_vars: { role: :secondary }, tags: [:traefik]
107
- nomad_job :"traefik-tertiary", template: :traefik, erb_vars: { role: :secondary }, tags: [:traefik]
108
-
109
- nomad_namespace :analytics do
105
+ nomad_job :"traefik-default", template: :admin,
106
+ erb_vars: { role: :default },
107
+ tags: [:traefik]
108
+ nomad_job :"traefik-secondary", template: :admin,
109
+ erb_vars: { role: :secondary },
110
+ tags: [:traefik]
111
+
112
+ nomad_namespace :analytics, tags: [:admin] do
110
113
  nomad_job :grafana
111
114
  end
112
115
 
@@ -131,15 +134,15 @@ cap production nomad:analytics:grafana:restart
131
134
  cap production nomad:postgres:status
132
135
  ```
133
136
 
134
- 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
135
138
 
136
139
  ```shell
137
140
  cap production nomad:analytics:deploy
138
141
  cap production nomad:analytics:upload_run
139
- cap production nomad:all:deploy
140
- cap production nomad:all:deploy TAG=traefik
141
- cap production nomad:all:upload_run
142
- cap production nomad:all:upload_run TAGS=traefik,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
143
146
  ```
144
147
 
145
148
  Open console
@@ -168,7 +171,7 @@ cap production nomad:app:ui
168
171
  Create missing and delete unused namespaces
169
172
 
170
173
  ```shell
171
- cap production nomad:all:modify_namespaces
174
+ cap production nomad:modify_namespaces
172
175
  ```
173
176
 
174
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.1"
5
+ spec.version = "0.10.0"
6
6
  spec.authors = ["James Hu"]
7
7
 
8
8
  spec.summary = "Capistrano plugin for deploying and managing Nomad jobs"
@@ -36,6 +36,16 @@ 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
+
42
+ attributes[:tags] ||= []
43
+
44
+ # Tags added to namespace should be added to all jobs within
45
+ if (nomad_namespace_options = capistrano_nomad_fetch_namespace_options(@nomad_namespace))
46
+ attributes[:tags] += nomad_namespace_options[:tags] || []
47
+ end
48
+
39
49
  nomad_jobs = fetch(:nomad_jobs) || Hash.new { |h, n| h[n] = {} }
40
50
  nomad_jobs[@nomad_namespace][name] = attributes
41
51
 
@@ -243,7 +243,7 @@ def capistrano_nomad_upload(local_path:, remote_path:, erb_vars: {})
243
243
  end
244
244
 
245
245
  def capistrano_nomad_fetch_namespace_options(namespace)
246
- fetch(:nomad_namespaces).dig(namespace)
246
+ fetch(:nomad_namespaces)&.dig(namespace)
247
247
  end
248
248
 
249
249
  def capistrano_nomad_fetch_job_options(name, *args, namespace: nil)
@@ -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: nil)
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
@@ -281,85 +281,93 @@ def capistrano_nomad_fetch_jobs_docker_image_types(names, namespace: nil)
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
- def capistrano_nomad_define_group_tasks(namespace:)
285
- namespace(namespace) do
286
- desc "Build #{namespace} job Docker images"
284
+ def capistrano_nomad_define_group_tasks(namespace: nil)
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)
@@ -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
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.1
4
+ version: 0.10.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Hu