Class: Ec2l::Client
- Inherits:
-
Object
- Object
- Ec2l::Client
- Defined in:
- lib/ec2l.rb
Overview
Public: Client to use amazon EC2 apis
Examples
Client.new.instances(["ipAddress"])[0..1]
=> [{:ipAddress=>"10.1.1.2"}, {:ipAddress=>"10.1.1.1"}]
Instance Method Summary collapse
-
#associate(address, id) ⇒ Object
Public: associate an elastic address with a VM instance.
-
#ins ⇒ Object
Public: return virtual machines instances with few details.
-
#instance(id) ⇒ Object
Public: displays inforation about a VM instance.
-
#instances(keep = ["instanceId", "ipAddress", "groups", "launchType", "instanceType", "tagSet"]) ⇒ Object
Public: return virtual machines instances.
-
#log(id) ⇒ Object
Public: get system log.
-
#read_credentials ⇒ Object
Public: reads credentials from configuration file.
-
#sgs ⇒ Object
Public: return a list of inforation about declared security groups.
-
#shell ⇒ Object
Public: opens up a pry shell.
-
#terminate(id) ⇒ Object
Public: terminates a VM instance.
-
#update_configuration(creds = nil) ⇒ Object
Public: update the credentials configuration.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object (private)
Internal: try and call method_missing_ec2
if its result is nil, display a list of the class
public methods
Examples
h
=>Usage: action parameters...
available actions:
[
[0] associate(address, id) Ec2l::Client
[1] i() Ec2l::Client
Return the result of method_missing_ec2 or nil
219 220 221 222 223 224 225 |
# File 'lib/ec2l.rb', line 219 def method_missing method, *args, &block result = method_missing_ec2(method, *args, &block) return result if not result.nil? puts "Usage: action parameters...", "available actions:" awesome_print (public_methods - "".public_methods) nil end |
Instance Method Details
#associate(address, id) ⇒ Object
Public: associate an elastic address with a VM instance
address - the elastic IP address id - the VM instance id
Examples
associate '10.1.1.2', 'i-deadbeef'
Returns info about the association
68 69 70 |
# File 'lib/ec2l.rb', line 68 def associate address, id @ec2.associate_address public_ip: address, instance_id: id end |
#ins ⇒ Object
Public: return virtual machines instances with few details
Examples
ins[0]
=> {:instanceId=>"i-deadbeef", :instanceState=>
{"code"=>"16", "name"=>"running"},
:ipAddress=>"10.1.1.2", :tagSet=>{:k=>"v"}}
Returns an array with instanceId, ipAddress, tagSet, instanceState in a hash
95 |
# File 'lib/ec2l.rb', line 95 def ins() instances ["instanceId", "ipAddress", "tagSet", "instanceState"] end |
#instance(id) ⇒ Object
Public: displays inforation about a VM instance
id - the VM instance id
Examples
instance "i-deadbeef"
=> {"xmlns"=>"http://ec2.amazonaws.com/doc/2010-08-31/",
"requestId"=>"89375055-16de-4ab7-84b5-5651670e7e3b",
"reservationSet"=>
{"item"=>
...
Returns a hash with description of the instance
57 |
# File 'lib/ec2l.rb', line 57 def instance(id) @ec2.describe_instances(instance_id: id) end |
#instances(keep = ["instanceId", "ipAddress", "groups", "launchType", "instanceType", "tagSet"]) ⇒ Object
Public: return virtual machines instances
keep - what field you want to extract.
valid fields names are documented in ec2 apis
groups is an arranged shortcut for groupSet
tagSet is also rearranged
Examples
instances(["ipAddress"])[0..1]
=> [{:ipAddress=>"10.1.1.2"}, {:ipAddress=>"10.1.1.1"}]
Returns an array with all fields requested for each VM in a hash
35 36 37 38 39 40 41 42 |
# File 'lib/ec2l.rb', line 35 def instances keep = ["instanceId", "ipAddress", "groups", "launchType", "instanceType", "tagSet"] @ec2.describe_instances.reservationSet.item.collect do |item| rearrange_fields item, keep item = item.instancesSet.item[0].reject{|k, v| not keep.include? k} Hash[item.map { |k, v| [k.to_sym, v] }] end end |
#log(id) ⇒ Object
Public: get system log
id - VM instance id
Examples
log("i-deadbeef")[0..1]
=> ["Initializing cgroup subsys cpuset", "Initializing cgroup subsys cpu"]
Returns an array containing the lines of the system log
106 107 108 |
# File 'lib/ec2l.rb', line 106 def log id Base64.decode64(@ec2.get_console_output(instance_id: id)["output"]).split("\r\n") end |
#read_credentials ⇒ Object
Public: reads credentials from configuration file
Examples
read_credentials
=> ["accesskey", "scretkey", "https://entry.com"]
Return a list with the credentials and entrypoint if defined
156 157 158 159 160 |
# File 'lib/ec2l.rb', line 156 def read_credentials creds = [] File.open(@conf) { |f| f.each_line { |l| creds << l.chomp } } if File.exists?(@conf) creds end |
#sgs ⇒ Object
Public: return a list of inforation about declared security groups
Examples
sgs[0..1]
=> [{"ownerId"=>"424242", "groupName"=>"sg1"},
{"ownerId"=>"424242", "groupName"=>"sg2"}]
Returns an array with for each SG, the ownerID and the groupName
80 81 82 83 84 |
# File 'lib/ec2l.rb', line 80 def sgs @ec2.describe_security_groups.securityGroupInfo.item.collect do |item| item.reject { |k, v| not ["groupName", "ownerId"].include? k } end end |
#shell ⇒ Object
Public: opens up a pry shell
120 |
# File 'lib/ec2l.rb', line 120 def shell() binding.pry end |
#terminate(id) ⇒ Object
Public: terminates a VM instance
id - the VM instance id
Examples
terminate "i-deadbeef"
Returns information about the termination status
118 |
# File 'lib/ec2l.rb', line 118 def terminate(id) @ec2.terminate_instances(instance_id: id) end |
#update_configuration(creds = nil) ⇒ Object
Public: update the credentials configuration
creds - current credentials
Examples
update_configuration
Will try and update configuration in /home/yazgoo/.awssecret
access key (foo):
secret access key (bar):
entry point (default being https://aws.amazon.com if blank)
(http://ec2.aws.com):
=> ["access key", "secret access key",
"entry point ..."]
Returns the description of the updated fields
136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/ec2l.rb', line 136 def update_configuration creds = nil puts "Will try and update configuration in #{@conf}" creds = read_credentials if creds.nil? File.open(@conf, "w") do |f| ["access key", "secret access key", "entry point (default being https://aws.amazon.com if blank)" ].each_with_index do |prompt, i| line = prompt_line prompt, creds[i] f.puts line if not line.empty? end end end |