Commit 0a75b0b9 authored by Addshore's avatar Addshore 🏄
Browse files

If a user ctrl+cs during install, always move LS.php back

Uses defer and listens for SIGTERM
As well as doing some other nice cleanup.

This will also no longer leave backup files in the
mw directory which is nice :)

Bug: T291529
parent a2c3c01b
......@@ -20,9 +20,11 @@ package cmd
import (
"fmt"
"os"
"os/signal"
"os/user"
"path/filepath"
"strings"
"syscall"
"time"
"gerrit.wikimedia.org/r/mediawiki/tools/cli/internal/exec"
......@@ -263,7 +265,23 @@ The process hidden within this command is:
return
}
// TODO make sure of composer caches
// Fix some container mount permission issues
// Owned by root, but our webserver needs to be able to write
mwdd.DefaultForUser().Exec("mediawiki", []string{"chown", "-R", "nobody", "/var/www/html/w/data"}, exec.HandlerOptions{}, "root")
mwdd.DefaultForUser().Exec("mediawiki", []string{"chown", "-R", "nobody", "/var/log/mediawiki"}, exec.HandlerOptions{}, "root")
// Record the wiki domain that we are trying to create
var domain string = DbName + ".mediawiki.mwdd.localhost"
mwdd.DefaultForUser().RecordHostUsageBySite(domain)
// Figure out what and where we are installing
var serverLink string = "http://" + domain + ":" + mwdd.DefaultForUser().Env().Get("PORT")
const adminUser string = "admin"
const adminPass string = "mwddpassword"
// Check composer dependencies are up to date
checkComposer := func() {
// TODO make use of composer caches
composerErr := mwdd.DefaultForUser().ExecNoOutput("mediawiki", []string{
"php", "/var/www/html/w/maintenance/checkComposerLockUpToDate.php",
},
......@@ -286,34 +304,42 @@ The process hidden within this command is:
os.Exit(1)
}
}
}
checkComposer()
// Fix some permissions
mwdd.DefaultForUser().Exec("mediawiki", []string{"chown", "-R", "nobody", "/var/www/html/w/data"}, exec.HandlerOptions{}, "root")
mwdd.DefaultForUser().Exec("mediawiki", []string{"chown", "-R", "nobody", "/var/log/mediawiki"}, exec.HandlerOptions{}, "root")
// Run install.php
runInstall := func() {
installStartTime := time.Now().Format("20060102150405")
// Record the wiki domain that we are trying to create
var domain string = DbName + ".mediawiki.mwdd.localhost"
mwdd.DefaultForUser().RecordHostUsageBySite(domain)
// Copy current local settings "somewhere safe", incase someone needs to restore it
currentTime := time.Now()
currentTimeString := currentTime.Format("20060102150405")
moveLocalSettingsBack := func() {
// Move the LocalSettings.php back after install (or SIGTERM cancellation)
// TODO Don't do this in docker, do it on disk...
// TODO Check that the file we are moving does indeed exist, and we are not overwriting what we actually want!
mwdd.DefaultForUser().Exec("mediawiki", []string{
"cp",
"mv",
"/var/www/html/w/LocalSettings.php.mwdd.bak." + installStartTime,
"/var/www/html/w/LocalSettings.php",
"/var/www/html/w/LocalSettings.php.mwdd.bak." + currentTimeString,
}, exec.HandlerOptions{}, User)
}, exec.HandlerOptions{}, "root")
}
// Set up signal handling for graceful shutdown while LocalSettings.php is moved
c := make(chan os.Signal)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
moveLocalSettingsBack()
os.Exit(1)
}()
defer func() {
moveLocalSettingsBack()
}()
// Move custom LocalSetting.php so the install doesn't overwrite it
// Move the current LocalSettings "somewhere safe", incase someone needs to restore it
mwdd.DefaultForUser().Exec("mediawiki", []string{
"mv",
"/var/www/html/w/LocalSettings.php",
"/var/www/html/w/LocalSettings.php.mwdd.tmp",
}, exec.HandlerOptions{}, "root")
var serverLink string = "http://" + domain + ":" + mwdd.DefaultForUser().Env().Get("PORT")
const adminUser string = "admin"
const adminPass string = "mwddpassword"
"/var/www/html/w/LocalSettings.php.mwdd.bak." + installStartTime,
}, exec.HandlerOptions{}, User)
// Do a DB type dependant install, writing the output LocalSettings.php to /tmp
if DbType == "sqlite" {
......@@ -359,21 +385,19 @@ The process hidden within this command is:
adminUser,
}, exec.HandlerOptions{}, "nobody")
}
}
runInstall()
// Move the custom one back
mwdd.DefaultForUser().Exec("mediawiki", []string{
"mv",
"/var/www/html/w/LocalSettings.php.mwdd.tmp",
"/var/www/html/w/LocalSettings.php",
}, exec.HandlerOptions{}, "root")
// Run update.php once too
// Run update.php
runUpdate := func() {
mwdd.DefaultForUser().Exec("mediawiki", []string{
"php",
"/var/www/html/w/maintenance/update.php",
"--wiki", DbName,
"--quick",
}, exec.HandlerOptions{}, "nobody")
}
runUpdate()
fmt.Println("")
fmt.Println("***************************************")
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment