diff --git a/features/COVERAGE.md b/features/COVERAGE.md index c5cac08b..95e7d98d 100644 --- a/features/COVERAGE.md +++ b/features/COVERAGE.md @@ -1,6 +1,6 @@ # site-command Behat coverage -Suite for `easyengine/site-command` (the `ee site` command and the html site type, plus `ee site php-compat` on php and wp sites). 20 features, 259 scenarios (299 with outlines expanded): main lane 263, `@wp` 25, `@le` 11. Run it the CI way (`.github/workflows/behat.yml`): the files are copied into a core checkout's `features/` and core's `vendor/bin/behat` runs from the core root as root. +Suite for `easyengine/site-command` (the `ee site` command and the html site type, plus `ee site php-compat` on php and wp sites). 20 features, 261 scenarios (301 with outlines expanded): main lane 265, `@wp` 25, `@le` 11. Run it the CI way (`.github/workflows/behat.yml`): the files are copied into a core checkout's `features/` and core's `vendor/bin/behat` runs from the core root as root. ## Tags @@ -8,8 +8,8 @@ Suite for `easyengine/site-command` (the `ee site` command and the html site typ |---|---|---| | (none of the tags below) | 177 | Local only: `*.test` sites, no public DNS, no remote storage, no global restarts. This is the default safe run. The php-compat scenarios pull the scanner image from Docker Hub when it isn't present. | | `@known-bug` | 0 | Scenarios that expose open bugs are kept outside this suite and come back with their fixes. The CI lanes still exclude the tag. | -| `@destructive` | 12 | Restarts the GLOBAL nginx-proxy (proxy cache) or runs `ssl-renew --all` (refuses to start when an enabled LE site that isn't the suite's exists). Every scenario leaves the proxy running. | -| `@remote` | 10 | Uses the REAL rclone remote (`rclone-path` in config.yml). Touches only `/bh-site-command-*`: the rclone path is listed first and only listed suite directories are purged (per site before a scenario, all of them after the feature). | +| `@destructive` | 14 | Restarts the GLOBAL nginx-proxy (proxy cache), runs `ssl-renew --all` (refuses to start when an enabled LE site that isn't the suite's exists), or replaces the host rclone (`@rclone-install`, CI only; an after hook puts it back). Every scenario leaves the proxy running. | +| `@remote` | 12 | Uses the REAL rclone remote (`rclone-path` in config.yml). Touches only `/bh-site-command-*`: the rclone path is listed first and only listed suite directories are purged (per site before a scenario, all of them after the feature). | | `@le` | 11 | Let's Encrypt issuance on `*.ee4-test.rt.gw`. Locally: real LE, at most 4 certificates per full run; names contain `{RUN_ID}`, so repeated runs never hit the duplicate-certificate limit. On CI all 11 run against Pebble (`.github/behat/pebble-setup.sh`: EE's hardcoded LE host is redirected to Pebble, `{LE_ISSUER}` comes from `EE_BEHAT_LE_ISSUER`). The failed-alias rollback and the ACME cleanup on delete are regression checks for EasyEngine/site-command#505. | | `@le-real` | 0 | Needs the public LE service itself (rate limits, the real chain); never runs on CI. None so far: every `@le` scenario works on Pebble. | | `@wp` | 25 | Needs the `wp` site type (site-type-wp in the core checkout) and `ee` on PATH; slow. | @@ -202,6 +202,7 @@ Legend: covered / partial / not covered (reason). Feature files are abbreviated: | partial SQL dump left in the webroot after a failed dump | not covered | needs a failing mysqldump (fault injection) | | invalid `no-of-backups` | covered | backup: An invalid no-of-backups keeps the backups and warns | | per-host remote layout, hosts sharing `rclone-path` | covered | backup: Hosts that share a remote keep their backups apart | +| official rclone install keeps the host rclone when the download fails (snap, packaged rclone behind an older copy) | covered (CI only) | backup: A failed rclone download keeps a snap rclone; A failed rclone download puts back a packaged rclone behind an older copy | | restore without any backup | covered | backup: Restoring a site without backups says so | | files and dotfiles not in the backup | covered | backup: A restore removes files that are not in the backup, dotfiles too | | WP webroot files beside WordPress, symlinked uploads | covered (@wp) | backup: A WordPress backup holds the files next to WordPress; Symlinked uploads are backed up and restored without a loop | @@ -260,7 +261,7 @@ DB and container migrations under `migrations/` (core upgrade flow), the AcmePhp ## Coverage estimate -Counting the rows above as items: 149 items, 114 covered, 1 partial, 34 not covered (8 of them, marked "not covered yet", are open bugs whose scenarios come back with their fixes), i.e. about 76% of the inventory. Every user-facing subcommand and option of the html type has at least one scenario except `ssl-renew --force`, `clone --ssl/--wildcard` and a live `share`. +Counting the rows above as items: 150 items, 115 covered, 1 partial, 34 not covered (8 of them, marked "not covered yet", are open bugs whose scenarios come back with their fixes), i.e. about 76% of the inventory. Every user-facing subcommand and option of the html type has at least one scenario except `ssl-renew --force`, `clone --ssl/--wildcard` and a live `share`. ## Known bugs left out diff --git a/features/backup-restore.feature b/features/backup-restore.feature index d9d25199..07f8fe1e 100644 --- a/features/backup-restore.feature +++ b/features/backup-restore.feature @@ -293,3 +293,38 @@ Feature: Backup and restore And the symlink "{SITES_DIR}/bh-site-command-153.test/app/htdocs/wp-content/uploads" should exist And the file "{SITES_DIR}/bh-site-command-153.test/app/htdocs/bh-shared/uploads/bh.txt" should contain "bh-upload" And the symlink "{SITES_DIR}/bh-site-command-153.test/app/htdocs/bh-shared/uploads/uploads" should not exist + + # The official rclone install must leave the host's rclone working when the release download fails. + # features/rclone-install.sh moves the runner's rclone aside (CI only); the @rclone-install hook puts it back. + @destructive @rclone-install + Scenario: A failed rclone download keeps a snap rclone + Given the html site "bh-site-command-154.test" exists + When I run 'bash features/rclone-install.sh snap && bash features/rclone-install.sh block-downloads' + Then the command should succeed + When I run 'PATH=/var/tmp/bh-site-command-rclone/bin:$PATH:/snap/bin bin/ee site backup bh-site-command-154.test --list' + Then the command should fail + And STDERR should contain "is installed by snap, and the official build could not be installed." + And the file "/snap/bin/rclone" should exist + When I run 'bash features/rclone-install.sh unblock-downloads' + Then the command should succeed + When I run 'PATH=/var/tmp/bh-site-command-rclone/bin:$PATH:/snap/bin bin/ee site backup bh-site-command-154.test --list' + Then the command should succeed + And STDOUT should contain "Installed the official rclone" + And the file "/snap/bin/rclone" should not exist + And the file "/usr/bin/rclone" should exist + + @destructive @rclone-install + Scenario: A failed rclone download puts back a packaged rclone behind an older copy + Given the html site "bh-site-command-154.test" exists + When I run 'bash features/rclone-install.sh packaged-behind-old-copy && bash features/rclone-install.sh block-downloads' + Then the command should succeed + When I run 'bin/ee site backup bh-site-command-154.test --list' + Then the command should fail + And STDOUT should contain "Put rclone v1.6" + And the file "/usr/bin/rclone" should exist + When I run 'bash features/rclone-install.sh unblock-downloads' + Then the command should succeed + When I run 'bin/ee site backup bh-site-command-154.test --list' + Then the command should succeed + And STDOUT should contain "Installed the official rclone" + And the file "/usr/local/bin/rclone" should not exist diff --git a/features/bootstrap/FeatureContext.php b/features/bootstrap/FeatureContext.php index b021dcc5..be1137ac 100644 --- a/features/bootstrap/FeatureContext.php +++ b/features/bootstrap/FeatureContext.php @@ -515,6 +515,23 @@ public function cleanupScenario(AfterScenarioScope $scope) } } + /** + * Puts the runner's own rclone back after a scenario replaced it (features/rclone-install.sh). + * + * @AfterScenario @rclone-install + */ + public function restoreRclone() + { + // The helper refuses to set anything up elsewhere, so there is nothing to put back. + if ( 'true' !== getenv( 'GITHUB_ACTIONS' ) ) { + return; + } + $run = EE::launch( 'bash ' . escapeshellarg( $this->ee_path . '/features/rclone-install.sh' ) . ' restore', false, true ); + if ( $run->return_code ) { + throw new Exception( "Could not put the runner's rclone back.\n" . $this->describe( $run ) ); + } + } + /** * On CI, EE's hardcoded Let's Encrypt host must point at Pebble or a blackhole (.github/behat/pebble-setup.sh), never the real service. * diff --git a/features/rclone-install.sh b/features/rclone-install.sh new file mode 100644 index 00000000..5fe8d915 --- /dev/null +++ b/features/rclone-install.sh @@ -0,0 +1,72 @@ +#!/bin/bash +# Host rclone setups for the official-install scenarios. They move the runner's rclone aside, so only on CI; `restore` undoes all. +set -e +if [ "$GITHUB_ACTIONS" != true ] || [ -z "$CI" ]; then + echo "$0 replaces the host rclone: only on GitHub Actions" >&2 + exit 1 +fi +keep=/var/tmp/bh-site-command-rclone +# Rewritten in place: /etc/hosts may be a mount that sed -i can't rename over. +unblock() { + sed '/# bh-site-command-rclone$/d' /etc/hosts > "$keep.hosts" + cat "$keep.hosts" > /etc/hosts + rm -f "$keep.hosts" +} +# systemd-resolved re-reads /etc/hosts at most every few seconds; wait until lookups see the change ($1: 0 blocked, 1 not). +wait_blocked() { + for _ in $(seq 30); do + # A failed or empty lookup counts as neither state. + addrs= + if out=$(getent ahostsv4 downloads.rclone.org); then + addrs=$(printf '%s\n' "$out" | awk '{ print $1 }') + fi + state= + if printf '%s\n' "$addrs" | grep -qx '127\.0\.0\.1'; then + state=0 + elif [ -n "$addrs" ]; then + state=1 + fi + [ "$state" = "$1" ] && return + sleep 1 + done + exit 1 +} +case $1 in +# A snap of the current release in /snap/bin; the fake `snap` in $keep/bin removes it. +snap) + mkdir -p "$keep/bin" /snap/bin + mv /usr/bin/rclone "$keep/rclone" + printf '#!/bin/sh\nexec %s/rclone "$@"\n' "$keep" > /snap/bin/rclone + printf '#!/bin/sh\n[ "$*" = "remove rclone" ] && rm /snap/bin/rclone\n' > "$keep/bin/snap" + chmod 755 /snap/bin/rclone "$keep/bin/snap" + ;; +# The distro package's /usr/bin/rclone behind an older unpackaged copy in /usr/local/bin. +packaged-behind-old-copy) + mkdir -p "$keep" + mv /usr/bin/rclone "$keep/rclone" + apt-get update -qq + DEBIAN_FRONTEND=noninteractive apt-get install -y -qq rclone > /dev/null + printf '#!/bin/sh\ncase "$1" in version|--version) echo "rclone v1.63.1"; exit ;; esac\nexec %s/rclone "$@"\n' "$keep" > /usr/local/bin/rclone + chmod 755 /usr/local/bin/rclone + ;; +block-downloads) + echo '127.0.0.1 downloads.rclone.org # bh-site-command-rclone' >> /etc/hosts + wait_blocked 0 + ;; +unblock-downloads) + unblock + wait_blocked 1 + ;; +# Only after a setup above, which created $keep. +restore) + unblock + [ -d "$keep" ] || exit 0 + rm -f /snap/bin/rclone /usr/local/bin/rclone + dpkg -r rclone > /dev/null 2>&1 || true + if [ -f "$keep/rclone" ]; then + mv -f "$keep/rclone" /usr/bin/rclone + fi + rm -rf "$keep" + ;; +*) exit 64 ;; +esac diff --git a/src/helper/Site_Backup_Restore.php b/src/helper/Site_Backup_Restore.php index 35f50c09..5cc452c3 100644 --- a/src/helper/Site_Backup_Restore.php +++ b/src/helper/Site_Backup_Restore.php @@ -2540,47 +2540,51 @@ private function install_official_rclone( array $found ) { $this->rclone_install_failed( $found, sprintf( 'Could not download the installer (%s).', trim( $fetch->stderr ) ) ); } + // install.sh skips the download when `rclone --version` in PATH reports the current release, as a snap usually + // does; an empty stub first in PATH makes it always install. Root-only dir: everything in it runs as root. + $stub = EE_BACKUP_DIR . '/.rclone-stub'; + $write = sprintf( 'mkdir -p -m 0700 %1$s && printf "#!/bin/sh\n" > %1$s/rclone && chmod 0755 %1$s/rclone', escapeshellarg( $stub ) ); + if ( EE::launch( $write )->return_code ) { + unlink( $script ); + $this->rclone_install_failed( $found, sprintf( 'Could not write %s/rclone.', $stub ) ); + } + // install.sh unpacks the release zip with unzip. $this->check_and_install( 'unzip', 'unzip' ); - // install.sh downloads the release only after the package is gone; keep a copy to put back if that fails. + // install.sh overwrites /usr/bin/rclone, which a package may own even when another rclone comes first in PATH. + $packaged = in_array( $found['owner'], [ 'dpkg', 'rpm' ], true ) ? $found : $this->inspect_rclone( self::RCLONE_OFFICIAL_PATH ); + // The release is downloaded only after the package is gone; keep a copy to put back if that fails. $copy = ''; - if ( in_array( $found['owner'], [ 'dpkg', 'rpm' ], true ) ) { + if ( '' !== $packaged['owner'] ) { $copy = tempnam( sys_get_temp_dir(), 'ee-rclone-old' ); - if ( EE::launch( sprintf( 'cp -p %s %s', escapeshellarg( $found['path'] ), escapeshellarg( $copy ) ) )->return_code ) { + if ( EE::launch( sprintf( 'cp -p %s %s', escapeshellarg( $packaged['path'] ), escapeshellarg( $copy ) ) )->return_code ) { unlink( $copy ); unlink( $script ); - $this->rclone_install_failed( $found, sprintf( 'Could not keep a copy of %s.', $found['path'] ) ); + $this->rclone_install_failed( $found, sprintf( 'Could not keep a copy of %s.', $packaged['path'] ) ); } - } - - // `snap remove` deletes the config the snap keeps in its own home. - $snap_config = 'snap' === $found['owner'] && '' !== $target; - if ( $snap_config ) { - $this->copy_rclone_config( $old, $target ); - EE::log( sprintf( 'Copied the rclone config %s to %s, where the official rclone reads it.', $old, $target ) ); - } - - // install.sh overwrites /usr/bin/rclone, which a package may own even when another rclone comes first in PATH. - $owners = array_filter( array_unique( [ $found['owner'], $this->inspect_rclone( self::RCLONE_OFFICIAL_PATH )['owner'] ] ) ); - foreach ( $owners as $owner ) { - if ( EE::launch( self::RCLONE_PACKAGE_REMOVE[ $owner ] )->return_code ) { + if ( EE::launch( self::RCLONE_PACKAGE_REMOVE[ $packaged['owner'] ] )->return_code ) { unlink( $script ); - // While the snap keeps its config, the copy would only block the re-run as an existing default config. - if ( $snap_config && is_file( $old ) ) { - unlink( $target ); - } - $this->restore_packaged_rclone( $found, $copy ); - $this->rclone_install_failed( $found, sprintf( 'Could not remove the %s rclone package.', $owner ) ); + $this->restore_packaged_rclone( $packaged, $copy ); + $this->rclone_install_failed( $found, sprintf( 'Could not remove the %s rclone package.', $packaged['owner'] ) ); } } + // A snap owns no file install.sh writes, so it stays until the official build works. // Exit codes can't be trusted (3 means already up to date); the re-check below decides. - $install = EE::launch( sprintf( 'timeout -k 10 %d bash %s', self::RCLONE_INSTALL_TIMEOUT, escapeshellarg( $script ) ) ); + $run = sprintf( 'timeout -k 10 %d bash %s', self::RCLONE_INSTALL_TIMEOUT, escapeshellarg( $script ) ); + $install = EE::launch( sprintf( 'PATH=%s${PATH:+:$PATH} %s', escapeshellarg( $stub ), $run ) ); + $this->fs->remove( $stub ); unlink( $script ); $official_ok = '' === $this->inspect_rclone( self::RCLONE_OFFICIAL_PATH )['problem']; - $moved = $official_ok && '' !== $target && ! $snap_config; - if ( $moved ) { + // With /usr/bin/rclone in place the copy is never put back. + if ( $official_ok && '' !== $copy ) { + unlink( $copy ); + $copy = ''; + } + if ( $official_ok && 'snap' === $found['owner'] ) { + $this->remove_snap_rclone( $found, $old, $target ); + } elseif ( $official_ok && '' !== $target ) { $this->move_rclone_config( $old, $target ); } $after = $this->inspect_rclone(); @@ -2597,7 +2601,7 @@ private function install_official_rclone( array $found ) { if ( 124 === $install->return_code ) { $output = sprintf( 'the installer timed out after %d seconds', self::RCLONE_INSTALL_TIMEOUT ); } - $this->restore_packaged_rclone( $found, $copy ); + $this->restore_packaged_rclone( $packaged, $copy ); $this->rclone_install_failed( $found, sprintf( 'After the installer rclone %s (%s).', $after['problem'], $output ) ); } if ( '' !== $copy ) { @@ -2619,6 +2623,32 @@ private function install_official_rclone( array $found ) { EE::log( sprintf( 'Installed the official rclone %s (was %s).', $after['version'], $was ?: 'an unknown version' ) ); } + /** + * Remove a snap rclone once the official build works; its config is copied first, since `snap remove` deletes it. + * + * @param array $found inspect_rclone() result for the snap. + * @param string $old Its config file. + * @param string $target Where the official rclone reads it; '' when nothing has to move. + */ + private function remove_snap_rclone( array $found, $old, $target ) { + // Copied, not moved: a snap that can't be removed keeps working with its own config. + if ( '' !== $target ) { + $this->copy_rclone_config( $old, $target ); + EE::log( sprintf( 'Copied the rclone config %s to %s, where the official rclone reads it.', $old, $target ) ); + } + if ( ! EE::launch( self::RCLONE_PACKAGE_REMOVE['snap'] )->return_code ) { + return; + } + // Still first in PATH, the snap keeps its config; the copy would only block the re-run as an existing default. + if ( 'snap' === $this->inspect_rclone()['owner'] ) { + if ( '' !== $target ) { + unlink( $target ); + } + $this->rclone_install_failed( $found, 'Could not remove the snap rclone package.' ); + } + EE::warning( 'Could not remove the snap rclone. Remove it with `snap remove rclone`.' ); + } + /** * Put the copy of a removed package's rclone back when nothing replaced it, so the host keeps a working rclone. *