change stunnel ip
[cloudstore.git] / rsync-piper.pl
index b23ab25..9979c23 100755 (executable)
@@ -49,17 +49,19 @@ my @transfer_names =          map { ( split(/:/,$_,3) )[0] } @transfer;
 my $transfer_log   = join('|',map { ( split(/:/,$_,3) )[1] } @transfer );
 
 if ( $ENV{SQL} ) {
-       print "CREATE TABLE rsync_transfer (\n\t",
+       warn "CREATE TABLE rsync_transfer (\n\t",
        join(",\n\t", map { my @m = split(/:/,$_,3); "$m[0] $m[2]" } @transfer),
        "\n);\n";
        exit 1;
 }
 
-my $store = CloudStore::Store->new;
+my $store = CloudStore::Store->new( $slice );
 
 unlink $log_fifo if -f $log_fifo;
 mkfifo $log_fifo, 0700 unless -p $log_fifo;
 
+sub rsync_rebuild_config {
+
 my $rsync_config = qq{
 
 #uid = nobody
@@ -82,8 +84,11 @@ pid file  = $pid_file
 # don't check secrets file permission (uid)
 strict modes = no
 
-pre-xfer exec = /srv/cloudstore/rsync-xfer-trigger.pl
-post-xfer exec = /srv/cloudstore/rsync-xfer-trigger.pl
+#pre-xfer exec = /srv/cloudstore/rsync-xfer-trigger.pl
+#post-xfer exec = /srv/cloudstore/rsync-xfer-trigger.pl
+
+# inplace breaks update of deduped files
+refuse options = inplace
 
 };
 
@@ -93,8 +98,14 @@ while(<$p>) {
 
        my ( $login, undef, $uid, $gid, $email, $path, $shell ) = split(/:/,$_,7);
 
-       if ( -d $path && -f "$path/.secrets" ) {
-               my @secrets = map { chomp; $_ } read_file "$path/.secrets";
+       if ( -d $path && -f "$path/.secrets" && ! -e "$path/.meta/secrets" ) {
+               $store->api->mkbasepath( "$path/.meta/secrets" );
+               rename "$path/.secrets", "$path/.meta/secrets";
+               warn "UPGRADE $login rsync secrets location\n";
+       }
+
+       if ( -d $path && -f "$path/.meta/secrets" ) {
+               my @secrets = map { chomp; $_ } read_file "$path/.meta/secrets";
                my $auth_users = join(', ', map { s/:.+$//; $_ } @secrets );
 
                $rsync_config .= <<__RSYNC_MODULE__;
@@ -102,17 +113,19 @@ while(<$p>) {
 [$login]
        path = $path
        auth users = $auth_users
-       secrets file = $path/.secrets
+       secrets file = $path/.meta/secrets
        read only = false
        uid = $uid
        gid = $gid
-       filter = - /.secrets
+       filter = - /.meta
 #      refuse options = c delete
 #      dont compress = *
+       incoming chmod = u=rwX,g+rX,o+rX
+
 
 __RSYNC_MODULE__
 
-               print "INFO: added $login = $auth_users\n";
+               warn "INFO: added $login = $auth_users\n";
 
        } else {
                warn "skipped $login: $!";
@@ -123,6 +136,10 @@ __RSYNC_MODULE__
 write_file $cfg_file, $rsync_config;
 warn "created $cfg_file ", -s $cfg_file, " bytes\n";
 
+} # sub rsync_rebuild_config
+
+rsync_rebuild_config;
+
 sub rsync_running_pid {
        return unless -e $pid_file;
        my $pid = read_file $pid_file;
@@ -166,7 +183,7 @@ sub REAPER {
 $SIG{CHLD} = \&REAPER;
 
 
-if ( ! -e $pid_file ) {
+if ( ! -e $pid_file || ! kill( 0, rsync_running_pid ) ) {
        my $exec = "$rsync --daemon --config $cfg_file --no-detach --port=$port";
        warn "START $exec\n";
 
@@ -197,8 +214,9 @@ while(1) {
                chomp $line;
                warn $line, $/;
 
-               if ( $line =~ /transfer-log:(.+)/ ) {
-                       my $transfer = $1;
+               if ( $line =~ /\[(\d+)\] transfer-log:(.+)/ ) {
+                       my $pid = $1;
+                       my $transfer = $2;
                        $transfer =~ s|(\d\d\d\d)/(\d\d)/(\d\d)[-\s](\d\d:\d\d:\d\d)|$1-$2-$3T$4|g;
                        my ( $yyyy,$mm,$dd,undef,$login,undef ) = split( /[\-T\|]/, $transfer, 6 );
                        my $host = $1 if $login =~ s/\+(.+)//;
@@ -218,6 +236,9 @@ if(0) {
                        my %data;
                        @data{@transfer_names} = @v ; # FIXME validate?
 
+                       $data{pid} = $pid;
+                       # overwrite pid from transfer log with consistant one for start/stop
+
                        print ">>> data ",dump( \%data ) if $ENV{DEBUG};
 
                        $store->rsync_transfer( \%data );
@@ -226,7 +247,10 @@ if(0) {
 =cut
 
                        die "no rsync running" unless kill 0, rsync_running_pid;
-               } elsif ( $line =~ m/unknown module/ ) {
+               } elsif ( $line =~ m/(unknown module|rebuild|reload|config)/ ) {
+                       warn "refresh modules, rebuild config and HUP rsync";
+                       Module::Refresh->refresh;
+                       rsync_rebuild_config;
                        my $pid = rsync_running_pid;
                        kill 1, $pid && warn "reload config";
                } else {