lsync 2.0.2 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -30,4 +30,11 @@ require 'optparse'
30
30
  require 'open-uri'
31
31
 
32
32
  module LSync
33
+
34
+ def self.run_script(options = {}, &block)
35
+ script = LSync::Script.new(options, &block)
36
+
37
+ script.run!
38
+ end
39
+
33
40
  end
@@ -0,0 +1,62 @@
1
+
2
+ module LSync
3
+ class BasicController
4
+ def initialize(script, logger)
5
+ @script = script
6
+ @logger = logger
7
+ end
8
+
9
+ # The containing script.
10
+ attr :script
11
+
12
+ # The output logger.
13
+ attr :logger
14
+ end
15
+
16
+ # The server controller provides event handlers with a unified interface
17
+ # for dealing with servers and associated actions.
18
+ class ServerController < BasicController
19
+ def initialize(script, logger, server)
20
+ super(script, logger)
21
+
22
+ @server = server
23
+ end
24
+
25
+ # The current server.
26
+ attr :server
27
+
28
+ # Run a given shell script on the server.
29
+ def run!(*function)
30
+ action = Action.new(function)
31
+ action.run_on_server(@server, @logger)
32
+ end
33
+ end
34
+
35
+ class CopyController < BasicController
36
+ def initialize(script, logger, master, target)
37
+ super(script, logger)
38
+
39
+ @master = ServerController.new(script, logger, master)
40
+ @target = ServerController.new(script, logger, target)
41
+ end
42
+
43
+ # The master server controller (where the data is being copied from).
44
+ attr :master
45
+
46
+ # The target server controller (where the data is being copied to).
47
+ attr :target
48
+ end
49
+
50
+ # The directory controller provides event handlers with a unified interface
51
+ # for dealing with a particular backup in a particular directory.
52
+ class DirectoryController < CopyController
53
+ def initialize(script, logger, master, target, directory)
54
+ super(script, logger, master, target)
55
+
56
+ @directory = directory
57
+ end
58
+
59
+ # The directory that the data is being copied within.
60
+ attr :directory
61
+ end
62
+ end
@@ -39,7 +39,7 @@ module LSync
39
39
  @fired = nil
40
40
  @timeout = nil
41
41
 
42
- @callback = Proc.new(&block)
42
+ @callback = block
43
43
  @processing = Mutex.new
44
44
  end
45
45
 
@@ -5,71 +5,29 @@ require 'lsync/action'
5
5
  require 'lsync/method'
6
6
  require 'lsync/server'
7
7
  require 'lsync/directory'
8
+ require 'lsync/controller'
8
9
 
9
10
  module LSync
10
-
11
- # The server controller provides event handlers with a unified interface
12
- # for dealing with servers and associated actions.
13
- class ServerController
14
- def initialize(script, server, logger)
15
- @script = script
16
- @server = server
17
- @logger = logger
18
- end
19
-
20
- # The containing script.
21
- attr :script
22
-
23
- # The current server.
24
- attr :server
25
-
26
- # The output logger.
27
- attr :logger
28
-
29
- # Run a given shell script on the server.
30
- def run!(*function)
31
- action = Action.new(function)
32
- action.run_on_server(@server, @logger)
33
- end
34
- end
35
-
36
- # The directory controller provides event handlers with a unified interface
37
- # for dealing with a particular backup in a particular directory.
38
- class DirectoryController < ServerController
39
- def initialize(script, master, server, directory, logger)
40
- super(script, server, logger)
41
-
42
- @master = master
43
- @directory = directory
44
- end
45
-
46
- # The master server where data is being copied from.
47
- attr :master
48
-
49
- # The directory that the data is being copied within.
50
- attr :directory
51
- end
52
-
53
11
  # The main backup/synchronisation mechanism is the backup script. It specifies all
54
12
  # servers and directories, and these are then combined specifically to produce the
55
13
  # desired data replication behaviour.
56
14
  class Script
57
15
  include EventHandler
58
-
16
+
59
17
  def initialize(options = {}, &block)
60
18
  @logger = options[:logger] || Logger.new($stdout)
61
19
  @method = nil
62
-
20
+
63
21
  @servers = {}
64
22
  @directories = []
65
23
 
66
24
  @log = nil
67
-
25
+
68
26
  if block_given?
69
27
  instance_eval &block
70
28
  end
71
29
  end
72
-
30
+
73
31
  # Given a name, find out which server config matches it
74
32
  def find_named_server name
75
33
  if @servers.key? name
@@ -79,19 +37,19 @@ module LSync
79
37
  return @servers.values.find { |s| s["host"] == hostname }
80
38
  end
81
39
  end
82
-
40
+
83
41
  alias :[] :find_named_server
84
-
42
+
85
43
  # Find the master server based on the name #master= specified
86
44
  def find_master_server
87
45
  find_named_server(@master)
88
46
  end
89
-
47
+
90
48
  # Find out the config section for the current server
91
49
  def find_current_server
92
50
  master = find_master_server
93
51
  server = nil
94
-
52
+
95
53
  # Find out if the master server is local...
96
54
  if master.is_local?
97
55
  server = master
@@ -99,59 +57,61 @@ module LSync
99
57
  # Find a server config that specifies the local host
100
58
  server = @servers.values.find { |s| s.is_local? }
101
59
  end
102
-
60
+
103
61
  return server
104
62
  end
105
-
63
+
106
64
  # Register a server with the backup script.
107
- def server(name, &block)
65
+ def server(name)
108
66
  case name
109
67
  when Symbol
110
68
  host = "localhost"
111
69
  else
112
70
  host = name.to_s
113
71
  end
114
-
72
+
115
73
  server = Server.new(host)
116
-
74
+
117
75
  yield server if block_given?
118
-
76
+
119
77
  @servers[name] = server
120
78
  end
121
-
79
+
122
80
  # Backup a particular path (or paths).
123
- def backup(*paths, &block)
81
+ def copy(*paths)
124
82
  paths.each do |path|
125
83
  directory = Directory.new(path)
126
-
84
+
127
85
  yield directory if block_given?
128
-
86
+
129
87
  @directories << directory
130
88
  end
131
89
  end
132
90
 
91
+ alias :backup :copy
92
+
133
93
  # The script logger which will be provided all events when the script is run.
134
94
  attr :logger, true
135
-
95
+
136
96
  # The master server name (e.g. symbolic or host name)
137
97
  attr :master, true
138
-
98
+
139
99
  # A specific method which will perform the backup (e.g. an isntance of LSync::Method)
140
100
  attr :method, true
141
-
101
+
142
102
  # All servers which are participating in the backup process.
143
103
  attr :servers
144
-
104
+
145
105
  # All directories which may be synchronised.
146
106
  attr :directories
147
-
107
+
148
108
  # Log data (an +IO+) specific to the current script.
149
109
  attr :log
150
-
110
+
151
111
  # Run the backup process for all servers and directories specified.
152
112
  def run!
153
113
  start_time = Time.now
154
-
114
+
155
115
  # We buffer the log data so that if there is an error it is available to the notification sub-system
156
116
  @log = StringIO.new
157
117
  local_logger = Logger.new(@log)
@@ -173,24 +133,24 @@ module LSync
173
133
  logger.info "Master server is #{@master}..."
174
134
  end
175
135
 
176
- master_controller = ServerController.new(self, master, logger)
136
+ master_controller = ServerController.new(self, logger, master)
177
137
 
178
138
  self.try do
179
139
  method.try do
180
140
  master.try(master_controller) do
181
141
  logger.info "Running backups for server #{current}..."
182
-
142
+
183
143
  run_backups!(master, current, logger)
184
144
  end
185
145
  end
186
146
  end
187
-
147
+
188
148
  end_time = Time.now
189
149
  logger.info "Backup Completed (#{end_time - start_time}s)."
190
150
  end
191
-
151
+
192
152
  protected
193
-
153
+
194
154
  # This function runs the method for each directory and server combination specified.
195
155
  def run_backups!(master, current, logger)
196
156
  @servers.each do |name, server|
@@ -203,15 +163,15 @@ module LSync
203
163
  next
204
164
  end
205
165
 
206
- server_controller = ServerController.new(self, server, logger)
166
+ server_controller = CopyController.new(self, logger, master, server)
207
167
 
208
168
  server.try(server_controller) do
209
169
  @directories.each do |directory|
210
- directory_controller = DirectoryController.new(self, master, server, directory, logger)
170
+ directory_controller = DirectoryController.new(self, logger, master, server, directory)
211
171
 
212
172
  directory.try(directory_controller) do
213
173
  logger.info "\t" + ("Processing " + directory.to_s).rjust(20) + " : #{server}"
214
-
174
+
215
175
  method.run(master, server, directory)
216
176
  end
217
177
  end
@@ -16,8 +16,8 @@
16
16
  module LSync
17
17
  module VERSION #:nodoc:
18
18
  MAJOR = 2
19
- MINOR = 0
20
- TINY = 2
19
+ MINOR = 1
20
+ TINY = 0
21
21
 
22
22
  STRING = [MAJOR, MINOR, TINY].join('.')
23
23
  end
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 2
7
+ - 1
7
8
  - 0
8
- - 2
9
- version: 2.0.2
9
+ version: 2.1.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Samuel Williams
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-07-19 00:00:00 +12:00
17
+ date: 2011-07-26 00:00:00 +12:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -61,6 +61,7 @@ files:
61
61
  - lib/lsync/actions/generic/rotate
62
62
  - lib/lsync/actions/linux/disk
63
63
  - lib/lsync/actions/linux/terminal
64
+ - lib/lsync/controller.rb
64
65
  - lib/lsync/directory.rb
65
66
  - lib/lsync/error.rb
66
67
  - lib/lsync/event_handler.rb