Adds tunnel routes for system reset.

Adds tunnel routes for system reset.
Makes forces flag local as options are not propogated down the stack.
Adds relevant test cases and swagger docs.

Signed-off-by: Sujil02 <sushah@redhat.com>
This commit is contained in:
Sujil02
2020-05-05 22:29:54 -04:00
parent dc7d6f4818
commit 1c6ae73a89
9 changed files with 95 additions and 9 deletions

View File

@ -26,10 +26,8 @@ var (
Long: systemResetDescription,
Run: reset,
}
)
var (
systemResetOptions entities.SystemResetOptions
forceFlag bool
)
func init() {
@ -39,12 +37,12 @@ func init() {
Parent: systemCmd,
})
flags := systemResetCommand.Flags()
flags.BoolVarP(&systemResetOptions.Force, "force", "f", false, "Do not prompt for confirmation")
flags.BoolVarP(&forceFlag, "force", "f", false, "Do not prompt for confirmation")
}
func reset(cmd *cobra.Command, args []string) {
// Prompt for confirmation if --force is not set
if !systemResetOptions.Force {
if !forceFlag {
reader := bufio.NewReader(os.Stdin)
fmt.Print(`
WARNING! This will remove:
@ -74,7 +72,7 @@ Are you sure you want to continue? [y/N] `)
}
defer engine.Shutdown(registry.Context())
if err := engine.Reset(registry.Context(), systemResetOptions); err != nil {
if err := engine.Reset(registry.Context()); err != nil {
fmt.Println(err)
os.Exit(125)
}

View File

@ -69,3 +69,13 @@ func SystemPrune(w http.ResponseWriter, r *http.Request) {
}
utils.WriteResponse(w, http.StatusOK, systemPruneReport)
}
// SystemReset Resets podman storage back to default state
func SystemReset(w http.ResponseWriter, r *http.Request) {
err := r.Context().Value("runtime").(*libpod.Runtime).Reset(r.Context())
if err != nil {
utils.InternalServerError(w, err)
return
}
utils.WriteResponse(w, http.StatusOK, nil)
}

View File

@ -27,6 +27,19 @@ func (s *APIServer) registerSystemHandlers(r *mux.Router) error {
// 500:
// $ref: "#/responses/InternalError"
r.Handle(VersionedPath("/libpod/system/prune"), s.APIHandler(libpod.SystemPrune)).Methods(http.MethodPost)
// swagger:operation POST /libpod/system/reset libpod resetSystem
// ---
// tags:
// - system
// summary: Reset podman storage
// description: All containers will be stopped and removed, and all images, volumes and container content will be removed.
// produces:
// - application/json
// responses:
// 200:
// description: no error
// 500:
// $ref: "#/responses/InternalError"
r.Handle(VersionedPath("/libpod/system/reset"), s.APIHandler(libpod.SystemReset)).Methods(http.MethodPost)
return nil
}

View File

@ -121,3 +121,16 @@ func Version(ctx context.Context) (*entities.SystemVersionReport, error) {
}
return &report, err
}
// Reset removes all unused system data.
func Reset(ctx context.Context) error {
conn, err := bindings.GetClient(ctx)
if err != nil {
return err
}
response, err := conn.DoRequest(nil, http.MethodPost, "/system/reset", nil)
if err != nil {
return err
}
return response.Process(response)
}

View File

@ -5,6 +5,7 @@ import (
"github.com/containers/libpod/pkg/bindings"
"github.com/containers/libpod/pkg/bindings/containers"
"github.com/containers/libpod/pkg/bindings/images"
"github.com/containers/libpod/pkg/bindings/pods"
"github.com/containers/libpod/pkg/bindings/system"
"github.com/containers/libpod/pkg/bindings/volumes"
@ -149,4 +150,45 @@ var _ = Describe("Podman system", func() {
// Volume should be pruned now as flag set true
Expect(len(systemPruneResponse.VolumePruneReport)).To(Equal(1))
})
It("podman system reset", func() {
// Adding an unused volume should work
_, err := volumes.Create(bt.conn, entities.VolumeCreateOptions{})
Expect(err).To(BeNil())
vols, err := volumes.List(bt.conn, nil)
Expect(err).To(BeNil())
Expect(len(vols)).To(Equal(1))
// Start a pod and leave it running
_, err = pods.Start(bt.conn, newpod)
Expect(err).To(BeNil())
imageSummary, err := images.List(bt.conn, nil, nil)
Expect(err).To(BeNil())
// Since in the begin context images are created
Expect(len(imageSummary)).To(Equal(3))
err = system.Reset(bt.conn)
Expect(err).To(BeNil())
// re-establish connection
s = bt.startAPIService()
time.Sleep(1 * time.Second)
// No pods
podSummary, err := pods.List(bt.conn, nil)
Expect(err).To(BeNil())
Expect(len(podSummary)).To(Equal(0))
// No images
imageSummary, err = images.List(bt.conn, &bindings.PTrue, nil)
Expect(err).To(BeNil())
Expect(len(imageSummary)).To(Equal(0))
// no volumes
vols, err = volumes.List(bt.conn, nil)
Expect(err).To(BeNil())
Expect(len(vols)).To(BeZero())
})
})

View File

@ -9,6 +9,6 @@ import (
type SystemEngine interface {
Renumber(ctx context.Context, flags *pflag.FlagSet, config *PodmanConfig) error
Migrate(ctx context.Context, flags *pflag.FlagSet, config *PodmanConfig, options SystemMigrateOptions) error
Reset(ctx context.Context, options SystemResetOptions) error
Reset(ctx context.Context) error
Shutdown(ctx context.Context)
}

View File

@ -375,7 +375,7 @@ func sizeOfPath(path string) (int64, error) {
return size, err
}
func (se *SystemEngine) Reset(ctx context.Context, options entities.SystemResetOptions) error {
func (se *SystemEngine) Reset(ctx context.Context) error {
return se.Libpod.Reset(ctx)
}

View File

@ -13,3 +13,8 @@ type ImageEngine struct {
type ContainerEngine struct {
ClientCxt context.Context
}
// Container-related runtime using an ssh-tunnel to utilize Podman service
type SystemEngine struct {
ClientCxt context.Context
}

View File

@ -27,6 +27,11 @@ func (ic *ContainerEngine) SystemPrune(ctx context.Context, options entities.Sys
return system.Prune(ic.ClientCxt, &options.All, &options.Volume)
}
// Reset removes all storage
func (ic *SystemEngine) Reset(ctx context.Context) error {
return system.Reset(ic.ClientCxt)
}
func (ic *ContainerEngine) SystemDf(ctx context.Context, options entities.SystemDfOptions) (*entities.SystemDfReport, error) {
panic(errors.New("system df is not supported on remote clients"))
}