Class: VagrantPlugins::VagrantHyperV::Action::Export

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-windows-hyperv/action/export.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ Export



13
14
15
# File 'lib/vagrant-windows-hyperv/action/export.rb', line 13

def initialize(app, env)
  @app = app
end

Instance Attribute Details

#temp_dirObject (readonly)

Returns the value of attribute temp_dir.



12
13
14
# File 'lib/vagrant-windows-hyperv/action/export.rb', line 12

def temp_dir
  @temp_dir
end

Instance Method Details

#add_metadata_jsonObject



46
47
48
49
50
# File 'lib/vagrant-windows-hyperv/action/export.rb', line 46

def 
  File.open(File.join(@env["package.directory"], "metadata.json"), "w") do |f|
    f.write(' { "provider" : "hyperv" }')
  end
end

#call(env) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/vagrant-windows-hyperv/action/export.rb', line 17

def call(env)
  @env = env

  raise "Please off the machine before package" if \
    @env[:machine].provider.state.id != :off

  @temp_dir = env[:tmp_path].join(Time.now.to_i.to_s)
  export
  

  @app.call(env)

  recover(env) # called to cleanup temp directory
end

#exportObject



38
39
40
41
42
43
44
# File 'lib/vagrant-windows-hyperv/action/export.rb', line 38

def export
  @env[:ui].info('Exporting the VM, this process may take a while.')
  result = @env[:machine].provider.driver.export_vm_to(temp_dir.to_s)
  # Hyper-V Exports the VM under the VM's name in to the temp directory.
  # Set the package directory to this folder, all files should go into this folder
  @env["package.directory"] = temp_dir.join(result["name"])
end

#recover(env) ⇒ Object



32
33
34
35
36
# File 'lib/vagrant-windows-hyperv/action/export.rb', line 32

def recover(env)
  if temp_dir && File.exist?(temp_dir)
    FileUtils.rm_rf(temp_dir)
  end
end