Class: Chef::Knife::Bootstrap::TrainConnector

Inherits:
Object
  • Object
show all
Defined in:
lib/chef/knife/bootstrap/train_connector.rb

Instance Method Summary collapse

Constructor Details

#initialize(host_url, default_protocol, opts) ⇒ TrainConnector

Returns a new instance of TrainConnector.



38
39
40
41
42
# File 'lib/chef/knife/bootstrap/train_connector.rb', line 38

def initialize(host_url, default_protocol, opts)
  @host_url = host_url
  @default_protocol = default_protocol
  @opts_in = opts
end

Instance Method Details

#configObject



44
45
46
47
48
49
# File 'lib/chef/knife/bootstrap/train_connector.rb', line 44

def config
  @config ||= begin
                uri_opts = opts_from_uri(@host_url, @default_protocol)
                transport_config(@host_url, @opts_in.merge(uri_opts))
              end
end

#connect!TrueClass

Establish a connection to the configured host.

Returns:

  • (TrueClass)

    true if the connection could be established.

Raises:

  • (TrainError)
  • (TrainUserError)


68
69
70
71
72
# File 'lib/chef/knife/bootstrap/train_connector.rb', line 68

def connect!
  # Force connection to establish
  connection.wait_until_ready
  true
end

#connectionObject



51
52
53
54
55
56
57
58
59
# File 'lib/chef/knife/bootstrap/train_connector.rb', line 51

def connection
  @connection ||= begin
               Train.validate_backend(config)
               train = Train.create(config[:backend], config)
               # Note that the train connection is not currently connected
               # to the remote host, but it's ready to go.
               train.connection
             end
end

#del_file!(path) ⇒ Object

Force-deletes the file at “path” from the remote host.

Parameters:

  • path (String)

    The path of the file on the remote host



169
170
171
172
173
174
175
176
# File 'lib/chef/knife/bootstrap/train_connector.rb', line 169

def del_file!(path)
  if windows?
    run_command!("If (Test-Path \"#{path}\") { Remove-Item -Force -Path \"#{path}\" }")
  else
    run_command!("rm -f \"#{path}\"")
  end
  nil
end

#hostnameString

Returns the configured hostname.

Returns:

  • (String)

    the configured hostname



76
77
78
# File 'lib/chef/knife/bootstrap/train_connector.rb', line 76

def hostname
  config[:host]
end

#linux?Boolean

Answers the question, “Am I connected to a linux host?”

Returns:

  • (Boolean)

    true if the connected host is linux.



89
90
91
# File 'lib/chef/knife/bootstrap/train_connector.rb', line 89

def linux?
  connection.platform.linux?
end

#normalize_path(path) ⇒ String

normalizes path across OS’s - always use forward slashes, which Windows and *nix understand.

Parameters:

  • path (String)

    The path to normalize

Returns:

  • (String)

    the normalized path



185
186
187
# File 'lib/chef/knife/bootstrap/train_connector.rb', line 185

def normalize_path(path)
  path.tr("\\", "/")
end

#password_auth?Boolean

Answers the question, “is this connection configured for password auth?”

Returns:

  • (Boolean)

    true if the connection is configured with password auth



82
83
84
# File 'lib/chef/knife/bootstrap/train_connector.rb', line 82

def password_auth?
  config.key? :password
end

#run_command(command, &data_handler) ⇒ Train::Extras::CommandResult

Runs a command on the remote host.

published via ‘data_handler.call(data)`. This can allow callers to receive and render updates from remote command execution.

Parameters:

  • command (String)

    The command to run.

  • data_handler (Proc)

    An optional block. When provided, inbound data will be

Returns:

  • (Train::Extras::CommandResult)

    an object containing stdout, stderr, and exit_status



198
199
200
# File 'lib/chef/knife/bootstrap/train_connector.rb', line 198

def run_command(command, &data_handler)
  connection.run_command(command, &data_handler)
end

#run_command!(command, &data_handler) ⇒ Train::Extras::CommandResult

Runs a command the remote host

published via ‘data_handler.call(data)`. This can allow callers to receive and render updates from remote command execution.

Parameters:

  • command (String)

    The command to run.

  • data_handler (Proc)

    An optional block. When provided, inbound data will be

Returns:

  • (Train::Extras::CommandResult)

    an object containing stdout, stderr, and exit_status

Raises:

  • Chef::Knife::Bootstrap::RemoteExecutionFailed if an error occurs (non-zero exit status)



212
213
214
215
216
217
218
219
# File 'lib/chef/knife/bootstrap/train_connector.rb', line 212

def run_command!(command, &data_handler)
  result = run_command(command, &data_handler)
  if result.exit_status != 0
    raise RemoteExecutionFailed.new(hostname, command, result)
  end

  result
end

#temp_dirString

Creates a temporary directory on the remote host if it hasn’t already. Caches directory location. For *nix, it will ensure that the directory is owned by the logged-in user

Returns:

  • (String)

    the temporary path created on the remote host.



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/chef/knife/bootstrap/train_connector.rb', line 117

def temp_dir
  @tmpdir ||= if windows?
                run_command!(MKTEMP_WIN_COMMAND).stdout.split.last
              else
                # Get a 6 chars string using secure random
                # eg. /tmp/chef_XXXXXX.
                # Use mkdir to create TEMP dir to get rid of mktemp
                dir = "#{DEFAULT_REMOTE_TEMP}/chef_#{SecureRandom.alphanumeric(6)}"
                run_command!("mkdir -p '#{dir}'")
                # Ensure that dir has the correct owner.  We are possibly
                # running with sudo right now - so this directory would be owned by root.
                # File upload is performed over SCP as the current logged-in user,
                # so we'll set ownership to ensure that works.
                run_command!("chown #{config[:user]} '#{dir}'") if config[:sudo]

                dir
              end
end

#unix?Boolean

Note:

this will always return true for a linux host

Answers the question, “Am I connected to a unix host?”

because train classifies linux as a unix

Returns:

  • (Boolean)

    true if the connected host is unix or linux



99
100
101
# File 'lib/chef/knife/bootstrap/train_connector.rb', line 99

def unix?
  connection.platform.unix?
end

#upload_file!(local_path, remote_path) ⇒ Object

Uploads a file from “local_path” to “remote_path”

Parameters:

  • local_path (String)

    The path to a file on the local file system

  • remote_path (String)

    The destination path on the remote file system.

Returns:

  • NilClass



142
143
144
145
# File 'lib/chef/knife/bootstrap/train_connector.rb', line 142

def upload_file!(local_path, remote_path)
  connection.upload(local_path, remote_path)
  nil
end

#upload_file_content!(content, remote_path) ⇒ Object

Uploads the provided content into the file “remote_path” on the remote host.

Parameters:

  • content (String)

    The content to upload into remote_path

  • remote_path (String)

    The destination path on the remote file system.

Returns:

  • NilClass



153
154
155
156
157
158
159
160
161
162
163
# File 'lib/chef/knife/bootstrap/train_connector.rb', line 153

def upload_file_content!(content, remote_path)
  t = Tempfile.new("chef-content")
  t.binmode
  t << content
  t.close
  upload_file!(t.path, remote_path)
  nil
ensure
  t.close
  t.unlink
end

#windows?Boolean

Answers the question, “Am I connected to a Windows host?”

Returns:

  • (Boolean)

    true if the connected host is Windows



107
108
109
# File 'lib/chef/knife/bootstrap/train_connector.rb', line 107

def windows?
  connection.platform.windows?
end