Init.pm 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. # ex:ts=8 sw=4:
  2. # $OpenBSD: Init.pm,v 1.28 2015/07/27 17:19:46 espie Exp $
  3. #
  4. # Copyright (c) 2010-2013 Marc Espie <espie@openbsd.org>
  5. #
  6. # Permission to use, copy, modify, and distribute this software for any
  7. # purpose with or without fee is hereby granted, provided that the above
  8. # copyright notice and this permission notice appear in all copies.
  9. #
  10. # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  11. # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  12. # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  13. # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  14. # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  15. # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  16. # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  17. use strict;
  18. use warnings;
  19. use DPB::Core;
  20. # this is the code responsible for initializing all cores
  21. package DPB::Task::Ncpu;
  22. our @ISA = qw(DPB::Task::Pipe);
  23. sub run
  24. {
  25. my ($self, $core) = @_;
  26. $core->shell->exec(OpenBSD::Paths->sysctl, '-n', 'hw.ncpu');
  27. }
  28. sub finalize
  29. {
  30. my ($self, $core) = @_;
  31. my $fh = $self->{fh};
  32. if ($core->{status} == 0) {
  33. my $line = <$fh>;
  34. chomp $line;
  35. if ($line =~ m/^\d+$/) {
  36. $core->prop->{jobs} = $line;
  37. }
  38. }
  39. close($fh);
  40. $core->prop->{jobs} //= 1;
  41. return 1;
  42. }
  43. package DPB::Task::WhoAmI;
  44. our @ISA = qw(DPB::Task::Pipe);
  45. sub run
  46. {
  47. my ($self, $core) = @_;
  48. $core->shell->nochroot->exec('/usr/bin/whoami');
  49. }
  50. sub finalize
  51. {
  52. my ($self, $core) = @_;
  53. my $fh = $self->{fh};
  54. if ($core->{status} == 0) {
  55. my $line = <$fh>;
  56. chomp $line;
  57. if ($line =~ m/^root$/) {
  58. $core->prop->{iamroot} = 1;
  59. }
  60. }
  61. close($fh);
  62. &{$self->{extra_code}};
  63. return 1;
  64. }
  65. package DPB::Job::Init;
  66. our @ISA = qw(DPB::Job);
  67. use DPB::Signature;
  68. sub new
  69. {
  70. my ($class, $logger) = @_;
  71. my $o = $class->SUPER::new('init');
  72. $o->{logger} = $logger;
  73. return $o;
  74. }
  75. # if everything is okay, we mark our jobs as ready
  76. sub finalize
  77. {
  78. my ($self, $core) = @_;
  79. $self->{signature}->print_out($core, $self->{logger});
  80. if ($self->{signature}->matches($core, $self->{logger})) {
  81. if (defined $core->prop->{squiggles}) {
  82. $core->host->{wantsquiggles} = $core->prop->{squiggles};
  83. } elsif ($core->prop->{jobs} > 3) {
  84. $core->host->{wantsquiggles} = 1;
  85. } elsif ($core->prop->{jobs} > 1) {
  86. $core->host->{wantsquiggles} = 0.8;
  87. }
  88. for my $i (1 .. $core->prop->{jobs}) {
  89. $core->clone->mark_ready;
  90. }
  91. return 1;
  92. } else {
  93. return 0;
  94. }
  95. }
  96. # this is a weird one !
  97. package DPB::Core::Init;
  98. our @ISA = qw(DPB::Core::WithJobs);
  99. my $init = {};
  100. sub new
  101. {
  102. my ($class, $host, $prop) = @_;
  103. if (DPB::Host->name_is_localhost($host)) {
  104. $host = 'localhost';
  105. $prop->{iamroot} = $< == 0;
  106. }
  107. return $init->{$host} //= DPB::Core->new_noreg($host, $prop);
  108. }
  109. sub hostcount
  110. {
  111. return scalar(keys %$init);
  112. }
  113. sub taint
  114. {
  115. my ($class, $host, $tag, $source) = @_;
  116. if (defined $init->{$host}) {
  117. $init->{$host}->prop->{tainted} = $tag;
  118. $init->{$host}->prop->{tainted_source} = $source;
  119. }
  120. }
  121. sub alive_hosts
  122. {
  123. my @l = ();
  124. while (my ($host, $c) = each %$init) {
  125. if (defined $c->prop->{tainted}) {
  126. $host = "$host(".$c->prop->{tainted}.")";
  127. }
  128. if ($c->is_alive) {
  129. push(@l, $host.$c->shell->stringize_master_pid);
  130. } else {
  131. push(@l, $host.'-');
  132. }
  133. }
  134. return "Hosts: ".join(' ', sort(@l))."\n";
  135. }
  136. sub changed_hosts
  137. {
  138. my @l = ();
  139. while (my ($host, $c) = each %$init) {
  140. my $was_alive = $c->{is_alive};
  141. if ($c->is_alive) {
  142. $c->{is_alive} = 1;
  143. } else {
  144. $c->{is_alive} = 0;
  145. }
  146. if ($was_alive && !$c->{is_alive}) {
  147. push(@l, "$host went down\n");
  148. } elsif (!$was_alive && $c->{is_alive}) {
  149. push(@l, "$host came up\n");
  150. }
  151. }
  152. return join('', sort(@l));
  153. }
  154. DPB::Core->register_report(\&alive_hosts, \&changed_hosts);
  155. sub cores
  156. {
  157. return values %$init;
  158. }
  159. sub add_startup
  160. {
  161. my ($self, $state, $logger, $core, $job, @startup) = @_;
  162. my $fetch = $state->{fetch_user};
  163. my $prop = $core->prop;
  164. my $build = $prop->{build_user};
  165. $job->add_tasks(DPB::Task::Fork->new(
  166. sub {
  167. my $shell = shift;
  168. $state->{log_user}->run_as(
  169. sub {
  170. DPB::Task->redirect(
  171. $logger->logfile("init.".
  172. $core->hostname));
  173. });
  174. $shell
  175. ->chdir($state->ports)
  176. ->as_root
  177. ->env(PORTSDIR => $state->ports,
  178. MAKE => $state->make,
  179. WRKOBJDIR => $prop->{wrkobjdir},
  180. LOCKDIR => $prop->{portslockdir},
  181. BUILD_USER => $build->{user},
  182. BUILD_GROUP => $build->{group},
  183. FETCH_USER => $fetch->{user},
  184. FETCH_GROUP => $fetch->{group})
  185. ->exec(@startup);
  186. }
  187. ));
  188. }
  189. sub init_cores
  190. {
  191. my ($self, $state) = @_;
  192. my $logger = $state->logger;
  193. my $startup = $state->{startup_script};
  194. my $stale = $state->stalelocks;
  195. DPB::Core->set_logdir($logger->{logdir});
  196. if (values %$init == 0) {
  197. $state->fatal("configuration error: no job runner");
  198. }
  199. for my $core (values %$init) {
  200. my $job = DPB::Job::Init->new($logger);
  201. my $t = DPB::Task::WhoAmI->new;
  202. # XXX can't get these before I know who I am
  203. $t->{extra_code} = sub {
  204. my $prop = $core->prop;
  205. ($prop->{wrkobjdir}, $prop->{portslockdir}) =
  206. DPB::Vars->get($core->shell, $state->{make},
  207. "WRKOBJDIR", "LOCKDIR");
  208. };
  209. $job->add_tasks($t);
  210. if (!defined $core->prop->{jobs}) {
  211. $job->add_tasks(DPB::Task::Ncpu->new);
  212. }
  213. DPB::Signature->add_tasks($state->{xenocara}, $job);
  214. # $self->add_startup($state, $logger, $core, $job, "/bin/sh",
  215. # $state->ports."/infrastructure/bin/default-dpb-startup");
  216. if (defined $startup) {
  217. $self->add_startup($state, $logger, $core, $job,
  218. split(/\s+/, $startup));
  219. }
  220. my $tag = $state->locker->find_tag($core->hostname);
  221. if (defined $tag) {
  222. $core->prop->{tainted} = $tag;
  223. }
  224. if (defined $stale->{$core->hostname}) {
  225. my $subdirlist=join(' ', @{$stale->{$core->hostname}});
  226. $job->add_tasks(DPB::Task::Fork->new(
  227. sub {
  228. my $shell = shift;
  229. DPB::Task->redirect($logger->logfile("init.".
  230. $core->hostname));
  231. $shell
  232. ->chdir($state->ports)
  233. ->env(SUBDIR => $subdirlist)
  234. ->exec($state->make, 'unlock');
  235. }
  236. ));
  237. }
  238. $core->start_job($job);
  239. }
  240. $state->{default_prop}{fetch_user} //= $state->{fetch_user};
  241. if ($state->opt('f')) {
  242. $state->{fetch_user}->enforce_local;
  243. for (1 .. $state->opt('f')) {
  244. DPB::Core::Fetcher->new('localhost',
  245. $state->{default_prop})->mark_ready;
  246. }
  247. }
  248. }
  249. 1;