test torrent_list and display test names
[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( $slice );
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" && ! -e "$path/.meta/secrets" ) {
99                 $store->api->mkbasepath( "$path/.meta/secrets" );
100                 rename "$path/.secrets", "$path/.meta/secrets";
101                 warn "UPGRADE $login rsync secrets location\n";
102         }
103
104         if ( -d $path && -f "$path/.meta/secrets" ) {
105                 my @secrets = map { chomp; $_ } read_file "$path/.meta/secrets";
106                 my $auth_users = join(', ', map { s/:.+$//; $_ } @secrets );
107
108                 $rsync_config .= <<__RSYNC_MODULE__;
109
110 [$login]
111         path = $path
112         auth users = $auth_users
113         secrets file = $path/.meta/secrets
114         read only = false
115         uid = $uid
116         gid = $gid
117         filter = - /.meta
118 #       refuse options = c delete
119 #       dont compress = *
120         incoming chmod = u=rwX,g+rX,o+rX
121
122
123 __RSYNC_MODULE__
124
125                 print "INFO: added $login = $auth_users\n";
126
127         } else {
128                 warn "skipped $login: $!";
129         }
130
131 }
132
133 write_file $cfg_file, $rsync_config;
134 warn "created $cfg_file ", -s $cfg_file, " bytes\n";
135
136 } # sub rsync_rebuild_config
137
138 rsync_rebuild_config;
139
140 sub rsync_running_pid {
141         return unless -e $pid_file;
142         my $pid = read_file $pid_file;
143         chomp($pid);
144         return $pid;
145 }
146
147 if ( my $pid = rsync_running_pid ) {
148         if ( kill 0, $pid ) {
149                 warn "found rsync pid $pid\n";
150                 kill 1, $pid && warn "reload config";
151 =for kill-rsync
152                 kill 2, $pid;
153                 while ( -e $pid_file ) {
154                         warn "waiting for rsync to die...\n";
155                         sleep 1;
156                 }
157                 
158                 open(my $fifo, '<', $log_fifo);
159                 while ( kill 0, $pid ) {
160                         my $line = <$fifo>;
161                         warn "<<< $line\n";
162                 }
163
164                 kill 0, $pid && die "can't kill it!";
165 =cut
166         } else {
167                 unlink $pid_file;
168         }
169 }
170
171 use POSIX ":sys_wait_h";
172 sub REAPER {
173         my $child;
174         while ((my $waitedpid = waitpid(-1,WNOHANG)) > 0) {
175                 warn "reaped $waitedpid" . ($? ? " with exit $?" : '');
176         }
177         $SIG{CHLD} = \&REAPER;  # loathe SysV
178 }
179
180 $SIG{CHLD} = \&REAPER;
181
182
183 if ( ! -e $pid_file ) {
184         my $exec = "$rsync --daemon --config $cfg_file --no-detach --port=$port";
185         warn "START $exec\n";
186
187         die "could not fork\n" unless defined(my $pid = fork);
188         unless ($pid) {
189                 warn "start server with $exec\n";
190                 exec $exec || die $!;
191         }
192
193         warn "wait for pid file";
194         while ( ! -e $pid_file ) {
195                 sleep 1;
196         }
197 }
198
199 =for gearman
200 use Gearman::Client;
201 my $gearman = Gearman::Client->new;
202 $gearman->job_servers('127.0.0.1:4730');
203 =cut
204
205 while(1) {
206         die "no rsync running" unless kill 0, rsync_running_pid;
207         warn "waiting for log from $log_fifo\n";
208         open(my $fifo, '<', $log_fifo);
209         while( my $line = <$fifo> ) {
210                 Module::Refresh->refresh;
211                 chomp $line;
212                 warn $line, $/;
213
214                 if ( $line =~ /\[(\d+)\] transfer-log:(.+)/ ) {
215                         my $pid = $1;
216                         my $transfer = $2;
217                         $transfer =~ s|(\d\d\d\d)/(\d\d)/(\d\d)[-\s](\d\d:\d\d:\d\d)|$1-$2-$3T$4|g;
218                         my ( $yyyy,$mm,$dd,undef,$login,undef ) = split( /[\-T\|]/, $transfer, 6 );
219                         my $host = $1 if $login =~ s/\+(.+)//;
220
221 if(0) {
222                         my $path = "users/$login/log";
223                         mkdir $path unless -d $path;
224                         $path .= "/$yyyy-$mm-$dd";
225                         my $new_log = ! -e $path;
226                         open( my $log, '>>', $path );
227                         print $log join('|',@transfer),"\n" if $new_log; # store header
228                         print $log "$transfer\n";
229                         close $log;
230 }
231
232                         my @v = split(/\|/,$transfer,$#transfer + 1);
233                         my %data;
234                         @data{@transfer_names} = @v ; # FIXME validate?
235
236                         $data{pid} = $pid;
237                         # overwrite pid from transfer log with consistant one for start/stop
238
239                         print ">>> data ",dump( \%data ) if $ENV{DEBUG};
240
241                         $store->rsync_transfer( \%data );
242 =for gearman
243                         $gearman->dispatch_background( 'rsync_transfer' => $json );
244 =cut
245
246                         die "no rsync running" unless kill 0, rsync_running_pid;
247                 } elsif ( $line =~ m/(unknown module|rebuild|reload|config)/ ) {
248                         warn "refresh modules, rebuild config and HUP rsync";
249                         Module::Refresh->refresh;
250                         rsync_rebuild_config;
251                         my $pid = rsync_running_pid;
252                         kill 1, $pid && warn "reload config";
253                 } else {
254                         $store->rsync_log( $line );
255                 }
256         }
257         close($fifo);
258         sleep 1;
259 }
260