Class: Deployer

Inherits:
Object
  • Object
show all
Defined in:
lib/deployer.rb

Constant Summary collapse

CONFIG_LOCATIONS =
%w(.rops.yaml platform/rops.yaml config/rops.yaml).freeze
CONFIG_DEFAULTS =
{
  'repository' => nil,
  'default_branch' => 'master',
  'registry' => 'r360',
  'default_context' => 'staging',
  'production_context' => 'production',
  'images' => []
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root = nil) ⇒ Deployer

Returns a new instance of Deployer.



30
31
32
33
34
35
36
37
# File 'lib/deployer.rb', line 30

def initialize(root = nil)
  @images = []
  @specs = {}

  @root = root || Dir.pwd
  load_config
  self.branch = default_branch
end

Instance Attribute Details

#branchObject

Returns the value of attribute branch.



20
21
22
# File 'lib/deployer.rb', line 20

def branch
  @branch
end

#commitObject (readonly)

Returns the value of attribute commit.



20
21
22
# File 'lib/deployer.rb', line 20

def commit
  @commit
end

#default_branchObject (readonly)

Returns the value of attribute default_branch.



19
20
21
# File 'lib/deployer.rb', line 19

def default_branch
  @default_branch
end

#default_contextObject (readonly)

Returns the value of attribute default_context.



19
20
21
# File 'lib/deployer.rb', line 19

def default_context
  @default_context
end

#image_tagObject (readonly)

Returns the value of attribute image_tag.



20
21
22
# File 'lib/deployer.rb', line 20

def image_tag
  @image_tag
end

#imagesObject (readonly)

Returns the value of attribute images.



18
19
20
# File 'lib/deployer.rb', line 18

def images
  @images
end

#production_contextObject (readonly)

Returns the value of attribute production_context.



19
20
21
# File 'lib/deployer.rb', line 19

def production_context
  @production_context
end

#registryObject (readonly)

Returns the value of attribute registry.



18
19
20
# File 'lib/deployer.rb', line 18

def registry
  @registry
end

#repositoryObject (readonly)

Returns the value of attribute repository.



18
19
20
# File 'lib/deployer.rb', line 18

def repository
  @repository
end

#rootObject (readonly)

Returns the value of attribute root.



18
19
20
# File 'lib/deployer.rb', line 18

def root
  @root
end

#ssh_hostObject (readonly)

Returns the value of attribute ssh_host.



18
19
20
# File 'lib/deployer.rb', line 18

def ssh_host
  @ssh_host
end

Class Method Details

.dockerObject



22
23
24
# File 'lib/deployer.rb', line 22

def self.docker
  @docker_path ||= File.which('docker') || File.which('podman')
end

.podman?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/deployer.rb', line 26

def self.podman?
  docker.include?('podman')
end

Instance Method Details

#deploy!(context) ⇒ Object



95
96
97
98
99
100
101
102
# File 'lib/deployer.rb', line 95

def deploy!(context)
  context ||= default_context
  context = 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?
end

#deploy_specs(context = nil) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/deployer.rb', line 104

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



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/deployer.rb', line 134

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



124
125
126
127
128
129
130
131
132
# File 'lib/deployer.rb', line 124

def specs(context = nil)
  context ||= default_context
  context = context.to_s
  @specs[context] ||= begin
    paths = git.ls_tree(commit, "platform/#{context}/")['blob'].keys
    raise "No specs found for context #{context}"  unless paths.present?
    paths.map { |path| YAML.load_stream( git.show(commit, path) ) }.flatten.compact
  end
end

#specs_running(context = nil) ⇒ Object



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
84
85
86
87
88
89
90
91
92
93
# File 'lib/deployer.rb', line 56

def specs_running(context = nil)
  context ||= default_context
  context = 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