Sha256: 1f33cd344dceafcde5eba0c7093b06296e11772fd15dbd9612ecb5af55b1fb70

Contents?: true

Size: 1.95 KB

Versions: 11

Compression:

Stored size: 1.95 KB

Contents

require "fileutils"

module VagrantPlugins
  module Parallels
    module Action
      class Export
        attr_reader :temp_dir

        include Util

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

        def call(env)
          @env = env

          raise Vagrant::Errors::VMPowerOffToPackage if \
            @env[:machine].provider.state.id != :stopped

          setup_temp_dir
          export
          compact

          @app.call(env)

          recover(env) # called to cleanup temp directory
        end

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

        def setup_temp_dir
          @env[:ui].info I18n.t("vagrant.actions.vm.export.create_dir")
          @temp_dir = @env["export.temp_dir"] = @env[:tmp_path].join(Time.now.to_i.to_s)
          FileUtils.mkpath(@env["export.temp_dir"])
        end

        #TODO: cleanup registered VM on interupt
        def export
          vm_name = generate_name(@env[:root_path], '_export')

          @env[:ui].info I18n.t("vagrant.actions.vm.export.exporting")
          @uuid = @env[:machine].provider.driver.export(@env["export.temp_dir"], vm_name) 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
        end

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

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

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
vagrant-parallels-0.2.0 lib/vagrant-parallels/action/export.rb
vagrant-parallels-0.1.3 lib/vagrant-parallels/action/export.rb
vagrant-parallels-0.1.2 lib/vagrant-parallels/action/export.rb
vagrant-parallels-0.1.1 lib/vagrant-parallels/action/export.rb
vagrant-parallels-0.1.0 lib/vagrant-parallels/action/export.rb
vagrant-parallels-0.0.9 lib/vagrant-parallels/action/export.rb
vagrant-parallels-0.0.8 lib/vagrant-parallels/action/export.rb
vagrant-parallels-0.0.7 lib/vagrant-parallels/action/export.rb
vagrant-parallels-0.0.6 lib/vagrant-parallels/action/export.rb
vagrant-parallels-0.0.5 lib/vagrant-parallels/action/export.rb
vagrant-parallels-0.0.4 lib/vagrant-parallels/action/export.rb