Sha256: 852ab43da9c4cc4ea65c2770a9c050131da7dc7fe6299ac372634553b03d2c79

Contents?: true

Size: 1.26 KB

Versions: 29

Compression:

Stored size: 1.26 KB

Contents

require "log4r"

module VagrantPlugins
  module HyperV
    module Action
      class SetName
        def initialize(app, env)
          @app = app
          @logger = Log4r::Logger.new("vagrant::hyperv::set_name")
        end

        def call(env)
          name = env[:machine].provider_config.vmname

          # If we already set the name before, then don't do anything
          sentinel = env[:machine].data_dir.join("action_set_name")
          if !name && sentinel.file?
            @logger.info("Default name was already set before, not doing it again.")
            return @app.call(env)
          end

          # If no name was manually set, then use a default
          if !name
            prefix = "#{env[:root_path].basename.to_s}_#{env[:machine].name}"
            prefix.gsub!(/[^-a-z0-9_]/i, "")

            # milliseconds + random number suffix to allow for simultaneous
            # `vagrant up` of the same box in different dirs
            name = prefix + "_#{(Time.now.to_f * 1000.0).to_i}_#{rand(100000)}"
          end

          env[:machine].provider.driver.set_name(name)

          # Create the sentinel
          sentinel.open("w") do |f|
            f.write(Time.now.to_i.to_s)
          end
          @app.call(env)
        end
      end
    end
  end
end

Version data entries

29 entries across 25 versions & 4 rubygems

Version Path
vagrant-unbundled-2.3.6.0 plugins/providers/hyperv/action/set_name.rb
tamtam-vagrant-reload-1.2.1 vendor/cache/vagrant-2092df529ae7/plugins/providers/hyperv/action/set_name.rb
vagrant-unbundled-2.3.3.0 plugins/providers/hyperv/action/set_name.rb
vagrant-unbundled-2.3.2.0 plugins/providers/hyperv/action/set_name.rb
vagrant-unbundled-2.2.19.0 plugins/providers/hyperv/action/set_name.rb
vagrant-unbundled-2.2.18.0 plugins/providers/hyperv/action/set_name.rb
vagrant-unbundled-2.2.16.0 plugins/providers/hyperv/action/set_name.rb
vagrant-unbundled-2.2.14.0 plugins/providers/hyperv/action/set_name.rb
vagrant-aws-mkubenka-0.7.2.pre.24 vendor/bundle/ruby/2.7.0/bundler/gems/vagrant-22795b161bf6/plugins/providers/hyperv/action/set_name.rb
vagrant-unbundled-2.2.10.0 plugins/providers/hyperv/action/set_name.rb
vagrant-unbundled-2.2.9.0 plugins/providers/hyperv/action/set_name.rb
vagrant-unbundled-2.2.8.0 plugins/providers/hyperv/action/set_name.rb
vagrant-unbundled-2.2.7.0 plugins/providers/hyperv/action/set_name.rb
vagrant-unbundled-2.2.6.2 plugins/providers/hyperv/action/set_name.rb
vagrant-unbundled-2.2.6.1 plugins/providers/hyperv/action/set_name.rb
vagrant-unbundled-2.2.6.0 plugins/providers/hyperv/action/set_name.rb
vagrant-unbundled-2.2.5.0 plugins/providers/hyperv/action/set_name.rb
vagrant-unbundled-2.2.4.0 plugins/providers/hyperv/action/set_name.rb
vagrant-unbundled-2.2.3.0 plugins/providers/hyperv/action/set_name.rb
vagrant-unbundled-2.2.2.0 plugins/providers/hyperv/action/set_name.rb