shell/lib/shell/runner.rb in sct-0.1.28 vs shell/lib/shell/runner.rb in sct-0.1.29
- old
+ new
@@ -1,34 +1,83 @@
+require 'yaml'
+
module Shell
class Runner
- # define launch work
- def launch
- tool_name = ARGV.first ? ARGV.first.downcase : nil
+ def launch
+ command = ARGV.join(" ")
- if tool_name && Shell::TOOLS.include?(tool_name.to_sym)
-
- require_relative "docker/#{tool_name}"
- command = "#{ARGV[1..(ARGV.length+1)].join(" ")}"
+ if ! File.exist? "./okteto.yml"
+ error "Could not find file 'okteto.yml'."
+ end
- case ARGV.first
- when 'php'
- Shell::Php.exec(command)
- when 'composer'
- Shell::Composer.exec(command)
- when 'yarn'
- Shell::Yarn.exec(command)
- else
- show_error
- 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
- show_error
+ system "kubectl exec #{pod} -it -- #{command}", { in: :in, out: :out, err: :err }
end
end
- def show_error
- tools = Shell::TOOLS.map { |tool| tool.to_s }.join(", ")
- UI.error("Tool not found. Try one of these: #{tools}")
+ 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
\ No newline at end of file
+end