capistrano-nomad 0.5.0 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 050bf4a39838b2c0a98c02c1d1c38fcee02818cdb3936d2b00073adea0223aec
4
- data.tar.gz: 96b95dd209e984447ed33bc3229562202887222641954d494bc922fd215ec050
3
+ metadata.gz: 0f5358b24228c869a704b8ed79aa76d76b2aeb559ffcd7bd3bced8aa7d3281d1
4
+ data.tar.gz: 95f8c93adc89fdbcffe9f40bc5d18433ccfc4b743cfcb194a2b7b2e9468f6bb5
5
5
  SHA512:
6
- metadata.gz: 53535889c6707debe917ceefa837c6f4310c037523ded6e101ff752ff6962db3eb02805d2d60dd3fec1b693a3d0bcbae1dd5fe2f4c3a3451aea1170bcca0b2af
7
- data.tar.gz: a43459682a67ba79448969660fbd659aca92ac90a0524e0ccf613212f537a148da93e06c3efb862dcec5c7b2441a3b46ad05f2a9ae0bdf8b5d761501ffba7839
6
+ metadata.gz: 834ccd5183128e4c3b4bed1067457b1a2a0036ed21c8ca954f82f25298aca61d6a108460b22c17cbe9f2fa561e02e4189942ddb99d784792ed7422b70e05ba0f
7
+ data.tar.gz: 1a4ade283a947f6243fb8c728ca3c3fcac55fac0471cad47b2fbabec66b66a7287fd342c29610bcad5a67d847a0251eb2ee66847ca27de449b963734d5a0d55d
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- capistrano-nomad (0.5.0)
4
+ capistrano-nomad (0.6.0)
5
5
  activesupport (<= 7.0.8)
6
6
  byebug
7
7
  capistrano (~> 3.0)
data/README.md CHANGED
@@ -31,11 +31,23 @@ require "capistrano/nomad"
31
31
  install_plugin Capistrano::Nomad
32
32
  ```
33
33
 
34
- Define Nomad jobs within `deploy.rb`
34
+ Within `deploy.rb`
35
35
 
36
36
  ```ruby
37
+
38
+ # Docker image types
39
+ nomad_docker_image_type :app,
40
+ path: "backend",
41
+ alias: ->(image_type:) { "gcr.io/axsuul/#{image_type}" },
42
+ target: "release",
43
+ build_args: { foo: "bar" }
44
+ nomad_docker_image_type :redis,
45
+ path: "/absolute/path/redis",
46
+ alias: "gcr.io/axsuul/redis"
47
+
48
+ # Jobs
37
49
  nomad_job :app
38
- nomad_job :redis
50
+ nomad_job :redis, docker_image_types: [:redis]
39
51
 
40
52
  nomad_namespace :analytics do
41
53
  nomad_job :grafana
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |spec|
4
4
  spec.name = "capistrano-nomad"
5
- spec.version = "0.5.0"
5
+ spec.version = "0.6.0"
6
6
  spec.authors = ["James Hu"]
7
7
 
8
8
  spec.summary = "Capistrano plugin for deploying and managing Nomad jobs"
@@ -101,7 +101,7 @@ def capistrano_nomad_build_docker_image_for_type(image_type)
101
101
  image_alias_args << "--tag #{tag}"
102
102
  end
103
103
 
104
- execute("docker build #{image_alias_args.join(' ')} .#{capistrano_nomad_root.join(attributes[:path])}")
104
+ execute("docker build #{image_alias_args.join(' ')} #{capistrano_nomad_root.join(attributes[:path])}")
105
105
  end
106
106
  end
107
107
 
@@ -90,15 +90,18 @@ def nomad_job(name, attributes = {})
90
90
 
91
91
  desc "Restart #{description_name} job"
92
92
  task :restart do
93
- on roles :manager do
93
+ on(roles(:manager)) do
94
94
  # We can't restart the job directly so we'll need to fetch all its running allocs and restart each of one
95
95
  # individually instead
96
- running_alloc_ids_output = capture(
97
- "nomad job allocs " \
98
- "-t '{{range .}}{{if eq .ClientStatus \"running\"}}{{ println .ID}}{{end}}{{end}}' " \
99
- "#{name}",
96
+ running_alloc_ids_output = capistrano_nomad_capture_nomad_command(
97
+ :job,
98
+ :allocs,
99
+ {
100
+ namespace: namespace,
101
+ t: "'{{range .}}{{if eq .ClientStatus \"running\"}}{{ println .ID}}{{end}}{{end}}'",
102
+ },
103
+ name,
100
104
  )
101
-
102
105
  running_alloc_ids = running_alloc_ids_output.strip.split("\n")
103
106
 
104
107
  running_alloc_ids.each do |alloc_id|
@@ -51,11 +51,11 @@ def capistrano_nomad_build_base_var_file_path(*args)
51
51
  capistrano_nomad_build_file_path(fetch(:nomad_var_files_path), *args)
52
52
  end
53
53
 
54
- def capistrano_nomad_build_local_path(path, *args)
55
- local_path = ".#{capistrano_nomad_root.join(path)}"
54
+ def capistrano_nomad_build_local_path(path)
55
+ local_path = capistrano_nomad_root.join(path)
56
56
 
57
57
  # Determine if it has .erb appended or not
58
- found_local_path = [local_path, "#{local_path}.erb"].find { |path| File.exist?(path) }
58
+ found_local_path = [local_path, "#{local_path}.erb"].find { |each_local_path| File.exist?(each_local_path) }
59
59
 
60
60
  raise StandardError, "Could not find local path: #{path}" unless found_local_path
61
61
 
@@ -78,7 +78,7 @@ def capistrano_nomad_build_release_var_file_path(*args)
78
78
  "#{release_path}#{capistrano_nomad_build_base_var_file_path(*args)}"
79
79
  end
80
80
 
81
- def capistrano_nomad_execute_nomad_command(*args)
81
+ def capistrano_nomad_run_nomad_command(kind, *args)
82
82
  converted_args = args.each_with_object([]) do |arg, collection|
83
83
  # If hash then convert it as options
84
84
  if arg.is_a?(Hash)
@@ -97,22 +97,60 @@ def capistrano_nomad_execute_nomad_command(*args)
97
97
  end
98
98
  end
99
99
 
100
+ # Ignore errors
101
+ public_send(kind, :nomad, *converted_args, raise_on_non_zero_exit: false)
102
+ end
103
+
104
+ def capistrano_nomad_execute_nomad_command(*args)
100
105
  on(roles(:manager)) do |host|
101
106
  run_interactively(host) do
102
- # Ignore errors
103
- execute(:nomad, *converted_args, raise_on_non_zero_exit: false)
107
+ capistrano_nomad_run_nomad_command(:execute, *args)
104
108
  end
105
109
  end
106
110
  end
107
111
 
112
+ def capistrano_nomad_capture_nomad_command(*args)
113
+ output = nil
114
+
115
+ on(roles(:manager)) do |host|
116
+ output = capistrano_nomad_run_nomad_command(:capture, *args)
117
+ end
118
+
119
+ output
120
+ end
121
+
108
122
  def capistrano_nomad_exec_within_job(name, command, namespace: nil, task: nil)
109
- capistrano_nomad_execute_nomad_command(
110
- :alloc,
111
- :exec,
112
- { namespace: namespace, task: task || name, job: true },
113
- name,
114
- command,
115
- )
123
+ on(roles(:manager)) do
124
+ task = name unless task
125
+
126
+ # Find alloc id that contains task
127
+ output = capistrano_nomad_capture_nomad_command(
128
+ :job,
129
+ :allocs,
130
+ { namespace: namespace, t: "'{{range .}}{{ .ID }},{{ .TaskGroup }}|{{end}}'" },
131
+ name,
132
+ )
133
+ alloc_id = output.split("|").map { |s| s.split(",") }.find { |_, t| t == task.to_s }&.first
134
+
135
+ if alloc_id
136
+ capistrano_nomad_execute_nomad_command(
137
+ :alloc,
138
+ :exec,
139
+ { namespace: namespace, task: task },
140
+ alloc_id,
141
+ command,
142
+ )
143
+ else
144
+ # If alloc can't be determined then choose at random
145
+ capistrano_nomad_execute_nomad_command(
146
+ :alloc,
147
+ :exec,
148
+ { namespace: namespace, job: true },
149
+ task,
150
+ command,
151
+ )
152
+ end
153
+ end
116
154
  end
117
155
 
118
156
  def capistrano_nomad_upload_file(local_path:, remote_path:, erb_vars: {})
@@ -9,8 +9,8 @@ require_relative "nomad/helpers/nomad"
9
9
  module Capistrano
10
10
  class Nomad < Capistrano::Plugin
11
11
  def set_defaults
12
- set_if_empty(:nomad_jobs_path, "/nomad/jobs")
13
- set_if_empty(:nomad_var_files_path, "/nomad/var_files")
12
+ set_if_empty(:nomad_jobs_path, "nomad/jobs")
13
+ set_if_empty(:nomad_var_files_path, "nomad/var_files")
14
14
  set_if_empty(:nomad_docker_image_alias, ->(**) {})
15
15
  end
16
16
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: capistrano-nomad
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Hu
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-10-14 00:00:00.000000000 Z
11
+ date: 2023-10-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport