1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00

Support to configure vapp and add multiple internet services

This commit is contained in:
Chirag Jog 2012-05-02 13:55:11 -07:00
parent b75ee61daf
commit 69539b059d
3 changed files with 133 additions and 25 deletions

View file

@ -1,5 +1,14 @@
require 'fog/core/model'
# { '0' => 'Being created', '1' => 'Being Deployed, '2' => 'Powered Off', '4' => 'Powered On'}
module VAppStatus
BEING_CREATED = "0"
BEING_DEPLOYED = "1"
POWERED_OFF = "2"
POWERED_ON = "4"
end
module Fog
module Terremark
module Shared
@ -21,9 +30,9 @@ module Fog
def destroy
case self.status
when "0"
when VAppStatus::BEING_CREATED, VAppStatus::BEING_DEPLOYED
return false
when "4"
when VAppStatus::POWERED_ON
data = connection.power_off(self.id).body
wait_for { off? }
end
@ -35,36 +44,35 @@ module Fog
#Find the internet service
services = connection.get_internet_services(connection.default_vdc_id)
internet_info = services.body["InternetServices"].find {|item| item["Name"] == self.name}
internet_services = services.body["InternetServices"].select {|item| item["Name"] == self.name}
#Delete all the associated nodes
if internet_info
nodes = connection.get_node_services(internet_info["Id"]).body["NodeServices"]
for service in internet_services:
nodes = connection.get_node_services(service["Id"]).body["NodeServices"]
#Delete all the associated nodes
nodes.select { |item| connection.delete_node_service(item["Id"]) }
#Clear out the services
connection.delete_internet_service(internet_info["Id"])
#Release IP Address
connection.delete_public_ip(internet_info["PublicIpAddress"]["Id"])
connection.delete_internet_service(service["Id"])
end
#Release IP Address
connection.delete_public_ip(service["PublicIpAddress"]["Id"])
true
end
# { '0' => 'Being created', '2' => 'Powered Off', '4' => 'Powered On'}
def ready?
state = connection.get_vapp(id).body["status"]
state == '2'
state == VAppStatus::POWERED_OFF
end
def on?
state = connection.get_vapp(id).body["status"]
state == '4'
state == VAppStatus::POWERED_ON
end
def off?
state = connection.get_vapp(id).body["status"]
state == '2'
state == VAppStatus::POWERED_OFF
end
def power_on(options = {})
@ -113,18 +121,55 @@ module Fog
power_on
end
def create_internet_service(protocol="TCP", port="22")
data = connection.create_internet_service(
vdc = connection.default_vdc_id,
name = self.name,
protocol = protocol,
port = port,
options = {'Enabled' => 'true',
"Description" => :name
}
)
merge_attributes(data.body)
ssh_service = data.body["Id"]
def create_internet_services(internet_spec)
public_ip_info = 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
data = connection.create_internet_service(
vdc = connection.default_vdc_id,
name = self.name,
protocol = proto,
port = port,
options = {
'Enabled' => 'true',
"Description" => :name
}
)
internet_service_id = data.body["Id"]
public_ip_info = data.body["PublicIpAddress"]
else
#create additional services to existing Public IP
data = connection.add_internet_service(
ip_id = public_ip_info["Id"],
name = self.name,
protocol = proto,
port = port,
options = {
'Enabled' => 'true',
"Description" => :name
}
)
internet_service_id = data.body["Id"]
end
#Create the associate node service to the server
self.create_node_service(
internet_service_id,
proto,
port
)
end
end
true
end
def create_node_service(internet_service_id, protocol="TCP", port="22")
ssh_service = internet_service_id
data = connection.add_node_service(
service_id = ssh_service,
ip = self.IpAddress,

View file

@ -0,0 +1,62 @@
module Fog
module Terremark
module Shared
module Real
include Common
def configure_vapp(vapp_id, vapp_name, options = {})
items = ""
vapp_uri = [@host, @path, "vApp", vapp_id.to_s].join("/")
if options['vcpus']
vcpu_item = <<-DATA
<Item xmlns="http://schemas.dmtf.org/ovf/envelope/1"> <InstanceID xmlns="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData">1</InstanceID><ResourceType xmlns="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData">3</ResourceType><VirtualQuantity xmlns="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData">#{options['vcpus']}</VirtualQuantity></Item>
DATA
items << vcpu_item
end
if options['memory']
memory_item = <<-DATA
<Item xmlns="http://schemas.dmtf.org/ovf/envelope/1"><InstanceID xmlns="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData">2</InstanceID><ResourceType xmlns="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData">4</ResourceType>38<VirtualQuantity xmlns="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData">#{options['memory']}</VirtualQuantity></Item>
DATA
items << memory_item
end
#Default root disk
virtual_disk_item = <<-DATA
<Item>
<AddressOnParent xmlns="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData">0</AddressOnParent> <HostResource xmlns="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData">1048576</HostResource><InstanceID xmlns="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData">9</InstanceID><ResourceType xmlns="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData">17</ResourceType><VirtualQuantity xmlns="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData">1048576</VirtualQuantity></Item>
DATA
items << virtual_disk_item
#Additional disks
if options['virtual_disks']
for disk in options['virtual_disks']
actual_size = disk.to_i * 1024 * 1024
virtual_disk_item = <<-DATA
<Item>
<AddressOnParent xmlns="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData">-1</AddressOnParent><HostResource xmlns="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData">#{actual_size}</HostResource><InstanceID xmlns="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData">9</InstanceID><ResourceType xmlns="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData">17</ResourceType><VirtualQuantity xmlns="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData">#{actual_size}</VirtualQuantity></Item>
DATA
items << virtual_disk_item
end
end
data = <<-DATA
<VApp href="#{vapp_uri}" type="application/vnd.vmware.vcloud.vApp+xml" name="#{vapp_name}" status="2" size="10485760" xmlns="http://www.vmware.com/vcloud/v0.8" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<VirtualHardwareSection xmlns="http://schemas.dmtf.org/ovf/envelope/1"><Info>Virtual Hardware</Info>#{items}
</VirtualHardwareSection>
</VApp>
DATA
puts "Data : #{data}"
request(
:body => data,
:expects => 202,
:headers => { 'Content-Type' => 'application/vnd.vmware.vCloud.vApp+xml' },
:method => 'PUT',
:path => "vapp/#{vapp_id}"
)
end
end
end
end
end

View file

@ -295,6 +295,7 @@ module Fog
require 'fog/terremark/requests/shared/get_vapp_template'
require 'fog/terremark/requests/shared/get_vdc'
require 'fog/terremark/requests/shared/instantiate_vapp_template'
require 'fog/terremark/requests/shared/configure_vapp'
require 'fog/terremark/requests/shared/power_off'
require 'fog/terremark/requests/shared/power_on'
require 'fog/terremark/requests/shared/power_reset'