From 984d384d148090cdb0f6aa2f234a8b946c3b9ee3 Mon Sep 17 00:00:00 2001 From: Matthew Holt Date: Tue, 31 Dec 2019 16:47:35 -0700 Subject: Change storage paths to follow OS conventions; migrate folder (#2955) --- cmd/commandfuncs.go | 21 ++++++++++++++++ cmd/main.go | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+) (limited to 'cmd') diff --git a/cmd/commandfuncs.go b/cmd/commandfuncs.go index 6dc7c52..cc55df2 100644 --- a/cmd/commandfuncs.go +++ b/cmd/commandfuncs.go @@ -154,6 +154,8 @@ func cmdRun(fl Flags) (int, error) { if err != nil { return caddy.ExitCodeFailedStartup, err } + // TODO: This is TEMPORARY, until the RCs + moveStorage() // set a fitting User-Agent for ACME requests goModule := caddy.GoModule() @@ -190,6 +192,25 @@ func cmdRun(fl Flags) (int, error) { } } + // warn if the environment does not provide enough information about the disk + hasXDG := os.Getenv("XDG_DATA_HOME") != "" && + os.Getenv("XDG_CONFIG_HOME") != "" && + os.Getenv("XDG_CACHE_HOME") != "" + switch runtime.GOOS { + case "windows": + if os.Getenv("HOME") == "" && os.Getenv("USERPROFILE") == "" && !hasXDG { + caddy.Log().Warn("neither HOME nor USERPROFILE environment variables are set - please fix; some assets might be stored in ./caddy") + } + case "plan9": + if os.Getenv("home") == "" && !hasXDG { + caddy.Log().Warn("$home environment variable is empty - please fix; some assets might be stored in ./caddy") + } + default: + if os.Getenv("HOME") == "" && !hasXDG { + caddy.Log().Warn("$HOME environment variable is empty - please fix; some assets might be stored in ./caddy") + } + } + select {} } diff --git a/cmd/main.go b/cmd/main.go index a86c04a..ca9b914 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -22,12 +22,15 @@ import ( "io/ioutil" "net" "os" + "path/filepath" + "runtime" "strconv" "strings" "time" "github.com/caddyserver/caddy/v2" "github.com/caddyserver/caddy/v2/caddyconfig" + "go.uber.org/zap" ) // Main implements the main function of the caddy command. @@ -226,3 +229,71 @@ func printEnvironment() { fmt.Println(v) } } + +// moveStorage moves the old default dataDir to the new default dataDir. +// TODO: This is TEMPORARY until the release candidates. +func moveStorage() { + // get the home directory (the old way) + oldHome := os.Getenv("HOME") + if oldHome == "" && runtime.GOOS == "windows" { + drive := os.Getenv("HOMEDRIVE") + path := os.Getenv("HOMEPATH") + oldHome = drive + path + if drive == "" || path == "" { + oldHome = os.Getenv("USERPROFILE") + } + } + if oldHome == "" { + oldHome = "." + } + oldDataDir := filepath.Join(oldHome, ".local", "share", "caddy") + + // nothing to do if old data dir doesn't exist + _, err := os.Stat(oldDataDir) + if os.IsNotExist(err) { + return + } + + // nothing to do if the new data dir is the same as the old one + newDataDir := caddy.AppDataDir() + if oldDataDir == newDataDir { + return + } + + logger := caddy.Log().Named("automigrate").With( + zap.String("old_dir", oldDataDir), + zap.String("new_dir", newDataDir)) + + logger.Info("beginning one-time data directory migration", + zap.String("details", "https://github.com/caddyserver/caddy/issues/2955")) + + // if new data directory exists, avoid auto-migration as a conservative safety measure + _, err = os.Stat(newDataDir) + if !os.IsNotExist(err) { + logger.Error("new data directory already exists; skipping auto-migration as conservative safety measure", + zap.Error(err), + zap.String("instructions", "https://github.com/caddyserver/caddy/issues/2955#issuecomment-570000333")) + return + } + + // construct the new data directory's parent folder + err = os.MkdirAll(filepath.Dir(newDataDir), 0700) + if err != nil { + logger.Error("unable to make new datadirectory - follow link for instructions", + zap.String("instructions", "https://github.com/caddyserver/caddy/issues/2955#issuecomment-570000333"), + zap.Error(err)) + return + } + + // folder structure is same, so just try to rename (move) it; + // this fails if the new path is on a separate device + err = os.Rename(oldDataDir, newDataDir) + if err != nil { + logger.Error("new data directory already exists; skipping auto-migration as conservative safety measure - follow link for instructions", + zap.String("instructions", "https://github.com/caddyserver/caddy/issues/2955#issuecomment-570000333"), + zap.Error(err)) + } + + logger.Info("successfully completed one-time migration of data directory", + zap.String("details", "https://github.com/caddyserver/caddy/issues/2955")) +} -- cgit v1.2.3