lib/sct/commands/cluster.rb in sct-0.1.17 vs lib/sct/commands/cluster.rb in sct-0.1.18
- old
+ new
@@ -4,15 +4,15 @@
module Sct
class ClusterCommand
IS_PUBLIC_COMMAND = true
- SYNTAX = 'sct cluster up | sct cluster down | sct cluster setup | sct cluster reset | sct cluster update-config | sct cluster delete-stalled-pods'
+ SYNTAX = 'sct cluster up | sct cluster down | sct cluster setup | sct cluster reset | sct cluster update-config | sct cluster delete-stalled-pods | sct cluster status'
SUMMARY = 'Perform actions on the minikube cluster.'
- EXAMPLE = 'sct cluster up | sct cluster down | sct cluster setup | sct cluster reset | sct cluster update-config | sct cluster delete-stalled-pods'
+ EXAMPLE = 'sct cluster up | sct cluster down | sct cluster setup | sct cluster reset | sct cluster update-config | sct cluster delete-stalled-pods | sct cluster status'
EXAMPLE_DESCRIPTION = 'Perform actions on the minikube cluster.'
- DESCRIPTION = "sct cluster allows you to start, stop, setup/reset, update the config, or delete the stalled pods of the Spend Cloud minikube cluster."
+ DESCRIPTION = "sct cluster allows you to start, stop, setup/reset, update the config, delete the stalled pods, or status of the Spend Cloud minikube cluster."
OPTIONS = []
def execute(args, options)
return puts "SCT has not been initialized. Run 'sct init' first.".red unless Sct::Config.exists
@@ -29,11 +29,11 @@
when "delete-stalled-pods"
delete_stalled_pods(feedback: true)
when "status"
status
else
- puts "Unknown or missing argument. Please run 'sct cluster up', 'sct cluster down', 'sct cluster setup', 'sct cluster reset', 'sct cluster update-config', or 'sct cluster delete-stalled-pods'.".red
+ puts "Unknown or missing argument. Please run 'sct cluster --help'.".red
end
end
def up
start
@@ -153,61 +153,63 @@
puts "Pods are now ready.".green
end
def status
- minikube_status
- pods_status
- system_pods
- current_contexts
- end
+ print_contexts
+ print_minikube_status
- def pods_status
- rows = []
- pods.map do |pod|
- status = pod[:status] == "Running" ? pod[:status].green : pod[:status].red
- rows << [pod[:name], status]
- end
- puts Terminal::Table.new :title => "Pods Status".green, :headings => ['Name', 'Status'], :rows => rows
+ if get_minikube_status.find_all { |status| status[1] == 'Stopped' }.count == 0
+ print_pods_status("kube-system")
+ print_pods_status
+ else
+ puts "Please check your minikube status. If all services are stopped you should start the minikube first.".yellow
+ end
end
- def system_pods
- rows = []
- pods("kube-system").map do |pod|
- status = pod[:status] == "Running" ? pod[:status].green : pod[:status].red
- rows << [pod[:name], status]
- end
- puts Terminal::Table.new :title => "System pods Status".green, :headings => ['Name', 'Status'], :rows => rows
- end
-
- def current_contexts
+ def print_contexts
output = `kubectl config get-contexts`
lines = output.split "\n"
lines = lines[1..-1]
- rows = []
- lines.map do |line|
- columns = line.split(" ")
+ rows = lines.map do |line|
+ columns = line.split(" ")
- current_context = columns[0] == "*" ? "Yes".green : "No".red
- rows << [columns[2], current_context]
+ current_context = columns[0] == "*" ? "Yes".green : "No".red
+
+ [columns[2], current_context]
end
- puts Terminal::Table.new :title => "Contexts".green, :headings => ['Cluster', 'Using context'], :rows => rows
+
+ puts Terminal::Table.new title: "Contexts".green, headings: ['Cluster', 'Using context'], rows: rows
end
- def minikube_status
+ def print_minikube_status
+
+ puts Terminal::Table.new title: "Minikube status".green, headings: ['Name', 'Status'], rows: get_minikube_status
+ end
+
+ def get_minikube_status
output = `#{minikube} status`
lines = output.split "\n"
- rows = []
- lines.map do |line|
+ rows = lines.map do |line|
columns = line.split(" ")
- rows << [columns[0], columns[1]]
+ [columns[0][0..-2], columns[1]]
end
- puts Terminal::Table.new :title => "Minikube Status".green, :headings => ['Name', 'Status'], :rows => rows
+ end
+
+ def print_pods_status(namespace = nil)
+ rows = pods(namespace).map do |pod|
+ [
+ pod[:name],
+ pod[:status] == "Running" ? pod[:status].green : pod[:status].red
+ ]
+ end
+
+ puts Terminal::Table.new title: "Pods (namespace: #{namespace || "default"})".green, headings: ['Name', 'Status'], rows: rows
end
def pods(namespace = nil)
if namespace
output = `kubectl get pods -n #{namespace}`