From bd132ed7c61121f33235e6c1a3ad868245519f3f Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Sat, 10 Oct 2020 17:25:25 -0400 Subject: [PATCH] feat: implement helper functions for dockeroptions --- plugins/docker-options/dockeroptions.go | 102 ++++++++++++++++++++++++ plugins/docker-options/go.mod | 12 +++ plugins/docker-options/go.sum | 8 ++ 3 files changed, 122 insertions(+) create mode 100644 plugins/docker-options/dockeroptions.go create mode 100644 plugins/docker-options/go.mod create mode 100644 plugins/docker-options/go.sum diff --git a/plugins/docker-options/dockeroptions.go b/plugins/docker-options/dockeroptions.go new file mode 100644 index 000000000..63fce5284 --- /dev/null +++ b/plugins/docker-options/dockeroptions.go @@ -0,0 +1,102 @@ +package dockeroptions + +import ( + "bufio" + "fmt" + "os" + "path/filepath" + "sort" + "strings" + + "github.com/dokku/dokku/plugins/common" +) + +// AddDockerOptionToPhases adds docker option to specified phases +func AddDockerOptionToPhases(appName string, phases []string, option string) error { + for _, phase := range phases { + if err := touchPhaseFile(appName, phase); err != nil { + return err + } + + options, err := GetDockerOptionsForPhase(appName, phase) + if err != nil { + return err + } + + options = append(options, option) + sort.Strings(options) + if err = writeDockerOptionsForPhase(appName, phase, options); err != nil { + return err + } + } + return nil +} + +// GetDockerOptionsForPhase returns the docker options for the specified phase +func GetDockerOptionsForPhase(appName string, phase string) ([]string, error) { + options := []string{} + + if err := touchPhaseFile(appName, phase); err != nil { + return options, err + } + + phaseFilePath := getPhaseFilePath(appName, phase) + file, err := os.Open(phaseFilePath) + if err != nil { + return options, fmt.Errorf("Unable to open docker options phase file %s.%s: %s", appName, appName, err.Error()) + } + defer file.Close() + + scanner := bufio.NewScanner(file) + for scanner.Scan() { + options = append(options, strings.TrimSpace(scanner.Text())) + } + + if err = scanner.Err(); err != nil { + return options, fmt.Errorf("Unable to read docker options phase file %s.%s: %s", appName, appName, err.Error()) + } + + return options, nil +} + +func getPhaseFilePath(appName string, phase string) string { + return filepath.Join(common.MustGetEnv("DOKKU_ROOT"), appName, "DOCKER_OPTIONS_"+strings.ToUpper(phase)) +} + +func touchPhaseFile(appName string, phase string) error { + phaseFilePath := getPhaseFilePath(appName, phase) + + _, err := os.Stat(phaseFilePath) + if !os.IsNotExist(err) { + return nil + } + + file, err := os.Create(phaseFilePath) + if err != nil { + return fmt.Errorf("Unable to create docker options phase file %s.%s: %s", appName, phase, err.Error()) + } + defer file.Close() + + return nil +} + +func writeDockerOptionsForPhase(appName string, phase string, options []string) error { + phaseFilePath := getPhaseFilePath(appName, phase) + file, err := os.OpenFile(phaseFilePath, os.O_RDWR|os.O_TRUNC, 0600) + if err != nil { + return err + } + + w := bufio.NewWriter(file) + for _, option := range options { + fmt.Fprintln(w, option) + } + if err = w.Flush(); err != nil { + return fmt.Errorf("Unable to update docker options phase file %s.%s: %s", appName, phase, err.Error()) + } + + file.Chmod(0600) + common.SetPermissions(phaseFilePath, 0600) + + return nil +} diff --git a/plugins/docker-options/go.mod b/plugins/docker-options/go.mod new file mode 100644 index 000000000..7d47c0fa0 --- /dev/null +++ b/plugins/docker-options/go.mod @@ -0,0 +1,12 @@ +module github.com/dokku/dokku/plugins/docker-options + +go 1.14 + +require ( + github.com/codeskyblue/go-sh v0.0.0-20190412065543-76bd3d59ff27 // indirect + github.com/dokku/dokku/plugins/common v0.0.0-00010101000000-000000000000 + github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 + github.com/ryanuber/columnize v1.1.2-0.20190319233515-9e6335e58db3 // indirect +) + +replace github.com/dokku/dokku/plugins/common => ../common diff --git a/plugins/docker-options/go.sum b/plugins/docker-options/go.sum new file mode 100644 index 000000000..ee7942d28 --- /dev/null +++ b/plugins/docker-options/go.sum @@ -0,0 +1,8 @@ +github.com/codegangsta/inject v0.0.0-20150114235600-33e0aa1cb7c0 h1:sDMmm+q/3+BukdIpxwO365v/Rbspp2Nt5XntgQRXq8Q= +github.com/codegangsta/inject v0.0.0-20150114235600-33e0aa1cb7c0/go.mod h1:4Zcjuz89kmFXt9morQgcfYZAYZ5n8WHjt81YYWIwtTM= +github.com/codeskyblue/go-sh v0.0.0-20190412065543-76bd3d59ff27 h1:HHUr4P/aKh4quafGxDT9LDasjGdlGkzLbfmmrlng3kA= +github.com/codeskyblue/go-sh v0.0.0-20190412065543-76bd3d59ff27/go.mod h1:VQx0hjo2oUeQkQUET7wRwradO6f+fN5jzXgB/zROxxE= +github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs= +github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8= +github.com/ryanuber/columnize v1.1.2-0.20190319233515-9e6335e58db3 h1:utdYOikI1XjNtTFGCwSM6OmFJblU4ld4gACoJsbadJg= +github.com/ryanuber/columnize v1.1.2-0.20190319233515-9e6335e58db3/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts=