Class: VagrantPlugins::ESXi::Action::SetESXiPassword

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

Overview

This action set the global variable esxi_password and attempt to login to the esxi server to verify connectivity.

Instance Method Summary collapse

Constructor Details

#initialize(app, _env) ⇒ SetESXiPassword

Returns a new instance of SetESXiPassword.



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

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

Instance Method Details

#call(env) ⇒ Object



16
17
18
19
# File 'lib/vagrant-vmware-esxi/action/esxi_password.rb', line 16

def call(env)
  set_esxi_password(env)
  @app.call(env)
end

#set_esxi_password(env) ⇒ Object



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
67
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/vagrant-vmware-esxi/action/esxi_password.rb', line 21

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

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

  #
  #  Set global variable config.esxi_password
  #
  if config.encoded_esxi_password.nil?
    if config.esxi_password =~ %r{^prompt:}i
      #
      #  Prompt for password
      #
      password_type = 'prompt'
      begin
        print "#{config.esxi_username}@#{config.esxi_hostname} password:"
        config.esxi_password = STDIN.noecho(&:gets).chomp
        puts ''
      rescue
        begin
          #  There is something funky with STDIN... (unsupported console)
          puts ''
          puts ''
          puts 'Error! Your console doesn\'t support hiding input. We\'ll ask for'
          puts 'input again below, but we WILL NOT be able to hide input. If this'
          puts 'is a problem for you, ctrl-C / [ENTER] to exit and fix your stdin.'
          puts ''
          print "#{config.esxi_username}@#{config.esxi_hostname} password:"
          config.esxi_password = $stdin.readline().chomp
          puts ''
        rescue
          raise Errors::ESXiError,
                message: 'Prompt for password error???'
        end
      end
    elsif config.esxi_password =~ %r{^env:}i
      #
      #  Get pw from environment variable
      #
      password_type = 'env'
      esxi_password_env = config.esxi_password.gsub(/env:/i, '').chomp
      if esxi_password_env.length < 1
        esxi_password_env = 'esxi_password'
      end
      begin
        stdin_pw = ENV[esxi_password_env]
        config.esxi_password = stdin_pw.chomp
      rescue
        raise Errors::ESXiError,
              message: "Unable to read environment variable: #{esxi_password_env}"
      end
    elsif config.esxi_password =~ %r{^file:}i
      #
      #  Get password from file
      #
      password_type = 'file'
      esxi_password_file = config.esxi_password.gsub(/file:/i, '').chomp
      if esxi_password_file.empty?
        esxi_password_file = '~/.esxi_password'
      end
      esxi_password_file = File.expand_path(esxi_password_file)
      #  Get password from file
      begin
        if File.file?(esxi_password_file)
          file_pw = ''
          fh = File.open(File.expand_path(esxi_password_file))
          file_pw = fh.readline
          fh.close
          config.esxi_password = file_pw.chomp
        else
          raise Errors::ESXiError, message: "Unable to open #{esxi_password_file}"
        end
      rescue
        raise Errors::ESXiError, message: "Unable to open #{esxi_password_file}"
      end
    elsif config.esxi_password =~ %r{^key:}i
      #
      #  use ssh keys
      #
      password_type = 'key'
      esxi_password_key = config.esxi_password.gsub(/key:/i, '').chomp
      config.esxi_password = ''
      unless esxi_password_key.empty?
        config.local_private_keys = esxi_password_key
      end
    else
      # Use plain text password from config
      password_type = 'plain'
    end

    #
    #  Encode special characters in PW
    #
    config.encoded_esxi_password = config.esxi_password.gsub('%', '%25').\
      gsub(' ', '%20').\
      gsub('!', '%21').\
      gsub('"', '%22').\
      gsub('#', '%23').\
      gsub('$', '%24').\
      gsub('&', '%26').\
      gsub('\'','%27').\
      gsub('(', '%28').\
      gsub(')', '%29').\
      gsub('*', '%2a').\
      gsub('+', '%2b').\
      gsub(',', '%2c').\
      gsub('-', '%2d').\
      gsub('.', '%2e').\
      gsub('/', '%2f').\
      gsub(':', '%3a').\
      gsub(';', '%3b').\
      gsub('<', '%3c').\
      gsub('=', '%3d').\
      gsub('>', '%3e').\
      gsub('?', '%3f').\
      gsub('@', '%40').\
      gsub('[', '%5b').\
      gsub('\\','%5c').\
      gsub(']', '%5d').\
      gsub('^', '%5e').\
      gsub('_', '%5f').\
      gsub('`', '%60').\
      gsub('{', '%7b').\
      gsub('|', '%7c').\
      gsub('}', '%7d').\
      gsub('~', '%7e')

    @logger.info('vagrant-vmware-esxi, connect_esxi: local_private_keys: '\
                 "#{config.local_private_keys}")

    #
    #  Test ESXi host connectivity
    #
    begin
      puts "RUBY_PLATFORM: #{RUBY_PLATFORM}" if config.debug =~ %r{true}i
      puts "Testing esxi connectivity" if config.debug =~ %r{ip}i
      puts "esxi_ssh_keys: #{config.local_private_keys}" if config.debug =~ %r{password}i
      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|

        esxi_version = ssh.exec!('vmware -v')
        ssh.close

        @logger = Log4r::Logger.new('vagrant_vmware_esxi::action::set_esxi_password')
        if (config.debug =~ %r{true}i) && $showVersionFlag.nil?
          $showVersionFlag = true
          env[:ui].info I18n.t('vagrant_vmware_esxi.vagrant_vmware_esxi_message',
                               message: "ESXi version    : #{esxi_version}")
        end
        if esxi_version !~ %r{^vmware esxi}i
          @logger.info('vagrant-vmware-esxi, set_esxi_password: '\
                       "ESXi version: #{esxi_version}")
          raise Errors::ESXiError,
                message: 'Unable to connect to ESXi host!'\
                        "Error: #{esxi_version}"
        end
      end
    rescue
      if password_type == 'prompt'
        access_error_message = 'Password incorrect.'
      elsif password_type == 'env'
        access_error_message = "Verify env:#{esxi_password_env}"
      elsif password_type == 'file'
        access_error_message = "Verify file:#{esxi_password_file}"
      elsif password_type == 'key'
        access_error_message = "Verify key:#{config.local_private_keys}"
      else
        access_error_message = 'Verify password in Vagrantfile is correct.'
      end

      env[:ui].info I18n.t('vagrant_vmware_esxi.vagrant_vmware_esxi_message',
                           message: "ESXi host access : #{access_error_message}")

      @logger.info('vagrant-vmware-esxi, set_esxi_password: '\
                  "ESXi host access : #{access_error_message}")

      raise Errors::ESXiError,
            message: 'Unable to connect to ESXi host!'
    end
  end
end