Added map validations.

This commit is contained in:
Glenn Y. Rolland 2015-08-12 09:29:59 +02:00
parent b835a8f587
commit f6d90e96f3
6 changed files with 93 additions and 35 deletions

View file

@ -2,9 +2,9 @@
require 'qasim/constants' require 'qasim/constants'
module Qasim module Qasim
autoload :Config, 'qasim/config' autoload :Config, 'qasim/config'
autoload :Map, 'qasim/map' autoload :Map, 'qasim/map'
autoload :Ui, 'qasim/ui' autoload :Ui, 'qasim/ui'
autoload :Cli, 'qasim/cli' autoload :Cli, 'qasim/cli'
end end

View file

@ -1,12 +1,24 @@
require 'fileutils' require 'fileutils'
require 'qasim/map/generic' require 'qasim/map/generic'
require 'qasim/map/ssh'
require 'qasim/map/webdav'
module Qasim ; module Map module Qasim ; module Map
class ParseError < RuntimeError ; end class ParseError < RuntimeError ; end
class ConnectError < RuntimeError ; end class ConnectError < RuntimeError ; end
def class_for type
plugin = nil
ObjectSpace.each_object(Class) do |cls|
if cls < Qasim::Map::Generic then
plugin = cls if cls.handles.include? type.to_sym
end
end
plugin
end
# #
# replace magic values withing map lines # replace magic values withing map lines
# #
@ -42,7 +54,9 @@ module Qasim ; module Map
# Load description from file and create a Map object # Load description from file and create a Map object
# #
def from_file appcfg, filename def from_file appcfg, filename
config = {} config = {
type: :ssh # for config V1, we assume SSHFS
}
map = nil map = nil
f = File.open filename f = File.open filename
@ -93,6 +107,8 @@ module Qasim ; module Map
f.puts "REMOTE_CYPHER=%s" % @cypher f.puts "REMOTE_CYPHER=%s" % @cypher
end end
end end
module_function :from_file, :env_substitute module_function :from_file,
:env_substitute,
:class_for
end ; end end ; end

View file

@ -1,11 +1,10 @@
require 'fileutils' require 'fileutils'
require 'qasim/map'
module Qasim ; module Map module Qasim ; module Map
end ; end end ; end
class Qasim::Map::Generic class Qasim::Map::Generic
def initialize config def initialize params
end end
@ -14,6 +13,7 @@ class Qasim::Map::Generic
# Format : # Format :
# Hash of (name:Symbol * [value:Object, optional:Boolean]) # Hash of (name:Symbol * [value:Object, optional:Boolean])
def self.parameters def self.parameters
{ {
map_name: [nil , true], map_name: [nil , true],
@ -22,4 +22,28 @@ class Qasim::Map::Generic
} }
end end
#
# Test map liveness (connected & working)
#
def alive?
end
#
# Test map
#
def mounted?
end
#
# Mount
#
def mount
end
#
# Umount
#
def umount
end
end end

View file

