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, #tsh, tsh

Class Method Details

.extract_ip_if_present(ip_address) ⇒ Object



68
69
70
71
72
# File 'lib/helpers.rb', line 68

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

  ip_address['ipAddress']
end

.fetch_pod(name, context:) ⇒ Object



24
25
26
27
# File 'lib/helpers.rb', line 24

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



18
19
20
21
22
# File 'lib/helpers.rb', line 18

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



49
50
51
52
# File 'lib/helpers.rb', line 49

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



38
39
40
# File 'lib/helpers.rb', line 38

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

.get_seira_app_config(context:) ⇒ Object



42
43
44
45
46
47
# File 'lib/helpers.rb', line 42

def get_seira_app_config(context:)
  yaml_file_path = "kubernetes/#{context[:cluster]}/#{context[:app]}/.seira.app.yaml"
  YAML.load_file(yaml_file_path)
rescue
  fail "Failed to load the configuration file at #{yaml_file_path}. Please add configuration file and retry."
end


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

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



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

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



54
55
56
57
58
# File 'lib/helpers.rb', line 54

def shell_username
  `whoami`
rescue
  'unknown'
end

.sql_ips(name, context:) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/helpers.rb', line 60

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