From 65942f2fe4f66f9f3cb899676e6de04cc36f1e75 Mon Sep 17 00:00:00 2001 From: Eugene Berger Date: Sat, 16 Apr 2022 01:44:27 +0300 Subject: [PATCH] Windows Support with CMD --- Makefile | 16 ++++- .../process-compose-chain-with-errors.yaml | 2 +- fixtures/process-compose-with-log.yaml | 10 ++-- process-compose-win.yaml | 58 +++++++++++++++++++ process-compose.yaml | 4 ++ src/process.go | 22 ++++++- src/system_test.go | 32 +++++++++- test_loop.ps1 | 18 ++++++ 8 files changed, 150 insertions(+), 12 deletions(-) create mode 100644 process-compose-win.yaml create mode 100644 test_loop.ps1 diff --git a/Makefile b/Makefile index 51e0dfd..f351c84 100644 --- a/Makefile +++ b/Makefile @@ -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}* \ No newline at end of file + $(RM) bin\${BINARY_NAME}* diff --git a/fixtures/process-compose-chain-with-errors.yaml b/fixtures/process-compose-chain-with-errors.yaml index e442e35..6f88a6f 100644 --- a/fixtures/process-compose-chain-with-errors.yaml +++ b/fixtures/process-compose-chain-with-errors.yaml @@ -64,7 +64,7 @@ processes: backoff_seconds: 2 - +log_location: pc_tst.log environment: - 'ABC=222' diff --git a/fixtures/process-compose-with-log.yaml b/fixtures/process-compose-with-log.yaml index bc6cfbd..f9cd0e7 100644 --- a/fixtures/process-compose-with-log.yaml +++ b/fixtures/process-compose-with-log.yaml @@ -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' diff --git a/process-compose-win.yaml b/process-compose-win.yaml new file mode 100644 index 0000000..cc37863 --- /dev/null +++ b/process-compose-win.yaml @@ -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 diff --git a/process-compose.yaml b/process-compose.yaml index b2864cd..1b41c99 100644 --- a/process-compose.yaml +++ b/process-compose.yaml @@ -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: diff --git a/src/process.go b/src/process.go index 4eeabf1..6e50dad 100644 --- a/src/process.go +++ b/src/process.go @@ -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" + } } diff --git a/src/system_test.go b/src/system_test.go index dd00096..c75d8b1 100644 --- a/src/system_test.go +++ b/src/system_test.go @@ -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, }, } diff --git a/test_loop.ps1 b/test_loop.ps1 new file mode 100644 index 0000000..20c8bf1 --- /dev/null +++ b/test_loop.ps1 @@ -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