remove distinction between local and distant running, always create

a shell object that can chdir, setenv, and exec commands.
(note that this executes stuff after fork, so permanent changes are cheap
and okay)

Also create it from "host" objects, which simplifies parameter passing.
This commit is contained in:
espie 2012-07-04 08:59:10 +00:00
parent 7aab912a21
commit f7ea023751
6 changed files with 130 additions and 104 deletions

View File

@ -1,5 +1,5 @@
# ex:ts=8 sw=4: # ex:ts=8 sw=4:
# $OpenBSD: Core.pm,v 1.11 2011/12/02 22:29:28 espie Exp $ # $OpenBSD: Core.pm,v 1.12 2012/07/04 08:59:10 espie Exp $
# #
# Copyright (c) 2010 Marc Espie <espie@openbsd.org> # Copyright (c) 2010 Marc Espie <espie@openbsd.org>
# #
@ -106,10 +106,23 @@ sub is_alive
return 1; return 1;
} }
sub shellclass
{
"DPB::Shell::Abstract"
}
sub shell
{
my $self = shift;
return $self->{shell};
}
sub new sub new
{ {
my ($class, $host, $prop) = @_; my ($class, $host, $prop) = @_;
bless {host => DPB::Host->new($host, $prop)}, $class; my $c = bless {host => DPB::Host->new($host, $prop)}, $class;
$c->{shell} = $class->shellclass->new($c->host);
return $c;
} }
sub host sub host
@ -353,13 +366,7 @@ our @ISA = qw(DPB::Task::Pipe);
sub run sub run
{ {
my ($self, $core) = @_; my ($self, $core) = @_;
my $shell = $core->{shell}; $core->shell->exec(OpenBSD::Path->sysctl, '-n', 'hw.ncpu');
my $sysctl = OpenBSD::Paths->sysctl;
if (defined $shell) {
$shell->run("$sysctl -n hw.ncpu");
} else {
exec{$sysctl} ($sysctl, '-n', 'hw.ncpu');
}
} }
sub finalize sub finalize
@ -441,11 +448,7 @@ sub init_cores
my $shell = shift; my $shell = shift;
DPB::Task->redirect($logger->logfile("init.". DPB::Task->redirect($logger->logfile("init.".
$core->hostname)); $core->hostname));
if (defined $shell) { $shell->exec($startup);
$shell->run($startup);
} else {
exec{$startup}($startup);
}
} }
)); ));
} }
@ -689,6 +692,11 @@ sub is_local
return 1; return 1;
} }
sub shellclass
{
"DPB::Shell::Local"
}
package DPB::Core::Fetcher; package DPB::Core::Fetcher;
our @ISA = qw(DPB::Core::Local); our @ISA = qw(DPB::Core::Local);
@ -713,4 +721,56 @@ sub start
}), 'clock')); }), 'clock'));
} }
# the shell package is used to exec commands.
# note that we're dealing with exec, so we can modify the object/context
# itself with abandon
package DPB::Shell::Abstract;
sub new
{
my ($class, $host) = @_;
bless {}, $class;
}
sub chdir
{
my ($self, $dir) = @_;
$self->{dir} = $dir;
return $self;
}
sub env
{
my ($self, %h) = @_;
while (my ($k, $v) = each %h) {
$self->{env}{$k} = $v;
}
return $self;
}
package DPB::Shell::Local;
our @ISA = qw(DPB::Shell::Abstract);
sub chdir
{
my ($self, $dir) = @_;
CORE::chdir($dir) or die "Can't chdir to $dir\n";
return $self;
}
sub env
{
my ($self, %h) = @_;
while (my ($k, $v) = each %h) {
$ENV{$k} = $v;
}
return $self;
}
sub exec
{
my ($self, @argv) = @_;
exec {$argv[0]} @argv;
}
1; 1;

View File

