update_responses 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  1. #!/usr/bin/perl -w
  2. use strict;
  3. use feature "state";
  4. use English;
  5. use FindBin;
  6. use YAML qw(LoadFile);
  7. use File::Slurp;
  8. use File::Path qw(make_path);
  9. use Digest::SHA qw(sha256_hex);
  10. use XML::Writer;
  11. use Cwd;
  12. use File::Copy;
  13. use File::Temp;
  14. use File::Find;
  15. use POSIX qw(setlocale LC_ALL);
  16. use IO::CaptureOutput qw(capture_exec);
  17. use Parallel::ForkManager;
  18. use File::Basename;
  19. use XML::LibXML '1.70';
  20. use LWP::Simple;
  21. use JSON;
  22. # Set umask and locale to provide a consistent environment for MAR file
  23. # generation, etc.
  24. umask(0022);
  25. $ENV{"LC_ALL"} = "C";
  26. setlocale(LC_ALL, "C");
  27. my $htdocsdir = "$FindBin::Bin/htdocs";
  28. my $config = LoadFile("$FindBin::Bin/config.yml");
  29. my %htdocsfiles;
  30. my $releases_dir = $config->{releases_dir};
  31. $releases_dir = "$FindBin::Bin/$releases_dir" unless $releases_dir =~ m/^\//;
  32. my @check_errors;
  33. my $initPATH = $ENV{PATH};
  34. my $initLD_LIBRARY_PATH = $ENV{LD_LIBRARY_PATH};
  35. sub exit_error {
  36. print STDERR "Error: ", $_[0], "\n";
  37. chdir '/';
  38. exit (exists $_[1] ? $_[1] : 1);
  39. }
  40. sub get_tmpdir {
  41. my ($config) = @_;
  42. return File::Temp->newdir($config->{tmp_dir} ?
  43. (DIR => $config->{tmp_dir})
  44. : ());
  45. }
  46. sub build_targets_by_os {
  47. return ($_[0]) unless $config->{build_targets}{$_[0]};
  48. my $r = $config->{build_targets}{$_[0]};
  49. return ref $r eq 'ARRAY' ? @$r : ($r);
  50. }
  51. sub get_nbprocs {
  52. return $ENV{NUM_PROCS} if defined $ENV{NUM_PROCS};
  53. if (-f '/proc/cpuinfo') {
  54. return scalar grep { m/^processor\s+:\s/ } read_file '/proc/cpuinfo';
  55. }
  56. return 4;
  57. }
  58. sub write_htdocs {
  59. my ($channel, $file, $content) = @_;
  60. mkdir $htdocsdir unless -d $htdocsdir;
  61. mkdir "$htdocsdir/$channel" unless -d "$htdocsdir/$channel";
  62. write_file("$htdocsdir/$channel/$file", $content);
  63. $htdocsfiles{$channel}->{$file} = 1;
  64. }
  65. sub clean_htdocs {
  66. my (@channels) = @_;
  67. foreach my $channel (@channels) {
  68. opendir(my $d, "$htdocsdir/$channel");
  69. my @files = grep { ! $htdocsfiles{$channel}->{$_} } readdir $d;
  70. closedir $d;
  71. unlink map { "$htdocsdir/$channel/$_" } @files;
  72. }
  73. }
  74. sub get_sha512_hex_of_file {
  75. my ($file) = @_;
  76. my $sha = Digest::SHA->new("512");
  77. $sha->addfile($file);
  78. return $sha->hexdigest;
  79. }
  80. sub get_version_files {
  81. my ($config, $version) = @_;
  82. return if $config->{versions}{$version}{files};
  83. my $appname = $config->{appname_marfile};
  84. my $files = {};
  85. my $vdir = version_dir($config, $version);
  86. my $download_url = "$config->{download}{mars_url}/$version";
  87. opendir(my $d, $vdir) or exit_error "Error opening directory $vdir";
  88. foreach my $file (readdir $d) {
  89. next unless -f "$vdir/$file";
  90. if ($file =~ m/^$appname-([^-]+)-${version}_(.+)\.mar$/) {
  91. my ($os, $lang) = ($1, $2);
  92. $files->{$os}{$lang}{complete} = {
  93. type => 'complete',
  94. URL => "$download_url/$file",
  95. size => -s "$vdir/$file",
  96. hashFunction => 'SHA512',
  97. hashValue => get_sha512_hex_of_file("$vdir/$file"),
  98. };
  99. next;
  100. }
  101. if ($file =~ m/^$appname-([^-]+)-(.+)-${version}_(.+)\.incremental\.mar$/) {
  102. my ($os, $from_version, $lang) = ($1, $2, $3);
  103. $files->{$os}{$lang}{partial}{$from_version} = {
  104. type => 'partial',
  105. URL => "$download_url/$file",
  106. size => -s "$vdir/$file",
  107. hashFunction => 'SHA512',
  108. hashValue => get_sha512_hex_of_file("$vdir/$file"),
  109. }
  110. }
  111. }
  112. closedir $d;
  113. $config->{versions}{$version}{files} = $files;
  114. }
  115. sub get_version_downloads {
  116. my ($config, $version) = @_;
  117. my $downloads = {};
  118. my $vdir = version_dir($config, $version);
  119. my $download_url = "$config->{download}{bundles_url}/$version";
  120. opendir(my $d, $vdir) or exit_error "Error opening directory $vdir";
  121. foreach my $file (readdir $d) {
  122. next unless -f "$vdir/$file";
  123. my ($os, $lang);
  124. if ($file =~ m/^$config->{appname_bundle_osx}-$version-osx64_(.+).dmg$/) {
  125. ($os, $lang) = ('osx64', $1);
  126. } elsif ($file =~ m/^$config->{appname_bundle_linux}-(linux32|linux64)-${version}_(.+).tar.xz$/) {
  127. ($os, $lang) = ($1, $2);
  128. } elsif ($file =~ m/^$config->{appname_bundle_win64}-${version}_(.+).exe$/) {
  129. ($os, $lang) = ('win64', $1);
  130. } elsif ($file =~ m/^$config->{appname_bundle_win32}-${version}_(.+).exe$/) {
  131. ($os, $lang) = ('win32', $1);
  132. } else {
  133. next;
  134. }
  135. $downloads->{$os}{$lang} = {
  136. binary => "$download_url/$file",
  137. sig => "$download_url/$file.asc",
  138. };
  139. }
  140. closedir $d;
  141. $config->{versions}{$version}{downloads} = $downloads;
  142. }
  143. sub extract_mar {
  144. my ($mar_file, $dest_dir, $compression) = @_;
  145. my $old_cwd = getcwd;
  146. mkdir $dest_dir;
  147. chdir $dest_dir or exit_error "Cannot enter $dest_dir";
  148. my $res = system('mar', '-x', $mar_file);
  149. exit_error "Error extracting $mar_file" if $res;
  150. if ($compression ne 'bzip2' && $compression ne 'xz') {
  151. exit_error "Unknown compression format $compression";
  152. }
  153. my $compr_ext = $compression eq 'bzip2' ? 'bz2' : 'xz';
  154. my $compr_cmd = $compression eq 'bzip2' ? 'bunzip2' : 'unxz';
  155. my $uncompress_file = sub {
  156. return unless -f $File::Find::name;
  157. rename $File::Find::name, "$File::Find::name.$compr_ext";
  158. system($compr_cmd, "$File::Find::name.$compr_ext") == 0
  159. || exit_error "Error decompressing $File::Find::name";
  160. };
  161. find($uncompress_file, $dest_dir);
  162. my $manifest = -f 'updatev3.manifest' ? 'updatev3.manifest'
  163. : 'updatev2.manifest';
  164. my @lines = read_file($manifest) if -f $manifest;
  165. foreach my $line (@lines) {
  166. if ($line =~ m/^addsymlink "(.+)" "(.+)"$/) {
  167. exit_error "$mar_file: Could not create symlink $1 -> $2"
  168. unless symlink $2, $1;
  169. }
  170. }
  171. chdir $old_cwd;
  172. }
  173. sub mar_filename {
  174. my ($config, $appname, $version, $os, $lang) = @_;
  175. version_dir($config, $version) . "/$appname-$os-${version}_$lang.mar";
  176. }
  177. sub create_incremental_mar {
  178. my ($config, $pm, $from_version, $new_version, $os, $lang) = @_;
  179. my $appname = $config->{appname_marfile};
  180. my $mar_file = "$appname-$os-${from_version}-${new_version}_$lang.incremental.mar";
  181. my $mar_file_path = version_dir($config, $new_version) . '/' . $mar_file;
  182. if ($ENV{MAR_SKIP_EXISTING} && -f $mar_file_path) {
  183. print "Skipping $mar_file\n";
  184. return;
  185. }
  186. print "Starting $mar_file\n";
  187. my $download_url = "$config->{download}{mars_url}/$new_version";
  188. my $finished_file = sub {
  189. exit_error "Error creating $mar_file" unless $_[1] == 0;
  190. print "Finished $mar_file\n";
  191. $config->{versions}{$new_version}{files}{$os}{$lang}{partial}{$from_version} = {
  192. type => 'partial',
  193. URL => "$download_url/$mar_file",
  194. size => -s $mar_file_path,
  195. hashFunction => 'SHA512',
  196. hashValue => get_sha512_hex_of_file($mar_file_path),
  197. };
  198. };
  199. return if $pm->start($finished_file);
  200. my $tmpdir = get_tmpdir($config);
  201. my $mar_c_from = get_config($config, $from_version, $os, 'mar_compression');
  202. my $mar_c_new = get_config($config, $new_version, $os, 'mar_compression');
  203. extract_mar(mar_filename($config, $appname, $from_version, $os, $lang),
  204. "$tmpdir/A", $mar_c_from);
  205. extract_mar(mar_filename($config, $appname, $new_version, $os, $lang),
  206. "$tmpdir/B", $mar_c_new);
  207. # bug 26054: make sure previous macOS version is code signed
  208. if (($os eq 'osx64') && ! -f "$tmpdir/A/Contents/_CodeSignature/CodeResources") {
  209. exit_error "Missing code signature in $from_version while creating $mar_file";
  210. }
  211. if ($ENV{CHECK_CODESIGNATURE_EXISTS}) {
  212. unless (-f "$tmpdir/A/Contents/_CodeSignature/CodeResources"
  213. && -f "$tmpdir/B/Contents/_CodeSignature/CodeResources") {
  214. exit_error "Missing code signature while creating $mar_file";
  215. }
  216. }
  217. my ($out, $err, $success) = capture_exec('make_incremental_update.sh',
  218. $mar_file_path, "$tmpdir/A", "$tmpdir/B");
  219. if (!$success) {
  220. unlink $mar_file_path if -f $mar_file_path;
  221. exit_error "making incremental mar:\n" . $err;
  222. }
  223. $pm->finish;
  224. }
  225. sub create_incremental_mars_for_version {
  226. my ($config, $version) = @_;
  227. my $pm = Parallel::ForkManager->new(get_nbprocs);
  228. $pm->run_on_finish(sub { $_[2]->(@_) });
  229. my $v = $config->{versions}{$version};
  230. foreach my $from_version (@{$v->{incremental_from}}) {
  231. $config->{versions}{$from_version} //= {};
  232. get_version_files($config, $from_version);
  233. my $from_v = $config->{versions}{$from_version};
  234. foreach my $os (keys %{$v->{files}}) {
  235. foreach my $lang (keys %{$v->{files}{$os}}) {
  236. next unless defined $from_v->{files}{$os}{$lang}{complete};
  237. create_incremental_mar($config, $pm, $from_version, $version, $os, $lang);
  238. }
  239. }
  240. }
  241. $pm->wait_all_children;
  242. }
  243. sub get_config {
  244. my ($config, $version, $os, $name) = @_;
  245. return $config->{versions}{$version}{$os}{$name}
  246. // $config->{versions}{$version}{$name}
  247. // $config->{$name};
  248. }
  249. sub version_dir {
  250. my ($config, $version) = @_;
  251. return get_config($config, $version, 'any', 'releases_dir') . "/$version";
  252. }
  253. sub channel_to_version {
  254. my ($config, @channels) = @_;
  255. return values %{$config->{channels}} unless @channels;
  256. foreach my $channel (@channels) {
  257. exit_error "Unknown channel $channel"
  258. unless $config->{channels}{$channel};
  259. }
  260. return map { $config->{channels}{$_} } @channels;
  261. }
  262. sub get_buildinfos {
  263. my ($config, $version) = @_;
  264. return if exists $config->{versions}{$version}{buildID};
  265. extract_martools($config, $version);
  266. my $files = $config->{versions}{$version}{files};
  267. foreach my $os (keys %$files) {
  268. foreach my $lang (keys %{$files->{$os}}) {
  269. next unless $files->{$os}{$lang}{complete};
  270. my $tmpdir = get_tmpdir($config);
  271. my $mar_compression = get_config($config, $version, $os, 'mar_compression');
  272. extract_mar(
  273. mar_filename($config, $config->{appname_marfile}, $version, $os, $lang),
  274. "$tmpdir",
  275. $mar_compression);
  276. my $appfile = "$tmpdir/application.ini" if -f "$tmpdir/application.ini";
  277. $appfile = "$tmpdir/Contents/Resources/application.ini"
  278. if -f "$tmpdir/Contents/Resources/application.ini";
  279. exit_error "Could not find application.ini" unless $appfile;
  280. foreach my $line (read_file($appfile)) {
  281. if ($line =~ m/^BuildID=(.*)$/) {
  282. $config->{versions}{$version}{buildID} = $1;
  283. return;
  284. }
  285. }
  286. exit_error "Could not extract buildID from application.ini";
  287. }
  288. }
  289. }
  290. sub get_response {
  291. my ($config, $version, $os, @patches) = @_;
  292. my $res;
  293. my $writer = XML::Writer->new(OUTPUT => \$res, ENCODING => 'UTF-8');
  294. $writer->xmlDecl;
  295. $writer->startTag('updates');
  296. if (get_config($config, $version, $os, 'unsupported')) {
  297. $writer->startTag('update',
  298. unsupported => 'true',
  299. detailsURL => get_config($config, $version, $os, 'detailsURL'),
  300. );
  301. goto CLOSETAGS;
  302. }
  303. my $minversion = get_config($config, $version, $os, 'minSupportedOSVersion');
  304. my $mininstruc = get_config($config, $version, $os, 'minSupportedInstructionSet');
  305. $writer->startTag('update',
  306. type => 'minor',
  307. displayVersion => $version,
  308. appVersion => $version,
  309. platformVersion => get_config($config, $version, $os, 'platformVersion'),
  310. buildID => get_config($config, $version, $os, 'buildID'),
  311. detailsURL => get_config($config, $version, $os, 'detailsURL'),
  312. actions => 'showURL',
  313. openURL => get_config($config, $version, $os, 'detailsURL'),
  314. defined $minversion ? ( minSupportedOSVersion => $minversion ) : (),
  315. defined $mininstruc ? ( minSupportedInstructionSet => $mininstruc ) : (),
  316. );
  317. foreach my $patch (@patches) {
  318. my @sorted_patch = map { $_ => $patch->{$_} } sort keys %$patch;
  319. $writer->startTag('patch', @sorted_patch);
  320. $writer->endTag('patch');
  321. }
  322. CLOSETAGS:
  323. $writer->endTag('update');
  324. $writer->endTag('updates');
  325. $writer->end;
  326. return $res;
  327. }
  328. sub write_responses {
  329. my ($config, @channels) = @_;
  330. @channels = keys %{$config->{channels}} unless @channels;
  331. foreach my $channel (@channels) {
  332. my $version = $config->{channels}{$channel};
  333. get_version_files($config, $version);
  334. get_buildinfos($config, $version);
  335. my $files = $config->{versions}{$version}{files};
  336. my $migrate_archs = $config->{versions}{$version}{migrate_archs} // {};
  337. foreach my $old_os (keys %$migrate_archs) {
  338. my $new_os = $migrate_archs->{$old_os};
  339. foreach my $lang (keys %{$files->{$new_os}}) {
  340. $files->{$old_os}{$lang}{complete} =
  341. $files->{$new_os}{$lang}{complete};
  342. }
  343. }
  344. foreach my $os (keys %$files) {
  345. foreach my $lang (keys %{$files->{$os}}) {
  346. my $resp = get_response($config, $version, $os,
  347. $files->{$os}{$lang}{complete});
  348. write_htdocs($channel, "$version-$os-$lang.xml", $resp);
  349. foreach my $from_version (keys %{$files->{$os}{$lang}{partial}}) {
  350. $resp = get_response($config, $version, $os,
  351. $files->{$os}{$lang}{complete},
  352. $files->{$os}{$lang}{partial}{$from_version});
  353. write_htdocs($channel, "$from_version-$version-$os-$lang.xml", $resp);
  354. }
  355. }
  356. }
  357. write_htdocs($channel, 'no-update.xml',
  358. '<?xml version="1.0" encoding="UTF-8"?>'
  359. . "\n<updates></updates>\n");
  360. }
  361. }
  362. sub write_htaccess {
  363. my ($config, @channels) = @_;
  364. @channels = keys %{$config->{channels}} unless @channels;
  365. my $flags = "[last]";
  366. foreach my $channel (@channels) {
  367. my $htaccess = "RewriteEngine On\n";
  368. $htaccess .= $config->{htaccess_rewrite_rules}{$channel} // '';
  369. my $version = $config->{channels}{$channel};
  370. my $migrate_langs = $config->{versions}{$version}{migrate_langs} // {};
  371. my $files = $config->{versions}{$version}{files};
  372. $htaccess .= "RewriteRule ^[^\/]+/$version/ no-update.xml $flags\n";
  373. foreach my $os (sort keys %$files) {
  374. foreach my $bt (build_targets_by_os($os)) {
  375. foreach my $lang (sort keys %{$files->{$os}}) {
  376. foreach my $from_version (sort keys %{$files->{$os}{$lang}{partial}}) {
  377. $htaccess .= "RewriteRule ^$bt/$from_version/$lang "
  378. . "$from_version-$version-$os-$lang.xml $flags\n";
  379. }
  380. $htaccess .= "RewriteRule ^$bt/[^\/]+/$lang "
  381. . "$version-$os-$lang.xml $flags\n";
  382. }
  383. foreach my $lang (sort keys %$migrate_langs) {
  384. $htaccess .= "RewriteRule ^$bt/[^\/]+/$lang "
  385. . "$version-$os-$migrate_langs->{$lang}.xml $flags\n";
  386. }
  387. $htaccess .= "RewriteRule ^$bt/ $version-$os-en-US.xml $flags\n";
  388. }
  389. }
  390. write_htdocs($channel, '.htaccess', $htaccess);
  391. }
  392. }
  393. sub write_downloads_json {
  394. my ($config, @channels) = @_;
  395. @channels = keys %{$config->{channels}} unless @channels;
  396. foreach my $channel (@channels) {
  397. my $version = $config->{channels}{$channel};
  398. my $data = {
  399. version => $version,
  400. downloads => get_version_downloads($config, $version),
  401. };
  402. write_htdocs($channel, 'downloads.json',
  403. JSON->new->utf8->canonical->encode($data));
  404. }
  405. }
  406. sub osname {
  407. my ($osname) = capture_exec('uname', '-s');
  408. my ($arch) = capture_exec('uname', '-m');
  409. chomp($osname, $arch);
  410. if ($osname eq 'Linux' && $arch eq 'x86_64') {
  411. return 'linux64';
  412. }
  413. if ($osname eq 'Linux' && $arch =~ m/^i.86$/) {
  414. return 'linux32';
  415. }
  416. exit_error 'Unknown OS';
  417. }
  418. my $martools_tmpdir;
  419. sub extract_martools {
  420. my ($config, $version) = @_;
  421. my $osname = osname;
  422. my $marzip = version_dir($config, $version) . "/mar-tools-$osname.zip";
  423. $martools_tmpdir = get_tmpdir($config);
  424. my $old_cwd = getcwd;
  425. chdir $martools_tmpdir;
  426. my (undef, undef, $success) = capture_exec('unzip', $marzip);
  427. chdir $old_cwd;
  428. exit_error "Error extracting $marzip" unless $success;
  429. $ENV{PATH} = "$martools_tmpdir/mar-tools:$initPATH";
  430. if ($initLD_LIBRARY_PATH) {
  431. $ENV{LD_LIBRARY_PATH} = "$initLD_LIBRARY_PATH:$martools_tmpdir/mar-tools";
  432. } else {
  433. $ENV{LD_LIBRARY_PATH} = "$martools_tmpdir/mar-tools";
  434. }
  435. }
  436. sub log_step {
  437. my ($url, $step, $status, $details) = @_;
  438. state $u;
  439. if (!defined $u || $url ne $u) {
  440. print "\n" if $u;
  441. print "$url\n";
  442. $u = $url;
  443. }
  444. print ' ', $step, $status ? ': OK' : ': ERROR',
  445. $details ? " - $details\n" : "\n";
  446. return if $status;
  447. push @check_errors, { url => $url, step => $step, details => $details };
  448. }
  449. sub get_remote_xml {
  450. my ($url) = @_;
  451. my $content = get $url;
  452. log_step($url, 'get', defined $content);
  453. return undef unless defined $content;
  454. my $dom = eval { XML::LibXML->load_xml(string => $content) };
  455. log_step($url, 'parse_xml', defined $dom, $@);
  456. return $dom;
  457. }
  458. sub check_get_version {
  459. my ($dom) = @_;
  460. my @updates = $dom->documentElement()->getChildrenByLocalName('update');
  461. return undef unless @updates;
  462. return $updates[0]->getAttribute('appVersion');
  463. }
  464. sub check_no_update {
  465. my ($dom) = @_;
  466. my @updates = $dom->documentElement()->getChildrenByLocalName('update');
  467. return @updates == 0;
  468. }
  469. sub check_has_incremental {
  470. my ($dom) = @_;
  471. my @updates = $dom->documentElement()->getChildrenByLocalName('update');
  472. return undef unless @updates;
  473. my @patches = $updates[0]->getChildrenByLocalName('patch');
  474. foreach my $patch (@patches) {
  475. return 1 if $patch->getAttribute('type') eq 'partial';
  476. }
  477. return undef;
  478. }
  479. sub build_targets_list {
  480. map { ref $_ eq 'ARRAY' ? @$_ : $_ } values %{$config->{build_targets}};
  481. }
  482. sub check_update_responses_channel {
  483. my ($config, $base_url, $channel) = @_;
  484. my $channel_version = $config->{channels}{$channel};
  485. foreach my $build_target (build_targets_list()) {
  486. foreach my $lang (qw(en-US de)) {
  487. my $url = "$base_url/$channel/$build_target/1.0/$lang";
  488. my $dom = get_remote_xml($url);
  489. if ($dom) {
  490. my $version = check_get_version($dom);
  491. log_step($url, 'version', $version eq $channel_version,
  492. "expected: $channel_version received: $version");
  493. }
  494. $url = "$base_url/$channel/$build_target/$channel_version/$lang";
  495. $dom = get_remote_xml($url);
  496. log_step($url, 'no_update', check_no_update($dom)) if $dom;
  497. my @inc = @{$config->{versions}{$channel_version}{incremental_from}}
  498. if $config->{versions}{$channel_version}{incremental_from};
  499. foreach my $inc_from (@inc) {
  500. my $url = "$base_url/$channel/$build_target/$inc_from/$lang";
  501. $dom = get_remote_xml($url);
  502. next unless $dom;
  503. my $version = check_get_version($dom);
  504. log_step($url, 'version', $version eq $channel_version,
  505. "expected: $channel_version received: $version");
  506. log_step($url, 'has_incremental', check_has_incremental($dom));
  507. }
  508. }
  509. }
  510. }
  511. sub download_version {
  512. my ($config, $version) = @_;
  513. my $tmpdir = get_tmpdir($config);
  514. my $destdir = version_dir($config, $version);
  515. my $urldir = "$config->{download}{archive_url}/$version";
  516. print "Downloading version $version\n";
  517. foreach my $file (qw(sha256sums-signed-build.txt sha256sums-signed-build.txt.asc)) {
  518. if (getstore("$urldir/$file", "$tmpdir/$file") != 200) {
  519. exit_error "Error downloading $urldir/$file";
  520. }
  521. }
  522. if (system('gpg', '--no-default-keyring', '--keyring',
  523. "$FindBin::Bin/$config->{download}{gpg_keyring}", '--verify',
  524. "$tmpdir/sha256sums-signed-build.txt.asc",
  525. "$tmpdir/sha256sums-signed-build.txt")) {
  526. exit_error "Error checking gpg signature for version $version";
  527. }
  528. make_path $destdir;
  529. move "$tmpdir/sha256sums-signed-build.txt.asc", "$destdir/sha256sums-signed-build.txt.asc";
  530. move "$tmpdir/sha256sums-signed-build.txt", "$destdir/sha256sums-signed-build.txt";
  531. my %sums = map { chomp; reverse split ' ', $_ }
  532. read_file "$destdir/sha256sums-signed-build.txt";
  533. my $martools = 'mar-tools-' . osname . '.zip';
  534. exit_error "Error downloading $urldir/$martools\n"
  535. unless getstore("$urldir/$martools", "$tmpdir/$martools") == 200;
  536. exit_error "Error downloading $urldir/$martools.asc\n"
  537. unless getstore("$urldir/$martools.asc", "$tmpdir/$martools.asc") == 200;
  538. if (system('gpg', '--no-default-keyring', '--keyring',
  539. "$FindBin::Bin/$config->{download}{gpg_keyring}", '--verify',
  540. "$tmpdir/$martools.asc", "$tmpdir/$martools")) {
  541. exit_error "Error checking gpg signature for $version/$martools";
  542. }
  543. exit_error "Wrong checksum for $version/$martools"
  544. unless $sums{$martools} eq sha256_hex(read_file("$tmpdir/$martools"));
  545. move "$tmpdir/$martools", "$destdir/$martools";
  546. move "$tmpdir/$martools.asc", "$destdir/$martools.asc";
  547. foreach my $file (sort grep { $_ =~ m/\.mar$/ } keys %sums) {
  548. print "Downloading $file\n";
  549. exit_error "Error downloading $urldir/$file\n"
  550. unless getstore("$urldir/$file", "$tmpdir/$file") == 200;
  551. exit_error "Wrong checksum for $file"
  552. unless $sums{$file} eq sha256_hex(read_file("$tmpdir/$file"));
  553. move "$tmpdir/$file", "$destdir/$file";
  554. }
  555. }
  556. sub download_missing_versions {
  557. my ($config, @channels) = @_;
  558. foreach my $channel (@channels) {
  559. exit_error "Unknown channel $channel"
  560. unless $config->{channels}{$channel};
  561. my $cversion = $config->{channels}{$channel};
  562. next unless $config->{versions}{$cversion}{incremental_from};
  563. foreach my $version (@{$config->{versions}{$cversion}{incremental_from}}) {
  564. next if -d version_dir($config, $version);
  565. download_version($config, $version);
  566. }
  567. }
  568. }
  569. sub check_update_responses {
  570. my ($config) = @_;
  571. exit_error "usage: $PROGRAM_NAME <base_url> [channels...]" unless @ARGV;
  572. my ($base_url, @channels) = @ARGV;
  573. foreach my $channel (@channels ? @channels : keys %{$config->{channels}}) {
  574. check_update_responses_channel($config, $base_url, $channel);
  575. }
  576. if (!@check_errors) {
  577. print "\n\nNo errors\n";
  578. return;
  579. }
  580. print "\n\nErrors list:\n";
  581. my $url = '';
  582. foreach my $error (@check_errors) {
  583. if ($url ne $error->{url}) {
  584. $url = $error->{url};
  585. print "$url\n";
  586. }
  587. print " $error->{step}",
  588. $error->{details} ? " - $error->{details}\n" : "\n";
  589. }
  590. }
  591. my %actions = (
  592. update_responses => sub {
  593. my ($config) = @_;
  594. my @channels = @ARGV ? @ARGV : keys %{$config->{channels}};
  595. foreach my $channel (@channels) {
  596. exit_error "Unknown channel $channel"
  597. unless $config->{channels}{$channel};
  598. $htdocsfiles{$channel} = { '.' => 1, '..' => 1 };
  599. }
  600. write_responses($config, @channels);
  601. write_htaccess($config, @channels);
  602. write_downloads_json($config, @channels);
  603. clean_htdocs(@channels);
  604. },
  605. gen_incrementals => sub {
  606. my ($config) = @_;
  607. foreach my $version (channel_to_version($config, @ARGV)) {
  608. extract_martools($config, $version);
  609. get_version_files($config, $version);
  610. create_incremental_mars_for_version($config, $version);
  611. }
  612. },
  613. download_missing_versions => sub {
  614. my ($config) = @_;
  615. my @channels = @ARGV ? @ARGV : keys %{$config->{channels}};
  616. download_missing_versions($config, @channels);
  617. },
  618. check_update_responses_deployement => \&check_update_responses,
  619. get_channel_version => sub {
  620. my ($config) = @_;
  621. exit_error "Wrong arguments" unless @ARGV == 1;
  622. exit_error "Unknown channel" unless $config->{channels}{$ARGV[0]};
  623. print $config->{channels}{$ARGV[0]}, "\n";
  624. },
  625. );
  626. my $action = fileparse($PROGRAM_NAME);
  627. exit_error "Unknown action $action" unless $actions{$action};
  628. $actions{$action}->($config);