capistrano-nomad 0.3.1 → 0.4.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: 8ccaa807c999207d2220ec964a010a83cee1c562d8ed6312bc2c2ddd1330be53
4
- data.tar.gz: 0bf27b5d766d966ac96e6dc1ab215257596778c870364a9a9ee029bb9b539097
3
+ metadata.gz: decb45a7958cc1a3b2bc96d8d48e72ff6132d160eac93e81e995b59661dfc5ff
4
+ data.tar.gz: adaf5bb0a848328cfd830f861b1f8ff744e46bba6eab66f9180fdd21f1bad57b
5
5
  SHA512:
6
- metadata.gz: a5528920fc76334182c1f958ae56f61320b8984df5101553610150105f5e93937bbefbb8a8ffa9374e7a2405870b03533994d8d1e1ff9f6946fd5d7f742c67c3
7
- data.tar.gz: 7ff7460adefcfca53e20e59b5edf3231a817ebacf63fbdb1573caf1ed981f9dcd657bcee8f0dc4199012c268b22360fcfbd07949181d05ca83890187d8e7d9ef
6
+ metadata.gz: cac6624d0caceedcd3ac833d87a3a498d0a023b74d604b626413df62f15e107324a538035d93e9a39800eba57dddca4b6a70a8bd4825fed99a002411634254a5
7
+ data.tar.gz: 3a622b2f506d8a3d98f5c97646fa6da662b8b6ea86e86c9e315c10a62ef4f88acd7befc7f22167f52b8de3b0d9ea9fc8dfccf5924da7da74a061f3b1ba450ea8
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.0)
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.0"
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
@@ -106,7 +93,22 @@ def capistrano_nomad_build_docker_image_for_type(image_type)
106
93
  args << "--build-arg #{key}=#{value}"
107
94
  end
108
95
 
109
- capistrano_nomad_build_docker_image(image_type, attributes[:path], args)
96
+ run_locally do
97
+ # If any of these files exist then we're in the middle of rebase so we should interrupt
98
+ if ["rebase-merge", "rebase-apply"].any? { |f| File.exist?("#{capistrano_nomad_git.dir.path}/.git/#{f}") }
99
+ raise StandardError, "Still in the middle of git rebase, interrupting docker image build"
100
+ end
101
+
102
+ image_alias_args = args.dup
103
+
104
+ [capistrano_nomad_build_docker_image_alias(image_type)]
105
+ .compact
106
+ .each do |tag|
107
+ image_alias_args << "--tag #{tag}"
108
+ end
109
+
110
+ execute("docker build #{image_alias_args.join(' ')} .#{capistrano_nomad_root.join(attributes[:path])}")
111
+ end
110
112
  end
111
113
 
112
114
  def capistrano_nomad_push_docker_image_for_type(image_type, is_manifest_updated: true)
@@ -114,23 +116,22 @@ def capistrano_nomad_push_docker_image_for_type(image_type, is_manifest_updated:
114
116
  invoke("nomad:docker_images:push")
115
117
 
116
118
  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
119
+ interaction_handler = CapistranoNomadDockerPushImageInteractionHandler.new
120
+ image_alias = capistrano_nomad_build_docker_image_alias(image_type)
121
+
122
+ # We should not proceed if image cannot be pushed
123
+ unless execute("docker push #{image_alias}", interaction_handler: interaction_handler)
124
+ raise StandardError, "Docker image push unsuccessful!"
126
125
  end
127
126
 
128
127
  return unless is_manifest_updated
129
128
 
130
129
  # Update image type manifest
131
130
  capistrano_nomad_update_docker_image_types_manifest(image_type,
132
- ref: git_commit_id,
133
- image_alias: revision_image_alias
131
+ alias: image_alias,
132
+
133
+ # Has the @sha256:xxxx appended so we have the ability to also target by digest
134
+ alias_digest: "#{image_alias}@#{interaction_handler.last_digest}",
134
135
  )
135
136
  end
136
137
  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
 
@@ -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)
@@ -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.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-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