aboutsummaryrefslogtreecommitdiff
path: root/cmd/podman/utils/utils.go
blob: 8063f4309e08b1fb37ade65b4ed49fe7149b0945 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package utils

import (
	"context"
	"fmt"
	"os"

	"github.com/containers/podman/v4/cmd/podman/registry"
	"github.com/containers/podman/v4/libpod/define"
	"github.com/containers/podman/v4/pkg/domain/entities"
	"github.com/containers/podman/v4/pkg/domain/entities/reports"
)

// IsDir returns true if the specified path refers to a directory.
func IsDir(path string) bool {
	file, err := os.Stat(path)
	if err != nil {
		return false
	}
	return file.IsDir()
}

// FileExists returns true if path refers to an existing file.
func FileExists(path string) bool {
	file, err := os.Stat(path)
	// All errors return file == nil
	if err != nil {
		return false
	}
	return !file.IsDir()
}

func PrintPodPruneResults(podPruneReports []*entities.PodPruneReport, heading bool) error {
	var errs OutputErrors
	if heading && len(podPruneReports) > 0 {
		fmt.Println("Deleted Pods")
	}
	for _, r := range podPruneReports {
		if r.Err == nil {
			fmt.Println(r.Id)
		} else {
			errs = append(errs, r.Err)
		}
	}
	return errs.PrintErrors()
}

func PrintContainerPruneResults(containerPruneReports []*reports.PruneReport, heading bool) error {
	var errs OutputErrors
	if heading && len(containerPruneReports) > 0 {
		fmt.Println("Deleted Containers")
	}
	for _, v := range containerPruneReports {
		fmt.Println(v.Id)
		if v.Err != nil {
			errs = append(errs, v.Err)
		}
	}
	return errs.PrintErrors()
}

func PrintVolumePruneResults(volumePruneReport []*reports.PruneReport, heading bool) error {
	var errs OutputErrors
	if heading && len(volumePruneReport) > 0 {
		fmt.Println("Deleted Volumes")
	}
	for _, r := range volumePruneReport {
		if r.Err == nil {
			fmt.Println(r.Id)
		} else {
			errs = append(errs, r.Err)
		}
	}
	return errs.PrintErrors()
}

func PrintImagePruneResults(imagePruneReports []*reports.PruneReport, heading bool) error {
	if heading && len(imagePruneReports) > 0 {
		fmt.Println("Deleted Images")
	}
	for _, r := range imagePruneReports {
		fmt.Println(r.Id)
		if r.Err != nil {
			fmt.Fprint(os.Stderr, r.Err.Error()+"\n")
		}
	}

	return nil
}

func PrintNetworkPruneResults(networkPruneReport []*entities.NetworkPruneReport, heading bool) error {
	var errs OutputErrors
	if heading && len(networkPruneReport) > 0 {
		fmt.Println("Deleted Networks")
	}
	for _, r := range networkPruneReport {
		if r.Error == nil {
			fmt.Println(r.Name)
		} else {
			errs = append(errs, r.Error)
		}
	}
	return errs.PrintErrors()
}

// IsCheckpointImage returns true with no error only if all values in
// namesOrIDs correspond to checkpoint images AND these images are
// compatible with the container runtime that is currently in use,
// e.g., crun or runc.
//
// IsCheckpointImage returns false with no error when none of the values
// in namesOrIDs corresponds to an ID or name of an image.
//
// Otherwise, IsCheckpointImage returns false with appropriate error.
func IsCheckpointImage(ctx context.Context, namesOrIDs []string) (bool, error) {
	inspectOpts := entities.InspectOptions{}
	imgData, _, err := registry.ImageEngine().Inspect(ctx, namesOrIDs, inspectOpts)
	if err != nil {
		return false, err
	}
	if len(imgData) == 0 {
		return false, nil
	}
	imgID := imgData[0].ID

	hostInfo, err := registry.ContainerEngine().Info(ctx)
	if err != nil {
		return false, err
	}

	for i := range imgData {
		checkpointRuntimeName, found := imgData[i].Annotations[define.CheckpointAnnotationRuntimeName]
		if !found {
			return false, fmt.Errorf("image is not a checkpoint: %s", imgID)
		}
		if hostInfo.Host.OCIRuntime.Name != checkpointRuntimeName {
			return false, fmt.Errorf("container image \"%s\" requires runtime: \"%s\"", imgID, checkpointRuntimeName)
		}
	}
	return true, nil
}