Class: Rbfs::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/rbfs/command.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCommand

Returns a new instance of Command.



12
13
14
15
16
# File 'lib/rbfs/command.rb', line 12

def initialize
  @config = parse_config
  logger.critical "No hosts file specified" unless config[:hosts]
  logger.critical "Root path not specified" unless config[:root]
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



10
11
12
# File 'lib/rbfs/command.rb', line 10

def config
  @config
end

Instance Method Details

#loggerObject



31
32
33
# File 'lib/rbfs/command.rb', line 31

def logger
  @config[:logger]
end

#parse_configObject



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/rbfs/command.rb', line 18

def parse_config
  config = {}
  cmdline_args = Rbfs::Args.new.parse
  if cmdline_args[:config]
    config_args = Rbfs::Config.new(cmdline_args[:config]).parse
    config = config_args.merge(cmdline_args)
  else
    config = cmdline_args
  end
  config[:logger] = Logger.new(config)
  config
end

#syncObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/rbfs/command.rb', line 35

def sync
  success = true
  results = sync_hosts
  results.each do |host, result|
    if result[:exitcode] != 0
      logger.error "#{host}: #{result[:exitcode].to_i}"
    else
      logger.info "#{host}: #{result[:exitcode].to_i}"
    end
    result[:output].split("\n").each do |line|
      if result[:exitcode] != 0
        logger.error "  | #{line}"
      else
        logger.info "  | #{line}"
      end
    end
    success = false if result[:exitcode] != 0
  end
  success
end

#sync_host(host) ⇒ Object



65
66
67
68
69
70
71
72
73
# File 'lib/rbfs/command.rb', line 65

def sync_host(host)
  if config[:threaded]
    Future.new do
      Rsync.new(config, host).sync
    end
  else
    Rsync.new(config, host).sync
  end
end

#sync_hostsObject



56
57
58
59
60
61
62
63
# File 'lib/rbfs/command.rb', line 56

def sync_hosts
  config[:root] = File.join(config[:root], config[:subpath]) if config[:subpath]
  logger.info "Syncing #{config[:root]}..."
  hosts = Rbfs::HostParser.new(File.open(config[:hosts]))
  hosts.collect do |host|
    [host, sync_host(host)]
  end
end