start/stop stunnel
[cloudstore.git] / rsync-piper.pl
1 #!/usr/bin/perl
2 use warnings;
3 use strict;
4
5 use autodie;
6 use POSIX;
7 use File::Slurp;
8 use IO::Select;
9 use Time::HiRes;
10 use Data::Dump qw(dump);
11 use English;
12 use Module::Refresh;
13
14 use lib 'lib';
15 use WarnColor;
16 use CloudStore::Store;
17
18 my $slice = $ARGV[0] || 's1';
19
20 my ( undef, $dir, $port, undef ) = getgrnam($slice);
21
22 my $log_fifo = "$dir/$port.log";
23 my $pid_file = "$dir/$port.pid";
24 my $cfg_file = "$dir/$port.conf";
25
26 my $rsync = 'rsync';
27 $rsync = 'bin/rsync' if -x 'bin/rsync'; # use 3.1dev version!
28
29 my @transfer = qw(
30 timestamp:%t:timestamp
31 login:%m:text
32 port:$port:int
33 auth:%u:text
34 host:%h:text
35 pid:%p:int
36 perms:%B:text
37 itemize:%i:text
38 mtime:%M:timestamp
39 md5:%C:text
40 op:%o:text
41 size:%l:int
42 transfered:%b:int
43 file:%f:text
44 );
45
46 $transfer[2] = "port:$port:int"; # expand $port
47
48 my @transfer_names =          map { ( split(/:/,$_,3) )[0] } @transfer;
49 my $transfer_log   = join('|',map { ( split(/:/,$_,3) )[1] } @transfer );
50
51 if ( $ENV{SQL} ) {
52         print "CREATE TABLE rsync_transfer (\n\t",
53         join(",\n\t", map { my @m = split(/:/,$_,3); "$m[0] $m[2]" } @transfer),
54         "\n);\n";
55         exit 1;
56 }
57
58 my $store = CloudStore::Store->new( dir => $dir );
59
60 unlink $log_fifo if -f $log_fifo;
61 mkfifo $log_fifo, 0700 unless -p $log_fifo;
62
63 sub rsync_rebuild_config {
64
65 my $rsync_config = qq{
66
67 #uid = nobody
68 #gid = nogroup
69 #use chroot = yes
70 use chroot = no
71
72 #max connections = 4
73 lock file = $dir/$port.lock
74
75 #syslog facility = local5
76 log file  = $log_fifo
77
78 transfer logging = yes
79 log format = transfer-log:$transfer_log
80 max verbosity = 5
81
82 pid file  = $pid_file
83
84 # don't check secrets file permission (uid)
85 strict modes = no
86
87 #pre-xfer exec = /srv/cloudstore/rsync-xfer-trigger.pl
88 #post-xfer exec = /srv/cloudstore/rsync-xfer-trigger.pl
89
90 };
91
92 open(my $p, '<', '/var/lib/extrausers/passwd');
93 while(<$p>) {
94         chomp;
95
96         my ( $login, undef, $uid, $gid, $email, $path, $shell ) = split(/:/,$_,7);
97
98         if ( -d $path && -f "$path/.secrets" ) {
99                 my @secrets = map { chomp; $_ } read_file "$path/.secrets";
100                 my $auth_users = join(', ', map { s/:.+$//; $_ } @secrets );
101
102                 $rsync_config .= <<__RSYNC_MODULE__;
103
104 [$login]
105         path = $path
106         auth users = $auth_users
107         secrets file = $path/.secrets
108         read only = false
109         uid = $uid
110         gid = $gid
111         filter = - /.secrets - /.md5
112 #       refuse options = c delete
113 #       dont compress = *
114         incoming chmod = u=rwX,g+rX,o+rX
115
116
117 __RSYNC_MODULE__
118
119                 print "INFO: added $login = $auth_users\n";
120
121         } else {
122                 warn "skipped $login: $!";
123         }
124
125 }
126
127 write_file $cfg_file, $rsync_config;
128 warn "created $cfg_file ", -s $cfg_file, " bytes\n";
129
130 } # sub rsync_rebuild_config
131
132 rsync_rebuild_config;
133
134 sub rsync_running_pid {
135         return unless -e $pid_file;
136         my $pid = read_file $pid_file;
137         chomp($pid);
138         return $pid;
139 }
140
141 if ( my $pid = rsync_running_pid ) {
142         if ( kill 0, $pid ) {
143                 warn "found rsync pid $pid\n";
144                 kill 1, $pid && warn "reload config";
145 =for kill-rsync
146                 kill 2, $pid;
147                 while ( -e $pid_file ) {
148                         warn "waiting for rsync to die...\n";
149                         sleep 1;
150                 }
151                 
152                 open(my $fifo, '<', $log_fifo);
153                 while ( kill 0, $pid ) {
154                         my $line = <$fifo>;
155                         warn "<<< $line\n";
156                 }
157
158                 kill 0, $pid && die "can't kill it!";
159 =cut
160         } else {
161                 unlink $pid_file;
162         }
163 }
164
165 use POSIX ":sys_wait_h";
166 sub REAPER {
167         my $child;
168         while ((my $waitedpid = waitpid(-1,WNOHANG)) > 0) {
169                 warn "reaped $waitedpid" . ($? ? " with exit $?" : '');
170         }
171         $SIG{CHLD} = \&REAPER;  # loathe SysV
172 }
173
174 $SIG{CHLD} = \&REAPER;
175
176
177 if ( ! -e $pid_file ) {
178         my $exec = "$rsync --daemon --config $cfg_file --no-detach --port=$port";
179         warn "START $exec\n";
180
181         die "could not fork\n" unless defined(my $pid = fork);
182         unless ($pid) {
183                 warn "start server with $exec\n";
184                 exec $exec || die $!;
185         }
186
187         warn "wait for pid file";
188         while ( ! -e $pid_file ) {
189                 sleep 1;
190         }
191 }
192
193 =for gearman
194 use Gearman::Client;
195 my $gearman = Gearman::Client->new;
196 $gearman->job_servers('127.0.0.1:4730');
197 =cut
198
199 while(1) {
200         die "no rsync running" unless kill 0, rsync_running_pid;
201         warn "waiting for log from $log_fifo\n";
202         open(my $fifo, '<', $log_fifo);
203         while( my $line = <$fifo> ) {
204                 Module::Refresh->refresh;
205                 chomp $line;
206                 warn $line, $/;
207
208                 if ( $line =~ /\[(\d+)\] transfer-log:(.+)/ ) {
209                         my $pid = $1;
210                         my $transfer = $2;
211                         $transfer =~ s|(\d\d\d\d)/(\d\d)/(\d\d)[-\s](\d\d:\d\d:\d\d)|$1-$2-$3T$4|g;
212                         my ( $yyyy,$mm,$dd,undef,$login,undef ) = split( /[\-T\|]/, $transfer, 6 );
213                         my $host = $1 if $login =~ s/\+(.+)//;
214
215 if(0) {
216                         my $path = "users/$login/log";
217                         mkdir $path unless -d $path;
218                         $path .= "/$yyyy-$mm-$dd";
219                         my $new_log = ! -e $path;
220                         open( my $log, '>>', $path );
221                         print $log join('|',@transfer),"\n" if $new_log; # store header
222                         print $log "$transfer\n";
223                         close $log;
224 }
225
226                         my @v = split(/\|/,$transfer,$#transfer + 1);
227                         my %data;
228                         @data{@transfer_names} = @v ; # FIXME validate?
229
230                         $data{pid} = $pid;
231                         # overwrite pid from transfer log with consistant one for start/stop
232
233                         print ">>> data ",dump( \%data ) if $ENV{DEBUG};
234
235                         $store->rsync_transfer( \%data );
236 =for gearman
237                         $gearman->dispatch_background( 'rsync_transfer' => $json );
238 =cut
239
240                         die "no rsync running" unless kill 0, rsync_running_pid;
241                 } elsif ( $line =~ m/(unknown module|rebuild|reload|config)/ ) {
242                         warn "refresh modules, rebuild config and HUP rsync";
243                         Module::Refresh->refresh;
244                         rsync_rebuild_config;
245                         my $pid = rsync_running_pid;
246                         kill 1, $pid && warn "reload config";
247                 } else {
248                         $store->rsync_log( $line );
249                 }
250         }
251         close($fifo);
252         sleep 1;
253 }
254