Class: VagrantPlugins::Utm::Action::SetName

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant_utm/action/set_name.rb

Overview

This action sets the name of the VM (UTM ‘Name’ field)

Instance Method Summary collapse

Constructor Details

#initialize(app, _env) ⇒ SetName

Returns a new instance of SetName.



13
14
15
16
# File 'lib/vagrant_utm/action/set_name.rb', line 13

def initialize(app, _env)
  @logger = Log4r::Logger.new("vagrant::action::vm::setname")
  @app = app
end

Instance Method Details

#call(env) ⇒ Object

rubocop:disable Metrics/AbcSize



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/vagrant_utm/action/set_name.rb', line 18

def call(env) # rubocop:disable Metrics/AbcSize
  name = env[:machine].provider_config.name

  # 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
  unless name
    prefix = "#{env[:root_path].basename}_#{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(100_000)}"
  end

  # TODO: Verify the name is not taken
  # vms = env[:machine].provider.driver.read_vms
  # raise Vagrant::Errors::VMNameExists, name: name if    #   vms.key?(name) && vms[name] != env[:machine].id

  # if vms.key?(name)
  #   @logger.info("Not setting the name because our name is already set.")
  # else
  env[:ui].info(I18n.t(
                  "vagrant.actions.vm.set_name.setting_name", name: name
                ))
  env[:machine].provider.driver.set_name(name)
  # end

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

  @app.call(env)
end