Skip to content

Commit

Permalink
Redirect app stdout/stderr to logfile and enable passing of entrypoin…
Browse files Browse the repository at this point in the history
…t args
  • Loading branch information
dbarbashov committed May 11, 2021
1 parent d57d559 commit 355775f
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 11 deletions.
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var rootCmd = &cobra.Command{

func Execute() {
//rootCmd.SetArgs([]string{ "--root", "/home/lkmfwe/ctcli", "install", "/home/lkmfwe/Programming/FSharp/opbot/packaging/package/crafttalk-opbot-release-2021-03-22-6-commit.tar.gz" })
//rootCmd.SetArgs([]string{ "--root", "/home/lkmfwe/ctcli", "release-info" })
//rootCmd.SetArgs([]string{ "--root", "/home/lkmfwe/ctcli", "start" })

workDir, err := os.Getwd()
if err != nil {
Expand Down
5 changes: 4 additions & 1 deletion cmd/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ var stopCmd = &cobra.Command{
cmd.PrintErr(err)
return
}
domain.StopApps(rootDir)
if err := domain.StopApps(rootDir); err != nil {
cmd.PrintErr(err)
return
}
},
}

Expand Down
1 change: 1 addition & 0 deletions domain/appConfig/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type AppPackageConfig struct {
LogsFolder string `json:"logsFolder"`
Configs []string `json:"configs"`
Data []string `json:"data"`
Entrypoint []string `json:"entrypoint"`
}

func GetAppConfig(path string) (AppPackageConfig, error) {
Expand Down
17 changes: 16 additions & 1 deletion domain/ctcliDir/paths.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package ctcliDir

import "path"
import (
"fmt"
"path"
"time"
)

const (
TempDir = "tmp"
Expand Down Expand Up @@ -42,4 +46,15 @@ func GetAppDataDir(rootDir string, app string) string {

func GetAppLogsDir(rootDir string, app string) string {
return path.Join(GetLogsDir(rootDir), app)
}

func GetAppStdoutLogFilePath(rootDir, app string) string {
return path.Join(GetAppLogsDir(rootDir, app), "stdout-stderr.log")
}

func GetNewArchiveStdoutLogFilePath(rootDir, app string) string {
return path.Join(
GetAppLogsDir(rootDir, app),
"stdout-stderr-arch",
fmt.Sprintf("%s.log", time.Now().UTC().Format(time.RFC3339)))
}
10 changes: 10 additions & 0 deletions domain/moving/loadRelease.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"io/ioutil"
"os"
"path"
"strings"
)

func CopyBinariesToRelease(rootDir string, packagePath string) error {
Expand Down Expand Up @@ -51,6 +52,15 @@ func configureRuncConfig(rootDir string, app string, config appConfig.AppPackage
return err
}

jsonValue.Get("process").Set("terminal", fastjson.MustParse(`false`))
if config.Entrypoint != nil {
resultValue := fastjson.MustParse("[]")
for i, cmdPart := range config.Entrypoint {
resultValue.SetArrayItem(i, fastjson.MustParse(`"` + strings.Replace(cmdPart, `"`, `\"`, -1) + `"`))
}
jsonValue.Get("process").Set("args", resultValue)
}

mountsMap := map[string]*fastjson.Value{}

for _, mount := range mounts {
Expand Down
44 changes: 36 additions & 8 deletions domain/start.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package domain

import (
"ctcli/domain/ctcliDir"
"ctcli/domain/release"
"ctcli/util"
"fmt"
"github.com/fatih/color"
"io/ioutil"
"os"
"os/exec"
"time"
"path"
)

func StartApps(rootDir string) error {
Expand All @@ -23,31 +25,57 @@ func StartApps(rootDir string) error {
for _, appFolder := range appFolders {
appName := appFolder.Name()
appPath := release.GetCurrentReleaseAppFolder(rootDir, appName)
if err := StartApp(appName, appPath, runcPath); err != nil {
if err := StartApp(rootDir, appName, appPath, runcPath); err != nil {
color.Red(fmt.Sprintf("error while starting %s app, error: %s", appName, err))
}
}
return nil
}

func StartApp(appName, appPath, runcPath string) error {


func StartApp(rootDir, appName, appPath, runcPath string) error {
cmd := exec.Command(
runcPath,
"run",
"create",
"--bundle",
appPath,
appName,
)

if err := cmd.Start(); err != nil {
logFilePath := ctcliDir.GetAppStdoutLogFilePath(rootDir, appName)
_ = os.MkdirAll(path.Dir(logFilePath), os.ModePerm)
if util.PathExists(logFilePath) {
archiveLogFilePath := ctcliDir.GetNewArchiveStdoutLogFilePath(rootDir, appName)
_ = os.MkdirAll(path.Dir(archiveLogFilePath), os.ModePerm)
_ = os.Rename(logFilePath, archiveLogFilePath)
}

stdout, err := os.OpenFile(logFilePath, os.O_CREATE | os.O_RDWR, os.ModePerm)
if err != nil {
return err
}
cmd.Stdout = stdout
cmd.Stderr = stdout
if err := cmd.Run(); err != nil {
return err
}

cmd = exec.Command(
runcPath,
"start",
appName,
)
defer stdout.Close()


if err := cmd.Run(); err != nil {
return err
}
// TODO Search how to do it without sleep
time.Sleep(4 * time.Second)

if err := cmd.Process.Release(); err != nil {
return err
}
color.Green(fmt.Sprintf("%s application started", appName))
color.Green(fmt.Sprintf("%s started", appName))
return nil
}
12 changes: 12 additions & 0 deletions domain/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/fatih/color"
"io/ioutil"
"os/exec"
"time"
)

func StopApps(rootDir string) error {
Expand All @@ -33,6 +34,17 @@ func StopApp(appName, runcPath string) error {
runcPath,
"kill",
appName,
"SIGTERM",
)
if err := cmd.Run(); err != nil {
return err
}

time.Sleep(time.Second)
cmd = exec.Command(
runcPath,
"delete",
appName,
)
if err := cmd.Run(); err != nil {
return err
Expand Down

0 comments on commit 355775f

Please sign in to comment.