From a71b26ac13ec97c5ba902faf538c05dfa1745f86 Mon Sep 17 00:00:00 2001 From: "Glenn Y. Rolland" Date: Wed, 2 Sep 2015 16:14:25 +0200 Subject: [PATCH] Split config part into map_manager. --- lib/qasim.rb | 9 ++++--- lib/qasim/cli.rb | 51 +++++++++++++++++++++++++++++------- lib/qasim/config.rb | 56 ++++++++++++---------------------------- lib/qasim/map.rb | 9 ++++--- lib/qasim/map/generic.rb | 21 ++++++++++++--- lib/qasim/map/ssh.rb | 36 ++++++++++++-------------- lib/qasim/map_manager.rb | 42 ++++++++++++++++++++++++++++++ 7 files changed, 143 insertions(+), 81 deletions(-) create mode 100644 lib/qasim/map_manager.rb diff --git a/lib/qasim.rb b/lib/qasim.rb index b20c3f8..b816fb9 100644 --- a/lib/qasim.rb +++ b/lib/qasim.rb @@ -6,9 +6,10 @@ def _ str end module Qasim - autoload :Config, 'qasim/config' - autoload :Map, 'qasim/map' - autoload :Ui, 'qasim/ui' - autoload :Cli, 'qasim/cli' + autoload :Config, 'qasim/config' + autoload :Map, 'qasim/map' + autoload :Ui, 'qasim/ui' + autoload :Cli, 'qasim/cli' + autoload :MapManager, 'qasim/map_manager' end diff --git a/lib/qasim/cli.rb b/lib/qasim/cli.rb index 327fc9a..02ed53c 100644 --- a/lib/qasim/cli.rb +++ b/lib/qasim/cli.rb @@ -1,23 +1,60 @@ require 'thor' +require 'pry' module Qasim class Cli < Thor + class_option :verbose, + type: :boolean, + aliases: '-v' + desc "init", "initialize user configuration" def init raise NotImplementedError end + + option :describe, + type: :boolean, + aliases: '-d' desc "list", "list" def list - @config.maps.sort do |mx,my| + @map_manager.sort do |mx,my| mx.host <=> my.host end.each do |map| puts map.name + if options[:describe] then + map.links.each do |link,where| + puts " - link: " + link + puts " to: " + where + puts " mounted: " + map.mounted? + end + end end end + desc "add MAP", "add a new map" + def add map_name + res = @config.maps.select do |map| + map.name == map_name + end + pp res + if not res.empty? then + puts "ERROR: name #{map_name} already exist !" + exit 1 + end + end + + desc "del MAP", "delete selected map" + def del map_name + res = @config.maps.select do |map| + map.name == map_name + end + pp res.first.filename + end + desc "mount MAPS", "mount selected maps" def mount + Map.select name: map_name raise NotImplementedError end @@ -27,19 +64,13 @@ module Qasim # def initialize *opts super - - @all_maps = nil @active_maps = nil - @config = Config.new - @config.parse_maps - #@config.parse_cmd_line ARGV - - @all_maps = {} + @map_manager = MapManager.new @config + @map_manager.parse_maps end - # create default map for each selected map # or default.map if none selected def run_init @@ -47,7 +78,7 @@ module Qasim def run_mount # asynchronous mount - @config.maps.select do |map| + @map_manager.select do |map| pp map map.online? # if map.available? then diff --git a/lib/qasim/config.rb b/lib/qasim/config.rb index 4ae9b44..5513330 100644 --- a/lib/qasim/config.rb +++ b/lib/qasim/config.rb @@ -7,48 +7,24 @@ require 'find' require 'qasim/map' require 'qasim/map/ssh' -module Qasim - class Config +class Qasim::Config + attr_reader :maps_active + attr_reader :maps + attr_reader :mount_dir + attr_reader :config_dir - attr_reader :maps_active - attr_reader :maps - attr_reader :mnt_dir - - def initialize - @mnt_dir = File.join ENV['HOME'], "mnt" - - @config_dir = APP_CONFIG_DIR - @config_file = nil - @maps = [] - @initialize_enable = false - @umount_enable = false - @target = nil - @verbose_enable = false - @debug = false - end - - # FIXME: move out of config - def parse_maps &blk - @maps = [] - map_dirs = [@config_dir, APP_SYSCONFIG_DIR].select{ |d| - File.exist? d and File.directory? d - } - - Find.find(*map_dirs) do |path| - # Skip unwanted files fast - next unless File.file? path - next unless File.basename(path) =~ /.map$/ - - begin - map = Map.from_file self, path - yield map if block_given? - maps.push map - rescue Map::ParseError - raise RuntimeError, "Error while parsing map file" - end - end - end + def initialize + @mnt_dir = File.join ENV['HOME'], "mnt" + @config_dir = Qasim::APP_CONFIG_DIR + @config_file = nil + @maps = [] + @initialize_enable = false + @umount_enable = false + @target = nil + @verbose_enable = false + @debug = false + @verbose = false end end diff --git a/lib/qasim/map.rb b/lib/qasim/map.rb index d85bcbb..ef7dcc9 100644 --- a/lib/qasim/map.rb +++ b/lib/qasim/map.rb @@ -108,8 +108,11 @@ module Qasim ; module Map f.puts "REMOTE_CYPHER=%s" % @cypher end end - module_function :from_file, - :env_substitute, - :class_for + + module_function :from_file + module_function :env_substitute + module_function :class_for + module_function :select + end ; end diff --git a/lib/qasim/map/generic.rb b/lib/qasim/map/generic.rb index 89a013b..166bcb8 100644 --- a/lib/qasim/map/generic.rb +++ b/lib/qasim/map/generic.rb @@ -4,9 +4,12 @@ module Qasim ; module Map end ; end class Qasim::Map::Generic + attr_reader :links + attr_reader :filename + attr_reader :name + def initialize app_config, params @app_config = app_config - #@params = params # FIXME: ? @links = params[:links] params.delete :links @@ -22,8 +25,6 @@ class Qasim::Map::Generic # # Format : # Hash of (name:Symbol * [value:Object, optional:Boolean]) - - def self.parameters { map_name: [nil , true], @@ -35,25 +36,37 @@ class Qasim::Map::Generic # # Test map liveness (connected & working) # + # MUST BE IMPLEMENTED BY SUBCLASSES + # def alive? + raise NotImplementedError end # # Test map # + # MUST BE IMPLEMENTED BY SUBCLASSES + # def mounted? + raise NotImplementedError end # # Mount # + # MUST BE IMPLEMENTED BY SUBCLASSES + # def mount + raise NotImplementedError end # # Umount # + # MUST BE IMPLEMENTED BY SUBCLASSES + # def umount + raise NotImplementedError end - end + diff --git a/lib/qasim/map/ssh.rb b/lib/qasim/map/ssh.rb index b7a46c1..760d19f 100644 --- a/lib/qasim/map/ssh.rb +++ b/lib/qasim/map/ssh.rb @@ -22,6 +22,7 @@ class Qasim::Map::Ssh < Qasim::Map::Generic super.merge({ ssh_user: { required: true }, # ex : foo ssh_password: { required: true }, # ex : bar + ssh_host: { required: true }, # ex : localhost, 127.0.0.1, ... ssh_port: { default: 80 }, # ex : 80, 8080, ... ssh_cypher: { default: CYPHER_AES256CBC } # ex : http, https }) @@ -36,38 +37,34 @@ class Qasim::Map::Ssh < Qasim::Map::Generic # def initialize *opts super - #@host = nil - #@port = 22 - #@user = nil - #@cypher = :arcfour end - # - # - # Test if map is connected / mounted - # - def mounted? + def mount_include? fs_type, local_path f = File.open("/proc/mounts") - sshfs_mounted = (f.readlines.select do |line| - line =~ /\s+fuse.sshfs\s+/ + fs_mounts = (f.readlines.select do |line| + line =~ /\s+#{fs_type}\s+/ end).map do |line| line.split(/\s+/)[1] end f.close + fs_mounts.include? local_path + end - score = 0 + # + # Test if map is connected / mounted + # + def mounted? + score = @links.size @links.each do |name, remotepath| - score += 1 - local_path = File.join @config.mnt_dir, name + local_path = File.join @app_config.mount_dir, name - if sshfs_mounted.include? local_path then + if mount_include?("fuse.sshfs", local_path) then score -= 1 end end - if score == 0 then return true - else return false - # FIXME: explain why ? - end + # FIXME: handle the case of partial mounts (for remount/umount) + return true if score == 0 + return false end @@ -122,7 +119,6 @@ class Qasim::Map::Ssh < Qasim::Map::Generic "-u", #umount "-z" ,#lazy localpath ] - #rdebug "command: %s" % [ cmd, cmd_args ].flatten.join(' ') if block_given? then yield name, cmd, cmd_args else diff --git a/lib/qasim/map_manager.rb b/lib/qasim/map_manager.rb new file mode 100644 index 0000000..542bb75 --- /dev/null +++ b/lib/qasim/map_manager.rb @@ -0,0 +1,42 @@ + +class Qasim::MapManager + # FIXME: move out of config + def initialize config + @maps = [] + @config = config + puts "MapManager::initialize" + end + + def sort &blk + @maps.sort &blk + end + + def select &blk + @maps.select &blk + end + + def each &blk + @maps.each &blk + end + + def parse_maps &blk + @maps = [] + map_dirs = [@config.config_dir, Qasim::APP_SYSCONFIG_DIR].select{ |d| + File.exist? d and File.directory? d + } + + Find.find(*map_dirs) do |path| + # Skip unwanted files fast + next unless File.file? path + next unless File.basename(path) =~ /.map$/ + + begin + map = Qasim::Map.from_file self, path + yield map if block_given? + @maps.push map + rescue Qasim::Map::ParseError + raise RuntimeError, "Error while parsing map file" + end + end + end +end