class RanchHand::KubeCtl

Public Instance Methods

add_command(namespace, pod) click to toggle source
# File lib/ranch_hand/kube_ctl.rb, line 123
def add_command(namespace, pod)
  type = prompt.ask('Global, namespace, pod? (g/N/p):')
  cmd = prompt.ask('Command:')

  if %w(global Global g G).include?(type)
    type = :global
    storage.set("exec:commands:global", (global_commands[:global] << cmd).uniq)
  elsif %w(pod Pod p P).include?(type)
    type = :pod
    storage.set("exec:commands:#{namespace}:#{pod_name(pod)}", (pod_commands(namespace, pod)[:pod] << cmd).uniq)
  else
    type = :namespace
    storage.set("exec:commands:#{namespace}", (namespace_commands(namespace)[:namespace] << cmd).uniq)
  end

  [type, cmd]
end
choose_command(namespace, options={}) click to toggle source
# File lib/ranch_hand/kube_ctl.rb, line 26
def choose_command(namespace, options={})
  pod = select_pod(namespace, options)
  type, cmd = select_command(namespace, pod)
  
  run_command(namespace, pod, cmd)
end
exec(args: [], cmd_options: {}) click to toggle source
# File lib/ranch_hand/kube_ctl.rb, line 5
def exec(args: [], cmd_options: {})
  args = Array(args)

  namespace = cmd_options.delete(:namespace)
  
  if cmd_options[:remove]
    remove_command(namespace)
  elsif cmd_options[:repeat]
    repeat_command(namespace)
  elsif cmd_options[:command]
    pod = select_pod(namespace, cmd_options)
    run_command(namespace, pod, cmd_options[:command], args)
  else
    choose_command(namespace, cmd_options)
  end
end
remove_command(namespace) click to toggle source
# File lib/ranch_hand/kube_ctl.rb, line 33
def remove_command(namespace)
  pod = select_pod(namespace)
  type, cmd = select_command(namespace, pod, remove: true)

  storage_key = case type
  when :global
    "exec:commands:global"
  when :namespace
    "exec:commands:#{namespace}"
  when :pod
    "exec:commands:#{namespace}:#{pod_name(pod)}"
  end

  storage.set(storage_key, storage.get(storage_key) - Array(cmd))
end
repeat_command(namespace) click to toggle source
# File lib/ranch_hand/kube_ctl.rb, line 49
def repeat_command(namespace)
  run_command(
    namespace,
    storage.get("exec:#{namespace}:latest:pod"),
    storage.get("exec:#{namespace}:latest:cmd")
  )
end
run_command(namespace, pod, cmd, args=[]) click to toggle source
# File lib/ranch_hand/kube_ctl.rb, line 22
def run_command(namespace, pod, cmd, args=[])
  system("rancher kubectl -n #{namespace} exec -it #{pod} -- #{cmd} #{args.join(' ')}".strip)
end
run_once(namespace, pod) click to toggle source
# File lib/ranch_hand/kube_ctl.rb, line 141
def run_once(namespace, pod)
  prompt.ask('Command:')
end
select_command(namespace, pod, options={}) click to toggle source
# File lib/ranch_hand/kube_ctl.rb, line 93
def select_command(namespace, pod, options={})
  commands =  all_commands(namespace, pod, options)

  ask = options[:remove] ? 'Remove command:' : 'Run command:'
  type, cmd = prompt.enum_select(ask) do |menu|
    menu.default commands.collect{|k,v| v}.flatten.index(
        storage.get("exec:#{namespace}:latest:cmd")
      ).to_i + 1

    commands.each do |type, commands|
      commands.each do |cmd|
        menu.choice cmd, [type, cmd]
      end
    end
  end

  unless options[:remove]
    if cmd == "Add command"
      type, cmd = add_command(namespace, pod)
    elsif cmd == "Run once"
      type, cmd = nil, run_once(namespace, pod)
    end

    # save cmd as latest
    storage.set("exec:#{namespace}:latest:cmd", cmd)
  end

  [type, cmd]
end
select_pod(namespace, options={}) click to toggle source
# File lib/ranch_hand/kube_ctl.rb, line 57
def select_pod(namespace, options={})
  pods = pods(namespace)

  if options[:filter]
    if options[:filter].start_with?('-')
      pods = pods.reject{|p| p.match?(/#{options[:filter][1..-1]}/)} 
    else
      pods = pods.select{|p| p.match?(/#{options[:filter]}/)} 
    end
  end
  prompt.error("No pods matching filter: '#{options[:filter]}'") and exit if pods.empty?

  if options[:group]
    pods = pods.group_by{|p| pod_name(p)}.map{|name, pods| pods.first}
  end

  if options[:pod]
    if options[:group]
      pod = pods.select{|p| p.match?(/#{options[:pod]}/)}.first
    else
      pod = pods.select{|p| options[:pod] == p}.first
    end

    prompt.error("No pods match: '#{options[:pod]}'. Did you mean to use the group (-g) switch?") and exit unless pod
  else
    pod = prompt.enum_select("Which pod?", pods, per_page: 10,
      default: pods.index(
        storage.get("exec:#{namespace}:latest:pod")
      ).to_i + 1
    )
  end

  storage.set("exec:#{namespace}:latest:pod", pod)
  pod
end

Private Instance Methods

all_commands(namespace, pod, options) click to toggle source
# File lib/ranch_hand/kube_ctl.rb, line 147
def all_commands(namespace, pod, options)
  commands = [base_commands(options), global_commands, namespace_commands(namespace), pod_commands(namespace, pod)]
  commands.inject({},:update)
end
base_commands(options={}) click to toggle source
# File lib/ranch_hand/kube_ctl.rb, line 152
def base_commands(options={})
  if options[:remove]
    {base: []}
  else
    {base: ["Add command", "Run once"]}
  end
end
global_commands() click to toggle source
# File lib/ranch_hand/kube_ctl.rb, line 160
def global_commands
  {global: storage.get("exec:commands:global") || []}
end
namespace_commands(namespace) click to toggle source
# File lib/ranch_hand/kube_ctl.rb, line 164
def namespace_commands(namespace)
  {namespace: storage.get("exec:commands:#{namespace}") || []}
end
pod_commands(namespace, pod) click to toggle source
# File lib/ranch_hand/kube_ctl.rb, line 168
def pod_commands(namespace, pod)
  {pod: storage.get("exec:commands:#{namespace}:#{pod_name(pod)}") || []}
end
pod_name(pod) click to toggle source
# File lib/ranch_hand/kube_ctl.rb, line 172
def pod_name(pod)
  pod.split('-')[0..-3].join('-')
end
pods(namespace) click to toggle source
# File lib/ranch_hand/kube_ctl.rb, line 176
def pods(namespace)
  pods_cmd = "rancher kubectl -n #{namespace} get po"
  pods = command(printer: :null).run(pods_cmd).out.split("\n")[1..-1]&.map{|l| l.split(/\s+/)[0]}

  pods || []
end
storage() click to toggle source
# File lib/ranch_hand/kube_ctl.rb, line 183
def storage
  @storage ||= RanchHand::Storage.new
end