Class: VagrantPlugins::ESXi::Action::ReadState

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-vmware-esxi/action/read_state.rb

Overview

This action reads the state of the machine and puts it in the ‘:machine_state_id` key in the environment.

Constant Summary collapse

@@nfs_valid_ids =
[]

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ ReadState



12
13
14
15
# File 'lib/vagrant-vmware-esxi/action/read_state.rb', line 12

def initialize(app, env)
  @app    = app
  @logger = Log4r::Logger.new('vagrant_vmware_esxi::action::read_state')
end

Instance Method Details

#call(env) ⇒ Object



17
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
59
60
61
62
63
64
65
66
# File 'lib/vagrant-vmware-esxi/action/read_state.rb', line 17

def call(env)
  env[:machine_state] = read_state(env)

  #  Do NFS stuff
  if (env[:machine_state].to_s.include? "running") && (@nfs_host_ip.nil?)
    ssh_info = env[:machine].ssh_info
    if defined?(ssh_info[:host])
      env[:nfs_machine_ip] = [ssh_info[:host]]
      @nfs_machine_ip = [ssh_info[:host]].dup
      @@nfs_valid_ids |= [env[:machine].id]
      env[:nfs_valid_ids] = @@nfs_valid_ids

      begin
        puts "Get local IP address for NFS. (pri)" if env[:machine].provider_config.debug =~ %r{ip}i
        #  The Standard way to get your IP.  Get your hostname, resolv it.
        env[:nfs_host_ip] = Socket::getaddrinfo(Socket.gethostname,"echo",Socket::AF_INET)[0][3]
      rescue
        puts "Get local IP address for NFS. (alt)" if env[:machine].provider_config.debug =~ %r{ip}i
        #  Alt method.  Get list of ip_addresses on system and use the first.
        Socket.ip_address_list.each do |ip|
          if (ip.ip_address =~ /^(\d{1,3}).(\d{1,3}).(\d{1,3}).(\d{1,3})$/) && (ip.ip_address !~ /^127./)
            env[:nfs_host_ip] = ip.ip_address
            break
          end
        end
      end

      @nfs_host_ip = env[:nfs_host_ip].dup
      if env[:nfs_host_ip].nil?
        #  Something bad happened above.  Give up on NFS.
        env[:nfs_machine_ip] = nil
        env[:nfs_host_ip] = nil
        env[:nfs_valid_ids] = nil
        #  Give an error, but continue..
        env[:ui].info I18n.t('vagrant_vmware_esxi.vagrant_vmware_esxi_message',
                             message: 'Configure NFS   : ERROR, Unable to configure NFS on this machine.')
      end
      if (env[:machine].provider_config.debug =~ %r{ip}i) && !env[:nfs_host_ip].nil?
        puts "nfs_host_ip: #{env[:nfs_host_ip]}"
      end
    end
  else
    # Use Cached entries
    env[:nfs_machine_ip] = @nfs_machine_ip
    env[:nfs_host_ip] = @nfs_host_ip
    env[:nfs_valid_ids] = @@nfs_valid_ids
  end

  @app.call(env)
end

#read_state(env) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/vagrant-vmware-esxi/action/read_state.rb', line 68

def read_state(env)
  @logger.info('vagrant-vmware-esxi, read_state: start...')

  # Get config.
  machine = env[:machine]
  config = env[:machine].provider_config

  return :not_created if machine.id.to_i < 1

  @logger.info("vagrant-vmware-esxi, read_state: machine id: #{machine.id}")
  @logger.info("vagrant-vmware-esxi, read_state: current state: #{env[:machine_state]}")

  Net::SSH.start(config.esxi_hostname, config.esxi_username,
    password:                   config.esxi_password,
    port:                       config.esxi_hostport,
    keys:                       config.local_private_keys,
    timeout:                    20,
    number_of_password_prompts: 0,
    non_interactive:            true
  ) do |ssh|

    r = ssh.exec!("vim-cmd vmsvc/power.getstate #{machine.id} || return 254")
    power_status = r

    return :not_created if r.exitstatus == 254

    if power_status == "" or r.exitstatus != 0
      raise Errors::ESXiError,
            message: 'Unable to get VM Power State!'
    end

    if (power_status.include? "Powered on") && !env[:machine].ssh_info.nil?
      return :running
    elsif power_status.include? "Powered on"
      return :powered_on
    elsif power_status.include? "Powered off"
      return :powered_off
    elsif power_status.include? "Suspended"
      return :suspended
    end

    return nil
  end

  return :not_created
end