regorg code, no actual change

This commit is contained in:
espie 2014-12-25 15:14:14 +00:00
parent 43b55dfa96
commit 0c44a25b22
6 changed files with 395 additions and 332 deletions

View File

@ -1,7 +1,7 @@
#! /usr/bin/perl
# ex:ts=8 sw=4:
# $OpenBSD: dpb,v 1.109 2014/12/22 09:05:24 espie Exp $
# $OpenBSD: dpb,v 1.110 2014/12/25 15:14:14 espie Exp $
#
# Copyright (c) 2010-2013 Marc Espie <espie@openbsd.org>
#
@ -106,6 +106,9 @@ use DPB::State;
use DPB::PkgPath;
use DPB::Core;
use DPB::Core::Init;
use DPB::HostProperties;
use DPB::Shell;
use DPB::Host;
use DPB::Vars;
use DPB::PortInfo;
use DPB::Engine;

View File

@ -1,5 +1,5 @@
# ex:ts=8 sw=4:
# $OpenBSD: Config.pm,v 1.29 2014/04/28 12:51:41 espie Exp $
# $OpenBSD: Config.pm,v 1.30 2014/12/25 15:14:14 espie Exp $
#
# Copyright (c) 2010-2013 Marc Espie <espie@openbsd.org>
#
@ -349,78 +349,4 @@ sub parse_hosts_file
}
}
package DPB::HostProperties;
my $has_sf = 0;
my $has_mem = 0;
my $sf;
sub new
{
my ($class, $default) = @_;
$default //= {};
bless {%$default}, $class;
}
sub add_overrides
{
my ($prop, $override) = @_;
while (my ($k, $v) = each %$override) {
$prop->{$k} = $v;
}
$sf //= $prop->{sf};
if (defined $prop->{sf} && $prop->{sf} != $sf) {
$has_sf = 1;
}
}
sub has_sf
{
return $has_sf;
}
sub has_mem
{
return $has_mem;
}
my $default_user;
sub finalize
{
my ($class, $prop) = @_;
$prop->{sf} //= 1;
$prop->{umask} //= sprintf("0%o", umask);
if (defined $prop->{stuck}) {
$prop->{stuck_timeout} = $prop->{stuck} * $prop->{sf};
}
if (defined $prop->{mem}) {
$prop->{memory} = $prop->{mem};
}
if (defined $prop->{chroot}) {
if ($prop->{chroot} eq '/' || $prop->{chroot} eq '') {
delete $prop->{chroot};
} else {
if (!defined $prop->{chroot_user}) {
$prop->{chroot_user} = $prop->{user};
}
}
}
if (defined $prop->{memory}) {
my $m = $prop->{memory};
if ($m =~ s/K$//) {
} elsif ($m =~ s/M$//) {
$m *= 1024;
} elsif ($m =~ s/G$//) {
$m *= 1024 * 1024;
}
$prop->{memory} = $m;
if ($prop->{memory} > 0) {
$has_mem = 1;
}
}
$prop->{small} //= 120;
$prop->{small_timeout} = $prop->{small} * $prop->{sf};
return $prop;
}
1;

View File

