rsync server now creates user objects
[cloudstore.git] / rsync-piper.pl
index 0bb827b..bcab4cd 100755 (executable)
@@ -9,10 +9,10 @@ use IO::Select;
 use Time::HiRes;
 use Data::Dump qw(dump);
 use English;
+use Module::Refresh;
 
 use lib 'lib';
-use CloudStore::JSON;
-use CloudStore::dedup;
+use CloudStore::Couchbase;
 
 my $dir   = $ENV{RSYNC_DIR}  || '/srv/cloudstore';
 my $port  = $ENV{RSYNC_PORT} || 6501;
@@ -54,6 +54,8 @@ if ( $ENV{SQL} ) {
        exit 1;
 }
 
+my $store = CloudStore::Couchbase->new;
+
 mkdir "$dir/var" if ! -e "$dir/var";
 
 unlink $log_fifo if -f $log_fifo;
@@ -107,6 +109,18 @@ __RSYNC_MODULE__
 
                print "INFO: added $login = $auth_users\n";
 
+               my $quota = read_file "$path/quota" if -e "$path/quota";
+               chomp $quota && $quota * 1;
+               $quota ||= 100 * 1024; # Kb - XXX default quota
+
+               $store->user_set({
+                       login => $login,
+                       path => $path,
+                       secrets => [ @secrets ],
+                       quota => $quota,
+                       port => $port,
+               });
+
        } else {
                warn "skipped $login: $!";
        }
@@ -120,17 +134,37 @@ if ( -e $pid_file ) {
        my $pid = read_file $pid_file;
        chomp($pid);
        if ( kill 0, $pid ) {
-               warn "found rsync pid $pid";
+               warn "found rsync pid $pid\n";
                kill 2, $pid;
                while ( -e $pid_file ) {
-                       warn "waiting for rsync to die...";
+                       warn "waiting for rsync to die...\n";
                        sleep 1;
                }
+               
+               open(my $fifo, '<', $log_fifo);
+               while ( kill 0, $pid ) {
+                       my $line = <$fifo>;
+                       warn "<<< $line\n";
+               }
+
+               kill 0, $pid && die "can't kill it!";
        } else {
                unlink $pid_file;
        }
 }
 
+use POSIX ":sys_wait_h";
+sub REAPER {
+       my $child;
+       while ((my $waitedpid = waitpid(-1,WNOHANG)) > 0) {
+               warn "reaped $waitedpid" . ($? ? " with exit $?" : '');
+       }
+       $SIG{CHLD} = \&REAPER;  # loathe SysV
+}
+
+$SIG{CHLD} = \&REAPER;
+
+
 if ( ! -e $pid_file ) {
        my $exec = "$rsync --daemon --config $cfg_file --no-detach --port=$port";
        warn "START $exec\n";
@@ -157,7 +191,8 @@ while(1) {
        warn "# reading log output from $log_fifo\n";
        open(my $fifo, '<', $log_fifo);
        while( my $line = <$fifo> ) {
-               die $line if $line =~ /rsync error:/;
+               Module::Refresh->refresh;
+               die "ERROR: $line" if $line =~ /rsync error:/;
                chomp $line;
                print $line, $/;
 
@@ -182,10 +217,7 @@ while(1) {
 
                        print ">>> data ",dump( \%data ) if $ENV{DEBUG};
 
-                       CloudStore::dedup::data \%data; # uses deleted json files!
-
-                       my $json = CloudStore::JSON::rsync_transfer \%data;
-
+                       $store->transfer( \%data );
 =for gearman
                        $gearman->dispatch_background( 'rsync_transfer' => $json );
 =cut