2011-03-07 23:27:53 +00:00
|
|
|
|
|
|
|
require 'rdebug/base'
|
|
|
|
|
2009-05-22 21:01:23 +00:00
|
|
|
module SshfsMapper
|
2010-01-24 18:31:02 +00:00
|
|
|
|
2009-05-22 21:01:23 +00:00
|
|
|
class Map
|
2009-12-14 23:56:06 +00:00
|
|
|
attr_reader :path, :host, :port, :user, :map
|
2010-01-24 18:31:02 +00:00
|
|
|
|
|
|
|
class MapParseError < RuntimeError
|
|
|
|
end
|
2009-12-14 23:56:06 +00:00
|
|
|
|
2009-06-12 15:49:30 +00:00
|
|
|
def initialize( map_path )
|
|
|
|
@path = map_path
|
2009-05-22 21:01:23 +00:00
|
|
|
@host = nil
|
|
|
|
@port = 22
|
|
|
|
@user = nil
|
2010-01-13 23:46:48 +00:00
|
|
|
@engine = :arcfour
|
|
|
|
@maps = {}
|
2011-03-07 23:27:53 +00:00
|
|
|
@debug = false
|
2009-05-22 21:01:23 +00:00
|
|
|
end
|
|
|
|
|
2010-01-24 18:31:02 +00:00
|
|
|
def parse
|
2009-09-19 23:51:18 +00:00
|
|
|
puts "Parsing map #{@path}"
|
2010-01-13 23:46:48 +00:00
|
|
|
f = File.open( @path )
|
2010-01-24 18:31:02 +00:00
|
|
|
linect = 0
|
2010-01-13 23:46:48 +00:00
|
|
|
f.each do |line|
|
2010-01-24 18:31:02 +00:00
|
|
|
line = line.strip
|
|
|
|
linect += 1
|
|
|
|
|
|
|
|
#puts " [#{line}]"
|
2010-01-13 23:46:48 +00:00
|
|
|
case line
|
2010-01-24 18:31:02 +00:00
|
|
|
when /^\s*REMOTE_USER\s*=\s*(.*)\s*$/ then
|
2010-01-13 23:46:48 +00:00
|
|
|
@user = $1
|
2011-03-07 23:27:53 +00:00
|
|
|
rdebug "d: remote_user => #{$1}"
|
2010-01-24 18:31:02 +00:00
|
|
|
when /^\s*REMOTE_PORT\s*=\s*(.*)\s*$/ then
|
2010-01-13 23:46:48 +00:00
|
|
|
@port = $1.to_i
|
2011-03-07 23:27:53 +00:00
|
|
|
rdebug "d: remote_port => #{$1}"
|
2010-01-24 18:31:02 +00:00
|
|
|
when /^\s*REMOTE_HOST\s*=\s*(.*)\s*$/ then
|
2010-01-13 23:46:48 +00:00
|
|
|
@host = $1
|
2011-03-07 23:27:53 +00:00
|
|
|
rdebug "d: remote_host => #{$1}"
|
2010-01-24 18:31:02 +00:00
|
|
|
when /^\s*REMOTE_CYPHER\s*=\s*(.*)\s*$/ then
|
|
|
|
cyphers = ["arcfour", "aes-256-cbc"]
|
|
|
|
if cyphers.include? $1 then
|
2009-12-14 23:56:06 +00:00
|
|
|
@host = $1
|
|
|
|
end
|
2010-01-24 18:31:02 +00:00
|
|
|
when /^\s*MAP\s*=\s*(.*)\s+(.*)\s*$/ then
|
2010-01-13 23:46:48 +00:00
|
|
|
@maps[$1] = $2
|
2011-03-07 23:27:53 +00:00
|
|
|
rdebug "d: map #{$1} => #{$2}"
|
2010-01-24 18:31:02 +00:00
|
|
|
when /^\s*$/ then
|
2011-03-07 23:27:53 +00:00
|
|
|
rdebug "d: dropping empty line"
|
2010-01-13 23:46:48 +00:00
|
|
|
else
|
2010-01-24 18:31:02 +00:00
|
|
|
raise MapParseError, "parse error at #{@path}:#{linect}"
|
2009-12-14 23:56:06 +00:00
|
|
|
end
|
|
|
|
end
|
2010-01-13 23:46:48 +00:00
|
|
|
f.close
|
|
|
|
#
|
2009-12-14 23:56:06 +00:00
|
|
|
end
|
|
|
|
|
2011-03-07 23:27:53 +00:00
|
|
|
def alive?
|
2009-12-14 23:56:06 +00:00
|
|
|
#FIXME: test liveness
|
|
|
|
end
|
|
|
|
|
|
|
|
def is_connected?
|
|
|
|
#FIXME test if connected / mounted
|
2009-09-19 23:51:18 +00:00
|
|
|
end
|
|
|
|
|
2011-03-07 23:27:53 +00:00
|
|
|
def connect
|
2010-01-13 23:46:48 +00:00
|
|
|
puts "[#{File.basename @path}] Connecting..."
|
|
|
|
puts " #{@user}@#{@host}:#{@port}"
|
2010-01-24 21:00:26 +00:00
|
|
|
puts " maps = %s" % @maps.map{ |k,v| "%s => %s" % [ k, v ] }.join(', ')
|
2009-09-19 23:51:18 +00:00
|
|
|
# do something
|
|
|
|
# test server connection
|
|
|
|
# mount
|
|
|
|
end
|
|
|
|
|
2011-03-07 23:27:53 +00:00
|
|
|
def disconnect
|
2009-09-19 23:51:18 +00:00
|
|
|
puts "Disconnecting map #{@path}"
|
|
|
|
# umount
|
2009-06-12 15:49:30 +00:00
|
|
|
end
|
2009-05-22 21:01:23 +00:00
|
|
|
end
|
2009-04-03 16:27:49 +00:00
|
|
|
end
|