From cb7c382ee2c41336813064ae33afb05fd17f409b Mon Sep 17 00:00:00 2001 From: Abraham Kuri Date: Thu, 2 May 2019 16:07:16 -0500 Subject: [PATCH 1/2] Adds incinerate function to remove everything from Docker, volumes and images --- cmd/incinerate.go | 99 +++++++++++++++++++++++++++++++++++++++++++++++ plis.go | 5 +++ 2 files changed, 104 insertions(+) create mode 100644 cmd/incinerate.go diff --git a/cmd/incinerate.go b/cmd/incinerate.go new file mode 100644 index 0000000..1899619 --- /dev/null +++ b/cmd/incinerate.go @@ -0,0 +1,99 @@ +package cmd + +import ( + "github.com/IcaliaLabs/plis/translation" + "github.com/urfave/cli" + "strings" + "fmt" +) + +func IdsGetter(cmdOut string) []string { + var ( + rawIds []string + ids []string + ) + + rawIds = strings.Split(cmdOut, "\n") + + ids = rawIds[:0] + for _, x := range rawIds { + if x != "" { + ids = append(ids, x) + } + } + + return ids +} + +func DockerSystemNetworksIds() []string { + cmdName := "docker" + cmdArgs := []string{"network", "ls", "-q"} + cmdOut := translation.ShellOut(cmdName, cmdArgs) + ids := IdsGetter(cmdOut) + + return ids +} + +func DockerSystemVolumesIds() []string { + cmdName := "docker" + cmdArgs := []string{"volume", "ls", "-q"} + cmdOut := translation.ShellOut(cmdName, cmdArgs) + ids := IdsGetter(cmdOut) + + return ids +} + +func ImagesIds() []string { + cmdName := "docker" + cmdArgs := []string{"image", "ls", "-aq"} + cmdOut := translation.ShellOut(cmdName, cmdArgs) + ids := IdsGetter(cmdOut) + + return ids +} + +func DockerRunningContainersIds() []string { + cmdName := "docker" + cmdArgs := []string{"ps", "-aq"} + cmdOut := translation.ShellOut(cmdName, cmdArgs) + ids := IdsGetter(cmdOut) + + return ids +} + +func IncinerateImages() { + fmt.Printf("Incinerating the images...\n") + imagesIds := ImagesIds() + cmdArgs := append([]string{"rmi", "-f"}, imagesIds...) + translation.ShellOut("docker", cmdArgs) +} + +func IncinerateVolumes() { + fmt.Printf("Incinerating the volumes...\n") + volumesIds := DockerSystemVolumesIds() + cmdArgs := append([]string{"volume", "rm", "-f"}, volumesIds...) + translation.ShellOut("docker", cmdArgs) +} + +func StopRunningContainers() { + fmt.Printf("Stopping the running containers...\n") + dockerContainerIds := DockerRunningContainersIds() + cmdArgs := append([]string{"stop"}, dockerContainerIds...) + translation.ShellOut("docker", cmdArgs) +} + +func RemoveRunningContainers() { + fmt.Printf("Removing the running containers...\n") + dockerContainerIds := DockerRunningContainersIds() + cmdArgs := append([]string{"rm"}, dockerContainerIds...) + translation.ShellOut("docker", cmdArgs) +} + +func Incinerate(c *cli.Context) { + StopRunningContainers() + RemoveRunningContainers() + IncinerateImages() + IncinerateVolumes() + + fmt.Printf("All done! Your OS has been incinerated\n") +} diff --git a/plis.go b/plis.go index 4836443..f34b657 100644 --- a/plis.go +++ b/plis.go @@ -14,6 +14,11 @@ func main() { app.Version = "0.0.0.build11" app.Commands = []cli.Command{ + { + Name: "incinerate", + Usage: "Cleans up all your docker images, networks, volumes", + Action: cmd.Incinerate, + }, { Name: "start", Usage: "Starts the project's containers", From abe989657dfa94840d3f36cb8f5c3725a8c51187 Mon Sep 17 00:00:00 2001 From: Abraham Kuri Date: Sun, 5 May 2019 20:08:44 -0500 Subject: [PATCH 2/2] Uses docker prune native support to remove images and volumes from the system --- cmd/incinerate.go | 89 ++--------------------------------------------- 1 file changed, 2 insertions(+), 87 deletions(-) diff --git a/cmd/incinerate.go b/cmd/incinerate.go index 1899619..c73001c 100644 --- a/cmd/incinerate.go +++ b/cmd/incinerate.go @@ -3,97 +3,12 @@ package cmd import ( "github.com/IcaliaLabs/plis/translation" "github.com/urfave/cli" - "strings" "fmt" ) -func IdsGetter(cmdOut string) []string { - var ( - rawIds []string - ids []string - ) - - rawIds = strings.Split(cmdOut, "\n") - - ids = rawIds[:0] - for _, x := range rawIds { - if x != "" { - ids = append(ids, x) - } - } - - return ids -} - -func DockerSystemNetworksIds() []string { - cmdName := "docker" - cmdArgs := []string{"network", "ls", "-q"} - cmdOut := translation.ShellOut(cmdName, cmdArgs) - ids := IdsGetter(cmdOut) - - return ids -} - -func DockerSystemVolumesIds() []string { - cmdName := "docker" - cmdArgs := []string{"volume", "ls", "-q"} - cmdOut := translation.ShellOut(cmdName, cmdArgs) - ids := IdsGetter(cmdOut) - - return ids -} - -func ImagesIds() []string { - cmdName := "docker" - cmdArgs := []string{"image", "ls", "-aq"} - cmdOut := translation.ShellOut(cmdName, cmdArgs) - ids := IdsGetter(cmdOut) - - return ids -} - -func DockerRunningContainersIds() []string { - cmdName := "docker" - cmdArgs := []string{"ps", "-aq"} - cmdOut := translation.ShellOut(cmdName, cmdArgs) - ids := IdsGetter(cmdOut) - - return ids -} - -func IncinerateImages() { - fmt.Printf("Incinerating the images...\n") - imagesIds := ImagesIds() - cmdArgs := append([]string{"rmi", "-f"}, imagesIds...) - translation.ShellOut("docker", cmdArgs) -} - -func IncinerateVolumes() { - fmt.Printf("Incinerating the volumes...\n") - volumesIds := DockerSystemVolumesIds() - cmdArgs := append([]string{"volume", "rm", "-f"}, volumesIds...) - translation.ShellOut("docker", cmdArgs) -} - -func StopRunningContainers() { - fmt.Printf("Stopping the running containers...\n") - dockerContainerIds := DockerRunningContainersIds() - cmdArgs := append([]string{"stop"}, dockerContainerIds...) - translation.ShellOut("docker", cmdArgs) -} - -func RemoveRunningContainers() { - fmt.Printf("Removing the running containers...\n") - dockerContainerIds := DockerRunningContainersIds() - cmdArgs := append([]string{"rm"}, dockerContainerIds...) - translation.ShellOut("docker", cmdArgs) -} - func Incinerate(c *cli.Context) { - StopRunningContainers() - RemoveRunningContainers() - IncinerateImages() - IncinerateVolumes() + cmdArgs := []string{"docker", "system", "prune", "-a", "-f", "--volumes"} + translation.Exec(cmdArgs) fmt.Printf("All done! Your OS has been incinerated\n") }