@ -1,5 +1,5 @@
# ex:ts=8 sw=4:
# $OpenBSD: Core.pm,v 1.74 2014/12/07 15:18:50 espie Exp $
# $OpenBSD: Core.pm,v 1.75 2014/12/25 15:14:14 espie Exp $
#
# Copyright (c) 2010-2013 Marc Espie <espie@openbsd.org>
#
@ -18,97 +18,6 @@ use strict;
use warnings;
use DPB::Util;
# we have unique objects for hosts, so we can put properties in there.
package DPB::Host;
my $hosts = {};
sub shell
{
my $self = shift;
return $self->{shell};
}
sub new
{
my ($class, $name, $prop) = @_;
if ($class->name_is_localhost($name)) {
$class = "DPB::Host::Localhost";
$name = 'localhost';
} else {
require DPB::Core::Distant;
$class = "DPB::Host::Distant";
}
if (!defined $hosts->{$name}) {
my $h = bless {host => $name,
prop => DPB::HostProperties->finalize($prop) }, $class;
# XXX have to register *before* creating the shell
$hosts->{$name} = $h;
$h->{shell} = $h->shellclass->new($h);
}
return $hosts->{$name};
}
sub name
{
my $self = shift;
return $self->{host};
}
sub fullname
{
my $self = shift;
my $name = $self->name;
if (defined $self->{prop}->{jobs}) {
$name .= "/$self->{prop}->{jobs}";
}
return $name;
}
sub name_is_localhost
{
my ($class, $host) = @_;
if ($host eq "localhost" or $host eq DPB::Core::Local->hostname) {
return 1;
} else {
return 0;
}
}
package DPB::Host::Localhost;
our @ISA = qw(DPB::Host);
sub is_localhost
{
return 1;
}
sub is_alive
{
return 1;
}
sub shellclass
{
my $self = shift;
if ($self->{prop}->{chroot}) {
return "DPB::Shell::Local::Chroot";
} else {
return "DPB::Shell::Local";
}
}
sub getshell
{
my ($class, $state) = @_;
my $h = bless { prop => {}}, $class;
if ($state->{chroot}) {
$h->{prop}{chroot} = $state->{chroot};
}
return $h->shellclass->new($h);
}
# here, a "core" is an entity responsible for scheduling cpu, such as
# running a job, which is a collection of tasks.
#
@ -860,168 +769,4 @@ 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) = @_;
$host //= {}; # this makes it possible to build "localhost" shells
bless {sudo => 0, prop => $host->{prop}}, $class;
}
sub prop
{
my $self = shift;
return $self->{prop};
}
sub stringize_master_pid
{
return "";
}
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;
}
sub sudo
{
my ($self, $val) = @_;
# XXX calling sudo without parms is equivalent to saying "1"
if (@_ == 1) {
$val = 1;
}
$self->{sudo} = $val;
return $self;
}
sub nochroot
{
my $self = shift;
$self->{nochroot} = 1;
return $self;
}
package DPB::Shell::Local;
our @ISA = qw(DPB::Shell::Abstract);
sub is_alive
{
return 1;
}
sub chdir
{
my ($self, $dir) = @_;
CORE::chdir($dir) or DPB::Util->die_bang("Can't chdir to $dir");
return $self;
}
sub env
{
my ($self, %h) = @_;
while (my ($k, $v) = each %h) {
$ENV{$k} = $v;
}
return $self;
}
sub exec
{
my ($self, @argv) = @_;
if ($self->{sudo}) {
unshift(@argv, OpenBSD::Paths->sudo, "-E");
}
if (-t STDIN) {
close(STDIN);
open STDIN, '</dev/null';
}
exec {$argv[0]} @argv;
}
package DPB::Shell::Chroot;
our @ISA = qw(DPB::Shell::Abstract);
sub exec
{
my ($self, @argv) = @_;
my $chroot = $self->prop->{chroot};
if ($self->{nochroot}) {
undef $chroot;
}
unshift @argv, 'exec' unless $self->{sudo} && !$chroot;
if ($self->{env}) {
while (my ($k, $v) = each %{$self->{env}}) {
$v //= '';
unshift @argv, "$k=\'$v\'";
}
}
if ($self->{sudo} && !$chroot) {
unshift(@argv, 'exec', OpenBSD::Paths->sudo, "-E");
}
my $cmd = join(' ', @argv);
if ($self->{dir}) {
$cmd = "cd $self->{dir} && $cmd";
}
if (defined $self->prop->{umask}) {
my $umask = $self->prop->{umask};
$cmd = "umask $umask && $cmd";
}
if ($chroot) {
my @cmd2 = ("chroot");
if (!$self->prop->{iamroot}) {
unshift(@cmd2, OpenBSD::Paths->sudo, "-E");
}
if (!$self->{sudo} && defined $self->prop->{chroot_user}) {
push(@cmd2, "-u", $self->prop->{chroot_user});
}
$self->_run(@cmd2, $chroot, "/bin/sh", "-c", $self->quote($cmd));
} else {
$self->_run($cmd);
}
}
package DPB::Shell::Local::Chroot;
our @ISA = qw(DPB::Shell::Chroot);
sub _run
{
my ($self, @argv) = @_;
if (-t STDIN) {
close(STDIN);
open STDIN, '</dev/null';
}
exec {$argv[0]} @argv;
}
sub quote
{
my ($self, $cmd) = @_;
return $cmd;
}
sub is_alive
{
return 1;
}
sub nochroot
{
my $self = shift;
bless $self, 'DPB::Shell::Local';
}
1;

View File

@ -0,0 +1,110 @@
# ex:ts=8 sw=4:
# $OpenBSD: Host.pm,v 1.1 2014/12/25 15:14:14 espie Exp $
#
# Copyright (c) 2010-2013 Marc Espie <espie@openbsd.org>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
use strict;
use warnings;
# we have unique objects for hosts, so we can put properties in there.
package DPB::Host;
my $hosts = {};
sub shell
{
my $self = shift;
return $self->{shell};
}
sub new
{
my ($class, $name, $prop) = @_;
if ($class->name_is_localhost($name)) {
$class = "DPB::Host::Localhost";
$name = 'localhost';
} else {
require DPB::Core::Distant;
$class = "DPB::Host::Distant";
}
if (!defined $hosts->{$name}) {
my $h = bless {host => $name,
prop => DPB::HostProperties->finalize($prop) }, $class;
# XXX have to register *before* creating the shell
$hosts->{$name} = $h;
$h->{shell} = $h->shellclass->new($h);
}
return $hosts->{$name};
}
sub name
{
my $self = shift;
return $self->{host};
}
sub fullname
{
my $self = shift;
my $name = $self->name;
if (defined $self->{prop}->{jobs}) {
$name .= "/$self->{prop}->{jobs}";
}
return $name;
}
sub name_is_localhost
{
my ($class, $host) = @_;
if ($host eq "localhost" or $host eq DPB::Core::Local->hostname) {
return 1;
} else {
return 0;
}
}
package DPB::Host::Localhost;
our @ISA = qw(DPB::Host);
sub is_localhost
{
return 1;
}
sub is_alive
{
return 1;
}
sub shellclass
{
my $self = shift;
if ($self->{prop}->{chroot}) {
return "DPB::Shell::Local::Chroot";
} else {
return "DPB::Shell::Local";
}
}
sub getshell
{
my ($class, $state) = @_;
my $h = bless { prop => {}}, $class;
if ($state->{chroot}) {
$h->{prop}{chroot} = $state->{chroot};
}
return $h->shellclass->new($h);
}
1;

View File

@ -0,0 +1,95 @@
# ex:ts=8 sw=4:
# $OpenBSD: HostProperties.pm,v 1.1 2014/12/25 15:14:14 espie Exp $
#
# Copyright (c) 2010-2013 Marc Espie <espie@openbsd.org>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
use strict;
use warnings;
package DPB::HostProperties;
my $has_sf = 0;
my $has_mem = 0;
my $sf;
sub new
{
my ($class, $default) = @_;
$default //= {};
bless {%$default}, $class;
}
sub add_overrides
{
my ($prop, $override) = @_;
while (my ($k, $v) = each %$override) {
$prop->{$k} = $v;
}
$sf //= $prop->{sf};
if (defined $prop->{sf} && $prop->{sf} != $sf) {
$has_sf = 1;
}
}
sub has_sf
{
return $has_sf;
}
sub has_mem
{
return $has_mem;
}
my $default_user;
sub finalize
{
my ($class, $prop) = @_;
$prop->{sf} //= 1;
$prop->{umask} //= sprintf("0%o", umask);
if (defined $prop->{stuck}) {
$prop->{stuck_timeout} = $prop->{stuck} * $prop->{sf};
}
if (defined $prop->{mem}) {
$prop->{memory} = $prop->{mem};
}
if (defined $prop->{chroot}) {
if ($prop->{chroot} eq '/' || $prop->{chroot} eq '') {
delete $prop->{chroot};
} else {
if (!defined $prop->{chroot_user}) {
$prop->{chroot_user} = $prop->{user};
}
}
}
if (defined $prop->{memory}) {
my $m = $prop->{memory};
if ($m =~ s/K$//) {
} elsif ($m =~ s/M$//) {
$m *= 1024;
} elsif ($m =~ s/G$//) {
$m *= 1024 * 1024;
}
$prop->{memory} = $m;
if ($prop->{memory} > 0) {
$has_mem = 1;
}
}
$prop->{small} //= 120;
$prop->{small_timeout} = $prop->{small} * $prop->{sf};
return $prop;
}
1;

View File

@ -0,0 +1,184 @@
# ex:ts=8 sw=4:
# $OpenBSD: Shell.pm,v 1.1 2014/12/25 15:14:14 espie Exp $
#
# Copyright (c) 2010-2014 Marc Espie <espie@openbsd.org>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
use strict;
use warnings;
# 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) = @_;
$host //= {}; # this makes it possible to build "localhost" shells
bless {sudo => 0, prop => $host->{prop}}, $class;
}
sub prop
{
my $self = shift;
return $self->{prop};
}
sub stringize_master_pid
{
return "";
}
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;
}
sub sudo
{
my ($self, $val) = @_;
# XXX calling sudo without parms is equivalent to saying "1"
if (@_ == 1) {
$val = 1;
}
$self->{sudo} = $val;
return $self;
}
sub nochroot
{
my $self = shift;
$self->{nochroot} = 1;
return $self;
}
package DPB::Shell::Local;
our @ISA = qw(DPB::Shell::Abstract);
sub is_alive
{
return 1;
}
sub chdir
{
my ($self, $dir) = @_;
CORE::chdir($dir) or DPB::Util->die_bang("Can't chdir to $dir");
return $self;
}
sub env
{
my ($self, %h) = @_;
while (my ($k, $v) = each %h) {
$ENV{$k} = $v;
}
return $self;
}
sub exec
{
my ($self, @argv) = @_;
if ($self->{sudo}) {
unshift(@argv, OpenBSD::Paths->sudo, "-E");
}
if (-t STDIN) {
close(STDIN);
open STDIN, '</dev/null';
}
exec {$argv[0]} @argv;
}
package DPB::Shell::Chroot;
our @ISA = qw(DPB::Shell::Abstract);
sub exec
{
my ($self, @argv) = @_;
my $chroot = $self->prop->{chroot};
if ($self->{nochroot}) {
undef $chroot;
}
unshift @argv, 'exec' unless $self->{sudo} && !$chroot;
if ($self->{env}) {
while (my ($k, $v) = each %{$self->{env}}) {
$v //= '';
unshift @argv, "$k=\'$v\'";
}
}
if ($self->{sudo} && !$chroot) {
unshift(@argv, 'exec', OpenBSD::Paths->sudo, "-E");
}
my $cmd = join(' ', @argv);
if ($self->{dir}) {
$cmd = "cd $self->{dir} && $cmd";
}
if (defined $self->prop->{umask}) {
my $umask = $self->prop->{umask};
$cmd = "umask $umask && $cmd";
}
if ($chroot) {
my @cmd2 = ("chroot");
if (!$self->prop->{iamroot}) {
unshift(@cmd2, OpenBSD::Paths->sudo, "-E");
}
if (!$self->{sudo} && defined $self->prop->{chroot_user}) {
push(@cmd2, "-u", $self->prop->{chroot_user});
}
$self->_run(@cmd2, $chroot, "/bin/sh", "-c", $self->quote($cmd));
} else {
$self->_run($cmd);
}
}
package DPB::Shell::Local::Chroot;
our @ISA = qw(DPB::Shell::Chroot);
sub _run
{
my ($self, @argv) = @_;
if (-t STDIN) {
close(STDIN);
open STDIN, '</dev/null';
}
exec {$argv[0]} @argv;
}
sub quote
{
my ($self, $cmd) = @_;
return $cmd;
}
sub is_alive
{
return 1;
}
sub nochroot
{
my $self = shift;
bless $self, 'DPB::Shell::Local';
}
1;