Class: Chopshop::Logreader::Executor

Inherits:
Object
  • Object
show all
Defined in:
lib/chopshop/logreader/executor.rb

Constant Summary collapse

REGEX =

The regex + time calculator takes the human readable output from the kubernetes CLI and calculates how long any given pod has been running. It then selects the most recently pod/shortest living pod

/(?<t1>\d+)(?<v1>[a-z]+)(?<t2>\d*)(?<v2>[a-z]*)/i
TIME_CALCULATOR =
{
  "s" => 1,
  "m" => 60,
  "h" => 60 * 60,
  "d" => 60 * 60 * 24,
  "" => 0 # protect for the scenario where a value isn't present in the capture group.
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeExecutor

Returns a new instance of Executor.



25
26
27
# File 'lib/chopshop/logreader/executor.rb', line 25

def initialize
  @parser = Chopshop::Logreader::Parser.new
end

Instance Attribute Details

#containerObject (readonly)

Returns the value of attribute container.



6
7
8
# File 'lib/chopshop/logreader/executor.rb', line 6

def container
  @container
end

#namespaceObject (readonly)

Returns the value of attribute namespace.



6
7
8
# File 'lib/chopshop/logreader/executor.rb', line 6

def namespace
  @namespace
end

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'lib/chopshop/logreader/executor.rb', line 6

def options
  @options
end

#parserObject (readonly)

Returns the value of attribute parser.



6
7
8
# File 'lib/chopshop/logreader/executor.rb', line 6

def parser
  @parser
end

#profileObject (readonly)

Returns the value of attribute profile.



6
7
8
# File 'lib/chopshop/logreader/executor.rb', line 6

def profile
  @profile
end

#regionObject (readonly)

Returns the value of attribute region.



6
7
8
# File 'lib/chopshop/logreader/executor.rb', line 6

def region
  @region
end

#service_nameObject (readonly)

Returns the value of attribute service_name.



6
7
8
# File 'lib/chopshop/logreader/executor.rb', line 6

def service_name
  @service_name
end

#tenantObject (readonly)

Returns the value of attribute tenant.



6
7
8
# File 'lib/chopshop/logreader/executor.rb', line 6

def tenant
  @tenant
end

Class Method Details

.execute!Object



21
22
23
# File 'lib/chopshop/logreader/executor.rb', line 21

def self.execute!
  new.execute!
end

Instance Method Details

#execute!Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/chopshop/logreader/executor.rb', line 39

def execute!
  parse_options
  service = nil

  # log into the EKS cluster, may require 2FA authing here.
  `rally-kubectl -a #{region} -e #{profile} -t #{tenant}`
  puts "looking for valid service container"
  while !service
    services = `kubectl get pods --namespace #{namespace} | grep #{service_name}`
    service = services.split("\n").map {|line| line.split(" ") }.each do |line|
      # get the length of time the pod has been running from the kubernetes CLI output
      match_data = REGEX.match(line[4])
      # calculate the human readable version of time into a single integer for comparison
      line[5] = TIME_CALCULATOR[match_data.captures[1].downcase] * match_data.captures[0].to_i + TIME_CALCULATOR[match_data.captures[3].downcase] * match_data.captures[2].to_i
      # select the most recent/shortest living pod
    end.select{|line| line[2] == options.status }.sort_by {|line| line[5] }.first

    # if we have a pod we want logs from, then get logs and be done
    if service
      if container
        exec "kubectl logs --follow=#{options.follow} --tail=#{options.lines} --namespace #{namespace} --container=#{container} #{service[0]}"
      else
        exec "kubectl logs --follow=#{options.follow} --tail=#{options.lines} --namespace #{namespace} #{service[0]}"
      end
    end

    # no pod with logs, wait 1 second and try again.
    print "."
    sleep 1
  end
end

#parse_optionsObject



29
30
31
32
33
34
35
36
37
# File 'lib/chopshop/logreader/executor.rb', line 29

def parse_options
  @options = @parser.parse
  @profile = options.profile || ENV["AWS_PROFILE"] || ENV["PROFILE"]
  @tenant = options.tenant || ENV["DEFAULT_TENANT"]
  @region = options.region || ENV["AWS_REGION"] || "us-east-1"
  @namespace = options.namespace || ENV["K8_NAMESPACE"] || "connect"
  @container = options.container
  @service_name = ARGV[0]
end