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

Inherits:
Server
  • Object
show all
Defined in:
lib/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 Method Summary collapse

Constructor Details

#initialize(new_attributes = {}) ⇒ Server

Returns a new instance of Server


49
50
51
52
53
54
55
56
# File 'lib/fog/ibm/models/compute/server.rb', line 49

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


110
111
112
113
114
115
116
117
118
119
# File 'lib/fog/ibm/models/compute/server.rb', line 110

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

#allocate_ip(wait_for_ready = true) ⇒ Object


101
102
103
104
105
106
107
108
# File 'lib/fog/ibm/models/compute/server.rb', line 101

def allocate_ip(wait_for_ready=true)
  requires :location_id
  new_ip = service.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


121
122
123
124
125
# File 'lib/fog/ibm/models/compute/server.rb', line 121

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

#destroyObject


86
87
88
89
# File 'lib/fog/ibm/models/compute/server.rb', line 86

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

#detach(volume_id) ⇒ Object


127
128
129
130
131
# File 'lib/fog/ibm/models/compute/server.rb', line 127

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

#expire!Object

Expires the instance immediately


154
155
156
# File 'lib/fog/ibm/models/compute/server.rb', line 154

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

#expire_at(time) ⇒ Object

Sets expiration time - Pass an instance of Time.


142
143
144
145
146
147
148
149
150
151
# File 'lib/fog/ibm/models/compute/server.rb', line 142

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

#expires_atObject


137
138
139
# File 'lib/fog/ibm/models/compute/server.rb', line 137

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

#imageObject


158
159
160
161
# File 'lib/fog/ibm/models/compute/server.rb', line 158

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

#launched_atObject


133
134
135
# File 'lib/fog/ibm/models/compute/server.rb', line 133

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

#locationObject


163
164
165
166
# File 'lib/fog/ibm/models/compute/server.rb', line 163

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

#public_hostnameObject


168
169
170
# File 'lib/fog/ibm/models/compute/server.rb', line 168

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

#public_ip_addressObject


172
173
174
# File 'lib/fog/ibm/models/compute/server.rb', line 172

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

#ready?Boolean

Returns:

  • (Boolean)

77
78
79
# File 'lib/fog/ibm/models/compute/server.rb', line 77

def ready?
  state == "Active"
end

#rebootObject


81
82
83
84
# File 'lib/fog/ibm/models/compute/server.rb', line 81

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

#rename(name) ⇒ Object


91
92
93
94
95
96
97
98
99
# File 'lib/fog/ibm/models/compute/server.rb', line 91

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

#saveObject


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

def save
  requires :name, :image_id, :instance_type, :location_id
  data = service.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


73
74
75
# File 'lib/fog/ibm/models/compute/server.rb', line 73

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


178
179
180
181
182
183
184
# File 'lib/fog/ibm/models/compute/server.rb', line 178

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