Skip to content

Commit

Permalink
caddycmd: Add --skip-cleanup to upgrade commands
Browse files Browse the repository at this point in the history
This is a partial fix for #4057, making it possible to retain the old build of Caddy, in case something went wrong.
  • Loading branch information
francislavoie committed Oct 19, 2021
1 parent 062657d commit c49af88
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
15 changes: 15 additions & 0 deletions cmd/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,11 @@ is always printed to stdout.`,
Long: `
Downloads an updated Caddy binary with the same modules/plugins at the
latest versions. EXPERIMENTAL: May be changed or removed.`,
Flags: func() *flag.FlagSet {
fs := flag.NewFlagSet("upgrade", flag.ExitOnError)
fs.Bool("skip-cleanup", false, "Skips removing the backed up binary")
return fs
}(),
})

RegisterCommand(Command{
Expand All @@ -308,6 +313,11 @@ Downloads an updated Caddy binary with the specified packages (module/plugin)
added. Retains existing packages. Returns an error if the any of packages are
already included. EXPERIMENTAL: May be changed or removed.
`,
Flags: func() *flag.FlagSet {
fs := flag.NewFlagSet("add-package", flag.ExitOnError)
fs.Bool("skip-cleanup", false, "Skips removing the backed up binary")
return fs
}(),
})

RegisterCommand(Command{
Expand All @@ -320,6 +330,11 @@ Downloads an updated Caddy binaries without the specified packages (module/plugi
Returns an error if any of the packages are not included.
EXPERIMENTAL: May be changed or removed.
`,
Flags: func() *flag.FlagSet {
fs := flag.NewFlagSet("remove-package", flag.ExitOnError)
fs.Bool("skip-cleanup", false, "Skips removing the backed up binary")
return fs
}(),
})

}
Expand Down
19 changes: 12 additions & 7 deletions cmd/packagesfuncs.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
"go.uber.org/zap"
)

func cmdUpgrade(_ Flags) (int, error) {
func cmdUpgrade(fl Flags) (int, error) {
_, nonstandard, _, err := getModules()
if err != nil {
return caddy.ExitCodeFailedStartup, fmt.Errorf("unable to enumerate installed plugins: %v", err)
Expand All @@ -41,7 +41,7 @@ func cmdUpgrade(_ Flags) (int, error) {
return caddy.ExitCodeFailedStartup, err
}

return upgradeBuild(pluginPkgs)
return upgradeBuild(pluginPkgs, fl)
}

func cmdAddPackage(fl Flags) (int, error) {
Expand All @@ -64,7 +64,7 @@ func cmdAddPackage(fl Flags) (int, error) {
pluginPkgs[arg] = struct{}{}
}

return upgradeBuild(pluginPkgs)
return upgradeBuild(pluginPkgs, fl)
}

func cmdRemovePackage(fl Flags) (int, error) {
Expand All @@ -88,10 +88,10 @@ func cmdRemovePackage(fl Flags) (int, error) {
delete(pluginPkgs, arg)
}

return upgradeBuild(pluginPkgs)
return upgradeBuild(pluginPkgs, fl)
}

func upgradeBuild(pluginPkgs map[string]struct{}) (int, error) {
func upgradeBuild(pluginPkgs map[string]struct{}, fl Flags) (int, error) {
l := caddy.Log()

thisExecPath, err := os.Executable()
Expand Down Expand Up @@ -161,9 +161,14 @@ func upgradeBuild(pluginPkgs map[string]struct{}) (int, error) {
fmt.Println()

// clean up the backup file
if err = os.Remove(backupExecPath); err != nil {
return caddy.ExitCodeFailedStartup, fmt.Errorf("download succeeded, but unable to clean up backup binary: %v", err)
if !fl.Bool("skip-cleanup") {
if err = os.Remove(backupExecPath); err != nil {
return caddy.ExitCodeFailedStartup, fmt.Errorf("download succeeded, but unable to clean up backup binary: %v", err)
}
} else {
l.Info("skipped cleaning up the backup file", zap.String("backup_path", backupExecPath))
}

l.Info("upgrade successful; please restart any running Caddy instances", zap.String("executable", thisExecPath))

return caddy.ExitCodeSuccess, nil
Expand Down

0 comments on commit c49af88

Please sign in to comment.