class Fission::Action::VM::Cloner
def clean_up_conf_file(conf_file_path)
@cloner.clean_up_conf_file '/vms/foo/foo.vmx'
Examples
conf_file_path - Aboslute path to the VM's conf file (.vmx).
and disable VMware tools warning.
includes removing generated MAC addresses, setting up for a new UUID,
Internal: Cleans up the conf file (*.vmx) for a newly cloned VM. This
def clean_up_conf_file(conf_file_path) conf_items_patterns = { /^tools\.remindInstall.*\n/ => "tools.remindInstall = \"FALSE\"", /^uuid\.action.*\n/ => "uuid.action = \"create\"", /^ethernet\.+generatedAddress.*\n/ => '' } content = File.read conf_file_path content << "\n" conf_items_patterns.each_pair do |pattern, new_item| unless content.include? new_item content.gsub(pattern, '').strip content << "#{new_item}\n" end end File.open(conf_file_path, 'w') { |f| f.print content } end
def clone
If successful, the Response's data attribute will be nil.
Returns a Response with the result.
@cloner.clone
Examples
files. It's recommended to clone VMs which are not running.
effort. This essentially is a directory copy with updates to relevant
Fusion doesn't provide a native cloning mechanism, this is a best
Public: Creates a new VM which is a clone of an existing VM. As
def clone unless @source_vm.exists? return Response.new :code => 1, :message => 'VM does not exist' end if @target_vm.exists? return Response.new :code => 1, :message => 'VM already exists' end FileUtils.cp_r @source_vm.path, @target_vm.path rename_vm_files @source_vm.name, @target_vm.name update_config @source_vm.name, @target_vm.name Response.new :code => 0 end
def files_to_rename(from, to)
The paths which match the from name will preceed any other files
Returns an Array containing the paths (String) to the files to rename.
# => ['/vms/vm1/foo.vmdk', '/vms/vm1/foo.vmx', 'vms/vm1/blah.other']
@cloner.files_to_rename 'foo', 'bar'
Examples
to - The name of the newly cloned VM.
from - The VM name that was used as the source of the clone.
newly cloned VM directory.
Internal: Provides the list of files which need to be renamed in a
def files_to_rename(from, to) files_which_match_source_vm = [] other_files = [] Dir.entries(@target_vm.path).each do |f| unless f == '.' || f == '..' f.include?(from) ? files_which_match_source_vm << f : other_files << f end end files_which_match_source_vm + other_files end
def initialize(source_vm, target_vm)
Fission::Action::VMCloner.new @my_source_vm, @my_target_vm
Examples:
target_vm - An instance of VM
source_vm - An instance of VM
target VM object.
Internal: Creates a new VMCloner object. This accepts a source and
def initialize(source_vm, target_vm) @source_vm = source_vm @target_vm = target_vm end
def rename_vm_files(from, to)
@cloner.rename_vm_files 'foo', 'bar'
Examples
to - The name of the newly cloned VM.
from - The VM name that was used as the source of the clone.
Internal: Renames the files of a newly cloned VM.
def rename_vm_files(from, to) files_to_rename(from, to).each do |file| text_to_replace = File.basename(file, File.extname(file)) if File.extname(file) == '.vmdk' if file.match /\-s\d+\.vmdk/ text_to_replace = file.partition(/\-s\d+.vmdk/).first end end unless File.exists?(File.join(@target_vm.path, file.gsub(text_to_replace, to))) FileUtils.mv File.join(@target_vm.path, file), File.join(@target_vm.path, file.gsub(text_to_replace, to)) end end end
def update_config(from, to)
@cloner.update_config 'foo', 'bar'
Examples
to - The name of the newly cloned VM.
from - The VM name that was used as the source of the clone.
Any binary '.vmdk' files will be skipped.
update any files with the extension of '.vmx', '.vmxf', and '.vmdk'.
Internal: Updates config files for a newly cloned VM. This will
def update_config(from, to) ['.vmx', '.vmxf', '.vmdk'].each do |ext| file = File.join @target_vm.path, "#{to}#{ext}" unless File.binary?(file) text = (File.read file).gsub from, to File.open(file, 'w'){ |f| f.print text } end clean_up_conf_file(file) if ext == '.vmx' end end
def vm_file_extensions
Returns an Array containing the file extensions of VM realted files.
# => ['.nvram', '.vmdk', '.vmem']
@cloner.vm_file_extension
Examples
Internal: Provides the list of file extensions for VM related files.
def vm_file_extensions ['.nvram', '.vmdk', '.vmem', '.vmsd', '.vmss', '.vmx', '.vmxf'] end
def vmrun_cmd
Returns a String for the configured value of
# => "/foo/bar/vmrun -T fusion"
@cloner.vmrun_cmd
Examples
Internal: Helper for getting the configured vmrun_cmd value.
def vmrun_cmd Fission.config['vmrun_cmd'] end