Module: OCCI::Backend::EC2::Compute

Defined in:
lib/occi/backend/ec2/compute.rb,
lib/occi/backend/ec2/Compute.rb

Overview


Instance Method Summary collapse

Instance Method Details

#compute_delete(compute) ⇒ Object




234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/occi/backend/ec2/compute.rb', line 234

def compute_delete(compute)
  OCCI::Log.debug("Deleting EC2 Compute instance with EC2 ID #{compute.backend[:id]}")

  # get the ec2 backend instance
  backend_instance = get_backend_instance(compute)

  if backend_instance.nil?
    OCCI::Log.debug("Problems refreshing compute instance: An instance with the EC2 ID #{compute.backend[:id]} could not be found.")
    return
  end
  # terminate the instance
  backend_instance.terminate()

  # delete networklinks to the private and public network and the ConsoleLink
  compute.links.each do |link|
    if link.backend[:backend_id] == backend_instance.id
      location = OCCI::Rendering::HTTP::LocationRegistry.get_location_of_object(link)
      OCCI::Log.debug("Deleting link to #{location}")
      OCCI::Rendering::HTTP::LocationRegistry.unregister(location)
    end
  end
  OCCI::Log.debug("Deleted EC2 Compute instance with EC2 ID #{compute.backend[:id]}")
end

#compute_deploy(compute) ⇒ Object




73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/occi/backend/ec2/compute.rb', line 73

def compute_deploy(compute)
  OCCI::Log.debug("Deploying EC2 instance.")

  # look what template and instance type to use
  image_id = "ami-973b06e3" # fallback os template
  instance_type = "t1.micro" # fallback resource template
  compute.mixins.each do |mixin|
    mixin.related.each do |related|
      if related.term == "os_tpl"
        image_id = mixin.term
        break # use the first template found
      elsif related.term == "resource_tpl"
        instance_type = mixin.term
        break # use the first template found
      end
    end
  end

  # get the ec2 interface
  ec2 = OCCI::Backend::EC2.get_ec2_interface()

  # create an instance
  if key_pair_exists("default_occi_key")
    backend_instance = ec2.instances.create(:image_id => image_id,
                                            :instance_type => instance_type,
                                            :key_name => "default_occi_key")
  else
    backend_instance = ec2.instances.create(:image_id => image_id,
                                            :instance_type => instance_type)
  end

  # save the id of the instance
  compute.backend[:id] = backend_instance.id

  # link it to the private ec2 network
  OCCI::Log.debug("Linking instance to \"/network/ec2_private_network\".")
  private_network = OCCI::Rendering::HTTP::LocationRegistry.get_object("/network/ec2_private_network")
  attributes = OCCI::Core::Attributes.new()
  attributes["occi.networkinterface.interface"] = ""
  attributes["occi.core.source"] = compute.get_location
  attributes["occi.core.target"] = "/network/ec2_private_network"
  ipnetwork = OCCI::CategoryRegistry.get_by_id("http://schemas.ogf.org/occi/infrastructure/networkinterface#ipnetworkinterface")
  ipnetwork.backend[:network] = "private"
  mixins = [ipnetwork]
  private_networkinterface = OCCI::Infrastructure::Networkinterface.new(attributes, mixins)
  # save the id of the compute backend instance in the network link for future identification
  private_networkinterface.backend[:backend_id] = backend_instance.id
  private_networkinterface.backend[:network] = "ec2_private_network"
  compute.links << private_networkinterface
  private_network.links << private_networkinterface
  OCCI::Rendering::HTTP::LocationRegistry.register(private_networkinterface.get_location, private_networkinterface)

  # link it to the public ec2 network
  OCCI::Log.debug("Linking instance to \"/network/ec2_public_network\".")
  public_network = OCCI::Rendering::HTTP::LocationRegistry.get_object("/network/ec2_public_network")
  attributes = OCCI::Core::Attributes.new()
  attributes["occi.networkinterface.interface"] = ""
  attributes["occi.core.source"] = compute.get_location
  attributes["occi.core.target"] = "/network/ec2_public_network"
  ipnetwork = OCCI::CategoryRegistry.get_by_id("http://schemas.ogf.org/occi/infrastructure/networkinterface#ipnetworkinterface")
  ipnetwork.backend[:network] = "ec2_public_network"
  mixins = [ipnetwork]
  public_networkinterface = OCCI::Infrastructure::Networkinterface.new(attributes, mixins)
  # save the id of the compute backend instance in the network link for future identification
  public_networkinterface.backend[:backend_id] = backend_instance.id
  public_networkinterface.backend[:network] = "public"
  compute.links << public_networkinterface
  public_network.links << public_networkinterface
  OCCI::Rendering::HTTP::LocationRegistry.register(public_networkinterface.get_location, public_networkinterface)

  OCCI::Log.debug("Deployed EC2 instance with EC2 ID: #{compute.backend[:id]}")
end

#compute_refresh(compute) ⇒ Object




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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/occi/backend/ec2/compute.rb', line 147

