fix(update): preserve the service unit when migration fails

This commit is contained in:
Hamed Sanaei
2026-12-03 04:17:35 +03:30
parent 8cb1e9f1df
commit 56e0372a09
4 changed files with 123 additions and 21 deletions
+4 -2
View File
@@ -1473,8 +1473,6 @@ EOF
echo -e "${green}SSL certificate already configured. No action needed.${plain}"
fi
fi
${xui_folder}/x-ui migrate
}
xui_env_file_path() {
@@ -1961,6 +1959,10 @@ install_x-ui() {
chmod +x /usr/bin/x-ui
mkdir -p /var/log/x-ui
config_after_install
if ! "${xui_folder}/x-ui" migrate; then
echo -e "${red}ERROR: Database migration failed; stopping installation.${plain}" >&2
exit 1
fi
setup_fail2ban "$fail2ban_mode"
# Etckeeper compatibility
+5 -4
View File
@@ -37,10 +37,11 @@ import (
"gorm.io/gorm/logger"
)
var db *gorm.DB
var publishedDB atomic.Pointer[gorm.DB]
var connectionGeneration atomic.Uint64
var (
db *gorm.DB
publishedDB atomic.Pointer[gorm.DB]
connectionGeneration atomic.Uint64
)
// ConnectionGeneration invalidates read histories when InitDB replaces the database pool.
func ConnectionGeneration() uint64 { return connectionGeneration.Load() }
+102 -5
View File
@@ -279,11 +279,9 @@ func TestScriptConfigReadFailureKeepsServiceSetup(t *testing.T) {
if err != nil || !strings.Contains(out, "Unable to read webBasePath") {
t.Errorf("service setup should continue after the settings warning: %v\n%s", err, out)
}
wantLifecycle, wantServices := "fail2ban\nunit\n", "daemon-reload\nenable x-ui\nstart x-ui\n"
wantLifecycle, wantServices := "migrate\nfail2ban\nunit\n", "daemon-reload\nenable x-ui\nstart x-ui\n"
if script == "install.sh" {
wantServices = "daemon-reload\nenable x-ui\nrestart x-ui\n"
} else {
wantLifecycle = "migrate\n" + wantLifecycle
}
for name, want := range map[string]string{
"path": "/a/", "username": "admin", "password": "admin", "writes": "",
@@ -297,8 +295,8 @@ func TestScriptConfigReadFailureKeepsServiceSetup(t *testing.T) {
}
}
func TestUpdateMigrationFailurePreventsServiceStart(t *testing.T) {
dir, out, err := runScriptConfig(t, "update.sh", "/a/", false, "migration_failure", 1, true)
func TestInstallMigrationFailurePreventsServiceStart(t *testing.T) {
dir, out, err := runScriptConfig(t, "install.sh", "/a/", false, "migration_failure", 1, true)
if err == nil || !strings.Contains(out, "Database migration failed") {
t.Fatalf("migration failure should abort the update: %v\n%s", err, out)
}
@@ -309,3 +307,102 @@ func TestUpdateMigrationFailurePreventsServiceStart(t *testing.T) {
t.Fatalf("lifecycle after failed migration: %q", got)
}
}
func TestUpdateMigrationFailureKeepsServiceUnitForRetry(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("updater regression requires a POSIX environment")
}
bash, err := exec.LookPath("bash")
if err != nil {
t.Skip("bash is required for the isolated updater regression")
}
for _, distro := range []struct {
name, unit, stopped, started string
}{
{"ubuntu", "systemd/x-ui.service", "systemctl stop x-ui\nsystemctl disable x-ui\n", "systemctl daemon-reload\nsystemctl enable x-ui\nsystemctl start x-ui\n"},
{"alpine", "init.d/x-ui", "rc-service x-ui stop\nrc-update del x-ui\n", "rc-update add x-ui\nrc-service x-ui start\n"},
} {
t.Run(distro.name, func(t *testing.T) {
dir := t.TempDir()
for _, folder := range []string{"x-ui", "systemd", "init.d", "release/x-ui", "bin"} {
if err := os.MkdirAll(filepath.Join(dir, folder), 0o700); err != nil {
t.Fatal(err)
}
}
const binary = `#!/bin/bash
if [[ $1 == migrate ]]; then
printf 'migrate\n' >> "$TEST_ROOT/events"
[[ $TEST_FAIL_MIGRATION == 0 ]]
else
printf 'test version\n'
fi
`
for name, value := range map[string]string{
distro.unit: "old unit\n", "x-ui/x-ui": binary, "release/x-ui/x-ui": binary,
"release/x-ui/x-ui.sh": "#!/bin/bash\n", "release/x-ui/x-ui.service": "new unit\n",
} {
if err := os.WriteFile(filepath.Join(dir, name), []byte(value), 0o700); err != nil {
t.Fatal(err)
}
}
driver := `red= green= yellow= blue= plain=
xui_folder="$TEST_ROOT/x-ui"
xui_service="$TEST_ROOT/systemd"
release="$TEST_DISTRO"
XUI_UPDATE_TAG=test
curl_bin=fake_curl
load_xui_env() { :; }
setup_fail2ban() { :; }
config_after_update() { :; }
_xui_service_write_paths_dropin() { :; }
_xui_warn_unsupported_hardening() { :; }
chown() { :; }
pkill() { :; }
systemctl() { printf 'systemctl %s\n' "$*" >> "$TEST_ROOT/events"; }
rc-service() { printf 'rc-service %s\n' "$*" >> "$TEST_ROOT/events"; }
rc-update() { printf 'rc-update %s\n' "$*" >> "$TEST_ROOT/events"; }
fake_curl() {
case "$*" in
*.sha256) printf 404 ;;
*/x-ui.rc) printf 'new unit\n' > "$2" ;;
*) tar -czf "$2" -C "$TEST_ROOT/release" x-ui ;;
esac
}
`
driver += scriptFunction(t, "update.sh", "_fail") + scriptFunction(t, "update.sh", "_install_xui_service_unit")
driver += strings.NewReplacer(
"/etc/init.d/x-ui", "$TEST_ROOT/init.d/x-ui",
"/usr/bin/x-ui", "$TEST_ROOT/bin/x-ui",
"/var/log/x-ui", "$TEST_ROOT/log",
).Replace(scriptFunction(t, "update.sh", "update_x-ui"))
driver += "update_x-ui\n"
run := func(failMigration string) (string, error) {
t.Helper()
cmd := exec.Command(bash, "-c", driver)
cmd.Dir = dir
cmd.Env = append(os.Environ(), "TEST_ROOT="+dir, "TEST_DISTRO="+distro.name, "TEST_FAIL_MIGRATION="+failMigration)
out, err := cmd.CombinedOutput()
return string(out), err
}
if out, err := run("1"); err == nil || !strings.Contains(out, "Database migration failed") {
t.Fatalf("first update should fail in migration: %v\n%s", err, out)
}
if unit := readScriptState(t, dir, distro.unit); unit != "old unit\n" {
t.Fatalf("failed update changed the service unit: %q", unit)
}
if got := readScriptState(t, dir, "events"); got != distro.stopped+"migrate\n" {
t.Fatalf("failed update service actions: %q", got)
}
if out, err := run("0"); err != nil {
t.Fatalf("ordinary update retry failed: %v\n%s", err, out)
}
if unit := readScriptState(t, dir, distro.unit); unit != "new unit\n" {
t.Fatalf("retry did not install the replacement unit: %q", unit)
}
if got := readScriptState(t, dir, "events"); got != strings.Repeat(distro.stopped+"migrate\n", 2)+distro.started {
t.Fatalf("retry service actions: %q", got)
}
})
}
}
+12 -10
View File
@@ -577,12 +577,12 @@ update_x-ui() {
if [[ -e ${xui_folder}/ ]]; then
echo -e "${green}Stopping x-ui...${plain}"
# Keep the stopped, disabled unit in place so an update can be retried
# if migration fails before the replacement unit is installed.
if [[ $release == "alpine" ]]; then
if [ -f "/etc/init.d/x-ui" ]; then
rc-service x-ui stop > /dev/null 2>&1
rc-update del x-ui > /dev/null 2>&1
echo -e "${green}Removing old service unit version...${plain}"
rm -f /etc/init.d/x-ui > /dev/null 2>&1
else
rm x-ui-linux-$(arch).tar.gz -f > /dev/null 2>&1
_fail "ERROR: x-ui service unit not installed."
@@ -591,9 +591,6 @@ update_x-ui() {
if [ -f "${xui_service}/x-ui.service" ]; then
systemctl stop x-ui > /dev/null 2>&1
systemctl disable x-ui > /dev/null 2>&1
echo -e "${green}Removing old systemd unit version...${plain}"
rm ${xui_service}/x-ui.service -f > /dev/null 2>&1
systemctl daemon-reload > /dev/null 2>&1
else
rm x-ui-linux-$(arch).tar.gz -f > /dev/null 2>&1
_fail "ERROR: x-ui systemd unit not installed."
@@ -607,10 +604,12 @@ update_x-ui() {
pkill -f 'tuic-server.*-c .*bin/tuic/tuic_[0-9]+\.json' > /dev/null 2>&1 || true
echo -e "${green}Removing old x-ui version...${plain}"
rm ${xui_folder} -f > /dev/null 2>&1
rm ${xui_folder}/x-ui.service -f > /dev/null 2>&1
rm ${xui_folder}/x-ui.service.debian -f > /dev/null 2>&1
rm ${xui_folder}/x-ui.service.arch -f > /dev/null 2>&1
rm ${xui_folder}/x-ui.service.rhel -f > /dev/null 2>&1
local template
for template in x-ui.service x-ui.service.debian x-ui.service.arch x-ui.service.rhel; do
if [[ ! "${xui_folder}/${template}" -ef "${xui_service}/x-ui.service" ]]; then
rm -f "${xui_folder}/${template}" > /dev/null 2>&1
fi
done
rm ${xui_folder}/x-ui -f > /dev/null 2>&1
rm ${xui_folder}/x-ui.sh -f > /dev/null 2>&1
echo -e "${green}Removing old mtg version...${plain}"
@@ -682,7 +681,10 @@ update_x-ui() {
# Finish the schema/data migrations before the service starts.
echo -e "${green}Migrating database...${plain}"
if ! "${xui_folder}/x-ui" migrate; then
_fail "ERROR: Database migration failed; x-ui was not started."
if [[ $release == "alpine" ]]; then
rm -f "${xui_rc_temp}"
fi
_fail "ERROR: Database migration failed; x-ui was not started. Fix the migration error and rerun the update."
fi
setup_fail2ban