Split config part into map_manager.

This commit is contained in:
Glenn Y. Rolland 2015-09-02 16:14:25 +02:00
parent 8f0c8701d9
commit a71b26ac13
7 changed files with 143 additions and 81 deletions

View file

@ -6,9 +6,10 @@ def _ str
end end
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'
autoload :MapManager, 'qasim/map_manager'
end end

View file

@ -1,23 +1,60 @@
require 'thor' require 'thor'
require 'pry'
module Qasim module Qasim
class Cli < Thor class Cli < Thor
class_option :verbose,
type: :boolean,
aliases: '-v'
desc "init", "initialize user configuration" desc "init", "initialize user configuration"
def init def init
raise NotImplementedError raise NotImplementedError
end end
option :describe,
type: :boolean,
aliases: '-d'
desc "list", "list" desc "list", "list"
def list def list
@config.maps.sort do |mx,my| @map_manager.sort do |mx,my|
mx.host <=> my.host mx.host <=> my.host
end.each do |map| end.each do |map|
puts map.name 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
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" desc "mount MAPS", "mount selected maps"
def mount def mount
Map.select name: map_name
raise NotImplementedError raise NotImplementedError
end end
@ -27,19 +64,13 @@ module Qasim
# #
def initialize *opts def initialize *opts
super super
@all_maps = nil
@active_maps = nil @active_maps = nil
@config = Config.new @config = Config.new
@config.parse_maps @map_manager = MapManager.new @config
#@config.parse_cmd_line ARGV @map_manager.parse_maps
@all_maps = {}
end end
# create default map for each selected map # create default map for each selected map
# or default.map if none selected # or default.map if none selected
def run_init def run_init
@ -47,7 +78,7 @@ module Qasim
def run_mount def run_mount
# asynchronous mount # asynchronous mount
@config.maps.select do |map| @map_manager.select do |map|
pp map pp map
map.online? map.online?
# if map.available? then # if map.available? then

View file

@ -7,48 +7,24 @@ require 'find'
require 'qasim/map' require 'qasim/map'
require 'qasim/map/ssh' require 'qasim/map/ssh'
module Qasim class Qasim::Config
class Config attr_reader :maps_active
attr_reader :maps
attr_reader :mount_dir
attr_reader :config_dir
attr_reader :maps_active def initialize
attr_reader :maps @mnt_dir = File.join ENV['HOME'], "mnt"
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
@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
end end

View file

@ -108,8 +108,11 @@ 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
:class_for module_function :env_substitute
module_function :class_for
module_function :select
end ; end end ; end

View file

@ -4,9 +4,12 @@ module Qasim ; module Map
end ; end end ; end
class Qasim::Map::Generic class Qasim::Map::Generic
attr_reader :links
attr_reader :filename
attr_reader :name
def initialize app_config, params def initialize app_config, params
@app_config = app_config @app_config = app_config
#@params = params # FIXME: ?
@links = params[:links] @links = params[:links]
params.delete :links params.delete :links
@ -22,8 +25,6 @@ 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],
@ -35,25 +36,37 @@ class Qasim::Map::Generic
# #
# Test map liveness (connected & working) # Test map liveness (connected & working)
# #
# MUST BE IMPLEMENTED BY SUBCLASSES
#
def alive? def alive?
raise NotImplementedError
end end
# #
# Test map # Test map
# #
# MUST BE IMPLEMENTED BY SUBCLASSES
#
def mounted? def mounted?
raise NotImplementedError
end end
# #
# Mount # Mount
# #
# MUST BE IMPLEMENTED BY SUBCLASSES
#
def mount def mount
raise NotImplementedError
end end
# #
# Umount # Umount
# #
# MUST BE IMPLEMENTED BY SUBCLASSES
#
def umount def umount
raise NotImplementedError
end end
end end

View file

@ -22,6 +22,7 @@ class Qasim::Map::Ssh < Qasim::Map::Generic
super.merge({ super.merge({
ssh_user: { required: true }, # ex : foo ssh_user: { required: true }, # ex : foo
ssh_password: { required: true }, # ex : bar ssh_password: { required: true }, # ex : bar
ssh_host: { required: true }, # ex : localhost, 127.0.0.1, ...
ssh_port: { default: 80 }, # ex : 80, 8080, ... ssh_port: { default: 80 }, # ex : 80, 8080, ...
ssh_cypher: { default: CYPHER_AES256CBC } # ex : http, https ssh_cypher: { default: CYPHER_AES256CBC } # ex : http, https
}) })
@ -36,38 +37,34 @@ class Qasim::Map::Ssh < Qasim::Map::Generic
# #
def initialize *opts def initialize *opts
super super
#@host = nil
#@port = 22
#@user = nil
#@cypher = :arcfour
end end
# def mount_include? fs_type, local_path
#
# Test if map is connected / mounted
#
def mounted?
f = File.open("/proc/mounts") f = File.open("/proc/mounts")
sshfs_mounted = (f.readlines.select do |line| fs_mounts = (f.readlines.select do |line|
line =~ /\s+fuse.sshfs\s+/ line =~ /\s+#{fs_type}\s+/
end).map do |line| end).map do |line|
line.split(/\s+/)[1] line.split(/\s+/)[1]
end end
f.close 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| @links.each do |name, remotepath|
score += 1 local_path = File.join @app_config.mount_dir, name
local_path = File.join @config.mnt_dir, name
if sshfs_mounted.include? local_path then if mount_include?("fuse.sshfs", local_path) then
score -= 1 score -= 1
end end
end end
if score == 0 then return true # FIXME: handle the case of partial mounts (for remount/umount)
else return false return true if score == 0
# FIXME: explain why ? return false
end
end end
@ -122,7 +119,6 @@ class Qasim::Map::Ssh < Qasim::Map::Generic
"-u", #umount "-u", #umount
"-z" ,#lazy "-z" ,#lazy
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

42
lib/qasim/map_manager.rb Normal file
View file

@ -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