Class: HoobsKubes

Inherits:
Object
  • Object
show all
Defined in:
lib/hoobskubes.rb

Class Method Summary collapse

Class Method Details

.apply_dir(dirname) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/hoobskubes.rb', line 48

def self.apply_dir(dirname)
  log "Applying #{dirname}:".bold.cyan
  path = "#{@@dir}/#{dirname}"
  Dir.foreach(path) do |item|
    next if item == '.' or item == '..'
    log "  #{item}"
    result = %x{kubectl apply -f #{path}/#{item}}.strip
    result.each_line do |line|
      if line.include? "unchanged"
        line = line.strip.bold.green
      elsif line.include? "configured"
        line = line.strip.bold.brown
      elsif line.include? "created"
        line = line.strip.bold.magenta
      else
        line = line.strip.bold.red
      end
      log "    #{line}"
    end
  end
end

.change_contextObject



9
10
11
12
# File 'lib/hoobskubes.rb', line 9

def self.change_context
  %x{kubectx #{context}}
  check_correct_context
end

.check_correct_contextObject



14
15
16
17
# File 'lib/hoobskubes.rb', line 14

def self.check_correct_context
  Kernel.abort("Context mismatch: expected is `#{context}` but current is `#{current_context}`") unless context == current_context
  log "Context is #{current_context.bold.green}"
end

.current_contextObject



5
6
7
# File 'lib/hoobskubes.rb', line 5

def self.current_context
  %x{kubectl config current-context}.strip
end

.do_deployObject



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/hoobskubes.rb', line 81

def self.do_deploy
  begin
    log "Starting deploy".bold.cyan
    deploy
    log "Done!".bold.green
  rescue
    log "Error!".bold.red
    raise
  end

  do_status
end

.do_proxyObject



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/hoobskubes.rb', line 94

def self.do_proxy
  begin
    log "Starting proxy".bold.cyan
    proxy_thread = Thread.new do
      %x{kubectl proxy}
    end
    log "Opening URL for #{@@options.proxy.to_s.green}".bold.cyan
    %x{open "http://127.0.0.1:8001/api/v1/namespaces/default/services/#{@@options.proxy}/proxy/"}
    proxy_thread.join
  rescue Interrupt
    puts ""
    log "Caught interrupt".bold.magenta
  end
  log "Exited proxy".bold.green
end

.do_statusObject



76
77
78
79
# File 'lib/hoobskubes.rb', line 76

def self.do_status
  log "#{current_context} Cluster Status:".bold.cyan
  status
end

.log(str) ⇒ Object



70
71
72
73
74
# File 'lib/hoobskubes.rb', line 70

def self.log(str)
  str.lines do |line|
    puts line.strip.empty? ? "" : "[#{Time.now.to_s.gray}] #{line}"
  end
end

.pretty_print_table(resource, namespace = nil) ⇒ Object



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
# File 'lib/hoobskubes.rb', line 19

def self.pretty_print_table(resource, namespace=nil)
  extra = resource == "nodes" ? " -Lbeta.kubernetes.io/instance-type -Lfailure-domain.beta.kubernetes.io/zone -Lkops.k8s.io/instancegroup" : ""
  extra += " -o wide" if @@options.wide
  all = @@options.all ? " --all-namespaces" : ""

  if namespace.nil?
    out = %x{kubectl get #{resource}#{extra}#{all}}
    label = resource.capitalize
  else
    out = %x{kubectl get #{resource} --namespace=#{namespace}#{extra}}
    label = "#{resource.capitalize} in #{namespace}"
  end

  first = true
  out.each_line do |line|
    if first
      first = false
      padlen = line.strip.length - label.length - 2
      lpad = "=" * (padlen / 2)
      rpad = lpad + ("=" * (padlen % 2))
      puts "#{lpad} #{label} #{rpad}".bold.cyan
      puts line.strip.bold
    else
      puts line.strip
    end
  end
  puts "\n"
end

.run(dir) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/hoobskubes.rb', line 110

def self.run(dir)
  @@dir = dir
  @@options = OpenStruct.new
  @@options.status = false
  @@options.all = false
  @@options.wide = false
  @@options.change = false
  @@options.resource = ""

  OptionParser.new do |opts|
    opts.banner = "Usage: deploy.rb [options]"

    opts.on("-s", "--status", "Displays status only") do |v|
      @@options.status = true
    end

    opts.on("-a", "--all", "Displays status for all namespaces") do |v|
      @@options.all = true
    end

    opts.on("-w", "--wide", "Displays wide status") do |v|
      @@options.wide = true
    end

    opts.on("-c", "--change-context", "Change context without deploying") do |v|
      @@options.change = true
    end

    opts.on("-r", "--resource [RESOURCE]", "Diplay status for only a specific resource") do |v|
      @@options.resource = v
    end

    opts.on("-p", "--proxy [SERVICE]", "Runs kubectl proxy and opens the service's proxy URL") do |v|
      @@options.proxy = v
    end
  end.parse!

  change_context if @@options.change
  if @@options.proxy
    do_proxy
  else
    if @@options.resource != ""
      pretty_print_table(@@options.resource)
    elsif @@options.status
      do_status
    elsif !@@options.change
      do_deploy
    end
  end
end