Class: Fog::Terremark::Shared::Server

Inherits:
Model
  • Object
show all
Defined in:
lib/fog/terremark/models/shared/server.rb

Instance Attribute Summary

Attributes inherited from Model

#collection, #service

Instance Method Summary collapse

Methods inherited from Model

#initialize, #inspect, #symbolize_keys, #to_json, #wait_for

Methods included from Attributes::ClassMethods

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

Methods included from Core::DeprecatedConnectionAccessors

#connection, #connection=, #prepare_service_value

Methods included from Attributes::InstanceMethods

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

Constructor Details

This class inherits a constructor from Fog::Model

Instance Method Details

#create_internet_services(internet_spec) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/fog/terremark/models/shared/server.rb', line 135

def create_internet_services(internet_spec)
  public_ip_info = nil
  internet_service_id = nil
  internet_spec.each do |proto, ports|
    for port in ports
      if not public_ip_info
        #Create the first internet service and allocate public IP
        inet_services = service.internetservices.create({
              "Name" => self.name,
              "Protocol" => proto,
              "Port" => port,
        })
        internet_service_id = inet_services.Id
        public_ip_info = inet_services.PublicIpAddress

      else
        #create additional services to existing Public IP
        inet_services = service.internetservices.create({
              "public_ip_address_id" => public_ip_info["Id"],
              "Name" => self.name,
              "Protocol" => proto,
              "Port" => port,
        }
            )
        internet_service_id = inet_services.Id
      end

      #Create the associate node service to the server
      service.nodeservices.create({"Name" => self.name,
                                   "IpAddress" => self.IpAddress,
                                   "Port" => port,
                                   "InternetServiceId" => internet_service_id
      })
    end
  end
  true
end

#delete_internet_servicesObject



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/fog/terremark/models/shared/server.rb', line 62

def delete_internet_services
    #Find the internet service

    while (inet_service = internet_services.pop) do

      nodes = service.nodeservices.all(inet_service.Id)
      #Delete all the associated nodes
      nodes.select { |item| item.destroy }
      #Clear out the services
      inet_service.destroy(delete_public_ip = !(internet_services.size > 0))
    end
    true
end

#destroyObject



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/fog/terremark/models/shared/server.rb', line 36

def destroy
  case self.status
    when VAppStatus::BEING_CREATED, VAppStatus::BEING_DEPLOYED
      return false
    when VAppStatus::POWERED_ON
      data = service.power_off(self.id).body
      wait_for { off? }
    end
    #Failsafe .. Always check if we are ready
  wait_for { ready? }
  service.delete_vapp(self.id)
  true
end

#graceful_restartObject



128
129
130
131
132
133
# File 'lib/fog/terremark/models/shared/server.rb', line 128

def graceful_restart
  requires :id
  shutdown
  wait_for { off? }
  power_on
end

#internet_servicesObject



58
59
60
# File 'lib/fog/terremark/models/shared/server.rb', line 58

def internet_services
    @internet_services ||= service.internetservices.all.select {|item| item.Name == self.name}
end

#off?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/fog/terremark/models/shared/server.rb', line 85

def off?
  status == VAppStatus::POWERED_OFF
end

#on?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/fog/terremark/models/shared/server.rb', line 81

def on?
  status == VAppStatus::POWERED_ON
end

#power_offObject



100
101
102
103
104
105
106
107
108
109
# File 'lib/fog/terremark/models/shared/server.rb', line 100

def power_off
  requires :id
  begin
    service.power_off(id)
  rescue Excon::Errors::InternalServerError => e
    #Frankly we shouldn't get here ...
    raise e unless e.to_s =~ /because it is already powered off/
  end
  true
end

#power_on(options = {}) ⇒ Object



89
90
91
92
93
94
95
96
97
98
# File 'lib/fog/terremark/models/shared/server.rb', line 89

def power_on(options = {})
  requires :id
  begin
    service.power_on(id)
  rescue Excon::Errors::InternalServerError => e
    #Frankly we shouldn't get here ...
    raise e unless e.to_s =~ /because it is already powered on/
  end
  true
end

#power_resetObject



122
123
124
125
126
# File 'lib/fog/terremark/models/shared/server.rb', line 122

def power_reset
  requires :id
  service.power_reset(id)
  true
end

#PublicIpAddressObject



50
51
52
53
54
55
56
# File 'lib/fog/terremark/models/shared/server.rb', line 50

def PublicIpAddress
  @PublicIpAddress ||=
    if internet_services.size > 0
      internet_services[0].PublicIpAddress["Name"]
    end
  @PublicIpAddress
end

#ready?Boolean

{ ‘0’ => ‘Being created’, ‘2’ => ‘Powered Off’, ‘4’ => ‘Powered On’}

Returns:

  • (Boolean)


77
78
79
# File 'lib/fog/terremark/models/shared/server.rb', line 77

def ready?
  status == VAppStatus::POWERED_OFF
end

#reloadObject



33
34
35
# File 'lib/fog/terremark/models/shared/server.rb', line 33

def reload
 merge_attributes(service.get_vapp(id).body)
end

#saveObject



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/fog/terremark/models/shared/server.rb', line 173

def save
  requires :name
  if powerOn
  end
  data = service.instantiate_vapp_template(
      server_name=name,
      vapp_template=image,
      options={
          'ssh_key_fingerprint' => sshkeyFingerPrint,
          'cpus' => vcpus,
          'memory' => memory
      })

  merge_attributes(data.body)
  wait_for { ready? }

  #Optional, incase further configuration required.
  if powerOn
    power_on
    wait_for { ready? }
  end

  true
end

#shutdownObject



111
112
113
114
115
116
117
118
119
120
# File 'lib/fog/terremark/models/shared/server.rb', line 111

def shutdown
  requires :id
  begin
    service.power_shutdown(id)
  rescue Excon::Errors::InternalServerError => e
    #Frankly we shouldn't get here ...
    raise e unless e.to_s =~ /because it is already powered off/
  end
  true
end