Class: Fog::Brightbox::Compute::Server

Inherits:
Compute::Server
  • Object
show all
Includes:
ResourceLocking, ModelHelper
Defined in:
lib/fog/brightbox/models/compute/server.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ResourceLocking

#lock!, #locked?, #unlock!

Methods included from ModelHelper

#collection_name, #resource_name

Constructor Details

#initialize(attributes = {}) ⇒ Server

Returns a new instance of Server.



50
51
52
53
54
# File 'lib/fog/brightbox/models/compute/server.rb', line 50

def initialize(attributes = {})
  # Call super first to initialize the service object for our default image
  super
  self.image_id ||= service.default_image
end

Instance Attribute Details

#volume_idObject

Returns the value of attribute volume_id.



10
11
12
# File 'lib/fog/brightbox/models/compute/server.rb', line 10

def volume_id
  @volume_id
end

#volume_sizeObject

Returns the value of attribute volume_size.



11
12
13
# File 'lib/fog/brightbox/models/compute/server.rb', line 11

def volume_size
  @volume_size
end

Instance Method Details

#activate_consoleObject



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

def activate_console
  requires :identity
  response = service.activate_console_server(identity)
  [response["console_url"], response["console_token"], response["console_token_expires"]]
end

#destroyObject



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

def destroy
  requires :identity
  service.delete_server(identity)
  true
end

#dns_nameString

Returns the public DNS name of the server

Returns:

  • (String)


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

def dns_name
  ["public", fqdn].join(".")
end

#flavorObject



142
143
144
145
# File 'lib/fog/brightbox/models/compute/server.rb', line 142

def flavor
  requires :flavor_id
  service.flavors.get(flavor_id)
end

#flavor_idObject



64
65
66
67
68
69
70
# File 'lib/fog/brightbox/models/compute/server.rb', line 64

def flavor_id
  if attributes[:flavor_id]
    attributes[:flavor_id]
  elsif server_type
    server_type[:id] || server_type["id"]
  end
end

#flavor_id=(incoming_flavour_id) ⇒ Object



76
77
78
# File 'lib/fog/brightbox/models/compute/server.rb', line 76

def flavor_id=(incoming_flavour_id)
  attributes[:flavor_id] = incoming_flavour_id
end

#imageObject



147
148
149
150
# File 'lib/fog/brightbox/models/compute/server.rb', line 147

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

#mapping_identityString

Replaces the server’s identifier with it’s interface’s identifier for Cloud IP mapping

Returns:

  • (String)

    the identifier to pass to a Cloud IP mapping request



221
222
223
# File 'lib/fog/brightbox/models/compute/server.rb', line 221

def mapping_identity
  interfaces.first["id"]
end

#private_ip_addressObject



160
161
162
163
164
165
166
# File 'lib/fog/brightbox/models/compute/server.rb', line 160

def private_ip_address
  if interfaces.empty?
    nil
  else
    interfaces.first["ipv4_address"]
  end
end

#public_ip_addressObject



168
169
170
171
172
173
174
# File 'lib/fog/brightbox/models/compute/server.rb', line 168

def public_ip_address
  if cloud_ips.empty?
    nil
  else
    cloud_ips.first["public_ip"]
  end
end

#ready?Boolean

Returns:

  • (Boolean)


176
177
178
# File 'lib/fog/brightbox/models/compute/server.rb', line 176

def ready?
  state == "active"
end

#reboot(use_hard_reboot = true) ⇒ Boolean

Issues a hard reset to the server (or an OS level reboot command)

Default behaviour is a hard reboot because it is more reliable because the state of the server’s OS is irrelevant.

Examples:

Hard reset

@server.reboot

Soft reset

@server.reboot(false)

Parameters:

  • use_hard_reboot (Boolean) (defaults to: true)

Returns:

  • (Boolean)


104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/fog/brightbox/models/compute/server.rb', line 104

def reboot(use_hard_reboot = true)
  requires :identity
  if ready?
    if use_hard_reboot
      service.reset_server(identity)
    else
      service.reboot_server(identity)
    end
  else
    # Not able to reboot if not ready in the first place
    false
  end
end

#saveObject

Raises:

  • (Fog::Errors::Error)


186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/fog/brightbox/models/compute/server.rb', line 186

def save
  raise Fog::Errors::Error, "Resaving an existing object may create a duplicate" if persisted?
  requires :image_id
  options = {
    name: name,
    zone: zone_id,
    user_data: user_data,
    server_groups: server_groups
  }.delete_if { |_k, v| v.nil? || v == "" }

  options[:server_type] = flavor_id unless flavor_id.nil? || flavor_id == ""
  options[:cloud_ip] = cloud_ip unless cloud_ip.nil? || cloud_ip == ""
  options[:disk_encrypted] = disk_encrypted if disk_encrypted

  if volume_id
    options[:volumes] = [{ volume: volume_id }]
  elsif volume_size
    options[:volumes] = [
      {
        image: image_id,
        size: volume_size
      }
    ]
  else
    options[:image] = image_id
  end

  data = service.create_server(options)
  merge_attributes(data)
  true
end

#shutdownObject



130
131
132
133
134
# File 'lib/fog/brightbox/models/compute/server.rb', line 130

def shutdown
  requires :identity
  service.shutdown_server(identity)
  true
end

#snapshot(return_snapshot = false) ⇒ Object



80
81
82
83
84
85
86
87
88
89
# File 'lib/fog/brightbox/models/compute/server.rb', line 80

def snapshot(return_snapshot = false)
  requires :identity
  response, snapshot_id = service.snapshot_server(identity, return_link: return_snapshot)

  if return_snapshot
    service.images.get(snapshot_id)
  else
    response
  end
end

#startObject



118
119
120
121
122
# File 'lib/fog/brightbox/models/compute/server.rb', line 118

def start
  requires :identity
  service.start_server(identity)
  true
end

#stopObject



124
125
126
127
128
# File 'lib/fog/brightbox/models/compute/server.rb', line 124

def stop
  requires :identity
  service.stop_server(identity)
  true
end

#zone_idObject



56
57
58
59
60
61
62
# File 'lib/fog/brightbox/models/compute/server.rb', line 56

def zone_id
  if attributes[:zone_id]
    attributes[:zone_id]
  elsif zone
    zone[:id] || zone["id"]
  end
end

#zone_id=(incoming_zone_id) ⇒ Object



72
73
74
# File 'lib/fog/brightbox/models/compute/server.rb', line 72

def zone_id=(incoming_zone_id)
  attributes[:zone_id] = incoming_zone_id
end