|
| 1 | +//go:build !remote |
| 2 | + |
| 3 | +package libpod |
| 4 | + |
| 5 | +import ( |
| 6 | + "fmt" |
| 7 | + "net/http" |
| 8 | + |
| 9 | + "github.com/containers/podman/v5/libpod" |
| 10 | + "github.com/containers/podman/v5/pkg/api/handlers" |
| 11 | + "github.com/containers/podman/v5/pkg/api/handlers/utils" |
| 12 | + api "github.com/containers/podman/v5/pkg/api/types" |
| 13 | + "github.com/containers/podman/v5/pkg/auth" |
| 14 | + "github.com/containers/podman/v5/pkg/domain/entities" |
| 15 | + "github.com/containers/podman/v5/pkg/domain/infra/abi" |
| 16 | + "github.com/containers/podman/v5/pkg/errorhandling" |
| 17 | + "github.com/gorilla/schema" |
| 18 | + "go.podman.io/image/v5/types" |
| 19 | +) |
| 20 | + |
| 21 | +func AutoUpdate(w http.ResponseWriter, r *http.Request) { |
| 22 | + decoder := r.Context().Value(api.DecoderKey).(*schema.Decoder) |
| 23 | + runtime := r.Context().Value(api.RuntimeKey).(*libpod.Runtime) |
| 24 | + |
| 25 | + query := struct { |
| 26 | + DryRun bool `schema:"dryRun"` |
| 27 | + Rollback bool `schema:"rollback"` |
| 28 | + TLSVerify types.OptionalBool `schema:"tlsVerify"` |
| 29 | + }{} |
| 30 | + |
| 31 | + if err := decoder.Decode(&query, r.URL.Query()); err != nil { |
| 32 | + utils.Error(w, http.StatusBadRequest, fmt.Errorf("failed to parse parameters for %s: %w", r.URL.String(), err)) |
| 33 | + return |
| 34 | + } |
| 35 | + |
| 36 | + _, authfile, err := auth.GetCredentials(r) |
| 37 | + if err != nil { |
| 38 | + utils.Error(w, http.StatusBadRequest, err) |
| 39 | + return |
| 40 | + } |
| 41 | + defer auth.RemoveAuthfile(authfile) |
| 42 | + |
| 43 | + containerEngine := abi.ContainerEngine{Libpod: runtime} |
| 44 | + |
| 45 | + options := entities.AutoUpdateOptions{ |
| 46 | + Authfile: authfile, |
| 47 | + DryRun: query.DryRun, |
| 48 | + Rollback: query.Rollback, |
| 49 | + InsecureSkipTLSVerify: types.OptionalBoolUndefined, |
| 50 | + } |
| 51 | + |
| 52 | + // If TLS verification is explicitly specified (True or False) in the query, |
| 53 | + // set the InsecureSkipTLSVerify option accordingly. |
| 54 | + // If TLSVerify was not set in the query, OptionalBoolUndefined is used and |
| 55 | + // handled later based off the target registry configuration. |
| 56 | + switch query.TLSVerify { |
| 57 | + case types.OptionalBoolTrue: |
| 58 | + options.InsecureSkipTLSVerify = types.NewOptionalBool(false) |
| 59 | + case types.OptionalBoolFalse: |
| 60 | + options.InsecureSkipTLSVerify = types.NewOptionalBool(true) |
| 61 | + case types.OptionalBoolUndefined: |
| 62 | + // If the user doesn't define TLSVerify in the query, do nothing and pass |
| 63 | + // it to the backend code to handle. |
| 64 | + default: // Should never happen |
| 65 | + panic("Unexpected handling occurred for TLSVerify") |
| 66 | + } |
| 67 | + |
| 68 | + autoUpdateReports, autoUpdateFailures := containerEngine.AutoUpdate(r.Context(), options) |
| 69 | + if autoUpdateReports == nil { |
| 70 | + if err := errorhandling.JoinErrors(autoUpdateFailures); err != nil { |
| 71 | + utils.Error(w, http.StatusInternalServerError, err) |
| 72 | + return |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + reports := handlers.LibpodAutoUpdateReports{Reports: autoUpdateReports, Errors: errorhandling.ErrorsToStrings(autoUpdateFailures)} |
| 77 | + |
| 78 | + utils.WriteResponse(w, http.StatusOK, reports) |
| 79 | +} |
0 commit comments