From 3ccaa2ec9b6c3568f1cdc2876760a45b5f5ec97e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kris=20N=C3=B3va?= Date: Wed, 3 Feb 2021 14:00:31 -0800 Subject: [PATCH] Adding changes! Working docker wrapper! MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Kris NĂ³va --- sample-app/app.go | 37 ++++++++++++++++++++++++++++++------- sample-app/exec.go | 35 ++++++++++++++++++++++++++++++----- sample-app/pcreate | 4 +--- sample-app/pdestroy | 5 +---- sample-app/plogs | 4 ---- sample-app/pstart | 4 +--- sample-app/pstop | 2 -- 7 files changed, 63 insertions(+), 28 deletions(-) diff --git a/sample-app/app.go b/sample-app/app.go index 655586c..f0cde38 100644 --- a/sample-app/app.go +++ b/sample-app/app.go @@ -1,6 +1,9 @@ package sampleapp import ( + "path/filepath" + "runtime" + "github.com/kris-nova/logger" photoprism "github.com/kris-nova/client-go" @@ -17,25 +20,45 @@ func New() *SampleApplication { // These are the bash scripts that can be used // to start/stop the Photoprism test application var ( - CreateCommand = `./pcreate` - DestroyCommand = `./pdestroy` - LogsCommand = `./plogs` - StartCommand = `./pstart` - StopCommand = `./pstop` + CreateCommand = `pcreate` + DestroyCommand = `pdestroy` + LogsCommand = `plogs` + StartCommand = `pstart` + StopCommand = `pstop` ) func (a *SampleApplication) Start() error { logger.Info("Starting Application...") - script := NewScript(StartCommand) + script, err := NewScriptFromPath(filepath.Join(PrintWorkingDirectory(), StartCommand)) + if err != nil { + return err + } return script.Interpret() } func (a *SampleApplication) Stop() error { logger.Info("Stopping Application...") - script := NewScript(StopCommand) + script, err := NewScriptFromPath(filepath.Join(PrintWorkingDirectory(), StopCommand)) + if err != nil { + return err + } return script.Interpret() } func (a *SampleApplication) GetAuth() photoprism.ClientAuthenticator { return nil } + +func PrintWorkingDirectory() string { + _, filename, _, ok := runtime.Caller(1) + if !ok { + logger.Info("Unable to PWD") + return "" + } + dir, err := filepath.Abs(filepath.Dir(filename)) + if err != nil { + logger.Info("Unable to PWD: %v", err) + return "" + } + return dir +} diff --git a/sample-app/exec.go b/sample-app/exec.go index 6f72eb7..ce66f36 100644 --- a/sample-app/exec.go +++ b/sample-app/exec.go @@ -3,17 +3,37 @@ package sampleapp import ( "bytes" "fmt" + "io/ioutil" "os/exec" + "path/filepath" "strings" "github.com/kris-nova/logger" ) +// Script is a set of commands delimited by newlines +// Comments # and // are ignored. type Script struct { commands []string } -func NewScript(str string) *Script { +// NewScriptFromPath is used to build an executable script from a path of disk. +func NewScriptFromPath(path string) (*Script, error) { + path, err := filepath.Abs(path) + if err != nil { + return nil, fmt.Errorf("unable to calculate fully qualified path for path: %s: %v", path, err) + } + + bytes, err := ioutil.ReadFile(path) + if err != nil { + return nil, fmt.Errorf("unable to read from path %s: %v", path, err) + } + content := string(bytes) + return NewScriptFromString(content), nil +} + +// NewScriptFromString is used to build an executable script from the content in string form. +func NewScriptFromString(str string) *Script { script := &Script{} spl := strings.Split(str, "\n") //logger.Info("Script lines: %d", len(spl)) @@ -23,6 +43,8 @@ func NewScript(str string) *Script { return script } +// Interpret is used to procedurally execute a script. The script will execute each line independently +// and can error at any point in the executation path. func (s *Script) Interpret() error { //logger.Info("Running script...") chResult := make(chan *ExecResult) @@ -37,7 +59,7 @@ func (s *Script) Interpret() error { // Ignore newlines // Ignore comments starting with # // Ignore comments starting with // - if cmdStr == "\n" || strings.HasPrefix(cmdStr, "#") || strings.HasPrefix(cmdStr, "//") { + if cmdStr == "\n" || cmdStr == "" || strings.HasPrefix(cmdStr, "#") || strings.HasPrefix(cmdStr, "//") { continue } //logger.Info("Executing: [%s]", cmdStr) @@ -45,7 +67,7 @@ func (s *Script) Interpret() error { if err != nil { return fmt.Errorf("error executing running command [%s] on line [%d]\n%v\n", cmdStr, i+1, err) } else if result.exitCode != 0 { - return fmt.Errorf("non zero exit code running command [%s] on line [%d]\n%s\n%s\n", cmdStr, i+1, result.Stdout(), result.stderr) + return fmt.Errorf("non zero exit code running command [%s] on line [%d]\n%s\n%s\n", cmdStr, i+1, result.Stdout(), result.Stderr()) } // Here is where we log STDOUT from a "script" // Right now it is set to DEBUG which can be enabled by @@ -104,7 +126,7 @@ func Exec(str string) (*ExecResult, error) { } fqpcmd, err := exec.LookPath(cmdstr) if err != nil { - return nil, fmt.Errorf("unable to find fully qualified path for executable %s: %v", cmdstr, err) + logger.Debug("unable to find fully qualified path for executable %s: %v", cmdstr, err) } //logger.Info("Command: %s", fqpcmd) @@ -112,7 +134,10 @@ func Exec(str string) (*ExecResult, error) { stdoutBuffer := bytes.Buffer{} stderrBuffer := bytes.Buffer{} - e := []string{fqpcmd, fmt.Sprint(args)} + e := []string{fqpcmd} + for _, arg := range args { + e = append(e, arg) + } cmd := exec.Command(e[0], e[1:]...) cmd.Stdout = &stdoutBuffer cmd.Stderr = &stderrBuffer diff --git a/sample-app/pcreate b/sample-app/pcreate index 8be863b..3965564 100755 --- a/sample-app/pcreate +++ b/sample-app/pcreate @@ -7,9 +7,7 @@ # # Startup Script for the Application #################################### -set -e - -echo "Starting [SampleApp]" +echo "Creating [SampleApp]" docker run -d \ --name photoprism \ diff --git a/sample-app/pdestroy b/sample-app/pdestroy index e6ba14a..5a3833e 100755 --- a/sample-app/pdestroy +++ b/sample-app/pdestroy @@ -7,10 +7,7 @@ # # Startup Script for the Application #################################### -set -e - -echo "Stopping [SampleApp]" - +echo "Destroying [SampleApp]" docker stop photoprism docker rm photoprism diff --git a/sample-app/plogs b/sample-app/plogs index de14a69..ec8f37a 100755 --- a/sample-app/plogs +++ b/sample-app/plogs @@ -7,9 +7,5 @@ # # Startup Script for the Application #################################### -set -e - -echo "Starting [SampleApp]" - docker logs -f --tail 100 photoprism diff --git a/sample-app/pstart b/sample-app/pstart index e416ab1..eec7cd3 100755 --- a/sample-app/pstart +++ b/sample-app/pstart @@ -7,8 +7,6 @@ # # Startup Script for the Application #################################### -set -e - -echo "Stopping [SampleApp]" +echo "Starting [SampleApp]" docker start photoprism diff --git a/sample-app/pstop b/sample-app/pstop index 143ec09..5ea4a18 100755 --- a/sample-app/pstop +++ b/sample-app/pstop @@ -7,8 +7,6 @@ # # Startup Script for the Application #################################### -set -e - echo "Stopping [SampleApp]" docker stop photoprism