Skip to content

Commit 3a1e8ae

Browse files
committed
Add -x option + a little cleanup.
It's not an ideal approach but it's good enough on control master.
1 parent 2104f74 commit 3a1e8ae

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

cl-sendfile.pl

+22-10
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ =head1 SYNOPSIS
2323
cl-sendfile.pl [-l $LOCAL_FILE] [-r $REMOTE_FILE] [-h] [-v] [--incl <pattern>] [--excl <pattern>]
2424
-l: local file/directory to rsync - passed through unmodified to rsync
2525
-r: remote location for rsync to write to - also unmodified
26+
-x: stage the file as a normal user and relocate using sudo (requires sudo root/NOPASSWD)
2627
-v: verbose output
2728
-h: print this message
2829
=cut
@@ -42,10 +43,13 @@ =head1 SYNOPSIS
4243
our $local_file = undef;
4344
our $remote_file = undef;
4445
our $help = undef;
46+
our $sudo = undef;
47+
our $final_file = undef;
4548

4649
GetOptions(
4750
"l=s" => \$local_file,
4851
"r=s" => \$remote_file,
52+
"x" => \$sudo,
4953
"h" => \$help
5054
);
5155

@@ -57,17 +61,25 @@ =head1 SYNOPSIS
5761
pod2usage();
5862
}
5963

60-
# save all the files sent out to a local tree so it's easy to reproduce the cluster
61-
#my $dir = dirname( $remote_file );
62-
#system( "mkdir -p $ENV{HOME}/files/$dir" );
63-
#copy( $local_file, "$ENV{HOME}/files/$remote_file" );
64-
65-
my $routine = sub {
66-
my $hostname = shift;
67-
scp( $local_file, "$hostname:$remote_file" );
68-
};
64+
$final_file = $remote_file;
65+
if ( $sudo ) {
66+
(my $fh, $remote_file) = my_tempfile();
67+
close $fh;
68+
unlink $remote_file;
69+
}
6970

70-
func_loop( $routine );
71+
func_loop(sub {
72+
my $host = shift;
73+
scp( $local_file, "$host:$remote_file" );
74+
});
75+
76+
if ( $sudo ) {
77+
func_loop(sub {
78+
my $host = shift;
79+
ssh( "$remote_user\@$host", "sudo cp $remote_file $final_file" );
80+
ssh( "$remote_user\@$host", "rm $remote_file" );
81+
});
82+
}
7183

7284
# vim: et ts=4 sw=4 ai smarttab
7385

0 commit comments

Comments
 (0)