Class: BuildCloud::Instance
- Inherits:
-
Object
- Object
- BuildCloud::Instance
- Includes:
- Component
- Defined in:
- lib/build-cloud/instance.rb
Constant Summary collapse
- @@objects =
[]
Class Method Summary collapse
Instance Method Summary collapse
- #create ⇒ Object
- #delete ⇒ Object
-
#initialize(fog_interfaces, log, options = {}) ⇒ Instance
constructor
A new instance of Instance.
- #read ⇒ Object (also: #fog_object)
- #ready_timeout ⇒ Object
Methods included from Component
Constructor Details
#initialize(fog_interfaces, log, options = {}) ⇒ Instance
Returns a new instance of Instance.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/build-cloud/instance.rb', line 25 def initialize ( fog_interfaces, log, = {} ) @ec2 = fog_interfaces[:compute] @log = log @options = @log.debug( .inspect ) (:image_id, :flavor_id, :private_ip_address, :name) require_one_of(:security_group_ids, :security_group_names, :network_interfaces) require_one_of(:subnet_id, :subnet_name, :network_interfaces) #require_one_of(:network_interfaces, :private_ip_address) require_one_of(:vpc_id, :vpc_name) end |
Class Method Details
.get_id_by_name(name) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/build-cloud/instance.rb', line 7 def self.get_id_by_name( name ) instance = self.search( :name => name ).first unless instance raise "Couldn't get an instance object for #{name} - is it defined?" end instance_fog = instance.read unless instance_fog raise "Couldn't get an instance fog object for #{name} - is it created?" end instance_fog.id end |
Instance Method Details
#create ⇒ Object
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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/build-cloud/instance.rb', line 45 def create return if exists? @log.info( "Creating instance #{@options[:name]}" ) = @options.dup if [:security_group_names] or [:security_group_ids] unless [:security_group_ids] [:security_group_ids] = [] [:security_group_names].each do |sg| [:security_group_ids] << BuildCloud::SecurityGroup.get_id_by_name( sg ) end .delete(:security_group_names) end end if [:subnet_id] or [:subnet_name] unless [:subnet_id] [:subnet_id] = BuildCloud::Subnet.get_id_by_name( [:subnet_name] ) .delete(:subnet_name) end end unless [:vpc_id] [:vpc_id] = BuildCloud::VPC.get_id_by_name( [:vpc_name] ) .delete(:vpc_name) end if [:private_ip_address] and [:network_interfaces] puts "WARNING: InvalidParameterCombination => Network interfaces and an instance-level private IP address should not be specified on the same request" puts "Using Network interface" .delete(:private_ip_address) end if [:subnet_id] and [:network_interfaces] puts "WARNING: InvalidParameterCombination => Network interfaces and subnet_ids should not be specified on the same request" puts "Using Network interface" .delete(:subnet_id) end [:user_data] = JSON.generate( @options[:user_data] ) [:network_interfaces].each { |iface| if ! iface[:network_interface_name].nil? interface_id = BuildCloud::NetworkInterface.get_id_by_name( iface[:network_interface_name] ) iface['NetworkInterfaceId'] = interface_id iface.delete(:network_interface_name) end } unless [:network_interfaces].nil? @log.debug( .inspect ) instance = @ec2.servers.new( ) instance.save [:tags].each do | tag | attributes = {} attributes[:resource_id] = instance.id.to_s attributes[:key] = tag[:key] attributes[:value] = tag[:value] new_tag = @ec2..new( attributes ) new_tag.save end unless [:tags].empty? or [:tags].nil? @log.debug( instance.inspect ) end |
#delete ⇒ Object
130 131 132 133 134 135 136 137 138 |
# File 'lib/build-cloud/instance.rb', line 130 def delete return unless exists? @log.info( "Deleting instance #{@options[:name]}" ) fog_object.destroy end |
#read ⇒ Object Also known as: fog_object
123 124 125 126 |
# File 'lib/build-cloud/instance.rb', line 123 def read instances = @ec2.servers.select{ |l| l.['Name'] == @options[:name]} instances.select{ |i| i.state =~ /(running|pending)/ }.first end |
#ready_timeout ⇒ Object
41 42 43 |
# File 'lib/build-cloud/instance.rb', line 41 def ready_timeout 5 * 60 # some instances (eg big EBS root vols) can take a while end |