Adding changes! Working docker wrapper!

Signed-off-by: Kris Nóva <kris@nivenly.com>
main
Kris Nóva 2021-02-03 14:00:31 -08:00
parent b107d98a37
commit 3ccaa2ec9b
7 changed files with 63 additions and 28 deletions

View File

@ -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
}

View File

@ -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

View File

@ -7,9 +7,7 @@
#
# Startup Script for the Application
####################################
set -e
echo "Starting [SampleApp]"
echo "Creating [SampleApp]"
docker run -d \
--name photoprism \

View File

@ -7,10 +7,7 @@
#
# Startup Script for the Application
####################################
set -e
echo "Stopping [SampleApp]"
echo "Destroying [SampleApp]"
docker stop photoprism
docker rm photoprism

View File

@ -7,9 +7,5 @@
#
# Startup Script for the Application
####################################
set -e
echo "Starting [SampleApp]"
docker logs -f --tail 100 photoprism

View File

@ -7,8 +7,6 @@
#
# Startup Script for the Application
####################################
set -e
echo "Stopping [SampleApp]"
echo "Starting [SampleApp]"
docker start photoprism

View File

@ -7,8 +7,6 @@
#
# Startup Script for the Application
####################################
set -e
echo "Stopping [SampleApp]"
docker stop photoprism