Class: Fog::Compute::IBM::Server

Inherits:
Server show all
Defined in:
lib/rackspace-fog/ibm/models/compute/server.rb

Constant Summary collapse

STATES =
{
  0  => 'New',
  1  => 'Provisioning',
  2  => 'Failed',
  3  => 'Removed',
  4  => 'Rejected',
  5  => 'Active',
  6  => 'Unknown',
  7  => 'Deprovisioning',
  8  => 'Restarting',
  9  => 'Starting',
  10 => 'Stopping',
  11 => 'Stopped',
  12 => 'Deprovisioning pending',
  13 => 'Restart pending',
  14 => 'Attaching',
  15 => 'Detaching'
}

Instance Attribute Summary

Attributes inherited from Model

#collection, #connection

Instance Method Summary collapse

Methods inherited from Server

#private_key=, #scp, #scp_download, #ssh, #ssh_port, #sshable?

Methods inherited from Model

#inspect, #reload, #symbolize_keys, #to_json, #wait_for

Methods included from Attributes::ClassMethods

#_load, #aliases, #attribute, #attributes, #identity, #ignore_attributes, #ignored_attributes

Methods included from Attributes::InstanceMethods

#_dump, #attributes, #dup, #identity, #identity=, #merge_attributes, #new_record?, #requires, #requires_one

Constructor Details

#initialize(new_attributes = {}) ⇒ Server

Returns a new instance of Server.



51
52
53
54
55
56
57
58
# File 'lib/rackspace-fog/ibm/models/compute/server.rb', line 51

def initialize(new_attributes={})
  super(new_attributes)
  self.name ||= 'fog-instance'
  self.image_id ||= '20010001'
  self.location_id ||= '41'
  self.instance_type ||= 'COP32.1/2048/60'
  self.key_name ||= 'fog'
end

Instance Method Details

#addressesObject



112
113
114
115
116
117
118
119
120
121
# File 'lib/rackspace-fog/ibm/models/compute/server.rb', line 112

def addresses
  addys = secondary_ip.map {|ip| Fog::Compute[:ibm].addresses.new(ip) }
  # Set an ID, in case someone tries to save
  addys << connection.addresses.new(attributes[:primary_ip].merge(
    :id => "0",
    :location => location_id,
    :state => 3
  ))
  addys
end

#allocate_ip(wait_for_ready = true) ⇒ Object



103
104
105
106
107
108
109
110
# File 'lib/rackspace-fog/ibm/models/compute/server.rb', line 103

def allocate_ip(wait_for_ready=true)
  requires :location_id
  new_ip = connection.addresses.new(:location => location_id)
  new_ip.save
  new_ip.wait_for(Fog::IBM.timeout) { ready? } if wait_for_ready
  secondary_ip << new_ip
  new_ip
end

#attach(volume_id) ⇒ Object



123
124
125
126
127
# File 'lib/rackspace-fog/ibm/models/compute/server.rb', line 123

def attach(volume_id)
  requires :id
  data = connection.modify_instance(id, {'type' => 'attach', 'storageID' => volume_id})
  data.body
end

#destroyObject



88
89
90
91
# File 'lib/rackspace-fog/ibm/models/compute/server.rb', line 88

def destroy
  requires :id
  connection.delete_instance(id).body['success']
end

#detach(volume_id) ⇒ Object



129
130
131
132
133
# File 'lib/rackspace-fog/ibm/models/compute/server.rb', line 129

def detach(volume_id)
  requires :id
  data = connection.modify_instance(id, {'type' => 'detach', 'storageID' => volume_id})
  data.body
end

#expire!Object

Expires the instance immediately



156
157
158
# File 'lib/rackspace-fog/ibm/models/compute/server.rb', line 156

def expire!
  expire_at(Time.now + 5)
end

#expire_at(time) ⇒ Object

Sets expiration time - Pass an instance of Time.



144
145
146
147
148
149
150
151
152
153
# File 'lib/rackspace-fog/ibm/models/compute/server.rb', line 144

def expire_at(time)
  expiry_time = (time.tv_sec * 1000).to_i
  data = connection.modify_instance(id, 'expirationTime' => expiry_time)
  if data.body['expirationTime'] == expiry_time
    attributes[:expires_at] = expiry_time
    true
  else
    false
  end
end

#expires_atObject



139
140
141
# File 'lib/rackspace-fog/ibm/models/compute/server.rb', line 139

def expires_at
  Time.at(attributes[:expires_at].to_f / 1000)
end

#imageObject



160
161
162
163
# File 'lib/rackspace-fog/ibm/models/compute/server.rb', line 160

def image
  requires :image_id
  connection.images.get(image_id)
end

#launched_atObject



135
136
137
# File 'lib/rackspace-fog/ibm/models/compute/server.rb', line 135

def launched_at
  Time.at(attributes[:launched_at].to_f / 1000)
end

#locationObject



165
166
167
168
# File 'lib/rackspace-fog/ibm/models/compute/server.rb', line 165

def location
  requires :location_id
  connection.locations.get(location_id)
end

#public_hostnameObject



170
171
172
# File 'lib/rackspace-fog/ibm/models/compute/server.rb', line 170

def public_hostname
  primary_ip ? primary_ip['hostname'] : nil
end

#public_ip_addressObject



174
175
176
# File 'lib/rackspace-fog/ibm/models/compute/server.rb', line 174

def public_ip_address
  primary_ip ? primary_ip['ip'] : nil
end

#ready?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/rackspace-fog/ibm/models/compute/server.rb', line 79

def ready?
  state == "Active"
end

#rebootObject



83
84
85
86
# File 'lib/rackspace-fog/ibm/models/compute/server.rb', line 83

def reboot
  requires :id
  connection.modify_instance(id, 'state' => 'restart').body['success']
end

#rename(name) ⇒ Object



93
94
95
96
97
98
99
100
101
# File 'lib/rackspace-fog/ibm/models/compute/server.rb', line 93

def rename(name)
  requires :id
  if connection.modify_instance(id, 'name' => name).body["success"]
    attributes[:name] = name
  else
    return false
  end
  true
end

#saveObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/rackspace-fog/ibm/models/compute/server.rb', line 60

def save
  requires :name, :image_id, :instance_type, :location_id
  data = connection.create_instance(name, image_id, instance_type, location_id,
                                    :key_name => key_name,
                                    :vlan_id => vlan_id,
                                    :secondary_ip => secondary_ip)
  data.body['instances'].each do |iattrs|
    if iattrs['name'] == name
      merge_attributes(iattrs)
      return true
    end
  end
  false
end

#stateObject



75
76
77
# File 'lib/rackspace-fog/ibm/models/compute/server.rb', line 75

def state
  STATES[attributes[:state]]
end

#to_image(opts = {}) ⇒ Object Also known as: create_image

Creates an image from the current instance if name isn’t passed then we’ll take the current name and timestamp it



180
181
182
183
184
185
186
# File 'lib/rackspace-fog/ibm/models/compute/server.rb', line 180

def to_image(opts={})
 options = {
   :name => name + " as of " + Time.now.strftime("%Y-%m-%d %H:%M"),
   :description => ""
 }.merge(opts)
 connection.create_image(id, options[:name], options[:description]).body
end