Class: SimpleSSH::Session

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/simplessh/session.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#file_mode

Constructor Details

#initialize(pointer) ⇒ Session

Returns a new instance of Session.



9
10
11
# File 'lib/simplessh/session.rb', line 9

def initialize(pointer)
  @session_pointer = pointer
end

Class Method Details

.from_pointer(pointer) ⇒ Object



5
6
7
# File 'lib/simplessh/session.rb', line 5

def self.from_pointer(pointer)
  self.new(pointer)
end

Instance Method Details

#authenticate_with_key(username, public_key, private_key, passphrase) ⇒ Object



20
21
22
23
24
25
# File 'lib/simplessh/session.rb', line 20

def authenticate_with_key(username, public_key, private_key, passphrase)
  either_pointer = SimpleSSH::Foreign.authenticate_with_key(@session_pointer, username, public_key, private_key, passphrase)
  SimpleSSH::Either.from_pointer!(either_pointer) do |session_pointer|
    SimpleSSH::Session.from_pointer(session_pointer)
  end
end

#authenticate_with_password(username, password) ⇒ Object



13
14
15
16
17
18
# File 'lib/simplessh/session.rb', line 13

def authenticate_with_password(username, password)
  either_pointer = SimpleSSH::Foreign.authenticate_with_password(@session_pointer, username, password)
  SimpleSSH::Either.from_pointer!(either_pointer) do |session_pointer|
    SimpleSSH::Session.from_pointer(session_pointer)
  end
end

#closeObject



70
71
72
# File 'lib/simplessh/session.rb', line 70

def close
  SimpleSSH::Foreign.close_session @session_pointer
end

#exec_command(cmd) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/simplessh/session.rb', line 27

def exec_command(cmd)
  either_pointer = SimpleSSH::Foreign.exec_command(@session_pointer, cmd)
  either_result = SimpleSSH::Either.from_pointer(either_pointer) do |result_pointer|
    SimpleSSH::Result.from_pointer(result_pointer)
  end
  SimpleSSH::Foreign.free_either_result(either_pointer)
  either_result
end

#finalizeObject



74
75
76
# File 'lib/simplessh/session.rb', line 74

def finalize
  close
end

#send_directory(source, target, &block) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/simplessh/session.rb', line 45

def send_directory(source, target, &block)
  block ||= Proc.new { |_| true }

  exec_command("mkdir -m #{file_mode(source).to_s(8)} #{target}").flat_map do |dir_creation_res|
    if dir_creation_res.success?
      Dir.open(source).reject { |p| p == '.' || p == '..' }.reduce(SimpleSSH::Either.return(nil)) do |prev, name|
        local_path  = File.join(source, name)
        remote_path = "#{target}/#{name}"

        if block.call local_path
          if File.file? local_path
            prev.flat_map { |s| send_file(file_mode(local_path), local_path, remote_path) }
          else
            prev.flat_map { |_| send_directory(local_path, remote_path, &block) }
          end
        else
          prev
        end
      end
    else
      SimpleSSH::Either::Left.new :could_not_create_directory
    end
  end.flat_map { |_| SimpleSSH::Either.return nil }
end

#send_file(mode, source, target) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/simplessh/session.rb', line 36

def send_file(mode, source, target)
  either_pointer = SimpleSSH::Foreign.send_file(@session_pointer, mode, source, target)
  either_count = SimpleSSH::Either.from_pointer(either_pointer) do |count_pointer|
    SimpleSSH::Foreign.count(count_pointer)
  end
  SimpleSSH::Foreign.free_either_count(either_pointer)
  either_count
end