Sha256: 4d6ccc9728800790497b895c888eacb813d23fbb83c0db1213238c8dc9c14094

Contents?: true

Size: 1.9 KB

Versions: 12

Compression:

Stored size: 1.9 KB

Contents

require "log4r"

require "digest/md5"

module VagrantPlugins
  module ProviderVirtualBox
    module Action
      class PrepareCloneSnapshot
        def initialize(app, env)
          @app = app
          @logger = Log4r::Logger.new("vagrant::action::vm::prepare_clone")
        end

        def call(env)
          if !env[:clone_id]
            @logger.info("no clone master, not preparing clone snapshot")
            return @app.call(env)
          end

          # If we're not doing a linked clone, snapshots don't matter
          if !env[:machine].provider_config.linked_clone
            return @app.call(env)
          end

          # We lock so that we don't snapshot in parallel
          lock_key = Digest::MD5.hexdigest("#{env[:clone_id]}-snapshot")
          env[:machine].env.lock(lock_key, retry: true) do
            prepare_snapshot(env)
          end

          # Continue
          @app.call(env)
        end

        protected

        def prepare_snapshot(env)
          name = env[:machine].provider_config.linked_clone_snapshot
          name_set = !!name
          name = "base" if !name
          env[:clone_snapshot] = name

          # Get the snapshots. We're done if it already exists
          snapshots = env[:machine].provider.driver.list_snapshots(env[:clone_id])
          if snapshots.include?(name)
            @logger.info("clone snapshot already exists, doing nothing")
            return
          end

          # If they asked for a specific snapshot, it is an error
          if name_set
            # TODO: Error
          end

          @logger.info("Creating base snapshot for master VM.")
          env[:machine].provider.driver.create_snapshot(
            env[:clone_id], name) do |progress|
              env[:ui].rewriting do |ui|
                ui.clear_line
                ui.report_progress(progress, 100, false)
              end
          end
        end
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 3 rubygems

Version Path
vagrant-unbundled-2.3.6.0 plugins/providers/virtualbox/action/prepare_clone_snapshot.rb
tamtam-vagrant-reload-1.2.1 vendor/cache/vagrant-2092df529ae7/plugins/providers/virtualbox/action/prepare_clone_snapshot.rb
vagrant-unbundled-2.3.3.0 plugins/providers/virtualbox/action/prepare_clone_snapshot.rb
vagrant-unbundled-2.3.2.0 plugins/providers/virtualbox/action/prepare_clone_snapshot.rb
vagrant-unbundled-2.2.19.0 plugins/providers/virtualbox/action/prepare_clone_snapshot.rb
vagrant-unbundled-2.2.18.0 plugins/providers/virtualbox/action/prepare_clone_snapshot.rb
vagrant-unbundled-2.2.16.0 plugins/providers/virtualbox/action/prepare_clone_snapshot.rb
vagrant-unbundled-2.2.14.0 plugins/providers/virtualbox/action/prepare_clone_snapshot.rb
vagrant-aws-mkubenka-0.7.2.pre.24 vendor/bundle/ruby/2.7.0/bundler/gems/vagrant-22795b161bf6/plugins/providers/virtualbox/action/prepare_clone_snapshot.rb
vagrant-unbundled-2.2.10.0 plugins/providers/virtualbox/action/prepare_clone_snapshot.rb
vagrant-unbundled-2.2.9.0 plugins/providers/virtualbox/action/prepare_clone_snapshot.rb
vagrant-unbundled-2.2.8.0 plugins/providers/virtualbox/action/prepare_clone_snapshot.rb