-
Notifications
You must be signed in to change notification settings - Fork 1
/
confsync.pl
419 lines (348 loc) · 8.83 KB
/
confsync.pl
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
use strict;
use warnings;
use diagnostics;
use JSON;
use IPC::Open3;
use POSIX qw/:sys_wait_h/;
our $config_file;
sub BEGIN
{
use Cwd qw/chdir/;
our $VERSION = '2.0';
printf " ConfSync version %s starting up...", $VERSION;
unless (exists $ENV{'GIT_WORK_TREE'})
{
printf "\n Error: The GIT_WORK_TREE environment variable is missing!\n";
exit 1;
}
unless (defined $ENV{'GIT_WORK_TREE'} && -d $ENV{'GIT_WORK_TREE'})
{
printf "\n Error: GIT_WORK_TREE is not a directory!\n";
exit 1;
}
chdir $ENV{'GIT_WORK_TREE'} or die $!;
$config_file = 'confsync.cfg';
unless (-f $config_file)
{
printf "\n Error: The configuration file is missing!\n";
exit 1;
}
printf "\n\n";
}
my $resyncing_all = 0;
my $command_errors = 0;
my $command_errors_saved = 0;
my $sync = +{};
my $types = +{};
my $config = +{};
my $servers = +{};
my $defaults = +{};
# Determine if an element is in an array
sub in_array
{
my $needle = shift;
return grep { $_ eq $needle } @_;
}
# Determine if a server is disabled
sub server_is_disabled
{
my $name = shift;
return exists $servers->{$name}->{'disabled'};
}
# Determine if a server exists
sub server_exists
{
my $name = shift;
return exists $servers->{$name};
}
# Determine if a server is a type
sub server_is_type
{
my ($name, $type) = @_;
return ($servers->{$name}->{'type'} eq $type);
}
# Get a list of configured types
sub get_configured_types
{
my @types = ();
foreach my $server (sort values %{$servers})
{
next if (in_array $server->{'type'}, @types);
push @types, $server->{'type'};
}
return @types;
}
# Given a type, return all servers in that type
sub get_servers_for_type
{
my ($type) = @_;
my @servers = ();
foreach my $name (sort keys %{$servers})
{
next unless ($servers->{$name}->{'type'} eq $type);
push @servers, $name;
}
return @servers;
}
# Get value given a key, useful for when you have defaults set
sub get_value_for_key
{
my ($name, $key) = @_;
if (exists $servers->{$name}->{$key})
{
return $servers->{$name}->{$key};
}
if (exists $types->{$servers->{$name}->{'type'}}->{$key})
{
return $types->{$servers->{$name}->{'type'}}->{$key};
}
if (exists $defaults->{$key})
{
return $defaults->{$key};
}
return undef;
}
# Boil the files down to per-type and per-server directories, ignoring duplicates
sub add_server_sync
{
my ($type, $name, $which_changed) = @_;
$sync->{$type} = +{} unless (exists $sync->{$type});
$sync->{$type}->{$name} = +{} unless (exists $sync->{$type}->{$name});
$sync->{$type}->{$name}->{'global'} = 1 unless ($which_changed == 1);
$sync->{$type}->{$name}->{'local'} = 1 unless ($which_changed == 0);
}
# Determine what servers need to be synced, given a list of modified files
sub server_process_filepath
{
my ($file) = @_;
if (my ($type, $name, $path) = ($file =~ m/^([^\/]+?)\/([^\/]+?)\/(.+)/))
{
unless ($name eq 'global')
{
unless (server_exists $name)
{
printf " Modified: %s (belongs to an unknown server - skipping)\n", $file;
return;
}
unless (server_is_type $name, $type)
{
printf " Modified: %s (server %s is not of type %s - skipping)\n", $file, $name, $type;
return;
}
printf " Modified: %s/%s/%s (local)\n", $type, $name, $path;
add_server_sync $type, $name, 1;
}
else
{
foreach $name (get_servers_for_type $type)
{
if (-e sprintf('%s/%s/%s', $type, $name, $path))
{
printf " Modified: %s/%s/%s (global) -- skipping due to local\n", $type, $name, $path;
return;
}
printf " Modified: %s/%s/%s (global)\n", $type, $name, $path;
add_server_sync $type, $name, 0;
}
}
}
else
{
printf " Modified: %s -- skipping (cannot handle)\n", $file;
}
}
# Execute a program cleanly and watch for any output
sub better_exec
{
my ($cmd_pretty_name, @command_args) = @_;
printf " Executing %s\n", join(' ', @command_args);
my ($child_in, $child_out) = (undef, undef);
open($child_in, '<', '/dev/null') // die $!;
my $pid = open3($child_in, $child_out, $child_out, @command_args);
while (<$child_out>)
{
s/^\s+|\s+$//g;
next unless $_;
printf " %s: %s\n", $cmd_pretty_name, $_;
}
close $child_in;
close $child_out;
waitpid $pid, 0;
if (WIFEXITED($?) && (my $status = WEXITSTATUS($?)) != 0)
{
printf " %s exited with status %d\n", $cmd_pretty_name, $status;
$command_errors++;
}
if (WIFSIGNALED($?) && (my $signal = WTERMSIG($?)) != 0)
{
printf " %s terminated due to signal %d\n", $cmd_pretty_name, $signal;
$command_errors++;
}
}
# Do rsync operation
sub do_rsync
{
my ($name, $dir) = @_;
my $user = get_value_for_key($name, 'user') // return;
my $host = get_value_for_key($name, 'host') // return;
my $confdir = get_value_for_key($name, 'confdir') // return;
my @command_args =
(
'/usr/bin/rsync', '-avxz', '-e', '/usr/bin/ssh',
$dir, sprintf('%s@%s:%s', $user, $host, $confdir)
);
better_exec 'rsync', @command_args;
}
# Run a command on a server
sub do_commands
{
my ($name, @commands) = @_;
my $user = get_value_for_key($name, 'user') // return;
my $host = get_value_for_key($name, 'host') // return;
foreach my $command (@commands)
{
my @command_args =
(
'/usr/bin/ssh',
sprintf('%s@%s', $user, $host),
$command
);
better_exec 'ssh', @command_args;
}
}
# Parse configuration
printf " Parsing configuration... ";
{
local $/;
open(CONFIG, '<', $config_file) // die $!;
eval { $config = decode_json(<CONFIG>) } or die $!;
close(CONFIG);
}
foreach my $required_sect (qw/defaults types servers/)
{
unless (exists $config->{$required_sect})
{
printf "Error: Configuration file is missing required section %s\n", $required_sect;
exit 1;
}
}
$types = $config->{'types'};
$servers = $config->{'servers'};
$defaults = $config->{'defaults'};
foreach my $server (keys %{$servers})
{
# This has to be defined on a per-server basis
unless (exists $servers->{$server}->{'type'})
{
printf "Error: Server '%s' is missing required config option 'type'\n", $server;
exit 1;
}
# If there is no host given, use the server name as host
# This allows putting host/address in ssh_config(5)
unless (exists $servers->{$server}->{'host'})
{
$servers->{$server}->{'host'} = $server;
}
# These can be defined on a per-server basis, or provided by global or type defaults
foreach my $val (qw/user confdir/)
{
next if (get_value_for_key $server, $val);
printf "Error: Server '%s' is missing required config option '%s'\n", $server, $val;
exit 1;
}
}
printf "done\n";
# Determine what files have changed since we last operated and which servers we need to update because of it
my @files = +();
while (my $file = <STDIN>)
{
$file =~ s/\s$//g;
push @files, $file;
if ($file eq $config_file)
{
$resyncing_all = 1;
printf " Configuration file changed -- resyncing all enabled servers\n\n";
foreach my $type (get_configured_types())
{
foreach my $server (get_servers_for_type $type)
{
add_server_sync $type, $server, 2;
}
}
last;
}
}
unless ($resyncing_all)
{
printf "\n";
foreach my $file (@files)
{
server_process_filepath $file;
}
}
# If there are no servers to sync, exit now
unless (scalar keys %{$sync})
{
printf " No work to do! Exiting.\n";
exit 0;
}
# Now we know which servers need syncing and whether they only need local or global files or both updated
foreach my $type (sort keys %{$sync})
{
foreach my $name (sort keys %{$sync->{$type}})
{
next if (server_is_disabled $name);
my $sourcedir = '';
printf "\n";
printf " Beginning sync for %s (%s) ...\n", $name, $type;
$command_errors_saved = $command_errors;
if (exists $sync->{$type}->{$name}->{'global'})
{
$sourcedir = sprintf('%s/global/', $type);
do_rsync($name, $sourcedir) if (-d $sourcedir);
}
if (exists $sync->{$type}->{$name}->{'local'})
{
$sourcedir = sprintf('%s/%s/', $type, $name);
do_rsync($name, $sourcedir) if (-d $sourcedir);
}
if ($command_errors == $command_errors_saved)
{
printf " File synchronisation successful\n";
}
else
{
printf " File synchronisation failed\n";
}
}
}
# Now execute any post-synchronisation commands
foreach my $type (sort keys %{$sync})
{
foreach my $name (sort keys %{$sync->{$type}})
{
next if (server_is_disabled $name);
my $postsync_cmds = get_value_for_key($name, 'postsync_cmds') or next;
next unless (ref $postsync_cmds eq 'ARRAY');
printf "\n";
printf " Executing post-synchronisation commands for %s (%s) ...\n", $name, $type;
$command_errors_saved = $command_errors;
do_commands($name, @{$postsync_cmds});
if ($command_errors == $command_errors_saved)
{
printf " Post-synchronisation commands successful\n";
}
else
{
printf " Post-synchronisation commands failed\n";
}
}
}
# Exit uncleanly if we encountered any command errors
if ($command_errors)
{
printf "\n Encountered %d command errors\n", $command_errors;
exit 1;
}
printf "\n\n All actions completed successfully\n";
exit 0;