Class: Synco::Script

Inherits:
Controller show all
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

Attributes inherited from Controller

#events

Instance Method Summary collapse

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, **options, &block)
	paths.each do |path|
		@directories << Directory.build(path, **options, &block)
	end
end

#masterObject

The master server name (e.g. symbolic or host name)



115
116
117
# File 'lib/synco/script.rb', line 115

def master
  @master
end

#methodObject

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

#serversObject

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_serverObject



93
94
95
# File 'lib/synco/script.rb', line 93

def current_server
	@current_server ||= find_current_server
end

#find_current_serverObject

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

#freezeObject



42
43
44
45
46
# File 'lib/synco/script.rb', line 42

def freeze
	current_server; master_server
	
	super
end

#localhost?(name) ⇒ Boolean

Returns:

  • (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_serverObject

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

Returns:

  • (Boolean)


48
49
50
# File 'lib/synco/script.rb', line 48

def running_on_master?
	current_server.same_host?(master_server)
end

#server(*arguments, **options, &block) ⇒ Object

Register a server with the backup script.



98
99
100
101
# File 'lib/synco/script.rb', line 98

def server(*arguments, **options, &block)
	server = Server.build(*arguments, **options, &block)
	@servers[server.name] = server
end