update_responses 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  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 (!$ENV{NO_CODESIGNATURE} && ($os eq 'osx64')
  209. && ! -f "$tmpdir/A/Contents/_CodeSignature/CodeResources") {
  210. exit_error "Missing code signature in $from_version while creating $mar_file";
  211. }
  212. if ($ENV{CHECK_CODESIGNATURE_EXISTS}) {
  213. unless (-f "$tmpdir/A/Contents/_CodeSignature/CodeResources"
  214. && -f "$tmpdir/B/Contents/_CodeSignature/CodeResources") {
  215. exit_error "Missing code signature while creating $mar_file";
  216. }
  217. }
  218. my ($out, $err, $success) = capture_exec('make_incremental_update.sh',
  219. $mar_file_path, "$tmpdir/A", "$tmpdir/B");
  220. if (!$success) {
  221. unlink $mar_file_path if -f $mar_file_path;
  222. exit_error "making incremental mar:\n" . $err;
  223. }
  224. $pm->finish;
  225. }
  226. sub create_incremental_mars_for_version {
  227. my ($config, $version) = @_;
  228. my $pm = Parallel::ForkManager->new(get_nbprocs);
  229. $pm->run_on_finish(sub { $_[2]->(@_) });
  230. my $v = $config->{versions}{$version};
  231. foreach my $from_version (@{$v->{incremental_from}}) {
  232. $config->{versions}{$from_version} //= {};
  233. get_version_files($config, $from_version);
  234. my $from_v = $config->{versions}{$from_version};
  235. foreach my $os (keys %{$v->{files}}) {
  236. foreach my $lang (keys %{$v->{files}{$os}}) {
  237. next unless defined $from_v->{files}{$os}{$lang}{complete};
  238. create_incremental_mar($config, $pm, $from_version, $version, $os, $lang);
  239. }
  240. }
  241. }
  242. $pm->wait_all_children;
  243. }
  244. sub get_config {
  245. my ($config, $version, $os, $name) = @_;
  246. return $config->{versions}{$version}{$os}{$name}
  247. // $config->{versions}{$version}{$name}
  248. // $config->{$name};
  249. }
  250. sub version_dir {
  251. my ($config, $version) = @_;
  252. return get_config($config, $version, 'any', 'releases_dir') . "/$version";
  253. }
  254. sub channel_to_version {
  255. my ($config, @channels) = @_;
  256. return values %{$config->{channels}} unless @channels;
  257. foreach my $channel (@channels) {
  258. exit_error "Unknown channel $channel"
  259. unless $config->{channels}{$channel};
  260. }
  261. return map { $config->{channels}{$_} } @channels;
  262. }
  263. sub get_buildinfos {
  264. my ($config, $version) = @_;
  265. return if exists $config->{versions}{$version}{buildID};
  266. extract_martools($config, $version);
  267. my $files = $config->{versions}{$version}{files};
  268. foreach my $os (keys %$files) {
  269. foreach my $lang (keys %{$files->{$os}}) {
  270. next unless $files->{$os}{$lang}{complete};
  271. my $tmpdir = get_tmpdir($config);
  272. my $mar_compression = get_config($config, $version, $os, 'mar_compression');
  273. extract_mar(
  274. mar_filename($config, $config->{appname_marfile}, $version, $os, $lang),
  275. "$tmpdir",
  276. $mar_compression);
  277. my $appfile = "$tmpdir/application.ini" if -f "$tmpdir/application.ini";
  278. $appfile = "$tmpdir/Contents/Resources/application.ini"
  279. if -f "$tmpdir/Contents/Resources/application.ini";
  280. exit_error "Could not find application.ini" unless $appfile;
  281. foreach my $line (read_file($appfile)) {
  282. if ($line =~ m/^BuildID=(.*)$/) {
  283. $config->{versions}{$version}{buildID} = $1;
  284. return;
  285. }
  286. }
  287. exit_error "Could not extract buildID from application.ini";
  288. }
  289. }
  290. }
  291. sub get_response {
  292. my ($config, $version, $os, @patches) = @_;
  293. my $res;
  294. my $writer = XML::Writer->new(OUTPUT => \$res, ENCODING => 'UTF-8');
  295. $writer->xmlDecl;
  296. $writer->startTag('updates');
  297. if (get_config($config, $version, $os, 'unsupported')) {
  298. $writer->startTag('update',
  299. unsupported => 'true',
  300. detailsURL => get_config($config, $version, $os, 'detailsURL'),
  301. );
  302. goto CLOSETAGS;
  303. }
  304. my $minversion = get_config($config, $version, $os, 'minSupportedOSVersion');
  305. my $mininstruc = get_config($config, $version, $os, 'minSupportedInstructionSet');
  306. $writer->startTag('update',
  307. type => 'minor',
  308. displayVersion => $version,
  309. appVersion => $version,
  310. platformVersion => get_config($config, $version, $os, 'platformVersion'),
  311. buildID => get_config($config, $version, $os, 'buildID'),
  312. detailsURL => get_config($config, $version, $os, 'detailsURL'),
  313. actions => 'showURL',
  314. openURL => get_config($config, $version, $os, 'detailsURL'),
  315. defined $minversion ? ( minSupportedOSVersion => $minversion ) : (),
  316. defined $mininstruc ? ( minSupportedInstructionSet => $mininstruc ) : (),
  317. );
  318. foreach my $patch (@patches) {
  319. my @sorted_patch = map { $_ => $patch->{$_} } sort keys %$patch;
  320. $writer->startTag('patch', @sorted_patch);
  321. $writer->endTag('patch');
  322. }
  323. CLOSETAGS:
  324. $writer->endTag('update');
  325. $writer->endTag('updates');
  326. $writer->end;
  327. return $res;
  328. }
  329. sub write_responses {
  330. my ($config, @channels) = @_;
  331. @channels = keys %{$config->{channels}} unless @channels;
  332. foreach my $channel (@channels) {
  333. my $version = $config->{channels}{$channel};
  334. get_version_files($config, $version);
  335. get_buildinfos($config, $version);
  336. my $files = $config->{versions}{$version}{files};
  337. my $migrate_archs = $config->{versions}{$version}{migrate_archs} // {};
  338. foreach my $old_os (keys %$migrate_archs) {
  339. my $new_os = $migrate_archs->{$old_os};
  340. foreach my $lang (keys %{$files->{$new_os}}) {
  341. $files->{$old_os}{$lang}{complete} =
  342. $files->{$new_os}{$lang}{complete};
  343. }
  344. }
  345. foreach my $os (keys %$files) {
  346. foreach my $lang (keys %{$files->{$os}}) {
  347. my $resp = get_response($config, $version, $os,
  348. $files->{$os}{$lang}{complete});
  349. write_htdocs($channel, "$version-$os-$lang.xml", $resp);
  350. foreach my $from_version (keys %{$files->{$os}{$lang}{partial}}) {
  351. $resp = get_response($config, $version, $os,
  352. $files->{$os}{$lang}{complete},
  353. $files->{$os}{$lang}{partial}{$from_version});
  354. write_htdocs($channel, "$from_version-$version-$os-$lang.xml", $resp);
  355. }
  356. }
  357. }
  358. write_htdocs($channel, 'no-update.xml',
  359. '<?xml version="1.0" encoding="UTF-8"?>'
  360. . "\n<updates></updates>\n");
  361. }
  362. }
  363. sub write_htaccess {
  364. my ($config, @channels) = @_;
  365. @channels = keys %{$config->{channels}} unless @channels;
  366. my $flags = "[last]";
  367. foreach my $channel (@channels) {
  368. my $htaccess = "RewriteEngine On\n";
  369. $htaccess .= $config->{htaccess_rewrite_rules}{$channel} // '';
  370. my $version = $config->{channels}{$channel};
  371. my $migrate_langs = $config->{versions}{$version}{migrate_langs} // {};
  372. my $files = $config->{versions}{$version}{files};
  373. $htaccess .= "RewriteRule ^[^\/]+/$version/ no-update.xml $flags\n";
  374. foreach my $os (sort keys %$files) {
  375. foreach my $bt (build_targets_by_os($os)) {
  376. foreach my $lang (sort keys %{$files->{$os}}) {
  377. foreach my $from_version (sort keys %{$files->{$os}{$lang}{partial}}) {
  378. $htaccess .= "RewriteRule ^$bt/$from_version/$lang "
  379. . "$from_version-$version-$os-$lang.xml $flags\n";
  380. }
  381. $htaccess .= "RewriteRule ^$bt/[^\/]+/$lang "
  382. . "$version-$os-$lang.xml $flags\n";
  383. }
  384. foreach my $lang (sort keys %$migrate_langs) {
  385. $htaccess .= "RewriteRule ^$bt/[^\/]+/$lang "
  386. . "$version-$os-$migrate_langs->{$lang}.xml $flags\n";
  387. }
  388. $htaccess .= "RewriteRule ^$bt/ $version-$os-en-US.xml $flags\n";
  389. }
  390. }
  391. write_htdocs($channel, '.htaccess', $htaccess);
  392. }
  393. }
  394. sub write_downloads_json {
  395. my ($config, @channels) = @_;
  396. return unless $config->{create_downloads_json};
  397. @channels = keys %{$config->{channels}} unless @channels;
  398. foreach my $channel (@channels) {
  399. my $version = $config->{channels}{$channel};
  400. my $data = {
  401. version => $version,
  402. downloads => get_version_downloads($config, $version),
  403. };
  404. write_htdocs($channel, 'downloads.json',
  405. JSON->new->utf8->canonical->encode($data));
  406. }
  407. }
  408. sub marzip_path {
  409. my ($config, $version) = @_;
  410. for my $osname (qw/linux64 linux32 mac64 win64 win32/) {
  411. my $marzip = version_dir($config, $version) . "/mar-tools-$osname.zip";
  412. return $marzip if -f $marzip;
  413. }
  414. exit_error 'Could not find mar-tools';
  415. }
  416. my $martools_tmpdir;
  417. sub extract_martools {
  418. my ($config, $version) = @_;
  419. my $marzip = marzip_path($config, $version);
  420. $martools_tmpdir = get_tmpdir($config);
  421. my $old_cwd = getcwd;
  422. chdir $martools_tmpdir;
  423. my (undef, undef, $success) = capture_exec('unzip', $marzip);
  424. chdir $old_cwd;
  425. exit_error "Error extracting $marzip" unless $success;
  426. $ENV{PATH} = "$martools_tmpdir/mar-tools:$initPATH";
  427. if ($initLD_LIBRARY_PATH) {
  428. $ENV{LD_LIBRARY_PATH} = "$initLD_LIBRARY_PATH:$martools_tmpdir/mar-tools";
  429. } else {
  430. $ENV{LD_LIBRARY_PATH} = "$martools_tmpdir/mar-tools";
  431. }
  432. }
  433. sub log_step {
  434. my ($url, $step, $status, $details) = @_;
  435. state $u;
  436. if (!defined $u || $url ne $u) {
  437. print "\n" if $u;
  438. print "$url\n";
  439. $u = $url;
  440. }
  441. print ' ', $step, $status ? ': OK' : ': ERROR',
  442. $details ? " - $details\n" : "\n";
  443. return if $status;
  444. push @check_errors, { url => $url, step => $step, details => $details };
  445. }
  446. sub get_remote_xml {
  447. my ($url) = @_;
  448. my $content = get $url;
  449. log_step($url, 'get', defined $content);
  450. return undef unless defined $content;
  451. my $dom = eval { XML::LibXML->load_xml(string => $content) };
  452. log_step($url, 'parse_xml', defined $dom, $@);
  453. return $dom;
  454. }
  455. sub check_get_version {
  456. my ($dom) = @_;
  457. my @updates = $dom->documentElement()->getChildrenByLocalName('update');
  458. return undef unless @updates;
  459. return $updates[0]->getAttribute('appVersion');
  460. }
  461. sub check_no_update {
  462. my ($dom) = @_;
  463. my @updates = $dom->documentElement()->getChildrenByLocalName('update');
  464. return @updates == 0;
  465. }
  466. sub check_has_incremental {
  467. my ($dom) = @_;
  468. my @updates = $dom->documentElement()->getChildrenByLocalName('update');
  469. return undef unless @updates;
  470. my @patches = $updates[0]->getChildrenByLocalName('patch');
  471. foreach my $patch (@patches) {
  472. return 1 if $patch->getAttribute('type') eq 'partial';
  473. }
  474. return undef;
  475. }
  476. sub build_targets_list {
  477. map { ref $_ eq 'ARRAY' ? @$_ : $_ } values %{$config->{build_targets}};
  478. }
  479. sub check_update_responses_channel {
  480. my ($config, $base_url, $channel) = @_;
  481. my $channel_version = $config->{channels}{$channel};
  482. foreach my $build_target (build_targets_list()) {
  483. foreach my $lang (qw(en-US de)) {
  484. my $url = "$base_url/$channel/$build_target/1.0/$lang";
  485. my $dom = get_remote_xml($url);
  486. if ($dom) {
  487. my $version = check_get_version($dom);
  488. log_step($url, 'version', $version eq $channel_version,
  489. "expected: $channel_version received: $version");
  490. }
  491. $url = "$base_url/$channel/$build_target/$channel_version/$lang";
  492. $dom = get_remote_xml($url);
  493. log_step($url, 'no_update', check_no_update($dom)) if $dom;
  494. my @inc = @{$config->{versions}{$channel_version}{incremental_from}}
  495. if $config->{versions}{$channel_version}{incremental_from};
  496. foreach my $inc_from (@inc) {
  497. my $url = "$base_url/$channel/$build_target/$inc_from/$lang";
  498. $dom = get_remote_xml($url);
  499. next unless $dom;
  500. my $version = check_get_version($dom);
  501. log_step($url, 'version', $version eq $channel_version,
  502. "expected: $channel_version received: $version");
  503. log_step($url, 'has_incremental', check_has_incremental($dom));
  504. }
  505. }
  506. }
  507. }
  508. sub download_version {
  509. my ($config, $version) = @_;
  510. my $tmpdir = get_tmpdir($config);
  511. my $destdir = version_dir($config, $version);
  512. my $urldir = "$config->{download}{archive_url}/$version";
  513. print "Downloading version $version\n";
  514. foreach my $file (qw(sha256sums-signed-build.txt sha256sums-signed-build.txt.asc)) {
  515. if (getstore("$urldir/$file", "$tmpdir/$file") != 200) {
  516. exit_error "Error downloading $urldir/$file";
  517. }
  518. }
  519. if (system('gpg', '--no-default-keyring', '--keyring',
  520. "$FindBin::Bin/$config->{download}{gpg_keyring}", '--verify',
  521. "$tmpdir/sha256sums-signed-build.txt.asc",
  522. "$tmpdir/sha256sums-signed-build.txt")) {
  523. exit_error "Error checking gpg signature for version $version";
  524. }
  525. make_path $destdir;
  526. move "$tmpdir/sha256sums-signed-build.txt.asc", "$destdir/sha256sums-signed-build.txt.asc";
  527. move "$tmpdir/sha256sums-signed-build.txt", "$destdir/sha256sums-signed-build.txt";
  528. my %sums = map { chomp; reverse split ' ', $_ }
  529. read_file "$destdir/sha256sums-signed-build.txt";
  530. my $martools = 'mar-tools-linux64.zip';
  531. exit_error "Error downloading $urldir/$martools\n"
  532. unless getstore("$urldir/$martools", "$tmpdir/$martools") == 200;
  533. exit_error "Error downloading $urldir/$martools.asc\n"
  534. unless getstore("$urldir/$martools.asc", "$tmpdir/$martools.asc") == 200;
  535. if (system('gpg', '--no-default-keyring', '--keyring',
  536. "$FindBin::Bin/$config->{download}{gpg_keyring}", '--verify',
  537. "$tmpdir/$martools.asc", "$tmpdir/$martools")) {
  538. exit_error "Error checking gpg signature for $version/$martools";
  539. }
  540. exit_error "Wrong checksum for $version/$martools"
  541. unless $sums{$martools} eq sha256_hex(read_file("$tmpdir/$martools"));
  542. move "$tmpdir/$martools", "$destdir/$martools";
  543. move "$tmpdir/$martools.asc", "$destdir/$martools.asc";
  544. foreach my $file (sort grep { $_ =~ m/\.mar$/ } keys %sums) {
  545. print "Downloading $file\n";
  546. exit_error "Error downloading $urldir/$file\n"
  547. unless getstore("$urldir/$file", "$tmpdir/$file") == 200;
  548. exit_error "Wrong checksum for $file"
  549. unless $sums{$file} eq sha256_hex(read_file("$tmpdir/$file"));
  550. move "$tmpdir/$file", "$destdir/$file";
  551. }
  552. }
  553. sub download_missing_versions {
  554. my ($config, @channels) = @_;
  555. foreach my $channel (@channels) {
  556. exit_error "Unknown channel $channel"
  557. unless $config->{channels}{$channel};
  558. my $cversion = $config->{channels}{$channel};
  559. next unless $config->{versions}{$cversion}{incremental_from};
  560. foreach my $version (@{$config->{versions}{$cversion}{incremental_from}}) {
  561. next if -d version_dir($config, $version);
  562. download_version($config, $version);
  563. }
  564. }
  565. }
  566. sub check_update_responses {
  567. my ($config) = @_;
  568. exit_error "usage: $PROGRAM_NAME <base_url> [channels...]" unless @ARGV;
  569. my ($base_url, @channels) = @ARGV;
  570. foreach my $channel (@channels ? @channels : keys %{$config->{channels}}) {
  571. check_update_responses_channel($config, $base_url, $channel);
  572. }
  573. if (!@check_errors) {
  574. print "\n\nNo errors\n";
  575. return;
  576. }
  577. print "\n\nErrors list:\n";
  578. my $url = '';
  579. foreach my $error (@check_errors) {
  580. if ($url ne $error->{url}) {
  581. $url = $error->{url};
  582. print "$url\n";
  583. }
  584. print " $error->{step}",
  585. $error->{details} ? " - $error->{details}\n" : "\n";
  586. }
  587. }
  588. my %actions = (
  589. update_responses => sub {
  590. my ($config) = @_;
  591. my @channels = @ARGV ? @ARGV : keys %{$config->{channels}};
  592. foreach my $channel (@channels) {
  593. exit_error "Unknown channel $channel"
  594. unless $config->{channels}{$channel};
  595. $htdocsfiles{$channel} = { '.' => 1, '..' => 1 };
  596. }
  597. write_responses($config, @channels);
  598. write_htaccess($config, @channels);
  599. write_downloads_json($config, @channels);
  600. clean_htdocs(@channels);
  601. },
  602. gen_incrementals => sub {
  603. my ($config) = @_;
  604. foreach my $version (channel_to_version($config, @ARGV)) {
  605. extract_martools($config, $version);
  606. get_version_files($config, $version);
  607. create_incremental_mars_for_version($config, $version);
  608. }
  609. },
  610. download_missing_versions => sub {
  611. my ($config) = @_;
  612. my @channels = @ARGV ? @ARGV : keys %{$config->{channels}};
  613. download_missing_versions($config, @channels);
  614. },
  615. check_update_responses_deployement => \&check_update_responses,
  616. get_channel_version => sub {
  617. my ($config) = @_;
  618. exit_error "Wrong arguments" unless @ARGV == 1;
  619. exit_error "Unknown channel" unless $config->{channels}{$ARGV[0]};
  620. print $config->{channels}{$ARGV[0]}, "\n";
  621. },
  622. );
  623. my $action = fileparse($PROGRAM_NAME);
  624. exit_error "Unknown action $action" unless $actions{$action};
  625. $actions{$action}->($config);