afterlife 1.4.0 → 1.5.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: 50bb80b8a5c213397eb84b52d78f39fc6ff72491a45df415e0c7700d1c2eb4bc
4
- data.tar.gz: d6a8963da3e0d48f511e182d4bcf5c49f20e4e7449e29e9d8cc1f35c2a43fcc8
3
+ metadata.gz: e03a1499e1af905014e5fb48d8865601a0bb354a856d56dc0a629a3b3c08a99b
4
+ data.tar.gz: 345db972b71109a69d3117e3ef5e3a8fc8cc4aec094c230f9b49ea46e2c206e3
5
5
  SHA512:
6
- metadata.gz: '0594d2bce7f949b133c67536d8a3115b2bcdc3a12dc6826135335d6aa5d1cd5e0bd938088a5437bb0381aef2b509b06f990c5268b5a21e8d667882b1355ed274'
7
- data.tar.gz: 8b40060493294350391a2aec32c866cadd04a7aaf087ada970fb267430e836c3f724b4dc1ef683beb3337e59cd8b3a77f420afd168830d6972b7c8ee301e8fb8
6
+ metadata.gz: 2a4b14afb3f286756663da5d5c6c039cd0c327cf8a9ad60a3c20b04d994fdba73b2270892b209cbf9ece8f424e17e2e6eab9798f93416c254aa4549b486a41fc
7
+ data.tar.gz: 89e9ec3e6d10345f8baeea198d0dde968d96b438cc5312a7d2f545176b4755bd6953b0ad0ad60663015c33b8ac477f5bacb3aa1948bc320a1495cfd1602396ef
data/lib/afterlife/cli.rb CHANGED
@@ -32,6 +32,7 @@ module Afterlife
32
32
  option 'no-build', type: :boolean
33
33
  option 'no-install', type: :boolean
34
34
  option 'no-auth', type: :boolean
35
+ option 'no-apply', type: :boolean
35
36
  option 'dry-run', type: :boolean
36
37
  option 'skip-after-hooks', type: :boolean
37
38
  option :yes, type: :boolean
@@ -27,7 +27,6 @@ module Afterlife
27
27
  authenticate_command,
28
28
  build_command,
29
29
  set_image_command,
30
- local_stage? ? delete_kubernetes_resource : nil,
31
30
  apply_kubernetes_settings,
32
31
  ].flatten.compact
33
32
  end
@@ -35,40 +34,47 @@ module Afterlife
35
34
  # commands
36
35
 
37
36
  def authenticate_command
38
- return if options['no-auth']
37
+ return if options['no-auth'] || local_stage?
39
38
 
40
- <<-BASH
41
- echo "#{aws_ecr_token}" | docker login --username AWS --password-stdin #{registry} &&
42
- kubectl delete secret regcred &&
43
- kubectl create secret docker-registry regcred
44
- --docker-server=#{registry}
45
- --docker-username=AWS
46
- --docker-password=#{aws_ecr_token}
47
- --docker-email=devs@mifiel.com
48
- BASH
39
+ AwsAuth.new(registry).commands
49
40
  end
50
41
 
51
42
  def build_command
52
43
  return if options['no-build']
53
44
 
54
45
  <<-BASH
55
- docker buildx bake -f docker-bake.hcl #{local_stage? ? '--load' : '--push'}
46
+ docker buildx bake -f docker-bake.hcl #{targets.join(' ')} #{local_stage? ? '--load' : '--push'}
56
47
  BASH
57
48
  end
58
49
 
59
50
  def set_image_command
60
51
  <<-BASH
61
- $(cd #{kubelocation} && kustomize edit set image #{image_name}:latest=#{full_image_name})
52
+ cd #{kubelocation} &&
53
+ #{
54
+ targets.map do |target|
55
+ "kustomize edit set image #{full_image_name(target)}:latest=#{registry_image_name(target)}"
56
+ end.join(' && ')
57
+ }
62
58
  BASH
63
59
  end
64
60
 
