Class: Synco::Script
- Inherits:
-
Controller
- Object
- Controller
- Synco::Script
- Defined in:
- lib/synco/script.rb
Overview
The main backup/synchronisation mechanism is the backup script. It specifies all servers and directories, and these are then combined specifically to produce the desired data replication behaviour.
Instance Attribute Summary collapse
-
#directories(*paths, **options, &block) ⇒ Object
(also: #copy, #backup, #sync)
All directories which may be synchronised.
-
#master ⇒ Object
The master server name (e.g. symbolic or host name).
-
#method ⇒ Object
A specific method which will perform the backup (e.g. an instance of Synco::Method).
-
#servers ⇒ Object
All servers which are participating in the backup process.
Attributes inherited from Controller
Instance Method Summary collapse
- #current_server ⇒ Object
-
#find_current_server ⇒ Object
Find the server that matches the current machine.
-
#find_named_server(name) ⇒ Object
(also: #[])
Given a name, find out which server config matches it.
- #freeze ⇒ Object
-
#initialize(method: nil, servers: {}, directories: [], master: :master, logger: nil) ⇒ Script
constructor
A new instance of Script.
- #localhost?(name) ⇒ Boolean
-
#master_server ⇒ Object
The master server based on the name #master= specified.
- #resolve_name(name) ⇒ Object
- #running_on_master? ⇒ Boolean
-
#server(*arguments, **options, &block) ⇒ Object
Register a server with the backup script.
Methods inherited from Controller
#abort!, build, #fire, #on, #try
Constructor Details
#initialize(method: nil, servers: {}, directories: [], master: :master, logger: nil) ⇒ Script
Returns a new instance of Script.
33 34 35 36 37 38 39 40 |
# File 'lib/synco/script.rb', line 33 def initialize(method: nil, servers: {}, directories: [], master: :master, logger: nil) super() @method = method @servers = servers @directories = directories @master = master end |
Instance Attribute Details
#directories(*paths, **options, &block) ⇒ Object Also known as: copy, backup, sync
All directories which may be synchronised.
104 105 106 107 108 |
# File 'lib/synco/script.rb', line 104 def directories(*paths, **, &block) paths.each do |path| @directories << Directory.build(path, **, &block) end end |
#master ⇒ Object
The master server name (e.g. symbolic or host name)
115 116 117 |
# File 'lib/synco/script.rb', line 115 def master @master end |
#method ⇒ Object
A specific method which will perform the backup (e.g. an instance of Synco::Method)
118 119 120 |
# File 'lib/synco/script.rb', line 118 def method @method end |
#servers ⇒ Object
All servers which are participating in the backup process.
121 122 123 |
# File 'lib/synco/script.rb', line 121 def servers @servers end |
Instance Method Details
#current_server ⇒ Object
93 94 95 |
# File 'lib/synco/script.rb', line 93 def current_server @current_server ||= find_current_server end |
#find_current_server ⇒ Object
Find the server that matches the current machine
82 83 84 85 86 87 88 89 90 91 |
# File 'lib/synco/script.rb', line 82 def find_current_server # There might be the case that the the local machine is both the master server and the backup server.. # thus we check first if the master server is the local machine: if master_server and localhost?(master_server.host) @master_server else # Find a server config that specifies the local host @servers.values.find{|server| localhost?(server.host)} end || Server.new('localhost') end |
#find_named_server(name) ⇒ Object Also known as: []
Given a name, find out which server config matches it.
65 66 67 68 69 70 71 72 |
# File 'lib/synco/script.rb', line 65 def find_named_server(name) if @servers.key? name @servers[name] else host = resolve_name(name) @servers.values.find{|server| server.host == host} end end |
#freeze ⇒ Object
42 43 44 45 46 |
# File 'lib/synco/script.rb', line 42 def freeze current_server; master_server super end |
#localhost?(name) ⇒ Boolean
56 57 58 59 60 61 62 |
# File 'lib/synco/script.rb', line 56 def localhost?(name) return true if name == "localhost" host = resolve_name(Socket.gethostname) return name == host end |
#master_server ⇒ Object
The master server based on the name #master= specified
77 78 79 |
# File 'lib/synco/script.rb', line 77 def master_server @master_server ||= find_named_server(@master) end |
#resolve_name(name) ⇒ Object
52 53 54 |
# File 'lib/synco/script.rb', line 52 def resolve_name(name) Socket.gethostbyname(name)[0] end |
#running_on_master? ⇒ Boolean
48 49 50 |
# File 'lib/synco/script.rb', line 48 def running_on_master? current_server.same_host?(master_server) end |