Class: Mocloud::Commands::Instances

Inherits:
Object
  • Object
show all
Defined in:
lib/mocloud/commands/instances.rb

Instance Method Summary collapse

Instance Method Details

#descriptionObject



5
6
7
# File 'lib/mocloud/commands/instances.rb', line 5

def description
	"Show instances in a stack"
end

#runObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/mocloud/commands/instances.rb', line 9

def run


	opts = Trollop::options do
		banner <<-ENDOFHELP
Usage: mobingi instances <stack name> [options]"

Displays the instances in a specified stack.
		ENDOFHELP
	end
	
	stackname = ARGV.shift

	if !stackname or stackname.length == 0
		puts "Please specify a stack. Type 'mobingi stacks' to see your stacks"
		exit!
	end

	creds = Mocloud::Utils::Credentials.new
	user = creds.read_credentials()
		token = creds.make_token(0)

	api = Mocloud::API.new
	res = api.get ("/v1/describe/#{user['uid']}/stack")

	Mocloud::Utils.handle_error(res, "Show stacks failed")

	data = JSON.parse(res[:body])

	stackMap = {}

	output = data.each do |x| 
		stackMap[x["stack_id"]["S"]] = x
		stackMap[x["nickname"]["S"]] = x
	end

	if stackMap.has_key?(stackname)
		instances = stackMap[stackname]["instances"]["L"]


		rows = []
		instances.each do |x|

			rows << [ 
				x["M"]["InstanceId"]["S"], 
				x["M"]["PublicIpAddress"]["S"], 
				x["M"]["PrivateIpAddress"]["S"], 
				x["M"]["State"]["M"]["Name"]["S"] ]

		end

		table = Terminal::Table.new :headings => ['Instance ID', 'Public IP', 'Private IP', 'State'], :rows => rows

		puts "Instances in #{stackname}"
		puts table

	else
		puts "No stack named #{stackname}. Type 'mobingi stacks' to see your stacks"
		exit!
	end



end