Config.pm 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. # ex:ts=8 sw=4:
  2. # $OpenBSD: Config.pm,v 1.69 2017/04/14 16:39:32 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. # all the code responsible for handling command line options and the
  20. # config file.
  21. package DPB::Config;
  22. use DPB::User;
  23. sub setup_users
  24. {
  25. my ($class, $state) = @_;
  26. for my $u (qw(build_user log_user fetch_user port_user)) {
  27. my $U = uc($u);
  28. if ($state->defines($U)) {
  29. $state->{$u} = DPB::User->new($state->defines($U));
  30. }
  31. if (defined $state->{$u}) {
  32. if ($state->defines("DIRMODE")) {
  33. $state->{$u}{dirmode} =
  34. oct($state->defines("DIRMODE"));
  35. }
  36. if ($state->defines("DROPPRIV")) {
  37. $state->{$u}{droppriv} =
  38. $state->defines("DROPPRIV");
  39. }
  40. }
  41. }
  42. my $u = DPB::User->new('_dpb');
  43. if (defined $u->{uid}) {
  44. $state->{unpriv_user} = $u;
  45. } else {
  46. $state->fatal("No _dpb user");
  47. }
  48. $state->{unpriv_user}->enforce_local;
  49. $> = $state->{unpriv_user}{uid};
  50. $) = $state->{unpriv_user}{grouplist};
  51. }
  52. sub parse_command_line
  53. {
  54. my ($class, $state) = @_;
  55. $state->{dontclean} = {};
  56. $state->{opt} = {
  57. A => sub {
  58. $state->{arch} = shift;
  59. },
  60. L => sub {
  61. $state->{flogdir} = shift;
  62. },
  63. l => sub {
  64. $state->{flockdir} = shift;
  65. },
  66. r => sub {
  67. $state->{random} = 1;
  68. $state->heuristics->random;
  69. },
  70. P => sub {
  71. push(@{$state->{paths}}, shift);
  72. },
  73. I => sub {
  74. push(@{$state->{ipaths}}, shift);
  75. },
  76. C => sub {
  77. push(@{$state->{cpaths}}, shift);
  78. },
  79. X => sub {
  80. push(@{$state->{xpaths}}, shift);
  81. },
  82. b => sub {
  83. push(@{$state->{build_files}}, shift);
  84. },
  85. h => sub {
  86. push(@{$state->{config_files}}, shift);
  87. },
  88. };
  89. $state->SUPER_handle_options('aceimqrRstuUvh:S:xX:A:B:C:f:F:I:j:J:M:p:P:b:l:L:',
  90. "[-aceimqrRsuUvx] [-A arch] [-B chroot] [-C plist] [-f m] [-F m]",
  91. "[-I pathlist] [-J p] [-j n] [-p parallel] [-P pathlist] [-h hosts]",
  92. "[-L logdir] [-l lockdir] [-b log] [-M threshold] [-X pathlist]",
  93. "[pathlist ...]");
  94. for my $l (qw(j f F)) {
  95. my $o = $state->{opt}{$l};
  96. if (defined $o && $o !~ m/^\d+$/) {
  97. $state->usage("-$l takes an integer argument, not $o");
  98. }
  99. }
  100. if ($state->opt('i')) {
  101. require DPB::Interactive;
  102. $state->{interactive} = DPB::Interactive->new;
  103. }
  104. $state->{chroot} = $state->opt('B');
  105. $state->{base_user} = DPB::User->from_uid($<);
  106. if (!defined $state->{base_user}) {
  107. $state->usage("Can't figure out who I am");
  108. }
  109. if ($state->{base_user}{uid} != 0) {
  110. $state->errsay("Running dpb as root with a build_user is the preferred setup");
  111. }
  112. $class->setup_users($state);
  113. ($state->{ports}, $state->{localarch},
  114. $state->{distdir}, $state->{xenocara}) =
  115. DPB::Vars->get(DPB::Host::Localhost->getshell($state),
  116. $state->make,
  117. "PORTSDIR", "MACHINE_ARCH", "DISTDIR",
  118. "PORTS_BUILD_XENOCARA_TOO");
  119. if (!defined $state->{ports}) {
  120. $state->usage("Can't obtain vital information from the ports tree");
  121. }
  122. $state->{arch} //= $state->{localarch};
  123. if (defined $state->{opt}{F}) {
  124. if (defined $state->{opt}{j} || defined $state->{opt}{f}) {
  125. $state->usage("Can't use -F with -f or -j");
  126. }
  127. $state->{fetch_only} = 1;
  128. $state->{opt}{f} = $state->{opt}{F};
  129. }
  130. if (defined $state->opt('j')) {
  131. if ($state->localarch ne $state->arch) {
  132. $state->usage(
  133. "Can't use -j if -A arch is not local architecture");
  134. }
  135. }
  136. $state->{realports} = $state->anchor($state->{ports});
  137. $state->{realdistdir} = $state->anchor($state->{distdir});
  138. if (defined $state->{config_files}) {
  139. for my $f (@{$state->{config_files}}) {
  140. $f = $state->expand_path($f);
  141. }
  142. }
  143. # keep cmdline subst values
  144. my %cmdline = %{$state->{subst}};
  145. $class->parse_config_files($state);
  146. # ... as those must override the config files contents
  147. while (my ($k, $v) = each %cmdline) {
  148. $state->{subst}->{$k} = $v;
  149. }
  150. $class->setup_users($state);
  151. $state->{build_user} //= $state->{default_prop}{build_user};
  152. if ($state->{base_user}{uid} == 0) {
  153. $state->{fetch_user} //= DPB::User->new('_pfetch');
  154. }
  155. if (!defined $state->{port_user}) {
  156. my ($uid, $gid) = (stat $state->{realports})[4,5];
  157. $state->{port_user} = DPB::User->from_uid($uid, $gid);
  158. }
  159. if (!defined $state->{build_user}) {
  160. $state->{build_user} = $state->{base_user};
  161. }
  162. $state->{log_user} //= $state->{build_user};
  163. $state->{fetch_user} //= $state->{build_user};
  164. $state->{log_user}->enforce_local;
  165. $state->{chroot} = $state->{default_prop}{chroot};
  166. # reparse things properly now that we can chroot
  167. my $p;
  168. ($state->{ports}, $state->{portspath}, $state->{repo}, $state->{localarch},
  169. $state->{distdir}, $state->{localbase}, $state->{xenocara}, $p) =
  170. DPB::Vars->get(DPB::Host::Localhost->getshell($state),
  171. $state->make,
  172. "PORTSDIR", "PORTSDIR_PATH", "PACKAGE_REPOSITORY",
  173. "MACHINE_ARCH", "DISTDIR", "LOCALBASE",
  174. "PORTS_BUILD_XENOCARA_TOO", "SIGNING_PARAMETERS");
  175. if (!defined $state->{portspath}) {
  176. $state->usage("Can't obtain vital information from the ports tree");
  177. }
  178. if ($state->{xenocara} =~ m/Yes/i) {
  179. $state->{xenocara} = 1;
  180. } else {
  181. $state->{xenocara} = 0;
  182. }
  183. $state->{portspath} = [ map {$state->anchor($_)} split(/:/, $state->{portspath}) ];
  184. $state->{realports} = $state->anchor($state->{ports});
  185. $state->{realdistdir} = $state->anchor($state->{distdir});
  186. if (!defined $state->{port_user}) {
  187. my ($uid, $gid) = (stat $state->{realports})[4,5];
  188. $state->{port_user} = DPB::User->from_uid($uid, $gid);
  189. }
  190. $state->say("Started as: #1", $state->{base_user}->user);
  191. $state->say("Port user: #1", $state->{port_user}->user);
  192. $state->say("Build user: #1", $state->{build_user}->user);
  193. $state->say("Fetch user: #1", $state->{fetch_user}->user);
  194. $state->say("Log user: #1", $state->{log_user}->user);
  195. $state->say("Unpriv user: #1", $state->{unpriv_user}->user);
  196. $state->{logdir} = $state->{flogdir} // $ENV{LOGDIR} // '%p/logs/%a';
  197. $state->{lockdir} //= $state->{flockdir} // "%L/locks";
  198. $state->{logdir} = $state->expand_path($state->{logdir});
  199. if ($p =~ m/^\s*$/) {
  200. $state->{signer} = '-Dunsigned';
  201. } elsif ($p =~ m/\-DSIGNER\=\S+/) {
  202. $state->{signer} = $&;
  203. }
  204. $state->{size_log} = "%f/build-stats/%a-size";
  205. if ($state->define_present('LOGDIR')) {
  206. $state->{logdir} = $state->{subst}->value('LOGDIR');
  207. }
  208. if ($state->{opt}{s}) {
  209. $state->{wantsize} = 1;
  210. } elsif ($state->define_present('WANTSIZE')) {
  211. $state->{wantsize} = $state->{subst}->value('WANTSIZE');
  212. } elsif (DPB::HostProperties->has_mem) {
  213. $state->{wantsize} = 1;
  214. }
  215. if ($state->define_present('COLOR')) {
  216. $state->{color} = $state->{subst}->value('COLOR');
  217. }
  218. if ($state->define_present('NO_CURSOR')) {
  219. $state->{nocursor} = $state->{subst}->value('NO_CURSOR');
  220. }
  221. if (DPB::HostProperties->has_mem || $state->{wantsize}) {
  222. require DPB::Heuristics::Size;
  223. $state->{sizer} = DPB::Heuristics::Size->new($state);
  224. } else {
  225. require DPB::Heuristics::Nosize;
  226. $state->{sizer} = DPB::Heuristics::Nosize->new($state);
  227. }
  228. if ($state->define_present('FETCH_JOBS') && !defined $state->{opt}{f}) {
  229. $state->{opt}{f} = $state->{subst}->value('FETCH_JOBS');
  230. }
  231. if ($state->define_present('LOCKDIR')) {
  232. $state->{lockdir} = $state->{subst}->value('LOCKDIR');
  233. }
  234. if ($state->define_present('TESTS')) {
  235. $state->{tests} = $state->{subst}->value('tests');
  236. }
  237. if ($state->{flogdir}) {
  238. $state->{logdir} = $state->{flogdir};
  239. }
  240. if ($state->{flockdir}) {
  241. $state->{lockdir} = $state->{flockdir};
  242. }
  243. if ($state->{opt}{t}) {
  244. $state->{tests} = 1;
  245. }
  246. $state->{opt}{f} //= 2;
  247. if ($state->opt('f')) {
  248. $state->{want_fetchinfo} = 1;
  249. }
  250. my $k = $state->is_interactive ? "STARTUPI" : "STARTUP";
  251. if ($state->define_present($k)) {
  252. $state->{startup_script} = $state->expand_chrooted_path($state->{subst}->value($k));
  253. }
  254. # redo this in case config files changed it
  255. $state->{logdir} = $state->expand_path($state->{logdir});
  256. if ($state->define_present("RECORD")) {
  257. $state->{record} = $state->{subst}->value("RECORD");
  258. }
  259. $state->{record} //= "%L/term-report.log";
  260. $state->{record} = $state->expand_path($state->{record});
  261. $state->{size_log} = $state->expand_path($state->{size_log});
  262. $state->{lockdir} = $state->expand_path($state->{lockdir});
  263. for my $cat (qw(build_files paths ipaths cpaths xpaths)) {
  264. next unless defined $state->{$cat};
  265. for my $f (@{$state->{$cat}}) {
  266. $f = $state->expand_path($f);
  267. }
  268. }
  269. if (!$state->{subst}->value("NO_BUILD_STATS")) {
  270. $state->{permanent_log} =
  271. $state->expand_path("%f/build-stats/%a");
  272. push(@{$state->{build_files}}, $state->{permanent_log});
  273. }
  274. $state->{dependencies_log} =
  275. $state->expand_path("%f/build-stats/%a-dependencies");
  276. $state->{display_timeout} =
  277. $state->{subst}->value('DISPLAY_TIMEOUT') // 10;
  278. if ($state->defines("DONT_BUILD_ONCE")) {
  279. $state->{build_once} = 0;
  280. }
  281. if ($state->define_present('MIRROR')) {
  282. $state->{mirror} = $state->{subst}->value('MIRROR');
  283. } else {
  284. $state->{mirror} = $state->{fetch_only};
  285. }
  286. $state->{fullrepo} = join("/", $state->{repo}, $state->arch, "all");
  287. }
  288. sub command_line_overrides
  289. {
  290. my ($class, $state) = @_;
  291. my $override_prop = DPB::HostProperties->new;
  292. if (defined $state->{base_user}) {
  293. $override_prop->{base_user} = $state->{base_user};
  294. }
  295. if (defined $state->{port_user}) {
  296. $override_prop->{port_user} = $state->{port_user};
  297. }
  298. if (!$state->{subst}->empty('HISTORY_ONLY')) {
  299. $state->{want_fetchinfo} = 1;
  300. $state->{opt}{f} = 0;
  301. $state->{opt}{j} = 1;
  302. $state->{opt}{e} = 1;
  303. $state->{all} = 1;
  304. $state->{scan_only} = 1;
  305. # XXX not really random, but no need to use dependencies
  306. $state->{random} = 1;
  307. }
  308. if ($state->opt('j')) {
  309. $override_prop->{jobs} = $state->opt('j');
  310. }
  311. if ($state->opt('p')) {
  312. $override_prop->{parallel} = $state->opt('p');
  313. }
  314. if ($state->opt('B')) {
  315. $override_prop->{chroot} = $state->opt('B');
  316. }
  317. if ($state->define_present('STUCK_TIMEOUT')) {
  318. $override_prop->{stuck} =
  319. $state->{subst}->value('STUCK_TIMEOUT');
  320. }
  321. if ($state->define_present('FETCH_TIMEOUT')) {
  322. $override_prop->{fetch_timeout} =
  323. $state->{subst}->value('FETCH_TIMEOUT');
  324. }
  325. if ($state->define_present('SMALL_TIME')) {
  326. $override_prop->{small} =
  327. $state->{subst}->value('SMALL_TIME');
  328. }
  329. if ($state->define_present('CONNECTION_TIMEOUT')) {
  330. $override_prop->{timeout} =
  331. $state->{subst}->value('CONNECTION_TIMEOUT');
  332. }
  333. if ($state->opt('J')) {
  334. $override_prop->{junk} = $state->opt('J');
  335. }
  336. if ($state->defines("ALWAYS_CLEAN")) {
  337. $override_prop->{always_clean} = 1;
  338. }
  339. if ($state->opt('M')) {
  340. $override_prop->{mem} = $state->opt('M');
  341. }
  342. if ($state->define_present('SYSLOG')) {
  343. require Sys::Syslog;
  344. Sys::Syslog::openlog('dpb', "nofatal");
  345. $override_prop->{syslog} = 1;
  346. }
  347. return $override_prop;
  348. }
  349. sub parse_config_files
  350. {
  351. my ($class, $state) = @_;
  352. my $override_prop = $class->command_line_overrides($state);
  353. my $default_prop = {
  354. junk => 150,
  355. parallel => '/2',
  356. small => 120,
  357. repair => 1,
  358. nochecksum => 1,
  359. };
  360. if ($state->{config_files}) {
  361. for my $config (@{$state->{config_files}}) {
  362. $class->parse_hosts_file($config, $state,
  363. \$default_prop, $override_prop);
  364. }
  365. }
  366. my $prop = DPB::HostProperties->new($default_prop);
  367. $prop->finalize_with_overrides($override_prop);
  368. if (!$state->{config_files}) {
  369. DPB::Core::Init->new('localhost', $prop);
  370. }
  371. $state->{default_prop} = $prop;
  372. }
  373. sub parse_hosts_file
  374. {
  375. my ($class, $filename, $state, $rdefault, $override) = @_;
  376. open my $fh, '<', $filename or
  377. $state->fatal("Can't read host files #1: #2", $filename, $!);
  378. my $cores = {};
  379. while (<$fh>) {
  380. chomp;
  381. s/\s*\#.*$//;
  382. next if m/^$/;
  383. if (m/^([A-Z_]+)\=\s*(.*)\s*$/) {
  384. $state->{subst}->add($1, $2);
  385. next;
  386. }
  387. if (defined $state->{build_user}) {
  388. $$rdefault->{build_user} //= $state->{build_user};
  389. }
  390. # copy default properties
  391. my $prop = DPB::HostProperties->new($$rdefault);
  392. my ($host, @properties) = split(/\s+/, $_);
  393. for my $arg (@properties) {
  394. if ($arg =~ m/^(.*?)=(.*)$/) {
  395. $prop->{$1} = $2;
  396. }
  397. }
  398. if (defined $prop->{arch} && $prop->{arch} ne $state->arch) {
  399. next;
  400. }
  401. if ($host eq 'DEFAULT') {
  402. $$rdefault = { %$prop };
  403. next;
  404. }
  405. $prop->finalize_with_overrides($override);
  406. DPB::Core::Init->new($host, $prop);
  407. if (defined $prop->{build_user} &&
  408. !defined $state->{build_user} &&
  409. !$state->defines("BUILD_USER")) {
  410. $state->{build_user} = $prop->{build_user};
  411. }
  412. }
  413. }
  414. 1;