Adding changes! Working docker wrapper!
Signed-off-by: Kris Nóva <kris@nivenly.com>main
parent
b107d98a37
commit
3ccaa2ec9b
|
@ -1,6 +1,9 @@
|
||||||
package sampleapp
|
package sampleapp
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"path/filepath"
|
||||||
|
"runtime"
|
||||||
|
|
||||||
"github.com/kris-nova/logger"
|
"github.com/kris-nova/logger"
|
||||||
|
|
||||||
photoprism "github.com/kris-nova/client-go"
|
photoprism "github.com/kris-nova/client-go"
|
||||||
|
@ -17,25 +20,45 @@ func New() *SampleApplication {
|
||||||
// These are the bash scripts that can be used
|
// These are the bash scripts that can be used
|
||||||
// to start/stop the Photoprism test application
|
// to start/stop the Photoprism test application
|
||||||
var (
|
var (
|
||||||
CreateCommand = `./pcreate`
|
CreateCommand = `pcreate`
|
||||||
DestroyCommand = `./pdestroy`
|
DestroyCommand = `pdestroy`
|
||||||
LogsCommand = `./plogs`
|
LogsCommand = `plogs`
|
||||||
StartCommand = `./pstart`
|
StartCommand = `pstart`
|
||||||
StopCommand = `./pstop`
|
StopCommand = `pstop`
|
||||||
)
|
)
|
||||||
|
|
||||||
func (a *SampleApplication) Start() error {
|
func (a *SampleApplication) Start() error {
|
||||||
logger.Info("Starting Application...")
|
logger.Info("Starting Application...")
|
||||||
script := NewScript(StartCommand)
|
script, err := NewScriptFromPath(filepath.Join(PrintWorkingDirectory(), StartCommand))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
return script.Interpret()
|
return script.Interpret()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *SampleApplication) Stop() error {
|
func (a *SampleApplication) Stop() error {
|
||||||
logger.Info("Stopping Application...")
|
logger.Info("Stopping Application...")
|
||||||
script := NewScript(StopCommand)
|
script, err := NewScriptFromPath(filepath.Join(PrintWorkingDirectory(), StopCommand))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
return script.Interpret()
|
return script.Interpret()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *SampleApplication) GetAuth() photoprism.ClientAuthenticator {
|
func (a *SampleApplication) GetAuth() photoprism.ClientAuthenticator {
|
||||||
return nil
|
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
|
||||||
|
}
|
||||||
|
|
|
@ -3,17 +3,37 @@ package sampleapp
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/kris-nova/logger"
|
"github.com/kris-nova/logger"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Script is a set of commands delimited by newlines
|
||||||
|
// Comments # and // are ignored.
|
||||||
type Script struct {
|
type Script struct {
|
||||||
commands []string
|
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{}
|
script := &Script{}
|
||||||
spl := strings.Split(str, "\n")
|
spl := strings.Split(str, "\n")
|
||||||
//logger.Info("Script lines: %d", len(spl))
|
//logger.Info("Script lines: %d", len(spl))
|
||||||
|
@ -23,6 +43,8 @@ func NewScript(str string) *Script {
|
||||||
return 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 {
|
func (s *Script) Interpret() error {
|
||||||
//logger.Info("Running script...")
|
//logger.Info("Running script...")
|
||||||
chResult := make(chan *ExecResult)
|
chResult := make(chan *ExecResult)
|
||||||
|
@ -37,7 +59,7 @@ func (s *Script) Interpret() error {
|
||||||
// Ignore newlines
|
// Ignore newlines
|
||||||
// Ignore comments starting with #
|
// Ignore comments starting with #
|
||||||
// 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
|
continue
|
||||||
}
|
}
|
||||||
//logger.Info("Executing: [%s]", cmdStr)
|
//logger.Info("Executing: [%s]", cmdStr)
|
||||||
|
@ -45,7 +67,7 @@ func (s *Script) Interpret() error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("error executing running command [%s] on line [%d]\n%v\n", cmdStr, i+1, err)
|
return fmt.Errorf("error executing running command [%s] on line [%d]\n%v\n", cmdStr, i+1, err)
|
||||||
} else if result.exitCode != 0 {
|
} 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"
|
// Here is where we log STDOUT from a "script"
|
||||||
// Right now it is set to DEBUG which can be enabled by
|
// 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)
|
fqpcmd, err := exec.LookPath(cmdstr)
|
||||||
if err != nil {
|
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)
|
//logger.Info("Command: %s", fqpcmd)
|
||||||
|
@ -112,7 +134,10 @@ func Exec(str string) (*ExecResult, error) {
|
||||||
|
|
||||||
stdoutBuffer := bytes.Buffer{}
|
stdoutBuffer := bytes.Buffer{}
|
||||||
stderrBuffer := 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 := exec.Command(e[0], e[1:]...)
|
||||||
cmd.Stdout = &stdoutBuffer
|
cmd.Stdout = &stdoutBuffer
|
||||||
cmd.Stderr = &stderrBuffer
|
cmd.Stderr = &stderrBuffer
|
||||||
|
|
|
@ -7,9 +7,7 @@
|
||||||
#
|
#
|
||||||
# Startup Script for the Application
|
# Startup Script for the Application
|
||||||
####################################
|
####################################
|
||||||
set -e
|
echo "Creating [SampleApp]"
|
||||||
|
|
||||||
echo "Starting [SampleApp]"
|
|
||||||
|
|
||||||
docker run -d \
|
docker run -d \
|
||||||
--name photoprism \
|
--name photoprism \
|
||||||
|
|
|
@ -7,10 +7,7 @@
|
||||||
#
|
#
|
||||||
# Startup Script for the Application
|
# Startup Script for the Application
|
||||||
####################################
|
####################################
|
||||||
set -e
|
echo "Destroying [SampleApp]"
|
||||||
|
|
||||||
echo "Stopping [SampleApp]"
|
|
||||||
|
|
||||||
docker stop photoprism
|
docker stop photoprism
|
||||||
docker rm photoprism
|
docker rm photoprism
|
||||||
|
|
||||||
|
|
|
@ -7,9 +7,5 @@
|
||||||
#
|
#
|
||||||
# Startup Script for the Application
|
# Startup Script for the Application
|
||||||
####################################
|
####################################
|
||||||
set -e
|
|
||||||
|
|
||||||
echo "Starting [SampleApp]"
|
|
||||||
|
|
||||||
docker logs -f --tail 100 photoprism
|
docker logs -f --tail 100 photoprism
|
||||||
|
|
||||||
|
|
|
@ -7,8 +7,6 @@
|
||||||
#
|
#
|
||||||
# Startup Script for the Application
|
# Startup Script for the Application
|
||||||
####################################
|
####################################
|
||||||
set -e
|
echo "Starting [SampleApp]"
|
||||||
|
|
||||||
echo "Stopping [SampleApp]"
|
|
||||||
|
|
||||||
docker start photoprism
|
docker start photoprism
|
||||||
|
|
|
@ -7,8 +7,6 @@
|
||||||
#
|
#
|
||||||
# Startup Script for the Application
|
# Startup Script for the Application
|
||||||
####################################
|
####################################
|
||||||
set -e
|
|
||||||
|
|
||||||
echo "Stopping [SampleApp]"
|
echo "Stopping [SampleApp]"
|
||||||
|
|
||||||
docker stop photoprism
|
docker stop photoprism
|
||||||
|
|
Loading…
Reference in New Issue