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) ⇒ Script

Returns a new instance of Script.



18
19
20
21
22
23
24
25
# File 'lib/synco/script.rb', line 18

def initialize(method: nil, servers: {}, directories: [], master: :master)
  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.



89
90
91
92
93
# File 'lib/synco/script.rb', line 89

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)



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

def master
  @master
end

#methodObject

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



103
104
105
# File 'lib/synco/script.rb', line 103

def method
  @method
end

#serversObject

All servers which are participating in the backup process.



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

def servers
  @servers
end

Instance Method Details

#current_serverObject



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

def current_server
  @current_server ||= find_current_server
end

#find_current_serverObject

Find the server that matches the current machine



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

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.



50
51
52
53
54
55
56
57
# File 'lib/synco/script.rb', line 50

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



27
28
29
30
31
# File 'lib/synco/script.rb', line 27

def freeze
  current_server; master_server
  
  super
end

#localhost?(name) ⇒ Boolean

Returns:

  • (Boolean)


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

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



62
63
64
# File 'lib/synco/script.rb', line 62

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

#resolve_name(name) ⇒ Object



37
38
39
# File 'lib/synco/script.rb', line 37

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

#running_on_master?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/synco/script.rb', line 33

def running_on_master?
  current_server.same_host?(master_server)
end

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

Register a server with the backup script.



83
84
85
86
# File 'lib/synco/script.rb', line 83

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