Cleanup build scratch dir if remote end disconnects while passing the context

Signed-off-by: Garth Bushell <garth@garthy.com>
This commit is contained in:
Garth Bushell 2024-03-15 10:38:46 +00:00
parent f5abca415d
commit 3a9eef4bee
1 changed files with 10 additions and 10 deletions

View File

@ -50,7 +50,7 @@ func BuildImage(w http.ResponseWriter, r *http.Request) {
} }
} }
contextDirectory, err := extractTarFile(r) anchorDir, err := os.MkdirTemp("", "libpod_builder")
if err != nil { if err != nil {
utils.InternalServerError(w, err) utils.InternalServerError(w, err)
return return
@ -64,12 +64,18 @@ func BuildImage(w http.ResponseWriter, r *http.Request) {
} }
} }
} }
err := os.RemoveAll(filepath.Dir(contextDirectory)) err := os.RemoveAll(anchorDir)
if err != nil { if err != nil {
logrus.Warn(fmt.Errorf("failed to remove build scratch directory %q: %w", filepath.Dir(contextDirectory), err)) logrus.Warn(fmt.Errorf("failed to remove build scratch directory %q: %w", anchorDir, err))
} }
}() }()
contextDirectory, err := extractTarFile(anchorDir, r)
if err != nil {
utils.InternalServerError(w, err)
return
}
query := struct { query := struct {
AddHosts string `schema:"extrahosts"` AddHosts string `schema:"extrahosts"`
AdditionalCapabilities string `schema:"addcaps"` AdditionalCapabilities string `schema:"addcaps"`
@ -884,13 +890,7 @@ func parseLibPodIsolation(isolation string) (buildah.Isolation, error) {
return parse.IsolationOption(isolation) return parse.IsolationOption(isolation)
} }
func extractTarFile(r *http.Request) (string, error) { func extractTarFile(anchorDir string, r *http.Request) (string, error) {
// build a home for the request body
anchorDir, err := os.MkdirTemp("", "libpod_builder")
if err != nil {
return "", err
}
path := filepath.Join(anchorDir, "tarBall") path := filepath.Join(anchorDir, "tarBall")
tarBall, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0o600) tarBall, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0o600)
if err != nil { if err != nil {