-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4558 from rhatdan/reset
Add podman system reset command
- Loading branch information
Showing
16 changed files
with
466 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
"github.com/containers/libpod/cmd/podman/cliconfig" | ||
"github.com/containers/libpod/pkg/adapter" | ||
"github.com/pkg/errors" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var ( | ||
systemResetCommand cliconfig.SystemResetValues | ||
systemResetDescription = `Reset podman storage back to default state" | ||
All containers will be stopped and removed, and all images, volumes and container content will be removed. | ||
` | ||
_systemResetCommand = &cobra.Command{ | ||
Use: "reset", | ||
Args: noSubArgs, | ||
Short: "Reset podman storage", | ||
Long: systemResetDescription, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
systemResetCommand.InputArgs = args | ||
systemResetCommand.GlobalFlags = MainGlobalOpts | ||
systemResetCommand.Remote = remoteclient | ||
return systemResetCmd(&systemResetCommand) | ||
}, | ||
} | ||
) | ||
|
||
func init() { | ||
systemResetCommand.Command = _systemResetCommand | ||
flags := systemResetCommand.Flags() | ||
flags.BoolVarP(&systemResetCommand.Force, "force", "f", false, "Do not prompt for confirmation") | ||
|
||
systemResetCommand.SetHelpTemplate(HelpTemplate()) | ||
systemResetCommand.SetUsageTemplate(UsageTemplate()) | ||
} | ||
|
||
func systemResetCmd(c *cliconfig.SystemResetValues) error { | ||
// Prompt for confirmation if --force is not set | ||
if !c.Force { | ||
reader := bufio.NewReader(os.Stdin) | ||
fmt.Print(` | ||
WARNING! This will remove: | ||
- all containers | ||
- all pods | ||
- all images | ||
- all build cache | ||
Are you sure you want to continue? [y/N] `) | ||
ans, err := reader.ReadString('\n') | ||
if err != nil { | ||
return errors.Wrapf(err, "error reading input") | ||
} | ||
if strings.ToLower(ans)[0] != 'y' { | ||
return nil | ||
} | ||
} | ||
|
||
runtime, err := adapter.GetRuntime(getContext(), &c.PodmanCommand) | ||
if err != nil { | ||
return errors.Wrapf(err, "error creating libpod runtime") | ||
} | ||
// No shutdown, since storage will be destroyed when command completes | ||
|
||
return runtime.Reset() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
% podman-system-reset(1) | ||
|
||
## NAME | ||
podman\-system\-reset - Reset storage back to initial state | ||
|
||
## SYNOPSIS | ||
**podman system reset** | ||
|
||
## DESCRIPTION | ||
**podman system reset** removes all pods, containers, images and volumes. | ||
|
||
## OPTIONS | ||
**--force**, **-f** | ||
|
||
Do not prompt for confirmation | ||
|
||
**--help**, **-h** | ||
|
||
Print usage statement | ||
|
||
## SEE ALSO | ||
`podman(1)`, `podman-system(1)` | ||
|
||
## HISTORY | ||
November 2019, Originally compiled by Dan Walsh (dwalsh at redhat dot com) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package libpod | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/containers/libpod/libpod/define" | ||
"github.com/containers/libpod/pkg/rootless" | ||
"github.com/pkg/errors" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
// Reset removes all storage | ||
func (r *Runtime) Reset(ctx context.Context) error { | ||
|
||
pods, err := r.GetAllPods() | ||
if err != nil { | ||
return err | ||
} | ||
for _, p := range pods { | ||
if err := r.RemovePod(ctx, p, true, true); err != nil { | ||
if errors.Cause(err) == define.ErrNoSuchPod { | ||
continue | ||
} | ||
logrus.Errorf("Error removing Pod %s: %v", p.ID(), err) | ||
} | ||
} | ||
|
||
ctrs, err := r.GetAllContainers() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for _, c := range ctrs { | ||
if err := r.RemoveContainer(ctx, c, true, true); err != nil { | ||
if err := r.RemoveStorageContainer(c.ID(), true); err != nil { | ||
if errors.Cause(err) == define.ErrNoSuchCtr { | ||
continue | ||
} | ||
logrus.Errorf("Error removing container %s: %v", c.ID(), err) | ||
} | ||
} | ||
} | ||
|
||
if err := stopPauseProcess(); err != nil { | ||
logrus.Errorf("Error stopping pause process: %v", err) | ||
} | ||
|
||
ir := r.ImageRuntime() | ||
images, err := ir.GetImages() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for _, i := range images { | ||
if err := i.Remove(ctx, true); err != nil { | ||
if errors.Cause(err) == define.ErrNoSuchImage { | ||
continue | ||
} | ||
logrus.Errorf("Error removing image %s: %v", i.ID(), err) | ||
} | ||
} | ||
volumes, err := r.state.AllVolumes() | ||
if err != nil { | ||
return err | ||
} | ||
for _, v := range volumes { | ||
if err := r.RemoveVolume(ctx, v, true); err != nil { | ||
if errors.Cause(err) == define.ErrNoSuchVolume { | ||
continue | ||
} | ||
logrus.Errorf("Error removing volume %s: %v", v.config.Name, err) | ||
} | ||
} | ||
|
||
_, prevError := r.store.Shutdown(true) | ||
if err := os.RemoveAll(r.store.GraphRoot()); err != nil { | ||
if prevError != nil { | ||
logrus.Error(prevError) | ||
} | ||
prevError = err | ||
} | ||
if err := os.RemoveAll(r.store.RunRoot()); err != nil { | ||
if prevError != nil { | ||
logrus.Error(prevError) | ||
} | ||
prevError = err | ||
} | ||
if err := os.RemoveAll(r.config.TmpDir); err != nil { | ||
if prevError != nil { | ||
logrus.Error(prevError) | ||
} | ||
prevError = err | ||
} | ||
if rootless.IsRootless() { | ||
configPath := filepath.Join(os.Getenv("HOME"), ".config/containers") | ||
if err := os.RemoveAll(configPath); err != nil { | ||
if prevError != nil { | ||
logrus.Error(prevError) | ||
} | ||
prevError = err | ||
} | ||
} | ||
|
||
return prevError | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// +build !remoteclient | ||
|
||
package adapter | ||
|
||
import ( | ||
"context" | ||
) | ||
|
||
// Reset the container storage back to initial states. | ||
// Removes all Pods, Containers, Images and Volumes. | ||
func (r *LocalRuntime) Reset() error { | ||
return r.Runtime.Reset(context.TODO()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// +build remoteclient | ||
|
||
package adapter | ||
|
||
import ( | ||
"github.com/containers/libpod/cmd/podman/varlink" | ||
) | ||
|
||
// Info returns information for the host system and its components | ||
func (r RemoteRuntime) Reset() error { | ||
return iopodman.Reset().Call(r.Conn) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.