chopshop-logreader 0.2.1 → 0.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c53ec0d00981d6a750be43cfa24862d8fd70d1279c059d48dc3b82373039b361
4
- data.tar.gz: abb8eef308f179833c6d79a5ff96381467ebebcfeee5781fc7345eafd9abd355
3
+ metadata.gz: ed7a741e3549b4d7b48cc7a30bb099a9debda0378c2d9873cea0ba07fafcad2d
4
+ data.tar.gz: 9b8f94a7360e0319370c5391a547f2ad874833a27ff00e4325a7731e30fb908d
5
5
  SHA512:
6
- metadata.gz: b45c9045b2f02568c1399dd1108cfd73105309e904210533ff820d0dcfaf3da2b2a099dd8332974ff8d871ebac86265db9d5c7d6775427035638b8ccd9e759d8
7
- data.tar.gz: 2334c245b37282fdaf7117b4d4e9ad45724f805eb72fd87497013cf928f49c71eb720553c84cfc50d915ac74f32f3664e7f2e0e1d76140ed0ecf8e23081a5735
6
+ metadata.gz: e2a513f331481b74f60a122b318552826d08d8fca8dcb651e212ca4fbad37257378efe8f286490aae16d02428814b918ba0ee3f5f262503a2bdc6ca3462e52b7
7
+ data.tar.gz: 3fc6ad5d1c1fbe8810132e45258672728079e1ebcb5871755b7ae8432025224d75e85bc12e142fd0c015836152788bf7122e1fa4fdb64076d54e81ab8266b2a6
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- chopshop-logreader (0.2.1)
4
+ chopshop-logreader (0.2.4)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -3,8 +3,12 @@ require "chopshop/logreader/parser"
3
3
  module Chopshop
4
4
  module Logreader
5
5
  class Executor
6
- attr_reader :parser
6
+ attr_reader :parser, :options, :profile, :tenant,
7
+ :region, :namespace, :container, :service_name
7
8
 
9
+ # The regex + time calculator takes the human readable output from the kubernetes CLI
10
+ # and calculates how long any given pod has been running. It then selects the most
11
+ # recently pod/shortest living pod
8
12
  REGEX = /(?<t1>\d+)(?<v1>[a-z]+)(?<t2>\d*)(?<v2>[a-z]*)/i
