MCPcopy
hub / github.com/caddyserver/caddy / upgradeBuild

Function upgradeBuild

cmd/packagesfuncs.go:124–214  ·  view source on GitHub ↗
(pluginPkgs map[string]pluginPackage, fl Flags)

Source from the content-addressed store, hash-verified

122}
123
124func upgradeBuild(pluginPkgs map[string]pluginPackage, fl Flags) (int, error) {
125 l := caddy.Log()
126
127 thisExecPath, err := os.Executable()
128 if err != nil {
129 return caddy.ExitCodeFailedStartup, fmt.Errorf("determining current executable path: %v", err)
130 }
131 thisExecStat, err := os.Stat(thisExecPath)
132 if err != nil {
133 return caddy.ExitCodeFailedStartup, fmt.Errorf("retrieving current executable permission bits: %v", err)
134 }
135 if thisExecStat.Mode()&os.ModeSymlink == os.ModeSymlink {
136 symSource := thisExecPath
137 // we are a symlink; resolve it
138 thisExecPath, err = filepath.EvalSymlinks(thisExecPath)
139 if err != nil {
140 return caddy.ExitCodeFailedStartup, fmt.Errorf("resolving current executable symlink: %v", err)
141 }
142 l.Info("this executable is a symlink", zap.String("source", symSource), zap.String("target", thisExecPath))
143 }
144 l.Info("this executable will be replaced", zap.String("path", thisExecPath))
145
146 // build the request URL to download this custom build
147 qs := url.Values{
148 "os": {runtime.GOOS},
149 "arch": {runtime.GOARCH},
150 }
151 for _, pkgInfo := range pluginPkgs {
152 qs.Add("p", pkgInfo.String())
153 }
154
155 // initiate the build
156 resp, err := downloadBuild(qs)
157 if err != nil {
158 return caddy.ExitCodeFailedStartup, fmt.Errorf("download failed: %v", err)
159 }
160 defer resp.Body.Close()
161
162 // back up the current binary, in case something goes wrong we can replace it
163 backupExecPath := thisExecPath + ".tmp"
164 l.Info("build acquired; backing up current executable",
165 zap.String("current_path", thisExecPath),
166 zap.String("backup_path", backupExecPath))
167 err = os.Rename(thisExecPath, backupExecPath)
168 if err != nil {
169 return caddy.ExitCodeFailedStartup, fmt.Errorf("backing up current binary: %v", err)
170 }
171 defer func() {
172 if err != nil {
173 err2 := os.Rename(backupExecPath, thisExecPath)
174 if err2 != nil {
175 l.Error("restoring original executable failed; will need to be restored manually",
176 zap.String("backup_path", backupExecPath),
177 zap.String("original_path", thisExecPath),
178 zap.Error(err2))
179 }
180 }
181 }()

Callers 3

cmdUpgradeFunction · 0.85
cmdAddPackageFunction · 0.85
cmdRemovePackageFunction · 0.85

Calls 12

downloadBuildFunction · 0.85
writeCaddyBinaryFunction · 0.85
listModulesFunction · 0.85
showVersionFunction · 0.85
StatMethod · 0.80
PrintlnMethod · 0.80
removeCaddyBinaryFunction · 0.70
StringMethod · 0.45
AddMethod · 0.45
CloseMethod · 0.45
ErrorMethod · 0.45
BoolMethod · 0.45

Tested by

no test coverage detected