Skip to content

Instantly share code, notes, and snippets.

@stonith
Created June 10, 2014 20:45
Show Gist options
  • Save stonith/574678ed3cb029c579da to your computer and use it in GitHub Desktop.
Save stonith/574678ed3cb029c579da to your computer and use it in GitHub Desktop.
Prototype API for vSphere VM Cloning from Template
#!/usr/bin/env ruby
require 'rubygems'
require 'pp'
require 'fog'
require 'highline/import'
require 'sinatra'
set :bind, '0.0.0.0'
credentials = {
:provider => "vsphere",
:vsphere_username => "root",
:vsphere_password => ENV['viserverpasswd'],
:vsphere_server => ENV['viserver'],
:vsphere_ssl => true,
:vsphere_expected_pubkey_hash => "******************************",
:vsphere_rev => "5.0"
}
connection = Fog::Compute.new(credentials)
# MUST BE Ruby v 1.9 to use this hash style
post '/vms' do
template = params[:template]
# Clone machine with specified paramters
vms = connection.vm_clone('datacenter' => ENV['datacenter'], "template_path"=>"/#{template}", "name"=>Time.now.to_i, "dest_folder"=>"New VMs", "numCPUs"=>2, "memoryMB"=>8192, "power_on"=>"false")
# Add 2nd Hard Disk
adddisk = connection.servers.get("#{vms["new_vm"]["id"]}")
adddisk.volumes.create("size_gb"=>10,"datastore"=>ENV['datastore'])
# Poweron VM
connection.vm_power_on("instance_uuid"=>"#{vms["new_vm"]["id"]}")
"#{vms}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment