From b6e12eff2a03afa4e4ca88c33baa027a3cdd812a Mon Sep 17 00:00:00 2001 From: jason Date: Tue, 19 May 2020 17:30:33 +0900 Subject: [PATCH] patch expose feature and permernant delete feature --- VERSION | 2 +- container/container.go | 36 ++++++++++++++++++++++++++++-------- main.go | 11 ++++++++--- 3 files changed, 37 insertions(+), 12 deletions(-) diff --git a/VERSION b/VERSION index fc4ab86..6bbce91 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -2020050315 +2020051916 diff --git a/container/container.go b/container/container.go index 2a5d1f7..f6133fb 100644 --- a/container/container.go +++ b/container/container.go @@ -2086,7 +2086,7 @@ func DockerCreate(name string, container_name string, volume_map string) *Error return err } -func DockerDelete(name string) *Error { +func DockerDelete(name string, permernant bool) *Error { currdir, err := GetCurrDir() if err != nil { return err @@ -2100,7 +2100,6 @@ func DockerDelete(name string) *Error { for key, value := range sys.Containers { if vval, vok := value.(map[string]interface{}); vok { config_path := vval["ConfigPath"].(string) - var con Container err = unmarshalObj(config_path, &con) if err != nil { @@ -2126,6 +2125,27 @@ func DockerDelete(name string) *Error { if err == nil { if val, ok := doc.Images[name]; ok { if vval, vok := val.(map[string]interface{}); vok { + if permernant { + //we need to delete image files and folder info + image_dir := vval["image"].(string) + base_dir := vval["base"].(string) + layer_order := vval["layer_order"].(string) + for _, layer := range strings.Split(layer_order, ":") { + layer_name := filepath.Base(layer) + LOGGER.WithFields(logrus.Fields{ + "folder to delete": fmt.Sprintf("%s/%s", base_dir, layer_name), + "file to delete": fmt.Sprintf("%s/%s", image_dir, layer_name), + }).Debug("Docker delete info") + _, rerr := RemoveAll(fmt.Sprintf("%s/%s", image_dir, layer_name)) + if rerr != nil { + return rerr + } + _, rerr = RemoveAll(fmt.Sprintf("%s/%s", base_dir, layer_name)) + if rerr != nil { + return rerr + } + } + } dir, _ := vval["rootdir"].(string) rok, rerr := RemoveAll(dir) if rok { @@ -2147,7 +2167,7 @@ func DockerDelete(name string) *Error { return err } -func Expose(id string, name string) *Error { +func Expose(id string, path string, name string) *Error { currdir, err := GetCurrDir() if err != nil { return err @@ -2168,11 +2188,11 @@ func Expose(id string, name string) *Error { if err != nil { return err } - if !strings.Contains(con.ExposeExe, name) { + if !strings.Contains(con.ExposeExe, path) { if con.ExposeExe == "" { - con.ExposeExe = name + con.ExposeExe = path } else { - con.ExposeExe = fmt.Sprintf("%s:%s", con.ExposeExe, name) + con.ExposeExe = fmt.Sprintf("%s:%s", con.ExposeExe, path) } } @@ -2184,7 +2204,7 @@ func Expose(id string, name string) *Error { } } - bname := filepath.Base(name) + bname := name bdir := fmt.Sprintf("%s/%s", bindir, bname) if FileExist(bdir) { RemoveFile(bdir) @@ -2195,7 +2215,7 @@ func Expose(id string, name string) *Error { return cerr } code := "#!/bin/bash\n" + os.Args[0] + - " resume " + id + " \"" + name + " " + "$@\"" + + " resume " + id + " \"" + path + " " + "$@\"" + "\n" fmt.Fprintf(f, code) diff --git a/main.go b/main.go index 326d75a..d113327 100644 --- a/main.go +++ b/main.go @@ -19,7 +19,7 @@ var ( ) const ( - VERSION = "alpha-1.4" + VERSION = "alpha-1.5" ) func checkCompleteness() *Error { @@ -469,6 +469,7 @@ func main() { } dockerRunCmd.Flags().StringVarP(&DockerRunVolume, "volume", "v", "", "optional") + var DockerDeletePermernant bool var dockerDeleteCmd = &cobra.Command{ Use: "delete", Short: "delete the local docker images", @@ -482,7 +483,7 @@ func main() { } }, Run: func(cmd *cobra.Command, args []string) { - err := DockerDelete(args[0]) + err := DockerDelete(args[0], DockerDeletePermernant) if err != nil { LOGGER.Fatal(err.Error()) return @@ -492,6 +493,7 @@ func main() { } }, } + dockerDeleteCmd.Flags().BoolVarP(&DockerDeletePermernant, "permernant", "p", false, "permernantly delete all layers of the target image(optional)") var dockerSearchCmd = &cobra.Command{ Use: "search", @@ -629,6 +631,7 @@ func main() { dockerCmd.AddCommand(dockerCreateCmd, dockerSearchCmd, dockerListCmd, dockerDeleteCmd, dockerDownloadCmd, dockerResetCmd, dockerPackageCmd, dockerAddCmd, dockerCommitCmd, dockerLoadCmd, dockerRunCmd) var ExposeId string + var ExposePath string var ExposeName string var exposeCmd = &cobra.Command{ Use: "expose", @@ -643,7 +646,7 @@ func main() { } }, Run: func(cmd *cobra.Command, args []string) { - err := Expose(ExposeId, ExposeName) + err := Expose(ExposeId, ExposePath, ExposeName) if err != nil { LOGGER.Fatal(err.Error()) return @@ -655,6 +658,8 @@ func main() { } exposeCmd.Flags().StringVarP(&ExposeId, "id", "i", "", "required") exposeCmd.MarkFlagRequired("id") + exposeCmd.Flags().StringVarP(&ExposePath, "path", "p", "", "required") + exposeCmd.MarkFlagRequired("path") exposeCmd.Flags().StringVarP(&ExposeName, "name", "n", "", "required") exposeCmd.MarkFlagRequired("name")