Class: Chef::Knife::Bootstrap::TrainConnector
- Inherits:
-
Object
- Object
- Chef::Knife::Bootstrap::TrainConnector
- Defined in:
- lib/chef/knife/bootstrap/train_connector.rb
Instance Method Summary collapse
- #config ⇒ Object
-
#connect! ⇒ TrueClass
Establish a connection to the configured host.
- #connection ⇒ Object
-
#del_file!(path) ⇒ Object
Force-deletes the file at “path” from the remote host.
-
#hostname ⇒ String
The configured hostname.
-
#initialize(host_url, default_protocol, opts) ⇒ TrainConnector
constructor
A new instance of TrainConnector.
-
#linux? ⇒ Boolean
Answers the question, “Am I connected to a linux host?”.
-
#normalize_path(path) ⇒ String
normalizes path across OS’s - always use forward slashes, which Windows and *nix understand.
-
#password_auth? ⇒ Boolean
Answers the question, “is this connection configured for password auth?”.
-
#run_command(command, &data_handler) ⇒ Train::Extras::CommandResult
Runs a command on the remote host.
-
#run_command!(command, &data_handler) ⇒ Train::Extras::CommandResult
Runs a command the remote host.
-
#temp_dir ⇒ String
Creates a temporary directory on the remote host if it hasn’t already.
-
#unix? ⇒ Boolean
Answers the question, “Am I connected to a unix host?”.
-
#upload_file!(local_path, remote_path) ⇒ Object
Uploads a file from “local_path” to “remote_path”.
-
#upload_file_content!(content, remote_path) ⇒ Object
Uploads the provided content into the file “remote_path” on the remote host.
-
#windows? ⇒ Boolean
Answers the question, “Am I connected to a Windows host?”.
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
#config ⇒ Object
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.
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 |
#connection ⇒ Object
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.
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 |
#hostname ⇒ String
Returns 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?”
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.
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?”
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.
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.
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_dir ⇒ String
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
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
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
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”
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.
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?”
107 108 109 |
# File 'lib/chef/knife/bootstrap/train_connector.rb', line 107 def windows? connection.platform.windows? end |