Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Package machines as plain VMs, not templates #227

Merged
merged 4 commits into from
Nov 9, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions lib/vagrant-parallels/action/box_register.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
require 'log4r'

#require 'digest/md5'

module VagrantPlugins
module Parallels
module Action
Expand Down Expand Up @@ -102,6 +100,12 @@ def register_box(env)
box_id_file.open('w+') do |f|
f.write(env[:clone_id])
end

if env[:machine].provider.pd_version_satisfies?('>= 10')
# Convert template to VM (compatibility with old-styled boxes)
env[:machine].provider.driver.execute_prlctl(
'set', env[:clone_id], '--template', 'off')
end
end
end
end
Expand Down
73 changes: 35 additions & 38 deletions lib/vagrant-parallels/action/export.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,88 +8,85 @@ def initialize(app, env)
end

def call(env)
@env = env
if env[:machine].state.id != :stopped
raise Vagrant::Errors::VMPowerOffToPackage
end

export
compact_template
unregister_template
clone(env)
compact(env)
unregister_vm(env)

@app.call(env)
end

def recover(env)
@env = env
unregister_template
unregister_vm(env)
end

private

def box_template_name
def box_vm_name(env)
# Use configured name if it is specified, or generate the new one
name = @env[:machine].provider_config.name
name = env[:machine].provider_config.name
if !name
name = "#{@env[:root_path].basename.to_s}_#{@env[:machine].name}"
name = "#{env[:root_path].basename.to_s}_#{env[:machine].name}"
name.gsub!(/[^-a-z0-9_]/i, '')
end

tpl_name = "#{name}_box"
vm_name = "#{name}_box"

# Ensure that the name is not in use
ind = 0
while @env[:machine].provider.driver.read_vms.has_key?(tpl_name)
while env[:machine].provider.driver.read_vms.has_key?(vm_name)
ind += 1
tpl_name = "#{name}_box_#{ind}"
vm_name = "#{name}_box_#{ind}"
end

tpl_name
vm_name
end

def export
@env[:ui].info I18n.t('vagrant.actions.vm.export.exporting')
def clone(env)
env[:ui].info I18n.t('vagrant.actions.vm.export.exporting')

options = {
template: true,
dst: @env['export.temp_dir'].to_s
dst: env['export.temp_dir'].to_s
}

@env[:package_box_id] = @env[:machine].provider.driver.clone_vm(
@env[:machine].id, options) do |progress|
@env[:ui].clear_line
@env[:ui].report_progress(progress, 100, false)
env[:package_box_id] = env[:machine].provider.driver.clone_vm(
env[:machine].id, options) do |progress|
env[:ui].clear_line
env[:ui].report_progress(progress, 100, false)

# # If we got interrupted, then the import could have been interrupted.
# Just rise an exception and then 'recover' will be called to cleanup.
raise Vagrant::Errors::VagrantInterrupt if @env[:interrupted]
# If we got interrupted, then rise an exception and 'recover'
# will be called to cleanup.
raise Vagrant::Errors::VagrantInterrupt if env[:interrupted]
end

# Set the template name
tpl_name = box_template_name
@env[:machine].provider.driver.set_name(@env[:package_box_id], tpl_name)
# Set the box VM name
name = box_vm_name(env)
env[:machine].provider.driver.set_name(env[:package_box_id], name)

# Clear the line a final time so the next data can appear
# alone on the line.
@env[:ui].clear_line
env[:ui].clear_line
end

def compact_template
@env[:ui].info I18n.t('vagrant_parallels.actions.vm.export.compacting')
@env[:machine].provider.driver.compact(@env[:package_box_id]) do |progress|
@env[:ui].clear_line
@env[:ui].report_progress(progress, 100, false)
def compact(env)
env[:ui].info I18n.t('vagrant_parallels.actions.vm.export.compacting')
env[:machine].provider.driver.compact(env[:package_box_id]) do |progress|
env[:ui].clear_line
env[:ui].report_progress(progress, 100, false)
end

# Clear the line a final time so the next data can appear
# alone on the line.
@env[:ui].clear_line
env[:ui].clear_line
end

def unregister_template
return if !@env[:package_box_id]
@logger.info("Unregister the box template: '#{@env[:package_box_id]}'")
@env[:machine].provider.driver.unregister(@env[:package_box_id])
def unregister_vm(env)
return if !env[:package_box_id]
@logger.info("Unregister the box VM: '#{env[:package_box_id]}'")
env[:machine].provider.driver.unregister(env[:package_box_id])
end
end
end
Expand Down
1 change: 0 additions & 1 deletion lib/vagrant-parallels/driver/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ def clone_vm(src_name, options={})
dst_name = "vagrant_temp_#{(Time.now.to_f * 1000.0).to_i}_#{rand(100000)}"

args = ['clone', src_name, '--name', dst_name]
args << '--template' if options[:template]
args.concat(['--dst', options[:dst]]) if options[:dst]

# Linked clone options
Expand Down
4 changes: 2 additions & 2 deletions locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,8 @@ en:
actions:
vm:
box:
register: Registering VM template from the base box '%{name}'...
unregister: Unregistering VM template...
register: Registering VM image from the base box '%{name}'...
unregister: Unregistering the box VM image...
clone:
full: Cloning new virtual machine...
linked: Creating new virtual machine as a linked clone...
Expand Down
7 changes: 3 additions & 4 deletions test/unit/support/shared/pd_driver_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,12 @@
subject.clone_vm(tpl_uuid)
end

it 'clones VM to template' do
it 'clones VM to the exported VM' do
subprocess.should_receive(:execute).
with('prlctl', 'clone', uuid, '--name', an_instance_of(String),
'--template', '--dst', an_instance_of(String),
an_instance_of(Hash)).
'--dst', an_instance_of(String), an_instance_of(Hash)).
and_return(subprocess_result(exit_code: 0))
subject.clone_vm(uuid, {dst: '/path/to/template', template: true})
subject.clone_vm(uuid, {dst: '/path/to/template'})
end
end

Expand Down