rename Couchbase to Store
[cloudstore.git] / rsync-piper.pl
index bcab4cd..ed6d11b 100755 (executable)
@@ -12,11 +12,13 @@ use English;
 use Module::Refresh;
 
 use lib 'lib';
-use CloudStore::Couchbase;
+use WarnColor;
+use CloudStore::Store;
 
 my $dir   = $ENV{RSYNC_DIR}  || '/srv/cloudstore';
 my $port  = $ENV{RSYNC_PORT} || 6501;
 my $users = "users";
+my $default_quota = $ENV{QUOTA} || 200 * 1024; # 200 Kb for test.sh
 
 my $log_fifo = "$dir/var/$port.log";
 my $pid_file = "$dir/var/$port.pid";
@@ -54,7 +56,7 @@ if ( $ENV{SQL} ) {
        exit 1;
 }
 
-my $store = CloudStore::Couchbase->new;
+my $store = CloudStore::Store->new;
 
 mkdir "$dir/var" if ! -e "$dir/var";
 
@@ -83,8 +85,8 @@ pid file  = $pid_file
 # don't check secrets file permission (uid)
 strict modes = no
 
-#pre-xfer exec = /srv/cloudstore/pre-xfer.sh
-#post-xfer exec = /srv/cloudstore/post-xfer.sh
+pre-xfer exec = /srv/cloudstore/rsync-xfer-trigger.pl
+post-xfer exec = /srv/cloudstore/rsync-xfer-trigger.pl
 
 };
 
@@ -109,9 +111,8 @@ __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
+               my $quota = $default_quota;
+               $quota = read_file("$path/quota") * 1 if -e "$path/quota";
 
                $store->user_set({
                        login => $login,
@@ -130,11 +131,17 @@ __RSYNC_MODULE__
 write_file $cfg_file, $rsync_config;
 warn "created $cfg_file ", -s $cfg_file, " bytes\n";
 
-if ( -e $pid_file ) {
+sub rsync_running_pid {
+       return unless -e $pid_file;
        my $pid = read_file $pid_file;
        chomp($pid);
+       return $pid;
+}
+
+if ( my $pid = rsync_running_pid ) {
        if ( kill 0, $pid ) {
                warn "found rsync pid $pid\n";
+=for kill-rsync
                kill 2, $pid;
                while ( -e $pid_file ) {
                        warn "waiting for rsync to die...\n";
@@ -148,6 +155,7 @@ if ( -e $pid_file ) {
                }
 
                kill 0, $pid && die "can't kill it!";
+=cut
        } else {
                unlink $pid_file;
        }
@@ -188,13 +196,13 @@ $gearman->job_servers('127.0.0.1:4730');
 =cut
 
 while(1) {
-       warn "# reading log output from $log_fifo\n";
+       die "no rsync running" unless kill 0, rsync_running_pid;
+       warn "waiting for log from $log_fifo\n";
        open(my $fifo, '<', $log_fifo);
        while( my $line = <$fifo> ) {
                Module::Refresh->refresh;
-               die "ERROR: $line" if $line =~ /rsync error:/;
                chomp $line;
-               print $line, $/;
+               warn $line, $/;
 
                if ( $line =~ /transfer-log:(.+)/ ) {
                        my $transfer = $1;
@@ -222,6 +230,7 @@ while(1) {
                        $gearman->dispatch_background( 'rsync_transfer' => $json );
 =cut
 
+                       die "no rsync running" unless kill 0, rsync_running_pid;
                }
        }
        close($fifo);