Sha256: f238157b9b1a97980355ccc23eac54c417982893f054e5bb824b44085252ffc8

Contents?: true

Size: 1.79 KB

Versions: 4

Compression:

Stored size: 1.79 KB

Contents

require 'rbfs/args'
require 'rbfs/config'
require "rbfs/host_parser"
require "rbfs/rsync"
require "rbfs/futures"
require "rbfs/logger"

module Rbfs
  class Command
    attr_accessor :config

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

    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

    def logger
      @config[:logger]
    end

    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

    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

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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rbfs-0.0.6 lib/rbfs/command.rb
rbfs-0.0.5 lib/rbfs/command.rb
rbfs-0.0.4 lib/rbfs/command.rb
rbfs-0.0.3 lib/rbfs/command.rb