Class: Hydra::Sync

Inherits:
Object
  • Object
show all
Defined in:
lib/hydra/sync.rb

Overview

Hydra class responsible for delegate work down to workers.

The Sync is run once for each remote worker.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(worker_opts, sync_opts, verbose = false) ⇒ Sync

Create a new Sync instance to rsync source from the local machine to a remote worker

Arguments:

  • :worker_opts

    • A hash of the configuration options for a worker.

  • :sync

    • A hash of settings specifically for copying the source directory to be tested to the remote worked

  • :verbose

    • Set to true to see lots of Hydra output (for debugging)



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/hydra/sync.rb', line 22

def initialize(worker_opts, sync_opts, verbose = false)
  worker_opts ||= {}
  worker_opts.stringify_keys!
  @verbose = verbose
  @connect = worker_opts.fetch('connect') { raise "You must specify an SSH connection target" }
  @ssh_opts = worker_opts.fetch('ssh_opts') { "" }
  @remote_dir = worker_opts.fetch('directory') { raise "You must specify a remote directory" }

  return unless sync_opts
  sync_opts.stringify_keys!
  @local_dir = sync_opts.fetch('directory') { raise "You must specify a synchronization directory" }
  @exclude_paths = sync_opts.fetch('exclude') { [] }

  trace "Initialized"
  trace "  Worker: (#{worker_opts.inspect})"
  trace "  Sync:   (#{sync_opts.inspect})"

  sync
end

Instance Attribute Details

#connectObject (readonly)

Returns the value of attribute connect.



10
11
12
# File 'lib/hydra/sync.rb', line 10

def connect
  @connect
end

#remote_dirObject (readonly)

Returns the value of attribute remote_dir.



10
11
12
# File 'lib/hydra/sync.rb', line 10

def remote_dir
  @remote_dir
end

#ssh_optsObject (readonly)

Returns the value of attribute ssh_opts.



10
11
12
# File 'lib/hydra/sync.rb', line 10

def ssh_opts
  @ssh_opts
end

Class Method Details

.sync_many(opts) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/hydra/sync.rb', line 59

def self.sync_many opts
  opts.stringify_keys!
  config_file = opts.delete('config') { nil }
  if config_file
    opts.merge!(ProxyConfig.load(IO.read(config_file)).stringify_keys!)
  end
  @verbose = opts.fetch('verbose') { false }
  @sync = opts.fetch('sync') { {} }

  workers_opts = opts.fetch('workers') { [] }
  @remote_worker_opts = []
  workers_opts.each do |worker_opts|
    worker_opts.stringify_keys!
    if worker_opts['type'].to_s == 'ssh'
      @remote_worker_opts << worker_opts
    end
  end

  trace "Initialized"
  trace "  Sync:   (#{@sync.inspect})"
  trace "  Workers: (#{@remote_worker_opts.inspect})"

  Thread.abort_on_exception = true
  trace "Processing workers"
  @listeners = []
  @remote_worker_opts.each do |worker_opts|
    @listeners << Thread.new do
      begin
        trace "Syncing #{worker_opts.inspect}"
        Sync.new worker_opts, @sync, @verbose
      rescue 
        trace "Syncing failed [#{worker_opts.inspect}]"
      end
    end
  end
  
  @listeners.each{|l| l.join}
end

Instance Method Details

#syncObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/hydra/sync.rb', line 42

def sync
  #trace "Synchronizing with #{connect}\n\t#{sync_opts.inspect}"
  exclude_opts = @exclude_paths.inject(''){|memo, path| memo += "--exclude=#{path} "}

  rsync_command = [
    'rsync',
    '-avz',
    '--delete',
    exclude_opts,
    File.expand_path(@local_dir)+'/',
    "-e \"ssh #{@ssh_opts}\"",
    "#{@connect}:#{@remote_dir}"
  ].join(" ")
  trace rsync_command
  trace `#{rsync_command}`
end