push bak update with --install
[bak-git.git] / server.pl
index e363df4..c7b842a 100755 (executable)
--- a/server.pl
+++ b/server.pl
@@ -24,6 +24,12 @@ use strict;
 use autodie;
 use IO::Socket::INET;
 use File::Path;
+use Getopt::Long;
+
+my $install = 0;
+GetOptions(
+       'install!' => \$install,
+) || die "$!\n";
 
 my ( $dir, $server_ip ) = @ARGV;
 die "usage: $0 /backup/directory\n" unless $dir;
@@ -31,12 +37,23 @@ $server_ip ||= '127.0.0.1';
 
 my $shell_client = <<__SHELL_CLIENT__;
 #!/bin/sh
-echo `pwd` \$* | nc $server_ip 9001
+echo `hostname -s` `pwd` \$* | nc $server_ip 9001
 __SHELL_CLIENT__
 
 chdir $dir;
 system 'git init' unless -e '.git';
 
+if ( $install ) {
+       open(my $fh, '>', '/tmp/bak');
+       print $fh $shell_client;
+       close($fh);
+
+       foreach my $hostname ( glob '*' ) {
+               warn "install on $hostname\n";
+               system "scp /tmp/bak root\@$hostname:/usr/local/bin/";
+       }
+}
+
 my $server = IO::Socket::INET->new(
        Proto     => 'tcp',
        LocalAddr => $server_ip,
@@ -52,44 +69,43 @@ warn "dir: $dir listen: $server_ip:9001\n"
 ;
 
 while (my $client = $server->accept()) {
-       my ($pwd,$command,$path,$message) = split(/\s+/,<$client>,4);
-       my $ip = $client->peerhost;
+       my ($hostname,$pwd,$command,$path,$message) = split(/\s+/,<$client>,5);
 
        if ( $pwd eq 'install' ) {
-               warn "install on $ip\n";
+               warn "install on $hostname\n";
                print $client $shell_client;
                close($client);
-               system 'ssh-copy-id', "root\@$ip" if ! -d $ip;
+               system 'ssh-copy-id', "root\@$hostname" if ! -d $hostname;
                next;
        }
 
        $message ||= '';
        $path = "$pwd/$path" unless $path =~ m{^/};
 
-       warn "$ip [$command] $path | $message\n";
+       warn "$hostname [$command] $path | $message\n";
 
 
        my $dir = $path;
        $dir =~ s{/[^/]+$}{};
 
-       mkpath "$ip/$dir" unless -e "$ip/$dir";
+       mkpath "$hostname/$dir" unless -e "$hostname/$dir";
 
        if ( ! $command ) {
-               system "find $ip -type f | sed 's,$ip,,' > /tmp/$ip.list";
-               system "rsync -avv --files-from /tmp/$ip.list root\@$ip:/ $ip/"
+               system "find $hostname -type f | sed 's,$hostname,,' > /tmp/$hostname.list";
+               system "rsync -avv --files-from /tmp/$hostname.list root\@$hostname:/ $hostname/"
        } elsif ( $command eq 'add' ) {
-               system 'rsync', '-avv', "root\@$ip:$path", "$ip/$path";
-               system 'git', 'add', "$ip/$path";
+               system 'rsync', '-avv', "root\@$hostname:$path", "$hostname/$path";
+               system 'git', 'add', "$hostname/$path";
        } elsif ( $command eq 'commit' ) {
-               system 'rsync', '-avv', "root\@$ip:$path", "$ip/$path" if $path;
-               $message ||= "$command $ip $path";
-               system 'git', 'commit', '-m', $message, "$ip/$path";
+               system 'rsync', '-avv', "root\@$hostname:$path", "$hostname/$path" if $path;
+               $message ||= "$command $hostname $path";
+               system 'git', 'commit', '-m', $message, "$hostname/$path";
        } elsif ( $command =~ m{(diff|status|log)} ) {
                my $opt = '--summary' if $command eq 'log';
-               print $client `git $command $opt $ip`;
+               print $client `git $command $opt $hostname`;
        } elsif ( $command eq 'revert' ) {
-               print $client `git checkout -- $ip/$path`;
-               system 'rsync', '-avv', "$ip/$path", "root\@$ip:$path";
+               print $client `git checkout -- $hostname/$path`;
+               system 'rsync', '-avv', "$hostname/$path", "root\@$hostname:$path";
        } else {
                print $client "Unknown command: $command\n";
        }