@ -1,5 +1,5 @@
# ex:ts=8 sw=4: # ex:ts=8 sw=4:
# $OpenBSD: Distant.pm,v 1.2 2011/12/04 12:05:41 espie Exp $ # $OpenBSD: Distant.pm,v 1.3 2012/07/04 08:59:10 espie Exp $
# #
# Copyright (c) 2010 Marc Espie <espie@openbsd.org> # Copyright (c) 2010 Marc Espie <espie@openbsd.org>
# #
@ -20,6 +20,7 @@ use warnings;
use DPB::Core; use DPB::Core;
use OpenBSD::Paths; use OpenBSD::Paths;
package DPB::Ssh; package DPB::Ssh;
our @ISA = qw(DPB::Shell::Abstract);
sub ssh sub ssh
{ {
@ -34,8 +35,11 @@ sub ssh
sub new sub new
{ {
my ($class, $host, $timeout) = @_; my ($class, $host) = @_;
bless {master => DPB::Ssh::Master->find($host, $timeout)}, $class; bless {
master => DPB::Ssh::Master->find($host->name,
$host->{prop}->{timeout})
}, $class;
} }
sub is_alive sub is_alive
@ -58,7 +62,7 @@ sub hostname
shift->{master}->hostname; shift->{master}->hostname;
} }
sub run sub _run
{ {
my ($self, $cmd) = @_; my ($self, $cmd) = @_;
exec {OpenBSD::Paths->ssh} exec {OpenBSD::Paths->ssh}
@ -66,6 +70,21 @@ sub run
$self->hostname, $cmd); $self->hostname, $cmd);
} }
sub exec
{
my ($self, @argv) = @_;
if ($self->{env}) {
while (my ($k, $v) = each %{$self->{env}}) {
unshift @argv, "$k=$v";
}
}
my $cmd = join(' ', @argv);
if ($self->{dir}) {
$cmd = "cd $self->{dir} && $cmd";
}
$self->_run($cmd);
}
package DPB::Task::SshMaster; package DPB::Task::SshMaster;
our @ISA = qw(DPB::Task::Fork); our @ISA = qw(DPB::Task::Fork);
sub run sub run
@ -191,20 +210,9 @@ package DPB::Core::Distant;
our @ISA = qw(DPB::Core); our @ISA = qw(DPB::Core);
my @dead_cores = (); my @dead_cores = ();
sub new sub shellclass
{ {
my ($class, $host, $prop) = @_; "DPB::Ssh"
my $o = $class->SUPER::new($host, $prop);
$o->{shell} = DPB::Ssh->new($host);
return $o;
}
sub new_noreg
{
my ($class, $host, $prop) = @_;
my $o = $class->SUPER::new_noreg($host, $prop);
$o->{shell} = DPB::Ssh->new($host, $prop->{timeout});
return $o;
} }
sub is_alive sub is_alive

View File

@ -1,5 +1,5 @@
# ex:ts=8 sw=4: # ex:ts=8 sw=4:
# $OpenBSD: Fetch.pm,v 1.40 2012/04/10 16:50:33 espie Exp $ # $OpenBSD: Fetch.pm,v 1.41 2012/07/04 08:59:10 espie Exp $
# #
# Copyright (c) 2010 Marc Espie <espie@openbsd.org> # Copyright (c) 2010 Marc Espie <espie@openbsd.org>
# #
@ -656,20 +656,17 @@ sub run
} }
my $ftp = OpenBSD::Paths->ftp; my $ftp = OpenBSD::Paths->ftp;
$self->redirect($job->{log}); $self->redirect($job->{log});
my @cmd = ($ftp, '-C', '-o', $job->{file}->tempfilename, '-v', my @cmd = ('-C', '-o', $job->{file}->tempfilename, '-v',
$site.$job->{file}->{short}); $site.$job->{file}->{short});
if ($ftp =~ /\s/) {
unshift @cmd, split(/\s+/, $ftp);
} else {
unshift @cmd, $ftp;
}
print STDERR "===> Trying $site\n"; print STDERR "===> Trying $site\n";
print STDERR join(' ', @cmd), "\n"; print STDERR join(' ', @cmd), "\n";
# run ftp; # run ftp;
if (defined $shell) { $core->shell->exec(@cmd);
$shell->run(join(' ', @cmd));
} else {
if ($ftp =~ /\s/) {
exec join(' ', @cmd);
} else {
exec{$ftp} @cmd;
}
}
} }
sub finalize sub finalize

