lib/gaptool-client.rb in gaptool-client-0.1.0 vs lib/gaptool-client.rb in gaptool-client-0.2.1
- old
+ new
@@ -1,6 +1,7 @@
#!/usr/bin/env ruby
+# coding: utf-8
require 'rainbow'
require 'peach'
require 'json'
require 'clamp'
require 'net/ssh'
@@ -53,10 +54,58 @@
system "SSH_AUTH_SOCK='' ssh -i /tmp/gtkey admin@#{@ssh['hostname']}"
end
end
+class InfoCommand < Clamp::Command
+ option ["-r", "--role"], "ROLE", "Role name to ssh to", :required => false
+ option ["-e", "--environment"], "ENVIRONMENT", "Which environment, e.g. production", :required => false
+ option ["-i", "--instance"], "INSTANCE", "Node instance, leave blank to query avilable nodes", :required => false
+ option ["-p", "--parseable"], :flag, "Display in non-pretty parseable JSON"
+ option ["-g", "--grepable"], :flag, "Display in non-pretty grep-friendly text"
+
+ def execute
+ @nodes = Array.new
+ if instance
+ @nodes = [$api.getonenode(instance)]
+ elsif role && environment
+ @nodes = $api.getenvroles(role, environment)
+ elsif role && !environment
+ @nodes = $api.getrolenodes(role)
+ else
+ @nodes = $api.getallnodes()
+ end
+ infohelper(@nodes, parseable?, grepable?)
+ end
+
+
+end
+
+def infohelper(nodes, parseable, grepable)
+ if parseable
+ puts nodes.to_json
+ else
+ nodes.each do |node|
+ @host = "#{node['role']}:#{node['environment']}:#{node['instance']}"
+ unless grepable
+ puts @host.color(:green)
+ end
+ node.keys.each do |key|
+ if grepable
+ puts "#{@host}|#{key}|#{node[key]}"
+ else
+ unless key == node.keys.last
+ puts " ┠ #{key.color(:cyan)}: #{node[key]}"
+ else
+ puts " ┖ #{key.color(:cyan)}: #{node[key]}\n\n"
+ end
+ end
+ end
+ end
+ end
+end
+
def sshcmd(node, commands)
Net::SSH.start(
node['hostname'],
'admin',
:key_data => [$api.ssh(node['role'], node['environment'], node['instance'])['key']],
@@ -80,11 +129,11 @@
option ["-e", "--environment"], "ENVIRONMENT", "Which environment, e.g. production", :required => true
option ["-i", "--instance"], "INSTANCE", "Instance ID, e.g. i-12345678", :required => false
def execute
if !instance.nil?
- nodes = [$api.getonenode(role, environment, instance)]
+ nodes = [$api.getonenode(instance)]
else
nodes = $api.getenvroles(role, environment)
end
nodes.peach do |node|
json = {
@@ -193,9 +242,10 @@
end
end
class MainCommand < Clamp::Command
+ subcommand "info", "Displays information about nodes", InfoCommand
subcommand "init", "Create new application cluster", InitCommand
subcommand "terminate", "Terminate instance", TerminateCommand
subcommand "ssh", "ssh to cluster host", SshCommand
subcommand "chefrun", "chefrun on a resource pool", ChefrunCommand
subcommand "deploy", "deploy on an application", DeployCommand