Class: Bard::SSHServer

Inherits:
Object
  • Object
show all
Defined in:
lib/bard/ssh_server.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri_string, **options) ⇒ SSHServer

Returns a new instance of SSHServer.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/bard/ssh_server.rb', line 8

def initialize(uri_string, **options)
  @uri_string = uri_string
  @options = options

  # Parse URI
  uri = parse_uri(uri_string)
  @user = uri.user || ENV['USER']
  @host = uri.host
  @port = uri.port ? uri.port.to_s : "22"

  # Store options
  @path = options[:path]
  @gateway = options[:gateway]
  @ssh_key = options[:ssh_key]
  @env = options[:env]
end

Instance Attribute Details

#envObject (readonly)

Returns the value of attribute env.



6
7
8
# File 'lib/bard/ssh_server.rb', line 6

def env
  @env
end

#gatewayObject (readonly)

Returns the value of attribute gateway.



6
7
8
# File 'lib/bard/ssh_server.rb', line 6

def gateway
  @gateway
end

#hostObject (readonly)

Returns the value of attribute host.



6
7
8
# File 'lib/bard/ssh_server.rb', line 6

def host
  @host
end

#pathObject (readonly)

Returns the value of attribute path.



6
7
8
# File 'lib/bard/ssh_server.rb', line 6

def path
  @path
end

#portObject (readonly)

Returns the value of attribute port.



6
7
8
# File 'lib/bard/ssh_server.rb', line 6

def port
  @port
end

#ssh_keyObject (readonly)

Returns the value of attribute ssh_key.



6
7
8
# File 'lib/bard/ssh_server.rb', line 6

def ssh_key
  @ssh_key
end

#userObject (readonly)

Returns the value of attribute user.



6
7
8
# File 'lib/bard/ssh_server.rb', line 6

def user
  @user
end

Instance Method Details

#connection_stringObject



33
34
35
# File 'lib/bard/ssh_server.rb', line 33

def connection_string
  "#{user}@#{host}"
end

#exec!(command) ⇒ Object



50
51
52
53
# File 'lib/bard/ssh_server.rb', line 50

def exec!(command)
  full_command = build_command(command)
  exec(full_command)
end

#hostnameObject



29
30
31
# File 'lib/bard/ssh_server.rb', line 29

def hostname
  host
end

#run(command) ⇒ Object



37
38
39
40
# File 'lib/bard/ssh_server.rb', line 37

def run(command)
  full_command = build_command(command)
  Open3.capture3(full_command)
end

#run!(command) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/bard/ssh_server.rb', line 42

def run!(command)
  output, error, status = run(command)
  if status.to_i.nonzero?
    raise Command::Error, "Command failed: #{command}\n#{error}"
  end
  output
end

#ssh_uriObject



25
26
27
# File 'lib/bard/ssh_server.rb', line 25

def ssh_uri
  URI("ssh://#{user}@#{host}:#{port}")
end