9
13
  TIME_CALCULATOR = {
10
14
  "s" => 1,
@@ -22,26 +26,43 @@ module Chopshop
22
26
  @parser = Chopshop::Logreader::Parser.new
23
27
  end
24
28
 
29
+ def parse_options
30
+ @options = @parser.parse
31
+ @profile = options.profile || ENV["AWS_PROFILE"] || ENV["PROFILE"]
32
+ @tenant = options.tenant || ENV["DEFAULT_TENANT"]
33
+ @region = options.region || ENV["AWS_REGION"] || "us-east-1"
34
+ @namespace = options.namespace || ENV["K8_NAMESPACE"] || "connect"
35
+ @container = options.container
36
+ @service_name = ARGV[0]
37
+ end
38
+
25
39
  def execute!
26
- options = @parser.parse
27
- profile = options.profile || ENV["AWS_PROFILE"] || ENV["PROFILE"]
28
- tenant = options.tenant || ENV["DEFAULT_TENANT"]
29
-
30
- container = nil
31
- service = ARGV[0]
32
- `rally-kubectl -e #{profile} -t #{tenant}`
33
- puts "looking for valid container"
34
- while !container
35
- containers = `kubectl get pods --namespace #{options.namespace} | grep #{service}`
36
- container = containers.split("\n").map {|line| line.split(" ") }.each do |line|
40
+ parse_options
41
+ service = nil
42
+
43
+ # log into the EKS cluster, may require 2FA authing here.
44
+ `rally-kubectl -a #{region} -e #{profile} -t #{tenant}`
45
+ puts "looking for valid service container"
46
+ while !service
47
+ services = `kubectl get pods --namespace #{namespace} | grep #{service_name}`
48
+ service = services.split("\n").map {|line| line.split(" ") }.each do |line|
49
+ # get the length of time the pod has been running from the kubernetes CLI output
37
50
  match_data = REGEX.match(line[4])
51
+ # calculate the human readable version of time into a single integer for comparison
38
52
  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
53
+ # select the most recent/shortest living pod
39
54
  end.select{|line| line[2] == options.status }.sort_by {|line| line[5] }.first
40
55
 
41
- if container
42
- exec "kubectl logs --follow=#{options.follow} --tail=#{options.lines} --namespace connect #{container[0]}"
56
+ # if we have a pod we want logs from, then get logs and be done
57
+ if service
58
+ if container
59
+ exec "kubectl logs --follow=#{options.follow} --tail=#{options.lines} --namespace #{namespace} --container=#{container} #{service[0]}"
60
+ else
61
+ exec "kubectl logs --follow=#{options.follow} --tail=#{options.lines} --namespace #{namespace} #{service[0]}"
62
+ end
43
63
  end
44
64
 
65
+ # no pod with logs, wait 1 second and try again.
45
66
  print "."
46
67
  sleep 1
47
68
  end
@@ -8,15 +8,17 @@ module Chopshop
8
8
  options = OpenStruct.new(
9
9
  follow: true, # follow output from log file
10
10
  lines: -1, # whole file always,
11
- status: "Running", # look for a currently running container
12
- namespace: "connect",
11
+ status: "Running", # look for a currently running service
12
+ namespace: nil,
13
13
  tenant: nil,
14
- profile: nil
14
+ profile: nil,
15
+ region: nil,
16
+ container: nil
15
17
  )
16
18
 
17
19
 
18
20
  OptionParser.new do |opts|
19
- opts.banner = "Usage: ruby log-reader.rb SERVICE [options]"
21
+ opts.banner = "Usage: chopshop-logreader SERVICE [options]"
20
22
 
21
23
  opts.on("-f [FOLLOW]", "--follow [FOLLOW]", "boolean true/false on whether to follow output from the file. default: true", TrueClass) do |follow|
22
24
  options[:follow] = follow
@@ -26,19 +28,27 @@ module Chopshop
26
28
  options[:lines] = lines
27
29
  end
28
30
 
29
- opts.on("-s [STATUS]", "--status [STATUS]", "valid values: Completed|Running|Error. will only look for containers with the given status. default: Running", String) do |status|
31
+ opts.on("-s [STATUS]", "--status [STATUS]", "valid values: Completed|Running|Error. will only look for services/jobs with the given status. default: Running", String) do |status|
30
32
  options[:status] = status
31
33
  end
32
34
 
33
- opts.on("-n [NAMESPACE]", "--namespace [NAMESPACE]", "sets the kubernetes namespace to look for containers in. default: connect", String) do |status|
34
- options[:status] = status
35
+ opts.on("-n [NAMESPACE]", "--namespace [NAMESPACE]", "sets the kubernetes namespace to look for service/job in. default: connect", String) do |namespace|
36
+ options[:namespace] = namespace
35
37
  end
36
38
 
37
39
  opts.on("-p [PROFILE]", "--profile [PROFILE]", "chooses the cloud profile to use for permissions. default: nil. You must provide this value or set the ENV VAR AWS_PROFILE' or the ENV VAR 'PROFILE'", String) do |profile|
38
40
  options[:profile] = profile
39
41
  end
40
42
 
41
- opts.on("-t [TENANT]", "--tenant [TENANT]", "sets the kubernetes tenant to look for containers in. default: nil. You must provide this value or set the ENV VAR DEFAULT_TENANT'", String) do |tenant|
43
+ opts.on("-r [REGION]", "--region [REGION]", "sets the cloud region to look for a tenant within. default: us-east-1. You may also provide this value via the ENV VAR 'AWS_REGION'", String) do |region|
44
+ options[:region] = region
45
+ end
46
+
47
+ opts.on("-c [CONTAINER]", "--container [CONTAINER]", "sets the kubernetes tenant to look for containers in. default: nil. Often not needed", String) do |container|
48
+ options[:container] = container
49
+ end
50
+
51
+ opts.on("-t [TENANT]", "--tenant [TENANT]", "sets the kubernetes tenant to look for the service/job in. default: nil. You must provide this value or set the ENV VAR DEFAULT_TENANT'", String) do |tenant|
42
52
  options[:tenant] = tenant
43
53
  end
44
54
 
@@ -54,4 +64,4 @@ module Chopshop
54
64
  end
55
65
  end
56
66
  end
57
- end
67
+ end
@@ -1,5 +1,5 @@
1
1
  module Chopshop
2
2
  module Logreader
3
- VERSION = "0.2.1"
3
+ VERSION = "0.2.4"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: chopshop-logreader
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Travis Perdue
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-12-10 00:00:00.000000000 Z
11
+ date: 2022-04-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler