Split config part into map_manager.
This commit is contained in:
parent
8f0c8701d9
commit
a71b26ac13
7 changed files with 143 additions and 81 deletions
|
@ -10,5 +10,6 @@ module Qasim
|
|||
autoload :Map, 'qasim/map'
|
||||
autoload :Ui, 'qasim/ui'
|
||||
autoload :Cli, 'qasim/cli'
|
||||
autoload :MapManager, 'qasim/map_manager'
|
||||
end
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -7,17 +7,16 @@ 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 :mnt_dir
|
||||
attr_reader :mount_dir
|
||||
attr_reader :config_dir
|
||||
|
||||
def initialize
|
||||
@mnt_dir = File.join ENV['HOME'], "mnt"
|
||||
|
||||
@config_dir = APP_CONFIG_DIR
|
||||
@config_dir = Qasim::APP_CONFIG_DIR
|
||||
@config_file = nil
|
||||
@maps = []
|
||||
@initialize_enable = false
|
||||
|
@ -25,30 +24,7 @@ module Qasim
|
|||
@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
|
||||
|
||||
@verbose = false
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
42
lib/qasim/map_manager.rb
Normal file
42
lib/qasim/map_manager.rb
Normal 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
|
Loading…
Reference in a new issue