View File

@ -1,5 +1,5 @@
# ex:ts=8 sw=4: # ex:ts=8 sw=4:
# $OpenBSD: Port.pm,v 1.30 2012/04/21 21:09:07 espie Exp $ # $OpenBSD: Port.pm,v 1.31 2012/07/04 08:59:10 espie Exp $
# #
# Copyright (c) 2010 Marc Espie <espie@openbsd.org> # Copyright (c) 2010 Marc Espie <espie@openbsd.org>
# #
@ -65,8 +65,6 @@ sub run
my $builder = $job->{builder}; my $builder = $job->{builder};
my $ports = $builder->ports; my $ports = $builder->ports;
my $fullpkgpath = $job->{v}->fullpkgpath; my $fullpkgpath = $job->{v}->fullpkgpath;
my $sudo = OpenBSD::Paths->sudo;
my $shell = $core->{shell};
$self->handle_output($job); $self->handle_output($job);
close STDIN; close STDIN;
open STDIN, '</dev/null'; open STDIN, '</dev/null';
@ -91,28 +89,17 @@ sub run
} }
} }
if (defined $shell) { unshift(@args, @l);
unshift(@args, @l); if ($self->{sudo}) {
if ($self->{sudo}) { unshift(@args, OpenBSD::Paths->sudo, "-E");
unshift(@args, $sudo, "-E");
}
$shell->run("cd $ports && ".
join(' ', "SUBDIR=$fullpkgpath",
"PHASE=$t",
"WRAPPER_OUTPUT=$builder->{rsslog}",
@args));
} else {
chdir($ports) or
die "Wrong ports tree $ports";
$ENV{SUBDIR} = $fullpkgpath;
$ENV{PHASE} = $t;
$ENV{WRAPPER_OUTPUT} = $builder->{rsslog};
if ($self->{sudo}) {
exec {$sudo}("sudo", "-E", @l, @args);
} else {
exec {$make} (@l, @args);
}
} }
$core->shell
->chdir($ports)
->env(SUBDIR => $fullpkgpath,
PHASE => $t,
WRAPPER_OUTPUT => $builder->{rsslog})
->exec(@args);
exit(1); exit(1);
} }
@ -228,8 +215,6 @@ sub run
$self->junk_lock($core); $self->junk_lock($core);
exit(0) unless %$dep; exit(0) unless %$dep;
my $sudo = OpenBSD::Paths->sudo;
my $shell = $core->{shell};
$self->handle_output($job); $self->handle_output($job);
my @cmd = ('/usr/sbin/pkg_add', '-a'); my @cmd = ('/usr/sbin/pkg_add', '-a');
if ($job->{builder}->{update}) { if ($job->{builder}->{update}) {
@ -240,13 +225,8 @@ sub run
} }
print join(' ', @cmd, (sort keys %$dep)), "\n"; print join(' ', @cmd, (sort keys %$dep)), "\n";
my $path = $job->{builder}->{fullrepo}.'/'; my $path = $job->{builder}->{fullrepo}.'/';
if (defined $shell) { $core->shell->env(PKG_PATH => $path)
$shell->run(join(' ', "PKG_PATH=$path", $sudo, @cmd, ->exec(OpenBSD::Paths->sudo, @cmd, (sort keys %$dep));
(sort keys %$dep)));
} else {
$ENV{PKG_PATH} = $path;
exec{$sudo}($sudo, @cmd, sort keys %$dep);
}
exit(1); exit(1);
} }
@ -269,7 +249,6 @@ sub run
my $job = $core->job; my $job = $core->job;
my $v = $job->{v}; my $v = $job->{v};
my $sudo = OpenBSD::Paths->sudo;
$self->handle_output($job); $self->handle_output($job);
my @cmd = ('/usr/sbin/pkg_add'); my @cmd = ('/usr/sbin/pkg_add');
if ($job->{builder}->{update}) { if ($job->{builder}->{update}) {
@ -281,7 +260,8 @@ sub run
print join(' ', @cmd, $v->fullpkgname, "\n"); print join(' ', @cmd, $v->fullpkgname, "\n");
my $path = $job->{builder}->{fullrepo}.'/'; my $path = $job->{builder}->{fullrepo}.'/';
$ENV{PKG_PATH} = $path; $ENV{PKG_PATH} = $path;
exec{$sudo}($sudo, @cmd, $v->fullpkgname); $core->shell->env(PKG_PATH => $path)
->exec(OpenBSD::Paths->sudo, @cmd, $v->fullpkgname);
exit(1); exit(1);
} }
@ -347,7 +327,6 @@ sub run
my $job = $core->job; my $job = $core->job;
my $v = $job->{v}; my $v = $job->{v};
my $sudo = OpenBSD::Paths->sudo;
$self->handle_output($job); $self->handle_output($job);
$self->junk_lock($core); $self->junk_lock($core);
@ -355,12 +334,7 @@ sub run
$core->hostname); $core->hostname);
my @cmd = ('/usr/sbin/pkg_delete', '-aX', @d); my @cmd = ('/usr/sbin/pkg_delete', '-aX', @d);
print join(' ', @cmd, "\n"); print join(' ', @cmd, "\n");
my $shell = $core->{shell}; $core->shell->exec(OpenBSD::Paths->sudo, @cmd);
if (defined $shell) {
$shell->run(join(' ', $sudo, @cmd));
} else {
exec{$sudo}($sudo, @cmd);
}
exit(1); exit(1);
} }

