github.com/containers/libpod@v1.9.4-0.20220419124438-4284fd425507/pkg/varlinkapi/volumes.go (about)

     1  // +build varlink
     2  
     3  package varlinkapi
     4  
     5  import (
     6  	"encoding/json"
     7  
     8  	"github.com/containers/libpod/cmd/podman/shared"
     9  	"github.com/containers/libpod/libpod"
    10  	iopodman "github.com/containers/libpod/pkg/varlink"
    11  )
    12  
    13  // VolumeCreate creates a libpod volume based on input from a varlink connection
    14  func (i *VarlinkAPI) VolumeCreate(call iopodman.VarlinkCall, options iopodman.VolumeCreateOpts) error {
    15  	var volumeOptions []libpod.VolumeCreateOption
    16  
    17  	if len(options.VolumeName) > 0 {
    18  		volumeOptions = append(volumeOptions, libpod.WithVolumeName(options.VolumeName))
    19  	}
    20  	if len(options.Driver) > 0 {
    21  		volumeOptions = append(volumeOptions, libpod.WithVolumeDriver(options.Driver))
    22  	}
    23  	if len(options.Labels) > 0 {
    24  		volumeOptions = append(volumeOptions, libpod.WithVolumeLabels(options.Labels))
    25  	}
    26  	if len(options.Options) > 0 {
    27  		parsedOptions, err := shared.ParseVolumeOptions(options.Options)
    28  		if err != nil {
    29  			return call.ReplyErrorOccurred(err.Error())
    30  		}
    31  		volumeOptions = append(volumeOptions, parsedOptions...)
    32  	}
    33  	newVolume, err := i.Runtime.NewVolume(getContext(), volumeOptions...)
    34  	if err != nil {
    35  		return call.ReplyErrorOccurred(err.Error())
    36  	}
    37  	return call.ReplyVolumeCreate(newVolume.Name())
    38  }
    39  
    40  // VolumeRemove removes volumes by options.All or options.Volumes
    41  func (i *VarlinkAPI) VolumeRemove(call iopodman.VarlinkCall, options iopodman.VolumeRemoveOpts) error {
    42  	success, failed, err := shared.SharedRemoveVolumes(getContext(), i.Runtime, options.Volumes, options.All, options.Force)
    43  	if err != nil {
    44  		return call.ReplyErrorOccurred(err.Error())
    45  	}
    46  	// Convert map[string]string to map[string]error
    47  	errStrings := make(map[string]string)
    48  	for k, v := range failed {
    49  		errStrings[k] = v.Error()
    50  	}
    51  	return call.ReplyVolumeRemove(success, errStrings)
    52  }
    53  
    54  // GetVolumes returns all the volumes known to the remote system
    55  func (i *VarlinkAPI) GetVolumes(call iopodman.VarlinkCall, args []string, all bool) error {
    56  	var (
    57  		err     error
    58  		reply   []*libpod.Volume
    59  		volumes []iopodman.Volume
    60  	)
    61  	if all {
    62  		reply, err = i.Runtime.GetAllVolumes()
    63  	} else {
    64  		for _, v := range args {
    65  			vol, err := i.Runtime.GetVolume(v)
    66  			if err != nil {
    67  				return err
    68  			}
    69  			reply = append(reply, vol)
    70  		}
    71  	}
    72  	if err != nil {
    73  		return call.ReplyErrorOccurred(err.Error())
    74  	}
    75  	// Build the iopodman.volume struct for the return
    76  	for _, v := range reply {
    77  		newVol := iopodman.Volume{
    78  			Driver:     v.Driver(),
    79  			Labels:     v.Labels(),
    80  			MountPoint: v.MountPoint(),
    81  			Name:       v.Name(),
    82  			Options:    v.Options(),
    83  		}
    84  		volumes = append(volumes, newVol)
    85  	}
    86  	return call.ReplyGetVolumes(volumes)
    87  }
    88  
    89  // InspectVolume inspects a single volume, returning its JSON as a string.
    90  func (i *VarlinkAPI) InspectVolume(call iopodman.VarlinkCall, name string) error {
    91  	vol, err := i.Runtime.LookupVolume(name)
    92  	if err != nil {
    93  		return call.ReplyErrorOccurred(err.Error())
    94  	}
    95  	inspectOut, err := vol.Inspect()
    96  	if err != nil {
    97  		return call.ReplyErrorOccurred(err.Error())
    98  	}
    99  	inspectJSON, err := json.Marshal(inspectOut)
   100  	if err != nil {
   101  		return call.ReplyErrorOccurred(err.Error())
   102  	}
   103  	return call.ReplyInspectVolume(string(inspectJSON))
   104  }
   105  
   106  // VolumesPrune removes unused images via a varlink call
   107  func (i *VarlinkAPI) VolumesPrune(call iopodman.VarlinkCall) error {
   108  	var (
   109  		prunedErrors []string
   110  		prunedNames  []string
   111  	)
   112  	responses, err := i.Runtime.PruneVolumes(getContext())
   113  	if err != nil {
   114  		return call.ReplyVolumesPrune([]string{}, []string{err.Error()})
   115  	}
   116  	for k, v := range responses {
   117  		if v == nil {
   118  			prunedNames = append(prunedNames, k)
   119  		} else {
   120  			prunedErrors = append(prunedErrors, v.Error())
   121  		}
   122  	}
   123  	return call.ReplyVolumesPrune(prunedNames, prunedErrors)
   124  }