« blib/lib/App/Netdisco/Web/Plugin/Device/SNMP.pm
↑ Summary
blib/lib/App/Netdisco/DB/ExplicitLocking.pm »
| 1 | package App::Netdisco::DB::Deploy; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use Dancer ':syntax'; | ||
| 7 | use DBICx::Sugar (); | ||
| 8 | use Try::Tiny; | ||
| 9 | |||
| 10 | use App::Netdisco::DB::SchemaVersioned; | ||
| 11 | use App::Netdisco::DB::UpgradeMessage qw( | ||
| 12 | strict_failure_message force_refusal_message force_already_applied_message | ||
| 13 | tolerant_summary unexpected_error_message forced_step_summary | ||
| 14 | forced_step_clean_message | ||
| 15 | ); | ||
| 16 | |||
| 17 | use base 'Exporter'; | ||
| 18 | our @EXPORT_OK = qw(parse_args validate_force_step run_deploy deploy_schema); | ||
| 19 | |||
| 20 | our $USAGE = "Usage: netdisco-db-deploy [--redeploy-all | --force-step N | --check]\n"; | ||
| 21 | |||
| 22 | sub _refuse_args { die shift() . "\n" . $USAGE } | ||
| 23 | |||
| 24 | sub _read_force_step { | ||
| 25 | my ($argument, $remaining) = @_; | ||
| 26 | my $value = ($argument =~ m/\A--force-step=(.*)\z/s) ? $1 : shift @$remaining; | ||
| 27 | _refuse_args('--force-step needs the number of the step to force, for example --force-step 57.') | ||
| 28 | unless defined $value and $value =~ m/\A[0-9]+\z/; | ||
| 29 | return 0 + $value; | ||
| 30 | } | ||
| 31 | |||
| 32 | sub _refuse_combinations { | ||
| 33 | my $options = shift; | ||
| 34 | my $other_option_given = $options->{redeploy_all} || defined $options->{force_step}; | ||
| 35 | _refuse_args('--check only reads the database, so it cannot be combined with another option.') | ||
| 36 | if $options->{check} and $other_option_given; | ||
| 37 | _refuse_args('--force-step cannot be combined with --redeploy-all: a replay already tolerates errors in every step.') | ||
| 38 | if $options->{redeploy_all} and defined $options->{force_step}; | ||
| 39 | } | ||
| 40 | |||
| 41 | sub parse_args { | ||
| 42 | my $argv = shift; | ||
| 43 | my @remaining = @$argv; | ||
| 44 | my %options = (redeploy_all => 0, force_step => undef, check => 0); | ||
| 45 | |||
| 46 | while (defined(my $argument = shift @remaining)) { | ||
| 47 | if ($argument eq '--redeploy-all') { $options{redeploy_all} = 1; next } | ||
| 48 | if ($argument eq '--check') { $options{check} = 1; next } | ||
| 49 | if ($argument =~ m/\A--force-step(?:=|\z)/) { | ||
| 50 | _refuse_args('--force-step can be given only once.') if defined $options{force_step}; | ||
| 51 | $options{force_step} = _read_force_step($argument, \@remaining); | ||
| 52 | next; | ||
| 53 | } | ||
| 54 | _refuse_args("Unknown argument '$argument'."); | ||
| 55 | } | ||
| 56 | |||
| 57 | _refuse_combinations(\%options); | ||
| 58 | return \%options; | ||
| 59 | } | ||
| 60 | |||
| 61 | # the deploy opens a database it is about to upgrade, where DBIC's connect-time | ||
| 62 | # version check could only tell the user to do what the deploy is doing (#1788) | ||
| 63 | sub deploy_schema { | ||
| 64 | my $label = shift || 'netdisco'; | ||
| 65 | my $dbic = setting('plugins')->{DBIC}; | ||
| 66 | my $options = $dbic->{$label} || {}; | ||
| 67 | $options = $dbic->{ $options->{alias} } if $options->{alias}; | ||
| 68 | # a copy, because every later schema() call shares this hash: only the | ||
| 69 | # deploy skips the check, which still warns the web and backend of a missed upgrade | ||
| 70 | my %deploy = (%$options, | ||
| 71 | options => { %{ $options->{options} || {} }, ignore_version => 1 }); | ||
| 72 | DBICx::Sugar::config($dbic); | ||
| 73 | return DBICx::Sugar::schema($label, \%deploy); | ||
| 74 | } | ||
| 75 | |||
| 76 | sub validate_force_step { | ||
| 77 | my ($requested, $db_version) = @_; | ||
| 78 | return 'already_applied' if $requested < $db_version; | ||
| 79 | return 'future' if $requested > $db_version; | ||
| 80 | return 'run'; | ||
| 81 | } | ||
| 82 | |||
| 83 | sub _say { my $options = shift; print @_, "\n" unless $options->{quiet} } | ||
| 84 | sub _warn { my $options = shift; print STDERR @_, "\n" unless $options->{quiet} } | ||
| 85 | |||
| 86 | sub _bootstrap_version { | ||
| 87 | my ($schema, $options) = @_; | ||
| 88 | |||
| 89 | if ($options->{redeploy_all}) { | ||
| 90 | $schema->storage->dbh_do(sub { | ||
| 91 | my (undef, $dbh) = @_; | ||
| 92 | $dbh->do('DROP TABLE dbix_class_schema_versions'); | ||
| 93 | }); | ||
| 94 | } | ||
| 95 | |||
| 96 | # installs the dbix_class_schema_versions table with version "1" | ||
| 97 | # which corresponds to an empty schema | ||
| 98 | if (not $schema->get_db_version) { | ||
| 99 | $schema->install(1); | ||
| 100 | $schema->storage->disconnect; | ||
| 101 | } | ||
| 102 | |||
| 103 | # test for existing schema at public release version, set v=2 if so | ||
| 104 | try { | ||
| 105 | $schema->storage->dbh_do(sub { | ||
| 106 | my (undef, $dbh) = @_; | ||
| 107 | $dbh->selectrow_arrayref('SELECT * FROM device WHERE 0 = 1'); | ||
| 108 | }); | ||
| 109 | $schema->_set_db_version({version => 2}) | ||
| 110 | if $schema->get_db_version == 1; | ||
| 111 | $schema->storage->disconnect; | ||
| 112 | }; | ||
| 113 | } | ||
| 114 | |||
| 115 | # Returns an exit code when the run must stop here, undef to carry on. | ||
| 116 | sub _check_force_step { | ||
| 117 | my ($schema, $options, $db_version, $target_version) = @_; | ||
| 118 | my $requested = $options->{force_step}; | ||
| 119 | return undef unless defined $requested; | ||
| 120 | |||
| 121 | my $verdict = validate_force_step($requested, $db_version); | ||
| 122 | my %message_args = (requested => $requested, next_pending => $db_version, | ||
| 123 | container => $options->{container}); | ||
| 124 | |||
| 125 | if ($verdict eq 'already_applied') { | ||
| 126 | _warn($options, force_already_applied_message(%message_args)); | ||
| 127 | return undef; | ||
| 128 | } | ||
| 129 | return _refuse_force_past_latest($requested, $db_version) | ||
| 130 | if $db_version >= $target_version; | ||
| 131 | return undef if $verdict eq 'run'; | ||
| 132 | |||
| 133 | print STDERR force_refusal_message(%message_args), "\n"; | ||
| 134 | return 2; | ||
| 135 | } | ||
| 136 | |||
| 137 | # force_refusal_message names a next pending step, and here there is none. | ||
| 138 | sub _refuse_force_past_latest { | ||
| 139 | my ($requested, $db_version) = @_; | ||
| 140 | print STDERR "Cannot force step $requested: the database is already at version" | ||
| 141 | . " $db_version, the latest, so there is no step to force.\n"; | ||
| 142 | return 2; | ||
| 143 | } | ||
| 144 | |||
| 145 | sub _missing_step_message { | ||
| 146 | my ($schema, $from) = @_; | ||
| 147 | return sprintf "The database upgrade stopped at step %d: no upgrade file for %d-%d" | ||
| 148 | . " was found in %s. Reinstall Netdisco so the file is present, then run" | ||
| 149 | . " netdisco-db-deploy again.", $from, $from, $from + 1, $schema->upgrade_directory; | ||
| 150 | } | ||
| 151 | |||
| 152 | sub _report_unexpected { | ||
| 153 | my ($options, $step_result) = @_; | ||
| 154 | foreach my $failure (@{ $step_result->{unexpected} }) { | ||
| 155 | _warn($options, unexpected_error_message( | ||
| 156 | file => $step_result->{file}, statement => $failure->{statement}, | ||
| 157 | state => ($failure->{state} // 'none'), error => $failure->{error})); | ||
| 158 | } | ||
| 159 | } | ||
| 160 | |||
| 161 | sub _report_forced { | ||
| 162 | my ($options, $from, $step_result) = @_; | ||
| 163 | my $skipped = scalar @{ $step_result->{skipped} || [] }; | ||
| 164 | _warn($options, $skipped | ||
| 165 | ? forced_step_summary(from => $from, skipped => $skipped, container => $options->{container}) | ||
| 166 | : forced_step_clean_message(from => $from)); | ||
| 167 | } | ||
| 168 | |||
| 169 | sub _strict_failure_text { | ||
| 170 | my ($schema, $options, $from, $step_result) = @_; | ||
| 171 | my $failed = $step_result->{failed} || {}; | ||
| 172 | return strict_failure_message( | ||
| 173 | from => $from, file => $step_result->{file}, | ||
| 174 | statement => ($failed->{statement} // '(the step as a whole)'), | ||
| 175 | error => ($failed->{error} // 'no error text was returned'), | ||
| 176 | database => ($schema->storage->dbh->{pg_db} // 'netdisco'), | ||
| 177 | container => $options->{container}); | ||
| 178 | } | ||
| 179 | |||
| 180 | sub _step_file_exists { | ||
| 181 | my ($schema, $from) = @_; | ||
| 182 | return -f $schema->ddl_filename( | ||
| 183 | $schema->storage->sqlt_type, $from + 1, $schema->upgrade_directory, $from); | ||
| 184 | } | ||
| 185 | |||
| 186 | # Returns the step's result, or undef and the reason the run must stop. | ||
| 187 | sub _apply_step { | ||
| 188 | my ($schema, $options, $from) = @_; | ||
| 189 | # checked here because upgrade_single_step only carps and returns undef | ||
| 190 | my $step_result = _step_file_exists($schema, $from) | ||
| 191 | ? $schema->upgrade_single_step($from, $from + 1, | ||
| 192 | App::Netdisco::DB::SchemaVersioned::step_mode($from, $options)) | ||
| 193 | : undef; | ||
| 194 | return (undef, _missing_step_message($schema, $from)) if not $step_result; | ||
| 195 | return (undef, _strict_failure_text($schema, $options, $from, $step_result)) | ||
| 196 | if not $step_result->{ok}; | ||
| 197 | return ($step_result); | ||
| 198 | } | ||
| 199 | |||
| 200 | sub _announce_run { | ||
| 201 | my ($options, $db_version, $target_version) = @_; | ||
| 202 | return _say($options, "Database schema is up to date at version $target_version.") | ||
| 203 | if $db_version >= $target_version; | ||
| 204 | return _say($options, "Replaying every upgrade step from version $db_version" | ||
| 205 | . " to $target_version...") if $options->{redeploy_all}; | ||
| 206 | # _bootstrap_version leaves version 1 only when there is no device table | ||
| 207 | return _say($options, "Installing database schema version $target_version...") | ||
| 208 | if $db_version == 1; | ||
| 209 | _say($options, "Versions out of sync. This is $target_version, your database" | ||
| 210 | . " contains version $db_version, running updates..."); | ||
| 211 | } | ||
| 212 | |||
| 213 | sub _step_outcome { | ||
| 214 | my ($options, $from, $step_result) = @_; | ||
| 215 | return 'FORCED' if _is_forced_step($options, $from) | ||
| 216 | and scalar @{ $step_result->{skipped} || [] }; | ||
| 217 | my $unexpected = scalar @{ $step_result->{unexpected} }; | ||
| 218 | return 'OK' if not $unexpected or _is_forced_step($options, $from); | ||
| 219 | return sprintf 'applied with %d unexpected error%s', $unexpected, ($unexpected == 1 ? '' : 's'); | ||
| 220 | } | ||
| 221 | |||
| 222 | sub _is_forced_step { | ||
| 223 | my ($options, $from) = @_; | ||
| 224 | return (defined $options->{force_step} and $options->{force_step} == $from); | ||
| 225 | } | ||
| 226 | |||
| 227 | sub _run_steps { | ||
| 228 | my ($schema, $options, $db_version, $target_version) = @_; | ||
| 229 | my %tally = (expected => 0, steps => 0, unexpected => 0); | ||
| 230 | |||
| 231 | for (my $from = $db_version; $from < $target_version; $from++) { | ||
| 232 | print 'Updating version ', $from, ' to ', $from + 1, '... ' unless $options->{quiet}; | ||
| 233 | my ($step_result, $failure) = _apply_step($schema, $options, $from); | ||
| 234 | if (not $step_result) { | ||
| 235 | _say($options, 'FAILED'); | ||
| 236 | print STDERR $failure, "\n"; | ||
| 237 | return (1, \%tally); | ||
| 238 | } | ||
| 239 | _say($options, _step_outcome($options, $from, $step_result)); | ||
| 240 | |||
| 241 | if (_is_forced_step($options, $from)) { | ||
| 242 | _report_forced($options, $from, $step_result); | ||
| 243 | next; | ||
| 244 | } | ||
| 245 | _report_unexpected($options, $step_result); | ||
| 246 | $tally{unexpected} += scalar @{ $step_result->{unexpected} }; | ||
| 247 | next unless $step_result->{expected}; | ||
| 248 | $tally{expected} += $step_result->{expected}; | ||
| 249 | $tally{steps}++; | ||
| 250 | } | ||
| 251 | return (0, \%tally); | ||
| 252 | } | ||
| 253 | |||
| 254 | # a summary that follows unexpected errors goes with them, to STDERR | ||
| 255 | sub _report_tolerant_summary { | ||
| 256 | my ($options, $tally) = @_; | ||
| 257 | return unless $tally->{expected} or $tally->{unexpected}; | ||
| 258 | my $summary = tolerant_summary(%$tally, | ||
| 259 | container => $options->{container}, replayed => $options->{redeploy_all}); | ||
| 260 | $tally->{unexpected} ? _warn($options, $summary) : _say($options, $summary); | ||
| 261 | } | ||
| 262 | |||
| 263 | sub run_deploy { | ||
| 264 | my ($schema, $options) = @_; | ||
| 265 | $options ||= {}; | ||
| 266 | |||
| 267 | _bootstrap_version($schema, $options); | ||
| 268 | |||
| 269 | # roll everything back if we're testing | ||
| 270 | my $txn_guard = $ENV{ND2_DB_ROLLBACK} | ||
| 271 | ? $schema->storage->txn_scope_guard : undef; | ||
| 272 | |||
| 273 | # get_db_version is 0 at first deploy | ||
| 274 | my $db_version = ($schema->get_db_version || 1); | ||
| 275 | my $target_version = $schema->schema_version; | ||
| 276 | |||
| 277 | my $refusal = _check_force_step($schema, $options, $db_version, $target_version); | ||
| 278 | return $refusal if defined $refusal; | ||
| 279 | |||
| 280 | # a step's progress line is finished only after the step runs, and its | ||
| 281 | # errors go to STDERR, so STDOUT must not hold the start of the line back | ||
| 282 | local $| = 1; | ||
| 283 | _announce_run($options, $db_version, $target_version); | ||
| 284 | |||
| 285 | my ($exit_code, $tally) = _run_steps($schema, $options, $db_version, $target_version); | ||
| 286 | _report_tolerant_summary($options, $tally) if $exit_code == 0; | ||
| 287 | |||
| 288 | undef $txn_guard if $ENV{ND2_DB_ROLLBACK}; | ||
| 289 | return $exit_code; | ||
| 290 | } | ||
| 291 | |||
| 292 | 1; |