View File

@ -1,5 +1,5 @@
# ex:ts=8 sw=4: # ex:ts=8 sw=4:
# $OpenBSD: Signature.pm,v 1.2 2011/06/04 12:58:24 espie Exp $ # $OpenBSD: Signature.pm,v 1.3 2012/07/04 08:59:10 espie Exp $
# #
# Copyright (c) 2010 Marc Espie <espie@openbsd.org> # Copyright (c) 2010 Marc Espie <espie@openbsd.org>
# #
@ -91,11 +91,7 @@ sub new
sub run sub run
{ {
my ($self, $core) = @_; my ($self, $core) = @_;
if (defined $core->{shell}) { $core->shell->exec("/bin/ls", $self->{dir});
$core->{shell}->run("ls $self->{dir}");
} else {
exec {"/bin/ls"} ("ls", $self->{dir});
}
} }
sub process sub process

View File

@ -1,5 +1,5 @@
# ex:ts=8 sw=4: # ex:ts=8 sw=4:
# $OpenBSD: Vars.pm,v 1.26 2012/01/29 12:02:20 espie Exp $ # $OpenBSD: Vars.pm,v 1.27 2012/07/04 08:59:10 espie Exp $
# #
# Copyright (c) 2010 Marc Espie <espie@openbsd.org> # Copyright (c) 2010 Marc Espie <espie@openbsd.org>
# #
@ -30,20 +30,11 @@ sub run_command
my $ports = $grabber->ports; my $ports = $grabber->ports;
if (defined $shell) { $shell->chdir($ports);
my $s=''; if (defined $subdirs) {
if (defined $subdirs) { $shell->env(SUBDIR => $class->subdirlist($subdirs));
$s="SUBDIR='".$class->subdirlist($subdirs)."'";
}
$shell->run("cd $ports && $s ".
join(' ', $grabber->make_args, @args));
} else {
if (defined $subdirs) {
$ENV{SUBDIR} = $class->subdirlist($subdirs);
}
chdir($ports) or die "Bad directory $ports";
exec {$grabber->make} ($grabber->make_args, @args);
} }
$shell->exec($grabber->make_args, @args);
exit(1); exit(1);
} }