Class: BuildCloud::Instance

Inherits:
Object
  • Object
show all
Includes:
Component
Defined in:
lib/build-cloud/instance.rb

Constant Summary collapse

@@objects =
[]

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Component

included

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, options = {} )

    @ec2     = fog_interfaces[:compute]
    @log     = log
    @options = options

    @log.debug( options.inspect )

    required_options(: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

#createObject



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 = @options.dup

    if options[:security_group_names] or options[:security_group_ids]
        unless options[:security_group_ids]

            options[:security_group_ids] = []

            options[:security_group_names].each do |sg|
                options[:security_group_ids] << BuildCloud::SecurityGroup.get_id_by_name( sg )
            end

            options.delete(:security_group_names)

        end
    end

    if options[:subnet_id] or options[:subnet_name]
        unless options[:subnet_id]

            options[:subnet_id] = BuildCloud::Subnet.get_id_by_name( options[:subnet_name] )
            options.delete(:subnet_name)

        end
    end

    unless options[:vpc_id]

        options[:vpc_id] = BuildCloud::VPC.get_id_by_name( options[:vpc_name] )
        options.delete(:vpc_name)

    end

    if options[:private_ip_address] and options[: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"
        options.delete(:private_ip_address)
    end

    if options[:subnet_id] and options[:network_interfaces]
        puts "WARNING: InvalidParameterCombination => Network interfaces and subnet_ids should not be specified on the same request"
        puts "Using Network interface"
        options.delete(:subnet_id)
    end

    options[:user_data] = JSON.generate( @options[:user_data] )

    options[: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 options[:network_interfaces].nil?

    @log.debug( options.inspect )

    instance = @ec2.servers.new( options )
    instance.save

    options[:tags].each do | tag |
        attributes = {}
        attributes[:resource_id] = instance.id.to_s
        attributes[:key] = tag[:key]
        attributes[:value] = tag[:value]
        new_tag = @ec2.tags.new( attributes )
        new_tag.save
    end unless options[:tags].empty? or options[:tags].nil?

    @log.debug( instance.inspect )

end

#deleteObject



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

#readObject 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.tags['Name'] == @options[:name]}
    instances.select{ |i| i.state =~ /(running|pending)/ }.first
end

#ready_timeoutObject



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