Class: Deployer
- Inherits:
-
Object
- Object
- Deployer
- Defined in:
- lib/deployer.rb
Instance Attribute Summary collapse
-
#branch ⇒ Object
Returns the value of attribute branch.
-
#commit ⇒ Object
readonly
Returns the value of attribute commit.
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#image_tag ⇒ Object
readonly
Returns the value of attribute image_tag.
-
#root ⇒ Object
readonly
Returns the value of attribute root.
-
#spec_dir ⇒ Object
Returns the value of attribute spec_dir.
Class Method Summary collapse
Instance Method Summary collapse
- #deploy!(context) ⇒ Object
- #deploy_specs(context = nil) ⇒ Object
-
#initialize(root = nil) ⇒ Deployer
constructor
A new instance of Deployer.
- #kubectl(context, cmd, data = nil) ⇒ Object
- #specs(context = nil) ⇒ Object
- #specs_running(context = nil) ⇒ Object
Constructor Details
#initialize(root = nil) ⇒ Deployer
27 28 29 30 31 32 |
# File 'lib/deployer.rb', line 27 def initialize(root = nil) @specs = {} @root = root || Dir.pwd load_config self.branch = default_branch end |
Instance Attribute Details
#branch ⇒ Object
Returns the value of attribute branch.
13 14 15 |
# File 'lib/deployer.rb', line 13 def branch @branch end |
#commit ⇒ Object (readonly)
Returns the value of attribute commit.
13 14 15 |
# File 'lib/deployer.rb', line 13 def commit @commit end |
#config ⇒ Object (readonly)
Returns the value of attribute config.
12 13 14 |
# File 'lib/deployer.rb', line 12 def config @config end |
#image_tag ⇒ Object (readonly)
Returns the value of attribute image_tag.
13 14 15 |
# File 'lib/deployer.rb', line 13 def image_tag @image_tag end |
#root ⇒ Object (readonly)
Returns the value of attribute root.
12 13 14 |
# File 'lib/deployer.rb', line 12 def root @root end |
#spec_dir ⇒ Object
Returns the value of attribute spec_dir.
14 15 16 |
# File 'lib/deployer.rb', line 14 def spec_dir @spec_dir end |
Class Method Details
.docker ⇒ Object
19 20 21 |
# File 'lib/deployer.rb', line 19 def self.docker @docker_path ||= File.which('docker') || File.which('podman') end |
.podman? ⇒ Boolean
23 24 25 |
# File 'lib/deployer.rb', line 23 def self.podman? docker.include?('podman') end |
Instance Method Details
#deploy!(context) ⇒ Object
85 86 87 88 89 90 91 92 |
# File 'lib/deployer.rb', line 85 def deploy!(context) context = (context || default_context).to_s specs = deploy_specs(context).presence or raise "No kubernetes specs to deploy" stdout, stderr, _success = kubectl(context, 'apply -f -', YAML.dump_stream(*specs)) puts stdout if stdout.present? puts stderr if stderr.present? notify!(context) end |
#deploy_specs(context = nil) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/deployer.rb', line 94 def deploy_specs(context = nil) dspecs = [] specs(context).deep_dup.each do |spec| containers = Array(spec.dig('spec', 'template', 'spec', 'containers')) + # deployments/statefulsets Array(spec.dig('spec', 'jobTemplate', 'spec', 'template', 'spec', 'containers')) # cronjobs containers.each do |container| image = images.detect { |image| image.remote_repo == container['image'] } if image container['image'] = image.remote_image dspecs << spec unless dspecs.include?(spec) elsif !container['image'].include?(':') raise "Unknown image #{container['image']}" end end end dspecs end |
#kubectl(context, cmd, data = nil) ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/deployer.rb', line 124 def kubectl(context, cmd, data = nil) cmd = "kubectl --context #{context} #{cmd}" if ssh_host.blank? stdout, stderr, cmd_status = Open3.capture3(cmd, stdin_data: data) [ stdout, stderr, cmd_status.success? ] else require 'net/ssh' exit_code = -1 stdout = String.new stderr = String.new ssh = Net::SSH.start(ssh_host) ssh.open_channel do |channel| channel.exec(cmd) do |_ch, success| success or raise "FAILED: couldn't execute command on #{ssh_host}: #{cmd.inspect}" channel.on_data { |_ch, in_data| stdout << in_data } channel.on_extended_data { |_ch, _type, in_data| stderr << in_data } channel.on_request('exit-status') { |_ch, in_data| exit_code = in_data.read_long } channel.send_data(data) if data channel.eof! end end ssh.loop [ stdout, stderr, exit_code.zero? ] end end |
#specs(context = nil) ⇒ Object
114 115 116 117 118 119 120 121 122 |
# File 'lib/deployer.rb', line 114 def specs(context = nil) spec_dir = self.spec_dir.presence || (context || default_context).to_s @specs[spec_dir] ||= begin spec_dir = "platform/#{spec_dir}/" paths = git.ls_tree(commit, spec_dir)['blob'].keys raise "No specs found in #{spec_dir}" unless paths.present? paths.map { |path| YAML.load_stream( git.show(commit, path) ) }.flatten.compact end end |
#specs_running(context = nil) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/deployer.rb', line 47 def specs_running(context = nil) context = (context || default_context).to_s specs = deploy_specs(context) cmd = String.new "--output=json" if (namespace = specs.first.dig('metadata', 'namespace')) cmd += " --namespace #{namespace}" end cmd += " get" specs.each do |spec| cmd += " #{spec['kind'].downcase}/#{spec.dig('metadata', 'name')}" end statuses, stderr, success = kubectl(context, cmd) unless (success || stderr.match(/not found/)) && statuses.present? puts stderr if stderr.present? return nil end spec_status = specs.map { |spec| [ spec, nil ] }.to_h statuses = JSON.parse(statuses) statuses = statuses['items'] if statuses.key?('items') Array.wrap(statuses).each do |item| containers = Array(item.dig('spec', 'template', 'spec', 'containers')) + Array(item.dig('spec', 'jobTemplate', 'spec', 'template', 'spec', 'containers')) version = containers.first['image'].split(':').last # FIXME: support multiple containers status = item.delete('status').with_indifferent_access spec = specs.detect do |s| (item['kind'] == s['kind']) && (item.dig('metadata', 'name') == s.dig('metadata', 'name')) && (item.dig('metadata', 'namespace') == (s.dig('metadata', 'namespace') || 'default')) end spec_status[spec] = { spec: item, version: version, status: status } end spec_status end |