65
- def delete_kubernetes_resource
66
- <<-BASH
67
- kubectl delete -k #{kubelocation}
68
- BASH
61
+ def targets
62
+ repo.conf.dig(:deploy, :targets) || %w[app]
63
+ end
64
+
65
+ def registry_image_name(target)
66
+ "#{registry}/#{full_image_name(target)}:#{repo.current_revision}"
67
+ end
68
+
69
+ def full_image_name(target)
70
+ return image_name if target == 'app'
71
+
72
+ "#{image_name}-#{target}"
69
73
  end
70
74
 
71
75
  def apply_kubernetes_settings
76
+ return if options['no-apply']
77
+
72
78
  <<-BASH
73
79
  kubectl apply -k #{kubelocation}
74
80
  BASH
@@ -88,12 +94,10 @@ module Afterlife
88
94
  ".afterlife/#{Afterlife.current_stage.name}"
89
95
  end
90
96
 
91
- def full_image_name
92
- "#{registry}/#{image_name}:#{repo.current_revision}"
93
- end
94
-
95
97
  def image_name
96
- @image_name ||= repo.conf.dig(:deploy, :image_name)
98
+ @image_name ||= repo.conf.dig(:deploy, :image_name).tap do |result|
99
+ fail Error, 'deploy.image_name for kubernetes deployments' unless result
100
+ end
97
101
  end
98
102
 
99
103
  # Priority:
@@ -106,10 +110,30 @@ module Afterlife
106
110
  @registry ||= Afterlife.current_repo.variable('deploy.registry') || Afterlife.current_stage.registry
107
111
  end
108
112
 
109
- # command outputs
113
+ class AwsAuth
114
+ attr_reader :registry
115
+
116
+ def initialize(registry)
117
+ @registry = registry
118
+ end
119
+
120
+ def commands
121
+ [docker_login]
122
+ end
123
+
124
+ def docker_login
125
+ <<-BASH
126
+ echo "#{aws_ecr_token}" | docker login --username AWS --password-stdin #{registry}
127
+ BASH
128
+ end
129
+
130
+ def aws_ecr_token
131
+ @aws_ecr_token ||= Exec.result("aws ecr get-login-password --region #{region}")
132
+ end
110
133
 
111
- def aws_ecr_token
112
- @aws_ecr_token ||= `aws ecr get-login-password --region us-west-2`.strip
134
+ def region
135
+ @region ||= registry.gsub(/[^.]+\.dkr\.ecr\.([^.]+)\.amazonaws\.com/, '\1')
136
+ end
113
137
  end
114
138
  end
115
139
  end
@@ -5,7 +5,11 @@ require 'active_support/core_ext/string'
5
5
 
6
6
  module Afterlife
7
7
  class Exec
8
- Error = Class.new StandardError
8
+ def self.result(arg)
9
+ fail Error, 'Exec.result only accepts strings' unless arg.is_a?(String)
10
+
11
+ new(arg).run(result: true).first
12
+ end
9
13
 
10
14
  def self.run(arg)
11
15
  new(arg).run
@@ -23,11 +27,14 @@ module Afterlife
23
27
  @commands = Array(arg)
24
28
  end
25
29
 
26
- def run
27
- parsed_commands.each do |command|
30
+ def run(result: false)
31
+ parsed_commands.map do |command|
28
32
  Afterlife.cli.log_info(command) if Afterlife.cli.options['verbose']
29
- system(env_hash, command.squish, exception: true) unless Afterlife.cli.options['dry-run']
30
- end
33
+ next if Afterlife.cli.options['dry-run']
34
+ next `#{command}`.squish if result
35
+
36
+ system(env_hash, command.squish, exception: true)
37
+ end.compact
31
38
  rescue RuntimeError => e
32
39
  raise Error, e
33
40
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Afterlife
4
- VERSION = '1.4.0'
4
+ VERSION = '1.5.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: afterlife
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.0
4
+ version: 1.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Genaro Madrid
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-01-19 00:00:00.000000000 Z
11
+ date: 2024-01-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport