Windows Support with CMD

This commit is contained in:
Eugene Berger 2022-04-16 01:44:27 +03:00
parent 93a1e6c59d
commit 65942f2fe4
8 changed files with 150 additions and 12 deletions

View File

@ -1,16 +1,26 @@
BINARY_NAME=process-compose
EXT=''
RM=rm
ifeq ($(OS),Windows_NT)
EXT=.exe
RM = cmd /C del /Q /F
endif
.PHONY: test run
buildrun: build run
build:
go build -o bin/${BINARY_NAME} ./src
go build -o bin/${BINARY_NAME}${EXT} ./src
compile:
#Linux
GOOS=linux GOARCH=386 go build -o bin/${BINARY_NAME}-linux-386 ./src
GOOS=linux GOARCH=amd64 go build -o bin/${BINARY_NAME}-linux-amd64 ./src
GOOS=linux GOARCH=arm64 go build -o bin/${BINARY_NAME}-linux-arm64 ./src
GOOS=linux GOARCH=arm go build -o bin/${BINARY_NAME}-linux-arm ./src
#Windows
GOOS=windows GOARCH=amd64 go build -o bin/${BINARY_NAME}-windows-amd64.exe ./src
test:
go test -cover ./src
coverhtml:
@ -18,7 +28,7 @@ coverhtml:
go tool cover -html=coverage.out
run:
./bin/${BINARY_NAME}
./bin/${BINARY_NAME}${EXT}
clean:
rm bin/${BINARY_NAME}*
$(RM) bin\${BINARY_NAME}*

View File

@ -64,7 +64,7 @@ processes:
backoff_seconds: 2
log_location: pc_tst.log
environment:
- 'ABC=222'

View File

@ -8,9 +8,10 @@ processes:
condition: process_completed_successfully
process2:
command: "echo 'process2 is removing the log' && rm ./pc.log-test.log"
command: "echo 'process2 is removing the log'"
availability:
restart: "on-failure"
# max_restarts: 3
depends_on:
process3:
condition: process_completed
@ -30,10 +31,11 @@ processes:
condition: process_completed_successfully
process5:
command: "echo 'process5 is removing the process6 log' && rm ./pc.proc6.log-test.log"
command: "python -c print(str(4+5))"
availability:
restart: "on-failure"
backoff_seconds: 2
backoff_seconds: 1
# max_restarts: 3
depends_on:
process6:
condition: process_completed_successfully
@ -61,8 +63,6 @@ processes:
restart: "on-failure"
backoff_seconds: 2
environment:
- 'ABC=222'

58
process-compose-win.yaml Normal file
View File

@ -0,0 +1,58 @@
version: "0.5"
log_level: debug
processes:
process0:
command: "ls ddd"
process1:
command: "powershell.exe ./test_loop.ps1 ${PROC4}"
availability:
restart: "on-failure"
backoff_seconds: 2
depends_on:
process2:
condition: process_completed_successfully
process3:
condition: process_completed
# process4:
# condition: process_completed_successfully
environment:
- 'EXIT_CODE=0'
process2:
command: "powershell.exe ./test_loop.ps1 process2"
log_location: ./pc.proc2.log
availability:
restart: "on-failure"
# depends_on:
# process3:
# condition: process_completed_successfully
environment:
- 'ABC=2221'
- 'PRINT_ERR=111'
- 'EXIT_CODE=0'
process3:
command: "powershell.exe ./test_loop.ps1 process3"
availability:
restart: "on-failure"
backoff_seconds: 2
depends_on:
process4:
condition: process_completed_successfully
process4:
command: "powershell.exe ./test_loop.ps1 process4"
# availability:
# restart: on-failure
environment:
- 'ABC=2221'
- 'EXIT_CODE=1'
kcalc:
command: "calc"
disabled: true
environment:
- 'ABC=222'
log_location: ./pc.log

View File

@ -6,6 +6,7 @@ processes:
process1:
command: "./test_loop.bash ${PROC4}"
win_command: "powershell.exe ./test_loop.ps1 ${PROC4}"
availability:
restart: "on-failure"
backoff_seconds: 2
@ -21,6 +22,7 @@ processes:
process2:
command: "./test_loop.bash process2"
win_command: "powershell.exe ./test_loop.ps1 process2"
log_location: ./pc.proc2.log
availability:
restart: "on-failure"
@ -34,6 +36,7 @@ processes:
process3:
command: "./test_loop.bash process3"
win_command: "powershell.exe ./test_loop.ps1 process3"
availability:
restart: "on-failure"
backoff_seconds: 2
@ -43,6 +46,7 @@ processes:
process4:
command: "./test_loop.bash process4"
win_command: "powershell.exe ./test_loop.ps1 process4"
# availability:
# restart: on-failure
environment:

View File

@ -7,6 +7,7 @@ import (
"math/rand"
"os"
"os/exec"
"runtime"
"sync"
"time"
@ -50,7 +51,7 @@ func NewProcess(globalEnv []string, logger PcLogger, procConf ProcessConfig, rep
func (p *Process) Run() error {
for {
cmd := exec.Command(getRunnerShell(), "-c", p.procConf.Command)
cmd := exec.Command(getRunnerShell(), getRunnerArg(), p.getCommand())
cmd.Env = p.getProcessEnvironment()
stdout, _ := cmd.StdoutPipe()
stderr, _ := cmd.StderrPipe()
@ -149,6 +150,10 @@ func (p *Process) GetNameWithReplica() string {
return fmt.Sprintf("%s_%d", p.procConf.Name, p.replica)
}
func (p *Process) getCommand() string {
return p.procConf.Command
}
func (p *Process) handleOutput(pipe io.ReadCloser,
handler func(message string)) {
@ -172,8 +177,21 @@ func (p *Process) handleError(message string) {
func getRunnerShell() string {
shell, ok := os.LookupEnv("SHELL")
if !ok {
return "bash"
if runtime.GOOS == "windows" {
shell = "cmd"
} else {
shell = "bash"
}
} else {
return shell
}
return shell
}
func getRunnerArg() string {
if runtime.GOOS == "windows" {
return "/C"
} else {
return "-c"
}
}

View File

@ -1,7 +1,9 @@
package main
import (
"os"
"path/filepath"
"strings"
"testing"
)
@ -17,6 +19,11 @@ func TestSystem_TestFixtures(t *testing.T) {
fixtures := getFixtures()
for _, fixture := range fixtures {
if strings.Contains(fixture, "process-compose-with-log.yaml") {
//there is a dedicated test for that TestSystem_TestComposeWithLog
continue
}
t.Run(fixture, func(t *testing.T) {
project := createProject(fixture)
project.Run()
@ -24,6 +31,29 @@ func TestSystem_TestFixtures(t *testing.T) {
}
}
func TestSystem_TestComposeWithLog(t *testing.T) {
fixture := filepath.Join("..", "fixtures", "process-compose-with-log.yaml")
t.Run(fixture, func(t *testing.T) {
project := createProject(fixture)
project.Run()
if _, err := os.Stat(project.LogLocation); err != nil {
t.Errorf("log file %s not found", project.LogLocation)
}
if err := os.Remove(project.LogLocation); err != nil {
t.Errorf("failed to delete the log file %s, %s", project.LogLocation, err.Error())
}
proc6log := project.Processes["process6"].LogLocation
if _, err := os.Stat(proc6log); err != nil {
t.Errorf("log file %s not found", proc6log)
}
if err := os.Remove(proc6log); err != nil {
t.Errorf("failed to delete the log file %s, %s", proc6log, err.Error())
}
})
}
func Test_autoDiscoverComposeFile(t *testing.T) {
type args struct {
pwd string
@ -47,7 +77,7 @@ func Test_autoDiscoverComposeFile(t *testing.T) {
args: args{
pwd: "../",
},
want: "../process-compose.yaml",
want: filepath.Join("..", "process-compose.yaml"),
wantErr: false,
},
}

18
test_loop.ps1 Normal file
View File

@ -0,0 +1,18 @@
#!/usr/bin/env bash
$LOOPS=3
foreach($i in 1..$LOOPS)
{
Start-Sleep 1
if (Test-Path 'env:PRINT_ERR') {
# Write-Warning "test loop $i this is error $Args[0] $Env:PC_PROC_NAME"
$Host.UI.WriteErrorLine("test loop $i this is error " + $Args[0] + " " + $Env:PC_PROC_NAME)
}
else{
Write-Host "test loop $i"$Args[0] [$Env:ABC]
}
}
exit $Env:EXIT_CODE