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:
# $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>
#
@ -106,10 +106,23 @@ sub is_alive
return 1;
}
sub shellclass
{
"DPB::Shell::Abstract"
}
sub shell
{
my $self = shift;
return $self->{shell};
}
sub new
{
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
@ -353,13 +366,7 @@ our @ISA = qw(DPB::Task::Pipe);
sub run
{
my ($self, $core) = @_;
my $shell = $core->{shell};
my $sysctl = OpenBSD::Paths->sysctl;
if (defined $shell) {
$shell->run("$sysctl -n hw.ncpu");
} else {
exec{$sysctl} ($sysctl, '-n', 'hw.ncpu');
}
$core->shell->exec(OpenBSD::Path->sysctl, '-n', 'hw.ncpu');
}
sub finalize
@ -441,11 +448,7 @@ sub init_cores
my $shell = shift;
DPB::Task->redirect($logger->logfile("init.".
$core->hostname));
if (defined $shell) {
$shell->run($startup);
} else {
exec{$startup}($startup);
}
$shell->exec($startup);
}
));
}
@ -689,6 +692,11 @@ sub is_local
return 1;
}
sub shellclass
{
"DPB::Shell::Local"
}
package DPB::Core::Fetcher;
our @ISA = qw(DPB::Core::Local);
@ -713,4 +721,56 @@ sub start
}), '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;

View File

@ -1,5 +1,5 @@
# 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>
#
@ -20,6 +20,7 @@ use warnings;
use DPB::Core;
use OpenBSD::Paths;
package DPB::Ssh;
our @ISA = qw(DPB::Shell::Abstract);
sub ssh
{
@ -34,8 +35,11 @@ sub ssh
sub new
{
my ($class, $host, $timeout) = @_;
bless {master => DPB::Ssh::Master->find($host, $timeout)}, $class;
my ($class, $host) = @_;
bless {
master => DPB::Ssh::Master->find($host->name,
$host->{prop}->{timeout})
}, $class;
}
sub is_alive
@ -58,7 +62,7 @@ sub hostname
shift->{master}->hostname;
}
sub run
sub _run
{
my ($self, $cmd) = @_;
exec {OpenBSD::Paths->ssh}
@ -66,6 +70,21 @@ sub run
$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;
our @ISA = qw(DPB::Task::Fork);
sub run
@ -191,20 +210,9 @@ package DPB::Core::Distant;
our @ISA = qw(DPB::Core);
my @dead_cores = ();
sub new
sub shellclass
{
my ($class, $host, $prop) = @_;
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;
"DPB::Ssh"
}
sub is_alive

View File

@ -1,5 +1,5 @@
# 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>
#
@ -656,20 +656,17 @@ sub run
}
my $ftp = OpenBSD::Paths->ftp;
$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});
if ($ftp =~ /\s/) {
unshift @cmd, split(/\s+/, $ftp);
} else {
unshift @cmd, $ftp;
}
print STDERR "===> Trying $site\n";
print STDERR join(' ', @cmd), "\n";
# run ftp;
if (defined $shell) {
$shell->run(join(' ', @cmd));
} else {
if ($ftp =~ /\s/) {
exec join(' ', @cmd);
} else {
exec{$ftp} @cmd;
}
}
$core->shell->exec(@cmd);
}
sub finalize

View File

@ -1,5 +1,5 @@
# 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>
#
@ -65,8 +65,6 @@ sub run
my $builder = $job->{builder};
my $ports = $builder->ports;
my $fullpkgpath = $job->{v}->fullpkgpath;
my $sudo = OpenBSD::Paths->sudo;
my $shell = $core->{shell};
$self->handle_output($job);
close STDIN;
open STDIN, '</dev/null';
@ -91,28 +89,17 @@ sub run
}
}
if (defined $shell) {
unshift(@args, @l);
if ($self->{sudo}) {
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);
}
unshift(@args, @l);
if ($self->{sudo}) {
unshift(@args, OpenBSD::Paths->sudo, "-E");
}
$core->shell
->chdir($ports)
->env(SUBDIR => $fullpkgpath,
PHASE => $t,
WRAPPER_OUTPUT => $builder->{rsslog})
->exec(@args);
exit(1);
}
@ -228,8 +215,6 @@ sub run
$self->junk_lock($core);
exit(0) unless %$dep;
my $sudo = OpenBSD::Paths->sudo;
my $shell = $core->{shell};
$self->handle_output($job);
my @cmd = ('/usr/sbin/pkg_add', '-a');
if ($job->{builder}->{update}) {
@ -240,13 +225,8 @@ sub run
}
print join(' ', @cmd, (sort keys %$dep)), "\n";
my $path = $job->{builder}->{fullrepo}.'/';
if (defined $shell) {
$shell->run(join(' ', "PKG_PATH=$path", $sudo, @cmd,
(sort keys %$dep)));
} else {
$ENV{PKG_PATH} = $path;
exec{$sudo}($sudo, @cmd, sort keys %$dep);
}
$core->shell->env(PKG_PATH => $path)
->exec(OpenBSD::Paths->sudo, @cmd, (sort keys %$dep));
exit(1);
}
@ -269,7 +249,6 @@ sub run
my $job = $core->job;
my $v = $job->{v};
my $sudo = OpenBSD::Paths->sudo;
$self->handle_output($job);
my @cmd = ('/usr/sbin/pkg_add');
if ($job->{builder}->{update}) {
@ -281,7 +260,8 @@ sub run
print join(' ', @cmd, $v->fullpkgname, "\n");
my $path = $job->{builder}->{fullrepo}.'/';
$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);
}
@ -347,7 +327,6 @@ sub run
my $job = $core->job;
my $v = $job->{v};
my $sudo = OpenBSD::Paths->sudo;
$self->handle_output($job);
$self->junk_lock($core);
@ -355,12 +334,7 @@ sub run
$core->hostname);
my @cmd = ('/usr/sbin/pkg_delete', '-aX', @d);
print join(' ', @cmd, "\n");
my $shell = $core->{shell};
if (defined $shell) {
$shell->run(join(' ', $sudo, @cmd));
} else {
exec{$sudo}($sudo, @cmd);
}
$core->shell->exec(OpenBSD::Paths->sudo, @cmd);
exit(1);
}

View File

@ -1,5 +1,5 @@
# 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>
#
@ -91,11 +91,7 @@ sub new
sub run
{
my ($self, $core) = @_;
if (defined $core->{shell}) {
$core->{shell}->run("ls $self->{dir}");
} else {
exec {"/bin/ls"} ("ls", $self->{dir});
}
$core->shell->exec("/bin/ls", $self->{dir});
}
sub process

View File

@ -1,5 +1,5 @@
# 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>
#
@ -30,20 +30,11 @@ sub run_command
my $ports = $grabber->ports;
if (defined $shell) {
my $s='';
if (defined $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->chdir($ports);
if (defined $subdirs) {
$shell->env(SUBDIR => $class->subdirlist($subdirs));
}
$shell->exec($grabber->make_args, @args);
exit(1);
}