class HoobsKubes
Public Class Methods
apply_dir(dirname)
click to toggle source
# 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_context()
click to toggle source
# File lib/hoobskubes.rb, line 9 def self.change_context %x{kubectx #{context}} check_correct_context end
check_correct_context()
click to toggle source
# 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_context()
click to toggle source
# File lib/hoobskubes.rb, line 5 def self.current_context %x{kubectl config current-context}.strip end
do_deploy()
click to toggle source
# 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_proxy()
click to toggle source
# 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_status()
click to toggle source
# File lib/hoobskubes.rb, line 76 def self.do_status log "#{current_context} Cluster Status:".bold.cyan status end
log(str)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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