Skip to content

Instantly share code, notes, and snippets.

@joakimk
Created June 9, 2011 13:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joakimk/1016739 to your computer and use it in GitHub Desktop.
Save joakimk/1016739 to your computer and use it in GitHub Desktop.
Example of how to setup a server in vmware that installs from a pxe install server
# config = ..
datacenter = VmwareDataCenter.new(config)
datacenter.create_vm(:name => "ServerName", :cpus => 1, :memory => 512, :disk => 5000,
:datastore => "VolumeName", :install_identifier_mac => "some mac addr",
:install_pxe_network_name => "network to look for pxe server on")
# server cookbook https://github.com/joakimk/cookbooks/tree/master/pxe_install_server
require 'rubygems'
require 'rbvmomi' # gem
class VmwareDataCenter
VIM = RbVmomi::VIM
def initialize(config)
@vim = VIM.connect :host => config.vmware_server, :user => config.vmware_user, :password => config.vmware_password, :insecure => true
@datacenter = @vim.serviceInstance.find_datacenter(config.vmware_datacenter)
end
def destroy_vm(name)
vm = @datacenter.find_vm(name)
vm.PowerOffVM_Task.wait_for_completion rescue nil
vm.Destroy_Task.wait_for_completion
end
def create_vm(opts = {})
vm_cfg = {
:name => opts[:name],
:guestId => 'otherGuest',
:files => { :vmPathName => opts[:datastore] },
:numCPUs => opts[:cpus],
:memoryMB => opts[:memory],
:deviceChange => [
{
:operation => :add,
:device => VIM.VirtualLsiLogicController(
:key => 1000,
:busNumber => 0,
:sharedBus => :noSharing
)
}, {
:operation => :add,
:fileOperation => :create,
:device => VIM.VirtualDisk(
:key => 0,
:backing => VIM.VirtualDiskFlatVer2BackingInfo(
:fileName => opts[:datastore],
:diskMode => :persistent,
:thinProvisioned => true
),
:controllerKey => 1000,
:unitNumber => 0,
:capacityInKB => opts[:disk] * 1000
)
},
{
:operation => :add,
:device => VIM.VirtualE1000(
:key => 0,
:deviceInfo => {
:label => 'Network Adapter',
:summary => 'VM Network'
},
:backing => VIM.VirtualEthernetCardNetworkBackingInfo(
:deviceName => 'VM Network'
),
:addressType => 'generated'
)
},
{
:operation => :add,
:device => VIM.VirtualE1000(
:macAddress => opts[:install_identifier_mac],
:key => 0,
:deviceInfo => {
:label => 'Network Adapter',
:summary => opts[:install_pxe_network_name]
},
:backing => VIM.VirtualEthernetCardNetworkBackingInfo(
:deviceName => opts[:install_pxe_network_name]
),
:addressType => 'manual'
)
}
],
:extraConfig => [
{
:key => 'bios.bootOrder',
:value => 'HDD,ethernet1'
}
]
}
hosts = @datacenter.hostFolder.children
rp = hosts.first.resourcePool
vm = @datacenter.vmFolder.CreateVM_Task(:config => vm_cfg, :pool => rp).wait_for_completion
vm.PowerOnVM_Task.wait_for_completion
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment