Class: TrainSH::Session
- Inherits:
-
Object
- Object
- TrainSH::Session
- Extended by:
- Forwardable
- Defined in:
- lib/trainsh/session.rb
Instance Attribute Summary collapse
-
#backend ⇒ Object
readonly
Returns the value of attribute backend.
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
-
#env ⇒ Object
Returns the value of attribute env.
-
#exitcode ⇒ Object
Returns the value of attribute exitcode.
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#ping ⇒ Object
Returns the value of attribute ping.
-
#pwd ⇒ Object
Returns the value of attribute pwd.
Instance Method Summary collapse
- #connect(url) ⇒ Object
- #disconnect ⇒ Object
-
#initialize(url = nil) ⇒ Session
constructor
A new instance of Session.
- #reconnect ⇒ Object
- #run(command, skip_affixes: false) ⇒ Object
- #run_idle ⇒ Object
-
#url ⇒ Object
Redact password information.
Constructor Details
#initialize(url = nil) ⇒ Session
Returns a new instance of Session.
94 95 96 |
# File 'lib/trainsh/session.rb', line 94 def initialize(url = nil) connect(url) unless url.nil? end |
Instance Attribute Details
#backend ⇒ Object (readonly)
Returns the value of attribute backend.
90 91 92 |
# File 'lib/trainsh/session.rb', line 90 def backend @backend end |
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
90 91 92 |
# File 'lib/trainsh/session.rb', line 90 def connection @connection end |
#env ⇒ Object
Returns the value of attribute env.
92 93 94 |
# File 'lib/trainsh/session.rb', line 92 def env @env end |
#exitcode ⇒ Object
Returns the value of attribute exitcode.
92 93 94 |
# File 'lib/trainsh/session.rb', line 92 def exitcode @exitcode end |
#host ⇒ Object (readonly)
Returns the value of attribute host.
90 91 92 |
# File 'lib/trainsh/session.rb', line 90 def host @host end |
#ping ⇒ Object
Returns the value of attribute ping.
92 93 94 |
# File 'lib/trainsh/session.rb', line 92 def ping @ping end |
#pwd ⇒ Object
Returns the value of attribute pwd.
92 93 94 |
# File 'lib/trainsh/session.rb', line 92 def pwd @pwd end |
Instance Method Details
#connect(url) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/trainsh/session.rb', line 98 def connect(url) @url = url data = Train.unpack_target_from_uri(url) data.transform_values! { |val| CGI.unescape(val) } # TODO: Wire up with "messy" parameter data[:cleanup] = false backend = Train.create(data[:backend], data) return false unless backend @backend = data[:backend] @host = data[:host] @connection = backend.connection connection.wait_until_ready at_exit { disconnect } end |
#disconnect ⇒ Object
119 120 121 122 123 |
# File 'lib/trainsh/session.rb', line 119 def disconnect puts "Closing session #{url}" connection.close end |
#reconnect ⇒ Object
125 126 127 128 129 |
# File 'lib/trainsh/session.rb', line 125 def reconnect disconnect connect(url) end |
#run(command, skip_affixes: false) ⇒ Object
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/trainsh/session.rb', line 136 def run(command, skip_affixes: false) command = Command.new(command, @connection) # Save exit code command.postfix(exitcode_get) { |output| @exitcode = output.to_i } # Request UTF-8 instead of UTF-16 # command.prefix("$PSDefaultParameterValues['Out-File:Encoding'] = 'utf8'") if platform.windows? unless skip_affixes command.prefix(pwd_set) command.postfix(pwd_get) { |output| @pwd = output } command.prefix(env_set) command.postfix(env_get) { |output| @env = output } end # Discovery tasks command.prefix(host_get) { |output| @host = output } if host.nil? || host == 'unknown' command.run end |
#run_idle ⇒ Object
158 159 160 |
# File 'lib/trainsh/session.rb', line 158 def run_idle @ping = ::Benchmark.measure { run('#', skip_affixes: true) }.real * 1000 end |
#url ⇒ Object
Redact password information
132 133 134 |
# File 'lib/trainsh/session.rb', line 132 def url Addressable::URI.parse(@url).omit(:password).to_s end |