github.com/containers/podman/v2@v2.2.2-0.20210501105131-c1e07d070c4c/pkg/api/handlers/libpod/system.go (about)

     1  package libpod
     2  
     3  import (
     4  	"net/http"
     5  
     6  	"github.com/containers/podman/v2/libpod"
     7  	"github.com/containers/podman/v2/pkg/api/handlers/compat"
     8  	"github.com/containers/podman/v2/pkg/api/handlers/utils"
     9  	"github.com/containers/podman/v2/pkg/domain/entities"
    10  	"github.com/containers/podman/v2/pkg/domain/infra/abi"
    11  	"github.com/gorilla/schema"
    12  	"github.com/pkg/errors"
    13  )
    14  
    15  // SystemPrune removes unused data
    16  func SystemPrune(w http.ResponseWriter, r *http.Request) {
    17  	var (
    18  		decoder           = r.Context().Value("decoder").(*schema.Decoder)
    19  		runtime           = r.Context().Value("runtime").(*libpod.Runtime)
    20  		systemPruneReport = new(entities.SystemPruneReport)
    21  	)
    22  	query := struct {
    23  		All     bool `schema:"all"`
    24  		Volumes bool `schema:"volumes"`
    25  	}{}
    26  
    27  	if err := decoder.Decode(&query, r.URL.Query()); err != nil {
    28  		utils.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest,
    29  			errors.Wrapf(err, "failed to parse parameters for %s", r.URL.String()))
    30  		return
    31  	}
    32  
    33  	podPruneReport, err := PodPruneHelper(w, r)
    34  	if err != nil {
    35  		utils.InternalServerError(w, err)
    36  		return
    37  	}
    38  	systemPruneReport.PodPruneReport = podPruneReport
    39  
    40  	// We could parallelize this, should we?
    41  	containerPruneReport, err := compat.PruneContainersHelper(w, r, nil)
    42  	if err != nil {
    43  		utils.InternalServerError(w, err)
    44  		return
    45  	}
    46  	systemPruneReport.ContainerPruneReport = containerPruneReport
    47  
    48  	results, err := runtime.ImageRuntime().PruneImages(r.Context(), query.All, nil)
    49  	if err != nil {
    50  		utils.InternalServerError(w, err)
    51  		return
    52  	}
    53  
    54  	report := entities.ImagePruneReport{
    55  		Report: entities.Report{
    56  			Id:  results,
    57  			Err: nil,
    58  		},
    59  	}
    60  
    61  	systemPruneReport.ImagePruneReport = &report
    62  
    63  	if query.Volumes {
    64  		volumePruneReport, err := pruneVolumesHelper(r)
    65  		if err != nil {
    66  			utils.InternalServerError(w, err)
    67  			return
    68  		}
    69  		systemPruneReport.VolumePruneReport = volumePruneReport
    70  	}
    71  	utils.WriteResponse(w, http.StatusOK, systemPruneReport)
    72  }
    73  
    74  func DiskUsage(w http.ResponseWriter, r *http.Request) {
    75  	// Options are only used by the CLI
    76  	options := entities.SystemDfOptions{}
    77  	runtime := r.Context().Value("runtime").(*libpod.Runtime)
    78  	ic := abi.ContainerEngine{Libpod: runtime}
    79  	response, err := ic.SystemDf(r.Context(), options)
    80  	if err != nil {
    81  		utils.InternalServerError(w, err)
    82  	}
    83  	utils.WriteResponse(w, http.StatusOK, response)
    84  }