forked from policygenius/laptop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kutil.rb
99 lines (83 loc) · 2.23 KB
/
kutil.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
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/env ruby
def pexec(command)
puts command
exec command
end
def apps
@apps ||= `kubectl get pods`
.split("\n")
.select { |line| line.include?(app) }
.map { |line| line.strip.split(' ').first }
.group_by { |line| line.split('-')[0...-2].uniq.join('-') }
end
def app
ARGV[1]
end
def matching_apps
apps[app] || []
end
def no_matching_apps
if apps.any?
puts "Ambigious match name #{app}."
puts "Maybe you meant one of the following:"
apps.keys.each { |p| puts " - #{p}" }
else
puts "No matching pods for #{app}."
end
end
def switch_to_production
pexec 'gcloud container clusters get-credentials production-v3 --zone us-central1-f --project pg-production-v1'
end
def switch_to_staging
pexec 'gcloud container clusters get-credentials staging-v3 --zone us-central1-f --project pg-staging-v1'
end
def print_app_names
puts 'Matching apps:'
apps.keys.each { |p| puts " - #{p}" }
end
def tail_logs
if matching_apps.any?
pod = matching_apps.first
pexec "kubectl logs -f #{pod}"
else
no_matching_apps
end
end
def exec_command
if matching_apps.any?
pod = matching_apps.first
pexec "kubectl exec -it #{pod} #{ARGV[2..-1].join(' ')}"
else
no_matching_apps
end
end
command = ARGV.first
context = `kubectl config current-context`
if context.include?('production') && ['staging', 'production'].include?(command)
puts <<-ASCII
_______ _ _ _ _ _ _
|__ __| | (_) (_) | | | | (_)
| | | |__ _ ___ _ ___ _ __ _ __ ___ __| |_ _ ___| |_ _ ___ _ __
| | | '_ \\| / __| | / __| | '_ \\| '__/ _ \\ / _` | | | |/ __| __| |/ _ \\| '_ \\
| | | | | | \\__ \\ | \\__ \\ | |_) | | | (_) | (_| | |_| | (__| |_| | (_) | | | |
|_| |_| |_|_|___/ |_|___/ | .__/|_| \\___/ \\__,_|\\__,_|\\___|\\__|_|\\___/|_| |_|
| |
|_|
ASCII
5.times do |i|
sleep 1
print "#{5 - i} "
end
puts
end
if command == 'staging'
switch_to_staging
elsif command == 'production'
switch_to_production
elsif command == 'apps'
print_app_names
elsif command == 'logs'
tail_logs
elsif command == 'exec'
exec_command
end