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.



35
36
37
38
39
40
41
42
# File 'lib/synco/script.rb', line 35

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.



106
107
108
109
110
# File 'lib/synco/script.rb', line 106

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)



117
118
119
# File 'lib/synco/script.rb', line 117

def master
  @master
end

#methodObject

A specific method which will perform the backup (e.g. an instance of Synco::Method)



120
121
122
# File 'lib/synco/script.rb', line 120

def method
  @method
end

#serversObject

All servers which are participating in the backup process.



123
124
125
# File 'lib/synco/script.rb', line 123

def servers
  @servers
end

Instance Method Details

#current_serverObject



95
96
97
# File 'lib/synco/script.rb', line 95

def current_server
	@current_server ||= find_current_server
end

#find_current_serverObject

Find the server that matches the current machine



84
85
86
87
88
89
90
91
92
93
# File 'lib/synco/script.rb', line 84

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.



67
68
69
70
71
72
73
74
# File 'lib/synco/script.rb', line 67

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



44
45
46
47
48
# File 'lib/synco/script.rb', line 44

def freeze
	current_server; master_server
	
	super
end

#localhost?(name) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
61
62
63
64
# File 'lib/synco/script.rb', line 58

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



79
80
81
# File 'lib/synco/script.rb', line 79

def master_server
	@master_server ||= find_named_server(@master)
end

#resolve_name(name) ⇒ Object



54
55
56
# File 'lib/synco/script.rb', line 54

def resolve_name(name)
	Socket.gethostbyname(name)[0]
end

#running_on_master?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/synco/script.rb', line 50

def running_on_master?
	current_server.same_host?(master_server)
end

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

Register a server with the backup script.



100
101
102
103
# File 'lib/synco/script.rb', line 100

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