Class: Checksum::Tools::Remote

Inherits:
Base
  • Object
show all
Defined in:
lib/checksum-tools/remote.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#host

Instance Method Summary collapse

Methods inherited from Base

#create_digest_file, #create_digest_files, #digest_filename, #digest_files, #verify_digest_file, #verify_digest_files, #verify_file

Constructor Details

#initialize(host, user, *args) ⇒ Remote

Returns a new instance of Remote.


17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/checksum-tools/remote.rb', line 17

def initialize(host, user, *args)
  super(*args)
  @host = host
  @user = user
  @digest_length_cache = {}
  unless (ssl = opts.delete(:openssl)).nil?
    begin
      remote_properties[:openssl] = ssl
    rescue ConfigurationError
      @remote_properties = { :openssl => ssl }
    end
    write_remote_properties
  end
end

Instance Attribute Details

#digest_typesObject (readonly)

Returns the value of attribute digest_types


14
15
16
# File 'lib/checksum-tools/remote.rb', line 14

def digest_types
  @digest_types
end

#optsObject (readonly)

Returns the value of attribute opts


15
16
17
# File 'lib/checksum-tools/remote.rb', line 15

def opts
  @opts
end

Instance Method Details

#digest_file(filename) ⇒ Object


79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/checksum-tools/remote.rb', line 79

def digest_file(filename)
  if file_exists?(filename)
    size = file_size(filename)
    yield(filename, size, 0) if block_given?
    output = {}
    digest_types.each { |key| 
      resp = exec! "#{openssl} dgst -#{key} $'#{filename.gsub(/[']/,'\\\\\'')}'"
      output[key] = resp.split(/\= /).last
    }
    yield(filename, size, size) if block_given?
    return output
  else
    raise Errno::ENOENT, filename
  end
end

#digest_length(type) ⇒ Object


71
72
73
74
75
76
77
# File 'lib/checksum-tools/remote.rb', line 71

def digest_length(type)
  if @digest_length_cache[type].nil?
    resp = exec! "echo - | #{openssl} dgst -#{type}"
    @digest_length_cache[type] = resp.split(/\s/).last.chomp.length
  end
  @digest_length_cache[type]
end

#digestsObject


65
66
67
68
69
# File 'lib/checksum-tools/remote.rb', line 65

def digests
  resp = ''
  resp = exec! "#{openssl} dgst -h 2>&1"
  resp.scan(/-(.+?)\s+to use the .+ message digest algorithm/).flatten.collect { |d| d.to_sym }
end

#exec!(cmd) ⇒ Object


53
54
55
56
57
58
59
60
61
62
63
# File 'lib/checksum-tools/remote.rb', line 53

def exec!(cmd)
  result = ''
  ssh do |ch|
    ch.exec("bash -l") do |ch2, success|
      ch2.on_data { |c,data| result += data }
      ch2.send_data "#{cmd}\n"
      ch2.send_data "exit\n"
    end
  end
  result.chomp
end

#opensslObject


32
33
34
# File 'lib/checksum-tools/remote.rb', line 32

def openssl
  remote_properties[:openssl] || remote_properties['openssl']
end

#sftpObject


36
37
38
39
40
# File 'lib/checksum-tools/remote.rb', line 36

def sftp
  @sftp ||= begin
    Net::SFTP.start(@host, @user, auth_methods: %w(publickey hostbased keyboard-interactive))
  end
end

#sshObject


42
43
44
45
46
47
48
49
50
51
# File 'lib/checksum-tools/remote.rb', line 42

def ssh
  result = sftp.session
  if block_given?
    channel = result.open_channel do |ch|
      yield(ch)
    end
    channel.wait
  end
  result
end