require 'yaml' module Shell class Runner def launch command = ARGV.join(" ") if ! File.exist? "./okteto.yml" error "Could not find file 'okteto.yml'." end manifest = YAML.load File.read "./okteto.yml" deployment = manifest["name"] pod = find_pod deployment if command.empty? system "kubectl exec #{pod} -it -- sh", { in: :in, out: :out, err: :err } else system "kubectl exec #{pod} -it -- #{command}", { in: :in, out: :out, err: :err } end end def find_pod deployment output = `kubectl get pods --show-labels` # split output lines lines = output.split "\n" # exclude first line (table header) lines = lines[1..-1] # get the name and labels of each pod pods = lines.map do |line| columns = line.split " " name = columns[0] labels = columns[5].split(",").reduce({}) do |labels, label| key, value = label.split "=" labels[key] = value labels end { name: name, labels: labels } end # filter by deployment pods = pods.filter { |pod| pod[:labels]["app"] == deployment } if pods.length == 0 error "Could not find pod for deployment #{deployment}." elsif pods.length > 1 error "Found more than one pod for deployment #{deployment}. Multiple pods are not supported." end pod = pods.first if pod[:labels]["dev.okteto.com"] != "true" print "The current pod is running a production image and was not started by Okteto. Are you sure you want to continue? [y/N] ".red answer = $stdin.readline.chomp.downcase if answer != "y" exit end end pod[:name] end def error message UI.error message exit 1 end end end