Class: Microsandbox::SshOps

Inherits:
Object
  • Object
show all
Defined in:
lib/microsandbox/ssh.rb

Overview

The SSH namespace for a sandbox, returned by Microsandbox::Sandbox#ssh. Use it to open a native in-process SSH client or prepare a reusable server endpoint.

Instance Method Summary collapse

Constructor Details

#initialize(native) ⇒ SshOps

Returns a new instance of SshOps.



209
210
211
# File 'lib/microsandbox/ssh.rb', line 209

def initialize(native)
  @native = native
end

Instance Method Details

#open_client(user: "root", term: nil, sftp: true) {|client| ... } ⇒ SshClient, Object

Open a native in-process SSH client to the sandbox. With a block, the client is yielded and closed when the block returns.

Parameters:

  • user (String) (defaults to: "root")

    guest user to authenticate as (default “root”)

  • term (String, nil) (defaults to: nil)

    TERM value for the session

  • sftp (Boolean) (defaults to: true)

    enable the SFTP subsystem (default true)

Yield Parameters:

Returns:



220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/microsandbox/ssh.rb', line 220

def open_client(user: "root", term: nil, sftp: true)
  opts = { "user" => user.to_s, "sftp" => sftp ? true : false }
  opts["term"] = term.to_s if term
  client = SshClient.new(@native.ssh_open_client(opts))
  return client unless block_given?

  begin
    yield client
  ensure
    client.close
  end
end

#prepare_server(host_key_path: nil, authorized_keys_path: nil, user: nil, sftp: true) ⇒ SshServer

Prepare a reusable SSH server endpoint for the sandbox.

Parameters:

  • host_key_path (String, nil) (defaults to: nil)

    PEM host key path (generated if omitted)

  • authorized_keys_path (String, nil) (defaults to: nil)

    authorized_keys file path

  • user (String, nil) (defaults to: nil)

    guest user connections run as

  • sftp (Boolean) (defaults to: true)

    enable the SFTP subsystem (default true)

Returns:



239
240
241
242
243
244
245
# File 'lib/microsandbox/ssh.rb', line 239

def prepare_server(host_key_path: nil, authorized_keys_path: nil, user: nil, sftp: true)
  opts = { "sftp" => sftp ? true : false }
  opts["host_key_path"] = host_key_path.to_s if host_key_path
  opts["authorized_keys_path"] = authorized_keys_path.to_s if authorized_keys_path
  opts["user"] = user.to_s if user
  SshServer.new(@native.ssh_prepare_server(opts))
end