-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rb
executable file
·88 lines (72 loc) · 1.92 KB
/
main.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'open3'
require 'yaml'
require 'base64'
require 'uri'
require_relative 'lib/kubectl'
require_relative 'lib/kube_host'
def flags(args)
flags = {}
args.each do |arg|
case arg
when '--help', '-h'
puts "Usage: kubectl-gethost <host>
OPTIONS:
--help, -h: Show this help message
--secrets, -s: Show secrets for the host
--interactive, -i: Set a maintenance pod for the host
--duration, -d: Set a duration for the maintenance pod
"
exit 1
when '--secrets', '-s'
flags[:secrets] = true
when '--interactive', '-i'
flags[:maintainance] = true
when '--duration', '-d'
flags[:duration] = args[args.index(arg) + 1].to_i
if flags[:duration].nil? || flags[:duration] <= 0
puts 'Duration must be a positive integer'
exit 1
end
end
end
flags
end
host = if URI.parse(ARGV[0] || '').is_a?(URI::Generic)
ARGV[0]
else
URI.parse(ARGV[0]).host
end
flags = flags(ARGV)
if host.nil? || host.empty?
puts 'No hosts requested, end of process !'
puts "Usage: kubectl-gethost <host>
OPTIONS:
--help, -h: Show this help message
--secrets, -s: Show secrets for the host
--interactive, -i: Set a maintenance pod for the host
--duration, -d: Set a duration for the maintenance pod
"
exit 1
end
puts 'Looking for Kubernetes host...'
syscmd = Lib::Kubectl.get_hosts
unless syscmd.status.success?
puts 'kubectl failed unexpectedly!'
puts "stderr:
#{syscmd.stderr}"
return
end
hosts = JSON.parse(syscmd.stdout)&.fetch('items', [])&.map { |item| Lib::KubeHost.from_hash(item) }
targets = hosts.select { |format| host.include?(format.host) }
if targets.empty?
puts "'#{host}' not found"
return
end
targets.each do |target|
target.print
target.get_secrets if flags[:secrets]
duration = flags[:duration] || 30
target.set_maintainance_pod(duration) if flags[:maintainance]
end