capistrano-nomad 0.3.1 → 0.4.1

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: 8ccaa807c999207d2220ec964a010a83cee1c562d8ed6312bc2c2ddd1330be53
4
- data.tar.gz: 0bf27b5d766d966ac96e6dc1ab215257596778c870364a9a9ee029bb9b539097
3
+ metadata.gz: 8e086bb0968563ebe8bc50d27aeae5fbf26c91c484e6bb4ff3774e06da3d013a
4
+ data.tar.gz: 190a466a7334240004d6e7ed7056555ad3872a331e22b2745124fb7394e166ec
5
5
  SHA512:
6
- metadata.gz: a5528920fc76334182c1f958ae56f61320b8984df5101553610150105f5e93937bbefbb8a8ffa9374e7a2405870b03533994d8d1e1ff9f6946fd5d7f742c67c3
7
- data.tar.gz: 7ff7460adefcfca53e20e59b5edf3231a817ebacf63fbdb1573caf1ed981f9dcd657bcee8f0dc4199012c268b22360fcfbd07949181d05ca83890187d8e7d9ef
6
+ metadata.gz: 74a497827c062c431a59a9b102e4a1f6a3cb4a8c47a700c1acd2f6e6b3591c78aad734867dc649da9b0f616021949bdd9e69cc8031481086d29c7e0e9380d1b7
7
+ data.tar.gz: f765b3cf57ddf929bf1fb324218be5e093e9709f9bb56a3aef1c8ede338f33c5bbdcbe6c14b9a884fa046680164d9739c23c42fa2b27a843cf87f86b06e17fd6
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- capistrano-nomad (0.3.1)
4
+ capistrano-nomad (0.4.1)
5
5
  activesupport (<= 7.0.8)
6
6
  byebug
7
7
  capistrano (~> 3.0)
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |spec|
4
4
  spec.name = "capistrano-nomad"
5
- spec.version = "0.3.1"
5
+ spec.version = "0.4.1"
6
6
  spec.authors = ["James Hu"]
7
7
 
8
8
  spec.summary = "Capistrano plugin for deploying and managing Nomad jobs"
@@ -1,3 +1,19 @@
1
+ class CapistranoNomadDockerPushImageInteractionHandler
2
+ def initialize(*args)
3
+ @last_digest = nil
4
+ end
5
+
6
+ def on_data(_command, stream_name, data, channel)
7
+ if (match = data.match(/digest: ([^\s]+)/))
8
+ @last_digest = match[1]
9
+ end
10
+ end
11
+
12
+ def last_digest
13
+ @last_digest
14
+ end
15
+ end
16
+
1
17
  def capistrano_nomad_docker_image_types_manifest_path
2
18
  shared_path.join("docker-image-types.json")
3
19
  end
@@ -10,7 +26,7 @@ def capistrano_nomad_read_docker_image_types_manifest
10
26
  execute("mkdir -p #{shared_path}")
11
27
  execute("touch #{capistrano_nomad_docker_image_types_manifest_path}")
12
28
 
13
- output = capture "cat #{capistrano_nomad_docker_image_types_manifest_path}"
29
+ output = capture("cat #{capistrano_nomad_docker_image_types_manifest_path}")
14
30
 
15
31
  unless output.blank?
16
32
  manifest = JSON.parse(output)
@@ -33,52 +49,23 @@ def capistrano_nomad_update_docker_image_types_manifest(image_type, properties =
33
49
  end
34
50
  end
35
51
 
36
- def capistrano_nomad_build_docker_image_alias(image_type, ref)
37
- # Define nomad_docker_image_alias has a proc to return image alias
38
- fetch(:nomad_docker_image_alias).call(image_type: image_type, ref: ref)
39
- end
52
+ def capistrano_nomad_build_docker_image_alias(image_type)
53
+ image_alias = fetch(:nomad_docker_image_types).dig(image_type, :alias)
54
+ image_alias = image_alias.call(image_type: image_type) if image_alias&.is_a?(Proc)
40
55
 
41
- # Fetch image alias from manifest
42
- def current_docker_service_image_alias(image_type)
43
- image_alias =
44
- capistrano_nomad_read_docker_image_types_manifest
45
- .try(:[], image_type.to_sym)
46
- .try(:[], :image_alias)
56
+ raise StandardError, ":alias not defined for #{image_type}" unless image_alias
47
57
 
48
- return image_alias if image_alias
58
+ # Add :latest if there's no tag
59
+ image_alias << ":latest" if image_alias.split(":").count == 1
49
60
 
50
- capistrano_nomad_build_docker_image_alias(image_type, capistrano_nomad_fetch_git_commit_id)
61
+ image_alias
51
62
  end
52
63
 
53
64
  # Builds docker image from image type
54
65
  #
55
66
  # @param image_type [String, Symbol]
56
- def capistrano_nomad_build_docker_image(image_type, path, *args)
57
- run_locally do
58
- git_commit_id = capistrano_nomad_fetch_git_commit_id
59
-
60
- [
61
- capistrano_nomad_build_docker_image_alias(image_type, git_commit_id),
62
- capistrano_nomad_build_docker_image_alias(image_type, "latest"),
63
- "trunk/#{fetch(:stage)}/#{image_type}:latest",
64
- ]
65
- .compact
66
- .each do |tag|
67
- args << "--tag #{tag}"
68
- end
69
-
70
- # If any of these files exist then we're in the middle of rebase so we should interrupt
71
- if ["rebase-merge", "rebase-apply"].any? { |f| File.exist?("#{capistrano_nomad_git.dir.path}/.git/#{f}") }
72
- raise StandardError, "still in the middle of git rebase, interrupting docker image build"
73
- end
74
-
75
- system "docker build #{args.join(' ')} .#{capistrano_nomad_root.join(path)}"
76
- end
77
- end
78
-
79
67
  def capistrano_nomad_build_docker_image_for_type(image_type)
80
68
  image_type = image_type.to_sym
81
-
82
69
  attributes = fetch(:nomad_docker_image_types)[image_type]
83
70
 
84
71
  return unless attributes
@@ -89,24 +76,33 @@ def capistrano_nomad_build_docker_image_for_type(image_type)
89
76
  "--platform linux/amd64",
90
77
  ]
91
78
 
92
- build_args =
93
- if attributes[:build_args].is_a?(Proc)
94
- proc_args = attributes[:build_args].arity == 1 ? [capistrano_nomad_fetch_git_commit_id] : []
95
-
96
- attributes[:build_args].call(*proc_args)
97
- else
98
- attributes[:build_args]
99
- end
100
-
101
79
  if (target = attributes[:target])
102
80
  args << "--target #{target}"
103
81
  end
104
82
 
83
+ build_args = attributes[:build_args]
84
+ build_args = build_args.call if build_args&.is_a?(Proc)
85
+
105
86
  (build_args || []).each do |key, value|
106
87
  args << "--build-arg #{key}=#{value}"
107
88
  end
108
89
 
109
- capistrano_nomad_build_docker_image(image_type, attributes[:path], args)
90
+ run_locally do
91
+ # If any of these files exist then we're in the middle of rebase so we should interrupt
92
+ if ["rebase-merge", "rebase-apply"].any? { |f| File.exist?("#{capistrano_nomad_git.dir.path}/.git/#{f}") }
93
+ raise StandardError, "Still in the middle of git rebase, interrupting docker image build"
94
+ end
95
+
96
+ image_alias_args = args.dup
97
+
98
+ [capistrano_nomad_build_docker_image_alias(image_type)]
99
+ .compact
100
+ .each do |tag|
101
+ image_alias_args << "--tag #{tag}"
102
+ end
103
+
104
+ execute("docker build #{image_alias_args.join(' ')} .#{capistrano_nomad_root.join(attributes[:path])}")
105
+ end
110
106
  end
111
107
 
112
108
  def capistrano_nomad_push_docker_image_for_type(image_type, is_manifest_updated: true)
@@ -114,23 +110,22 @@ def capistrano_nomad_push_docker_image_for_type(image_type, is_manifest_updated:
114
110
  invoke("nomad:docker_images:push")
115
111
 
116
112
  run_locally do
117
- git_commit_id = capistrano_nomad_fetch_git_commit_id
118
- revision_image_alias = capistrano_nomad_build_docker_image_alias(image_type, git_commit_id)
119
- latest_image_alias = capistrano_nomad_build_docker_image_alias(image_type, "latest")
120
-
121
- [revision_image_alias, latest_image_alias].each do |image_alias|
122
- # We should not proceed if image cannot be pushed
123
- unless system("docker push #{image_alias}")
124
- raise StandardError, "Docker image push unsuccessful!"
125
- end
113
+ interaction_handler = CapistranoNomadDockerPushImageInteractionHandler.new
114
+ image_alias = capistrano_nomad_build_docker_image_alias(image_type)
115
+
116
+ # We should not proceed if image cannot be pushed
117
+ unless execute("docker push #{image_alias}", interaction_handler: interaction_handler)
118
+ raise StandardError, "Docker image push unsuccessful!"
126
119
  end
127
120
 
128
121
  return unless is_manifest_updated
129
122
 
130
123
  # Update image type manifest
131
124
  capistrano_nomad_update_docker_image_types_manifest(image_type,
132
- ref: git_commit_id,
133
- image_alias: revision_image_alias
125
+ alias: image_alias,
126
+
127
+ # Has the @sha256:xxxx appended so we have the ability to also target by digest
128
+ alias_digest: "#{image_alias}@#{interaction_handler.last_digest}",
134
129
  )
135
130
  end
136
131
  end
@@ -4,6 +4,6 @@ def capistrano_nomad_git
4
4
  @capistrano_nomad_git ||= Git.open(".")
5
5
  end
6
6
 
7
- def capistrano_nomad_fetch_git_commit_id
8
- capistrano_nomad_git.log.first.sha
7
+ def capistrano_nomad_git_commit_id
8
+ @capistrano_nomad_git_commit_id ||= capistrano_nomad_git.log.first.sha
9
9
  end
@@ -1,7 +1,7 @@
1
1
  require "active_support/core_ext/string"
2
2
  require "sshkit/interactive"
3
3
 
4
- class NomadErbNamespace
4
+ class CapistranoNomadErbNamespace
5
5
  def initialize(context:, vars: {})
6
6
  @context = context
7
7
 
@@ -106,7 +106,7 @@ def capistrano_nomad_execute_nomad_command(*args)
106
106
  end
107
107
 
108
108
  def capistrano_nomad_exec_within_job(name, command, namespace: nil)
109
- capistrano_nomad_execute_nomad_command(:alloc, :exec, { namespace: namespace, task: name, job: name }, command)
109
+ capistrano_nomad_execute_nomad_command(:alloc, :exec, { namespace: namespace, task: name, job: true }, name, command)
110
110
  end
111
111
 
112
112
  def capistrano_nomad_upload_file(local_path:, remote_path:, erb_vars: {})
@@ -122,7 +122,7 @@ def capistrano_nomad_upload_file(local_path:, remote_path:, erb_vars: {})
122
122
  erb = ERB.new(File.open(local_path).read, trim_mode: "-")
123
123
 
124
124
  final_erb_vars = {
125
- git_commit_id: fetch(:current_revision) || capistrano_nomad_fetch_git_commit_id,
125
+ git_commit_id: fetch(:current_revision) || capistrano_nomad_git_commit_id,
126
126
  docker_image_types: docker_image_types,
127
127
  }
128
128
 
@@ -134,7 +134,7 @@ def capistrano_nomad_upload_file(local_path:, remote_path:, erb_vars: {})
134
134
 
135
135
  # We use a custom namespace class so that we can include helper methods into the namespace to make them available for
136
136
  # template to access
137
- namespace = NomadErbNamespace.new(
137
+ namespace = CapistranoNomadErbNamespace.new(
138
138
  context: self,
139
139
  vars: final_erb_vars,
140
140
  )
@@ -159,7 +159,7 @@ def capistrano_nomad_fetch_jobs_names_by_namespace
159
159
  end
160
160
 
161
161
  def capistrano_nomad_fetch_jobs_docker_image_types(names, namespace: nil)
162
- names.map { |n| fetch(:nomad_jobs).dig(namespace, n.to_sym, :image_types) }.flatten.compact.uniq
162
+ names.map { |n| fetch(:nomad_jobs).dig(namespace, n.to_sym, :docker_image_types) }.flatten.compact.uniq
163
163
  end
164
164
 
165
165
  def capistrano_nomad_build_jobs_docker_images(names, *args)
@@ -6,7 +6,7 @@ class Capistrano::SCM::GitLocal < Capistrano::SCM::Plugin
6
6
  namespace :git_local do
7
7
  task :set_current_revision do
8
8
  on release_roles :manager do
9
- set :current_revision, capistrano_nomad_fetch_git_commit_id
9
+ set :current_revision, capistrano_nomad_git_commit_id
10
10
  end
11
11
  end
12
12
 
@@ -1,4 +1,5 @@
1
1
  require "capistrano/plugin"
2
+ require "byebug"
2
3
  require_relative "nomad/helpers/base"
3
4
  require_relative "nomad/helpers/docker"
4
5
  require_relative "nomad/helpers/dsl"
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.3.1
4
+ version: 0.4.1
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-10 00:00:00.000000000 Z
11
+ date: 2023-10-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -129,7 +129,6 @@ files:
129
129
  - lib/capistrano/nomad/helpers/nomad.rb
130
130
  - lib/capistrano/nomad/scm/git_local.rb
131
131
  - lib/capistrano/nomad/tasks/nomad.rake
132
- - sig/capistrano/nomad.rbs
133
132
  homepage: https://github.com/axsuul/capistrano-nomad
134
133
  licenses:
135
134
  - MIT
@@ -1,6 +0,0 @@
1
- module Capistrano
2
- module Nomad
3
- VERSION: String
4
- # See the writing guide of rbs: https://github.com/ruby/rbs#guides
5
- end
6
- end