Method: Train.validate_backend

Defined in:
lib/train.rb

.validate_backend(credentials, default_transport_name = 'local') ⇒ Object

Examine the given credential information, and if all is well, return the transport name. TODO: this actually does no validation of the credential options whatsoever



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
# File 'lib/train.rb', line 147

def self.validate_backend(credentials, default_transport_name = 'local')
  return default_transport_name if credentials.nil?
  transport_name = credentials[:backend]

  # TODO: Determine if it is ever possible (or supported) for transport_name to be 'localhost'
  # TODO: After inspec/inspec/pull/3750 is merged, should be able to remove nil from the list
  if credentials[:sudo] && [nil, 'local', 'localhost'].include?(transport_name)
    fail Train::UserError, 'Sudo is only valid when running against a remote host. '\
      'To run this locally with elevated privileges, run the command with `sudo ...`.'
  end

  return transport_name if !transport_name.nil?

  if !credentials[:target].nil?
    # We should not get here, because if target_uri unpacking was successful,
    # it would have set credentials[:backend]
    fail Train::UserError, 'Cannot determine backend from target '\
         "configuration #{credentials[:target]}. Valid example: ssh://192.168.0.1"
  end

  if !credentials[:host].nil?
    fail Train::UserError, 'Host configured, but no backend was provided. Please '\
         'specify how you want to connect. Valid example: ssh://192.168.0.1'
  end

  credentials[:backend] = default_transport_name
end