class Mocloud::Commands::Instances

Public Instance Methods

description() click to toggle source
# File lib/mocloud/commands/instances.rb, line 5
def description
        "Show instances in a stack"
end
run() click to toggle source
# 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