def compute_refresh(compute)
  OCCI::Log.debug("Refreshing EC2 compute object with backend ID: #{compute.backend[:id]}")

  # get the ec2 backend instance
  backend_instance = get_backend_instance(compute)

  # check if there are any problems with the backend instance
  if backend_instance.nil?
    OCCI::Log.debug("Problems refreshing compute instance: An instance with the EC2 ID #{compute.backend[:id]} could not be found.")
    return
  end

  # update the state
  compute_update_state(compute)
  OCCI::Log.debug("Refreshed EC2 compute object with backend ID: #{compute.backend[:id]}")

  # setting the architecture
  compute.attributes["occi.compute.architecture"] = backend_instance.architecture.to_s
  compute.attributes["ec2.compute.platform"] = backend_instance.platform
  compute.attributes["ec2.compute.kernel_id"] = backend_instance.kernel_id

  # update public and private ip
  compute.links.each do |link|
    if link.kind.term == "networkinterface" and link.backend[:backend_id] == backend_instance.id
      if link.backend[:network] == "public" and backend_instance.ip_address != nil
        link.mixins.each do |mixin|
          if mixin.backend[:network] == "ec2_public_network"
            link.attributes["occi.networkinterface.address"] = backend_instance.ip_address
            state = OCCI::Infrastructure::Networkinterface::STATE_ACTIVE
            link.state_machine.set_state(state)
            link.attributes['occi.networkinterface.state'] = "active"
          end
        end
      elsif link.backend[:network] == "private" and backend_instance.private_ip_address != nil
        link.mixins.each do |mixin|
          if mixin.backend[:network] == "ec2_private_network"
            link.attributes["occi.networkinterface.address"] = backend_instance.private_ip_address
            state = OCCI::Infrastructure::Networkinterface::STATE_ACTIVE
            link.state_machine.set_state(state)
            link.attributes['occi.networkinterface.state'] = "active"
          end
        end
      end
    end
  end

  # create the console link if not already existent and if in state active
  if compute.state_machine.current_state == OCCI::Infrastructure::Compute::STATE_ACTIVE and not has_console_link(compute) and backend_instance.key_name == "default_occi_key"
    # create a ConsoleLink
    OCCI::Log.debug("Creating a ConsoleLink to the EC2 Compute instance.")
    attributes = OCCI::Core::Attributes.new()
    attributes['occi.core.target'] = "ssh://" + backend_instance.public_dns_name
    attributes['occi.core.source'] = compute.get_location
    mixins = []
    console_link = OCCI::Infrastructure::ConsoleLink.new(attributes, mixins)
    # save the id of the compute backend instance in the network link for future identification
    console_link.backend[:backend_id] = backend_instance.id
    # link and register
    location = console_link.get_location
    compute.links << console_link
    OCCI::Rendering::HTTP::LocationRegistry.register(location, console_link)
  end
end

#compute_restart(compute, parameters = nil) ⇒ Object


Action restart



288
289
290
291
292
293
294
295
296
297
# File 'lib/occi/backend/ec2/compute.rb', line 288

def compute_restart(compute, parameters=nil)
  OCCI::Log.debug("Restarting EC2 VM with EC2 instance ID #{compute.backend[:id]}")

  # get the ec2 backend instance
  backend_instance = get_backend_instance(compute)

  # restart the instance
  backend_instance.reboot()
  OCCI::Log.debug("Restarted EC2 VM with EC2 instance ID #{compute.backend[:id]}")
end

#compute_start(compute, parameters = nil) ⇒ Object


COMPUTE ACTIONS

COMPUTE Action start



262
263
264
265
266
267
268
269
270
271
# File 'lib/occi/backend/ec2/compute.rb', line 262

def compute_start(compute, parameters=nil)
  OCCI::Log.debug("Starting EC2 VM with EC2 instance ID #{compute.backend[:id]}")

  # get the ec2 backend instance
  backend_instance = get_backend_instance(compute)

  # suspend the instance (in EC2 lingo stop it)
  backend_instance.start()
  OCCI::Log.debug("Started EC2 VM with EC2 instance ID #{compute.backend[:id]}")
end

#compute_stop(compute, parameters = nil) ⇒ Object


Action stop



275
276
277
278
279
280
281
282
283
284
# File 'lib/occi/backend/ec2/compute.rb', line 275

def compute_stop(compute, parameters=nil)
  OCCI::Log.debug("Stopping EC2 VM with EC2 instance ID #{compute.backend[:id]}")

  # get the ec2 backend instance
  backend_instance = get_backend_instance(compute)

  # stop the instance
  backend_instance.stop()
  OCCI::Log.debug("Stopped EC2 VM with EC2 instance ID #{compute.backend[:id]}")
end

#compute_suspend(compute, parameters = nil) ⇒ Object


Action suspend



301
302
303
# File 'lib/occi/backend/ec2/compute.rb', line 301

def compute_suspend(compute, parameters=nil)
  OCCI::Log.warning("Stop suspend is not supported on EC2 Compute instances.")
end

#compute_update_state(compute) ⇒ Object




212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/occi/backend/ec2/compute.rb', line 212

def compute_update_state(compute)
  # get the ec2 backend instance
  backend_instance = get_backend_instance(compute)

  # map the ec2 state to an OCCI state
  OCCI::Log.debug("Current EC2 VM state is: #{backend_instance.status}")
  state = case backend_instance.status
            when :running then
              OCCI::Infrastructure::Compute::STATE_ACTIVE
            when :pending, :shutting_down, :stopping, :terminated then
              OCCI::Infrastructure::Compute::STATE_INACTIVE
            when :stopped then
              OCCI::Infrastructure::Compute::STATE_SUSPENDED
            else
              OCCI::Infrastructure::Compute::STATE_INACTIVE
          end
  # set the state
  compute.state_machine.set_state(state)
  compute.attributes['occi.compute.state'] = compute.state_machine.current_state.name
end