@ -12,13 +12,20 @@ class Qasim::Map::Ssh < Qasim::Map::Generic
:name :name
CYPHER_ARCFOUR = :arcfour CYPHER_ARCFOUR = :arcfour
CYPHER_AES256CBC = "aes-256-cbc".to_sym CYPHER_AES256CBC = :"aes-256-cbc"
CYPHERS = [ CYPHER_ARCFOUR, CYPHER_AES256CBC ] CYPHERS = [
CYPHER_ARCFOUR,
CYPHER_AES256CBC
]
def self.parameters def self.parameters
super super
end end
def self.handles
[ :ssh, :sshfs ]
end
# #
# Set defaults properties for maps # Set defaults properties for maps
# #
@ -37,21 +44,11 @@ class Qasim::Map::Ssh < Qasim::Map::Generic
self.load @path self.load @path
end end
# #
# Test map liveness (how ?)
# FIXME: not implemented
#
def online?
#rdebug "testing online? %s " % self.inspect
#FIXME: test liveness
end
# #
# Test if map is connected / mounted # Test if map is connected / mounted
# #
def connected? def mounted?
f = File.open("/proc/mounts") f = File.open("/proc/mounts")
sshfs_mounted = (f.readlines.select do |line| sshfs_mounted = (f.readlines.select do |line|
line =~ /\s+fuse.sshfs\s+/ line =~ /\s+fuse.sshfs\s+/
@ -79,14 +76,7 @@ class Qasim::Map::Ssh < Qasim::Map::Generic
# #
# Connect map # Connect map
# #
def connect &block def mount &block
puts "[#{File.basename @path}] Connecting..."
puts " #{@user}@#{@host}:#{@port}"
#puts " links = %s" % @links.map{ |k,v| "%s => %s" % [ k, v ] }.join(', ')
# do something
# test server connection
# mount
#
# FIXME: test connexion with Net::SSH + timeout or ask password # FIXME: test connexion with Net::SSH + timeout or ask password
@links.each do |name, remotepath| @links.each do |name, remotepath|
localpath = File.join ENV['HOME'], "mnt", name localpath = File.join ENV['HOME'], "mnt", name
@ -111,7 +101,6 @@ class Qasim::Map::Ssh < Qasim::Map::Generic
"-o","Port=%s" % @port, "-o","Port=%s" % @port,
"%s@%s:%s" % [@user,@host,remotepath], "%s@%s:%s" % [@user,@host,remotepath],
localpath ] localpath ]
#rdebug "command: %s" % [ cmd, cmd_args ].flatten.join(' ')
if block_given? then if block_given? then
yield name, cmd, cmd_args yield name, cmd, cmd_args
else else
@ -127,8 +116,7 @@ class Qasim::Map::Ssh < Qasim::Map::Generic
# #
# Disconnect map # Disconnect map
# #
def disconnect &block def umount &block
puts "Disconnecting map #{@path}"
@links.each do |name, remotepath| @links.each do |name, remotepath|
localpath = File.join ENV['HOME'], "mnt", name localpath = File.join ENV['HOME'], "mnt", name
cmd = "fusermount" cmd = "fusermount"
@ -148,3 +136,4 @@ class Qasim::Map::Ssh < Qasim::Map::Generic
end end
end end
end end

View file

@ -6,7 +6,7 @@ class Qasim::Map::Webdav < Qasim::Map::Generic
def initialize def initialize
end end
def requirements def self.parameters
req = super req = super
req << :webdav_user # ex: foo req << :webdav_user # ex: foo
req << :webdav_password # ex: bar req << :webdav_password # ex: bar
@ -14,5 +14,9 @@ class Qasim::Map::Webdav < Qasim::Map::Generic
req << :webdav_protocol # ex: http, https req << :webdav_protocol # ex: http, https
req req
end end
def self.handles
[ :webdav, :fusedav ]
end
end end

View file

@ -1,9 +1,17 @@
require_relative 'spec_helper' require_relative 'spec_helper'
require 'minitest/spec'
require 'qasim/map' require 'qasim/map'
require 'securerandom'
class Qasim::Map::Fake < Qasim::Map::Generic
def self.handles
[:fake]
end
end
describe Qasim::Map do describe Qasim::Map do
describe 'env_substitute' do describe 'env_substitute' do# {{{
it "returns a normal string unchanged" do it "returns a normal string unchanged" do
str = "5cb0c49325df2d526116ef7b49eb7329" str = "5cb0c49325df2d526116ef7b49eb7329"
assert_equal Qasim::Map.env_substitute(str), str assert_equal Qasim::Map.env_substitute(str), str
@ -35,10 +43,27 @@ describe Qasim::Map do
ref = "SOMETHING = OK" ref = "SOMETHING = OK"
assert_equal ref, Qasim::Map.env_substitute(str) assert_equal ref, Qasim::Map.env_substitute(str)
end end
end# }}}
describe 'class_for' do
it 'must return a class for given type' do
expected = Qasim::Map::Fake
result = Qasim::Map.class_for 'fake'
assert_equal expected, result
end
it 'must return nil when no type found' do
random_string = SecureRandom.hex
expected = nil
result = Qasim::Map.class_for random_string
assert_equal expected, result
end
end end
describe 'from_file' do describe 'from_file' do
end end
end end