shell/lib/shell/runner.rb in sct-0.1.35 vs shell/lib/shell/runner.rb in sct-1.0.0

- old
+ new

@@ -1,83 +1,48 @@ require 'yaml' module Shell - class Runner + class Runner - def launch - command = ARGV.join(" ") + def error message + UI.error message + exit 1 + end - if ! File.exist? "./okteto.yml" - error "Could not find file 'okteto.yml'." - end + def launch + command = ARGV.empty? ? "sh" : ARGV.join(" ") - manifest = YAML.load File.read "./okteto.yml" + file = "docker-compose.dev.yml" - deployment = manifest["name"] + if !File.exist? file + error "Could not find file '#{file}'." + end - pod = find_pod deployment + manifest = YAML.load File.read file - 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 + services = manifest["services"].to_a - def find_pod deployment - output = `kubectl get pods --show-labels` + if services.length != 1 + error "Currently sct only supports a single service declaration in '#{file}'. Contact the infra guild if you consider this a limitation." + end - # split output lines - lines = output.split "\n" + service, service_spec = services.first - # exclude first line (table header) - lines = lines[1..-1] + container = service_spec["container_name"] - # get the name and labels of each pod - pods = lines.map do |line| - columns = line.split " " + project = `docker container inspect --format '{{index .Config.Labels "com.docker.compose.project"}}' #{container}`.chomp - name = columns[0] + if project == "spend-cloud" + print "This container was not started with 'sct dev'. Are you sure you want to continue? [y/N] ".red - labels = columns[5].split(",").reduce({}) do |labels, label| - key, value = label.split "=" - labels[key] = value - labels - end + answer = $stdin.readline.chomp.downcase - { - 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] + if answer != "y" + exit end + end - def error message - UI.error message - exit 1 - end - + system "docker exec -it #{container} #{command}" end + + end end