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

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

Instance Method Summary collapse

Instance Method Details

#create_internet_services(internet_spec) ⇒ Object



133
134
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
# File 'lib/fog/terremark/models/shared/server.rb', line 133

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



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

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



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

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



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

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

#internet_servicesObject



56
57
58
# File 'lib/fog/terremark/models/shared/server.rb', line 56

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

#off?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/fog/terremark/models/shared/server.rb', line 83

def off?
  status == VAppStatus::POWERED_OFF
end

#on?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/fog/terremark/models/shared/server.rb', line 79

def on?
  status == VAppStatus::POWERED_ON
end

#power_offObject



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

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



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

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



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

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

#PublicIpAddressObject



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

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)


75
76
77
# File 'lib/fog/terremark/models/shared/server.rb', line 75

def ready?
  status == VAppStatus::POWERED_OFF
end

#reloadObject



30
31
32
# File 'lib/fog/terremark/models/shared/server.rb', line 30

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

#saveObject



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

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



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

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