amazon ec2 - Ruby: Parse API Response -



amazon ec2 - Ruby: Parse API Response -

i trying geht script run: http://dysinger.net/2008/10/13/using-amazon-ec2-metadata-as-a-simple-dns dosnt work because using old amazon sdk version, rewrote utilize new one:

#!/usr/bin/env ruby require "rubygems" require "aws-sdk" %w(optparse rubygems aws-sdk resolv pp).each {|l| require l} options = {} parser = optionparser.new |p| p.banner = "usage: hosts [options]" p.on("-a", "--access-key user", "the user's aws access key id.") |aki| options[:access_key_id] = aki end p.on("-s", "--secret-key password", "the user's aws secret access key.") |sak| options[:secret_access_key] = sak end p.on_tail("-h", "--help", "show message") { puts(p) exit } p.parse!(argv) rescue puts(p) end if options.key?(:access_key_id) , options.key?(:secret_access_key) puts "127.0.0.1 localhost" aws.config(options) aws::ec2.new(options) reply = aws::ec2::client.new.describe_instances answer.reservationset.item.each |r| r.instancesset.item.each |i| if i.instancestate.name =~ /running/ puts(resolv::dns.new.getaddress(i.privatednsname).to_s + " #{i.keyname}.ec2 #{i.keyname}") end end end else puts(parser) exit(1) end

what should outputing new /etc/hosts file ec2 instances in it.

and response =d, reply hash , hence

error undefined method `reservationset' #<hash:0x7f7573b27880>.

and problem, since dont know ruby @ ( doing reading amazon documentation , playing around reply ). somehow in original illustration seemed work. suppose then, api did not homecoming hash, anyway...how can iterate through hash above, work?

this code may help you:

answer = aws::ec2::client.new.describe_instances reservations = answer[:reservation_set] reservations.each |reservation| instances = reservation[:instances_set] instances.each |instance| if instance[:instance_state][:name] == "running" private_dns_name = instance[:private_dns_name] key_name = instance[:key_name] address = resolv::dns.new.getaddress(private_dns_name) puts "{address} #{key_name}.ec2 #{key_name}" end end end

generally alter code using methods names e.g. item.foobarbaz using hash e.g. item[:foo_bar_baz]

when you're learning ruby "pp" command useful pretty-printing variables go, such as:

pp reservations pp instances pp private_dns_name

ruby amazon-ec2 hosts

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -