File Coverage

blib/lib/urpm/parallel_ssh.pm
Criterion Covered Total %
statement 7 9 77.7
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 10 12 83.3


line stmt bran cond sub pod time code
1             package urpm::parallel_ssh;
2              
3              
4             #- Copyright (C) 2002, 2003, 2004, 2005 MandrakeSoft SA
5             #- Copyright (C) 2005-2010 Mandriva SA
6              
7 1     1   964 use strict;
  1         1  
  1         29  
8 1     1   4 use urpm::util 'dirname';
  1         1  
  1         38  
9 1     1   30 use urpm::msg;
  0            
  0            
10             use urpm::parallel;
11              
12             our @ISA = 'urpm::parallel';
13              
14             sub _localhost { $_[0] eq 'localhost' }
15             sub _ssh { &_localhost ? '' : "ssh $_[0] " }
16             sub _host { &_localhost ? '' : "$_[0]:" }
17              
18             sub _scp {
19             my ($urpm, $host, @para) = @_;
20             my $dest = pop @para;
21              
22             $urpm->{log}("parallel_ssh: scp " . join(' ', @para) . " $host:$dest");
23             system('scp', @para, _host($host) . $dest) == 0
24             or $urpm->{fatal}(1, N("scp failed on host %s (%d)", $host, $? >> 8));
25             }
26              
27             sub copy_to_dir {
28             my ($parallel, $urpm, @para) = @_;
29             my $dir = pop @para;
30              
31             foreach my $host (keys %{$parallel->{nodes}}) {
32             if (_localhost($host)) {
33             if (my @f = grep { dirname($_) ne $dir } @para) {
34             $urpm->{log}("parallel_ssh: cp @f $urpm->{cachedir}/rpms");
35             system('cp', @f, $dir) == 0
36             or $urpm->{fatal}(1, N("cp failed on host %s (%d)", $host, $? >> 8));
37             }
38             } else {
39             _scp($urpm, $host, @para, $dir);
40             }
41             }
42             }
43              
44             sub propagate_file {
45             my ($parallel, $urpm, $file) = @_;
46             foreach (grep { !_localhost($_) } keys %{$parallel->{nodes}}) {
47             _scp($urpm, $_, '-q', $file, $file);
48             }
49             }
50              
51             sub _ssh_urpm {
52             my ($urpm, $node, $cmd, $para) = @_;
53              
54             $cmd ne 'urpme' && _localhost($node) and $para = "--nolock $para";
55              
56             # it doesn't matter for urpmq, and previous version of urpmq didn't handle it:
57             $cmd ne 'urpmq' and $para = "--no-locales $para";
58              
59             $urpm->{log}("parallel_ssh: $node: $cmd $para");
60             _ssh($node) . " $cmd $para";
61             }
62             sub _ssh_urpm_popen {
63             my ($urpm, $node, $cmd, $para) = @_;
64              
65             my $command = _ssh_urpm($urpm, $node, $cmd, $para);
66             open(my $fh, "$command |") or $urpm->{fatal}(1, "Can't fork ssh: $!");
67             $fh;
68             }
69              
70             sub urpm_popen {
71             my ($parallel, $urpm, $cmd, $para, $do) = @_;
72              
73             my @errors;
74              
75             foreach my $node (keys %{$parallel->{nodes}}) {
76             my $fh = _ssh_urpm_popen($urpm, $node, $cmd, $para);
77              
78             while (my $s = <$fh>) {
79             chomp $s;
80             $urpm->{debug}("parallel_ssh: $node: received: $s") if $urpm->{debug};
81             $do->($node, $s) and last;
82             }
83             close $fh or push @errors, N("%s failed on host %s (maybe it does not have a good version of urpmi?) (exit code: %d)", $cmd, $node, $? >> 8);
84             $urpm->{debug}("parallel_ssh: $node: $cmd finished") if $urpm->{debug};
85             }
86              
87             @errors;
88             }
89              
90             sub run_urpm_command {
91             my ($parallel, $urpm, $cmd, $para) = @_;
92              
93             foreach my $node (keys %{$parallel->{nodes}}) {
94             system(_ssh_urpm($urpm, $node, $cmd, $para));
95             }
96             }
97              
98             #- allow to bootstrap from urpmi code directly (namespace is urpm).
99              
100             package urpm;
101              
102             no warnings 'redefine';
103              
104             sub handle_parallel_options {
105             my (undef, $options) = @_;
106             my ($id, @nodes) = split /:/, $options;
107              
108             if ($id =~ /^ssh(?:\(([^\)]*)\))?$/) {
109             my %nodes; @nodes{@nodes} = undef;
110             return bless {
111             media => $1,
112             nodes => \%nodes,
113             }, "urpm::parallel_ssh";
114             }
115             return undef;
116             }
117              
118             1;