mirror of
https://github.com/containers/podman.git
synced 2025-07-15 03:02:52 +08:00
Merge pull request #10789 from flouthoc/system-reset-prune-external
reset: remove external containers on podman system reset
This commit is contained in:
@ -45,16 +45,29 @@ func init() {
|
||||
}
|
||||
|
||||
func reset(cmd *cobra.Command, args []string) {
|
||||
// Get all the external containers in use
|
||||
listCtn, _ := registry.ContainerEngine().ContainerListExternal(registry.Context())
|
||||
listCtnIds := make([]string, 0, len(listCtn))
|
||||
for _, externalCtn := range listCtn {
|
||||
listCtnIds = append(listCtnIds, externalCtn.ID)
|
||||
}
|
||||
// Prompt for confirmation if --force is not set
|
||||
if !forceFlag {
|
||||
reader := bufio.NewReader(os.Stdin)
|
||||
fmt.Print(`
|
||||
fmt.Println(`
|
||||
WARNING! This will remove:
|
||||
- all containers
|
||||
- all pods
|
||||
- all images
|
||||
- all build cache
|
||||
Are you sure you want to continue? [y/N] `)
|
||||
- all build cache`)
|
||||
if len(listCtn) > 0 {
|
||||
fmt.Println(`WARNING! The following external containers will be purged:`)
|
||||
// print first 12 characters of ID and first configured name alias
|
||||
for _, externalCtn := range listCtn {
|
||||
fmt.Printf(" - %s (%s)\n", externalCtn.ID[0:12], externalCtn.Names[0])
|
||||
}
|
||||
}
|
||||
fmt.Print(`Are you sure you want to continue? [y/N] `)
|
||||
answer, err := reader.ReadString('\n')
|
||||
if err != nil {
|
||||
logrus.Error(err)
|
||||
@ -65,6 +78,8 @@ Are you sure you want to continue? [y/N] `)
|
||||
}
|
||||
}
|
||||
|
||||
// Purge all the external containers with storage
|
||||
registry.ContainerEngine().ContainerRm(registry.Context(), listCtnIds, entities.RmOptions{Force: true, All: true, Ignore: true, Volumes: true})
|
||||
// Shutdown all running engines, `reset` will hijack repository
|
||||
registry.ContainerEngine().Shutdown(registry.Context())
|
||||
registry.ImageEngine().Shutdown(registry.Context())
|
||||
|
@ -32,6 +32,7 @@ type ContainerEngine interface {
|
||||
ContainerInspect(ctx context.Context, namesOrIds []string, options InspectOptions) ([]*ContainerInspectReport, []error, error)
|
||||
ContainerKill(ctx context.Context, namesOrIds []string, options KillOptions) ([]*KillReport, error)
|
||||
ContainerList(ctx context.Context, options ContainerListOptions) ([]ListContainer, error)
|
||||
ContainerListExternal(ctx context.Context) ([]ListContainer, error)
|
||||
ContainerLogs(ctx context.Context, containers []string, options ContainerLogsOptions) error
|
||||
ContainerMount(ctx context.Context, nameOrIDs []string, options ContainerMountOptions) ([]*ContainerMountReport, error)
|
||||
ContainerPause(ctx context.Context, namesOrIds []string, options PauseUnPauseOptions) ([]*PauseUnpauseReport, error)
|
||||
|
@ -854,6 +854,10 @@ func (ic *ContainerEngine) ContainerList(ctx context.Context, options entities.C
|
||||
return ps.GetContainerLists(ic.Libpod, options)
|
||||
}
|
||||
|
||||
func (ic *ContainerEngine) ContainerListExternal(ctx context.Context) ([]entities.ListContainer, error) {
|
||||
return ps.GetExternalContainerLists(ic.Libpod)
|
||||
}
|
||||
|
||||
// ContainerDiff provides changes to given container
|
||||
func (ic *ContainerEngine) ContainerDiff(ctx context.Context, nameOrID string, opts entities.DiffOptions) (*entities.DiffReport, error) {
|
||||
if opts.Latest {
|
||||
|
@ -646,6 +646,12 @@ func (ic *ContainerEngine) ContainerList(ctx context.Context, opts entities.Cont
|
||||
return containers.List(ic.ClientCtx, options)
|
||||
}
|
||||
|
||||
func (ic *ContainerEngine) ContainerListExternal(ctx context.Context) ([]entities.ListContainer, error) {
|
||||
options := new(containers.ListOptions).WithAll(true)
|
||||
options.WithNamespace(true).WithSize(true).WithSync(true).WithExternal(true)
|
||||
return containers.List(ic.ClientCtx, options)
|
||||
}
|
||||
|
||||
func (ic *ContainerEngine) ContainerRun(ctx context.Context, opts entities.ContainerRunOptions) (*entities.ContainerRunReport, error) {
|
||||
con, err := containers.CreateWithSpec(ic.ClientCtx, opts.Spec, nil)
|
||||
if err != nil {
|
||||
|
34
pkg/ps/ps.go
34
pkg/ps/ps.go
@ -71,18 +71,11 @@ func GetContainerLists(runtime *libpod.Runtime, options entities.ContainerListOp
|
||||
}
|
||||
|
||||
if options.All && options.External {
|
||||
externCons, err := runtime.StorageContainers()
|
||||
listCon, err := GetExternalContainerLists(runtime)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, con := range externCons {
|
||||
listCon, err := ListStorageContainer(runtime, con, options)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
pss = append(pss, listCon)
|
||||
}
|
||||
pss = append(pss, listCon...)
|
||||
}
|
||||
|
||||
// Sort the containers we got
|
||||
@ -97,6 +90,27 @@ func GetContainerLists(runtime *libpod.Runtime, options entities.ContainerListOp
|
||||
return pss, nil
|
||||
}
|
||||
|
||||
// GetExternalContainerLists returns list of external containers for e.g created by buildah
|
||||
func GetExternalContainerLists(runtime *libpod.Runtime) ([]entities.ListContainer, error) {
|
||||
var (
|
||||
pss = []entities.ListContainer{}
|
||||
)
|
||||
|
||||
externCons, err := runtime.StorageContainers()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, con := range externCons {
|
||||
listCon, err := ListStorageContainer(runtime, con)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
pss = append(pss, listCon)
|
||||
}
|
||||
return pss, nil
|
||||
}
|
||||
|
||||
// BatchContainerOp is used in ps to reduce performance hits by "batching"
|
||||
// locks.
|
||||
func ListContainerBatch(rt *libpod.Runtime, ctr *libpod.Container, opts entities.ContainerListOptions) (entities.ListContainer, error) {
|
||||
@ -231,7 +245,7 @@ func ListContainerBatch(rt *libpod.Runtime, ctr *libpod.Container, opts entities
|
||||
return ps, nil
|
||||
}
|
||||
|
||||
func ListStorageContainer(rt *libpod.Runtime, ctr storage.Container, opts entities.ContainerListOptions) (entities.ListContainer, error) {
|
||||
func ListStorageContainer(rt *libpod.Runtime, ctr storage.Container) (entities.ListContainer, error) {
|
||||
name := "unknown"
|
||||
if len(ctr.Names) > 0 {
|
||||
name = ctr.Names[0]
|
||||
|
Reference in New Issue
Block a user