forked from ShibaOnTheMoon/docker-arch-repo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
watchdog.rb
executable file
·97 lines (85 loc) · 2.35 KB
/
watchdog.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/usr/bin/ruby
require 'yaml'
require 'net/http'
require 'uri'
require 'logger'
LOGGER = Logger.new(STDOUT)
def load_config
config = YAML.load_file "/etc/arch-mirror/config.yml"
[ config['excludes'], config['sync'] ]
end
def create_excludes(arch)
returned = []
returned << "*/#{arch}"
returned << "pool/*/*-#{arch}.pkg.tar.xz"
returned << "pool/*/*-#{arch}.pkg.tar.gz"
returned
end
def create_rsync_excludes(excludes)
final_file = []
final_file << 'iso' if excludes['iso']
final_file += create_excludes("i686") if excludes['32bits']
final_file += create_excludes("x86_64") if excludes['64bits']
File.write "/etc/arch-mirror/excludes.txt", final_file.join("\n")
end
def every_to_seconds(value)
case value
when 'minutes'
60
when 'hours'
3600
when 'days'
86400
end
end
def get_mirror_lastupdate(url)
Net::HTTP.get(URI.parse(url)).to_i
end
def check_for_updates(last_update_url, mirror)
mirror_lastupdate = get_mirror_lastupdate last_update_url
local_update = File.read("/var/mirror/lastupdate").to_i
if mirror_lastupdate > local_update
LOGGER.info "Update found, syncing..."
do_sync mirror, local_update
else
LOGGER.info "No update found, sleeping until next check..."
end
end
def do_sync(mirror, local_update)
rsync_log = "/etc/arch-mirror/logs/log-from-#{local_update}.log"
rsync_bin = '/usr/bin/rsync'
rsync_params = '-rtlvH --safe-links --progress --delete-after --timeout=600 --contimeout=60 -p --delay-updates --no-motd --exclude-from="/etc/arch-mirror/excludes.txt"'
log_file = '/var/run/mirror.lock'
fork do
unless File.exist? log_file
File.write(log_file, 'working')
exec "#{rsync_bin} #{rsync_params} #{mirror} /var/mirror/"
File.delete log_file
end
end
end
def start_processes(sync)
pid_nginx = fork do
LOGGER.info "Starting HTTP server : nginx"
exec "/usr/sbin/nginx"
end
sleep_period = every_to_seconds sync['every']
skip = sync['skip_first']
LOGGER.info "Syncing every #{sleep_period} seconds"
while true
if skip
LOGGER.info "Skipping initial check..."
skip = false
else
LOGGER.info "Checking for updates..."
check_for_updates sync['last_update_url'], sync['mirror']
end
sleep sleep_period
end
end
def main
excludes, sync = load_config
create_rsync_excludes excludes
start_processes sync
end
main