Class: Seira::Helpers

Inherits:
Object
  • Object
show all
Includes:
Commands
Defined in:
lib/helpers.rb

Class Method Summary collapse

Methods included from Commands

#gcloud, gcloud, kubectl, #kubectl

Class Method Details

.extract_ip_if_present(ip_address) ⇒ Object



59
60
61
62
63
# File 'lib/helpers.rb', line 59

def extract_ip_if_present(ip_address)
  return nil if ip_address.nil?

  ip_address['ipAddress']
end

.fetch_pod(name, context:) ⇒ Object



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

def fetch_pod(name, context:)
  output = Seira::Commands.kubectl("get pod #{name} -o json", context: context, return_output: true)
  JSON.parse(output) unless output.empty?
end

.fetch_pods(filters:, context:) ⇒ Object



16
17
18
19
20
# File 'lib/helpers.rb', line 16

def fetch_pods(filters:, context:)
  filter_string = { app: context[:app] }.merge(filters).map { |k, v| "#{k}=#{v}" }.join(',')
  output = Seira::Commands.kubectl("get pods -o json --selector=#{filter_string}", context: context, return_output: true)
  JSON.parse(output)['items']
end

.get_current_replicas(deployment:, context:) ⇒ Object



40
41
42
43
# File 'lib/helpers.rb', line 40

def get_current_replicas(deployment:, context:)
  output = Seira::Commands.kubectl("get deployment #{deployment} -o json", context: context, return_output: true)
  JSON.parse(output)['spec']['replicas']
end

.get_secret(key:, context:) ⇒ Object



36
37
38
# File 'lib/helpers.rb', line 36

def get_secret(key:, context:)
  Secrets.new(app: context[:app], action: 'get', args: [], context: context).get(key)
end


27
28
29
30
31
32
33
34
# File 'lib/helpers.rb', line 27

def log_link(context:, query:)
  link = context[:settings].log_link_format
  return nil if link.nil?
  link.gsub! 'APP', context[:app]
  link.gsub! 'CLUSTER', context[:cluster]
  link.gsub! 'QUERY', query
  link
end

.rails_env(context:) ⇒ Object



6
7
8
9
10
11
12
13
14
# File 'lib/helpers.rb', line 6

def rails_env(context:)
  parsed_env = context[:settings].settings['seira']['clusters'][context[:cluster]]['environment']
  parsed_env = context[:cluster] if parsed_env.nil?
  if parsed_env == 'internal'
    'production'
  else
    parsed_env
  end
end

.shell_usernameObject



45
46
47
48
49
# File 'lib/helpers.rb', line 45

def shell_username
  `whoami`
rescue
  'unknown'
end

.sql_ips(name, context:) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/helpers.rb', line 51

def sql_ips(name, context:)
  describe_command = "sql instances describe #{name}"
  json = JSON.parse(Seira::Commands.gcloud(describe_command, context: context, format: :json))
  private_ip = extract_ip_if_present(json['ipAddresses'].find { |address| address['type'] == 'PRIVATE' })
  public_ip = extract_ip_if_present(json['ipAddresses'].find { |address| address['type'] == 'PRIMARY' })
  { private: private_ip, public: public_ip }
end