2011-03-07 23:27:53 +00:00
|
|
|
|
2011-03-07 23:50:27 +00:00
|
|
|
require 'rubygems'
|
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
|
2011-03-07 23:50:27 +00:00
|
|
|
attr_reader :path,
|
|
|
|
:host,
|
|
|
|
:port,
|
|
|
|
:enable,
|
|
|
|
:user,
|
|
|
|
:map
|
2010-01-24 18:31:02 +00:00
|
|
|
|
|
|
|
class MapParseError < RuntimeError
|
|
|
|
end
|
2009-12-14 23:56:06 +00:00
|
|
|
|
2011-03-07 23:50:27 +00:00
|
|
|
CYPHER_ARCFOUR = :arcfour
|
|
|
|
CYPHER_AES256CBC = :"aes-256-cbc"
|
|
|
|
CYPHERS = [ CYPHER_ARCFOUR, CYPHER_AES256CBC ]
|
|
|
|
|
|
|
|
|
|
|
|
def initialize map_path
|
2009-06-12 15:49:30 +00:00
|
|
|
@path = map_path
|
2009-05-22 21:01:23 +00:00
|
|
|
@host = nil
|
|
|
|
@port = 22
|
2011-03-07 23:50:27 +00:00
|
|
|
@enable = false
|
2009-05-22 21:01:23 +00:00
|
|
|
@user = nil
|
2011-03-07 23:50:27 +00:00
|
|
|
@cypher = :arcfour
|
2010-01-13 23:46:48 +00:00
|
|
|
@maps = {}
|
2011-03-07 23:27:53 +00:00
|
|
|
@debug = false
|
2011-03-07 23:50:27 +00:00
|
|
|
|
|
|
|
self.load @path
|
2009-05-22 21:01:23 +00:00
|
|
|
end
|
|
|
|
|
2011-03-07 23:50:27 +00:00
|
|
|
def load path=nil
|
|
|
|
@path=path unless path.nil?
|
|
|
|
rdebug "Parsing map #{@path}"
|
|
|
|
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
|
|
|
|
|
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
|
2011-03-07 23:50:27 +00:00
|
|
|
if CYPHERS.map{|x| x.to_s}.include? $1 then
|
|
|
|
@host = $1.to_sym
|
2009-12-14 23:56:06 +00:00
|
|
|
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}"
|
2011-03-07 23:50:27 +00:00
|
|
|
when /^\s*$/,/^\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
|
2011-03-07 23:50:27 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def write path=nil
|
|
|
|
@path=path unless path.nil?
|
|
|
|
|
|
|
|
File.open @path, "w" do |f|
|
|
|
|
f.puts "REMOTE_USER=%s" % @user
|
|
|
|
f.puts "REMOTE_PORT=%s" % @port
|
|
|
|
f.puts "REMOTE_HOST=%s" % @host
|
|
|
|
f.puts "REMOTE_CYPHER=%s" % @cypher
|
|
|
|
end
|
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
|
|
|
|
|
2011-03-07 23:50:27 +00:00
|
|
|
def connected?
|
2009-12-14 23:56:06 +00:00
|
|
|
#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
|