From 9eda9df82ec8150be1d82cdf6294cc10992345dc Mon Sep 17 00:00:00 2001 From: Yuri Date: Fri, 14 Jul 2023 00:54:28 -0500 Subject: [PATCH 1/3] wincmd mess --- engine/compiler/script.go | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/engine/compiler/script.go b/engine/compiler/script.go index 5b39440..df71f0c 100644 --- a/engine/compiler/script.go +++ b/engine/compiler/script.go @@ -26,7 +26,34 @@ func setupScript(src *resource.Step, dst *engine.Step, os string) { // helper function configures the pipeline script for the // windows operating system. -func setupScriptWindows(src *resource.Step, dst *engine.Step) { +// WIP, BROKEN +func setupScriptWindowsCmd(src *resource.Step, dst *engine.Step) { + + //dst.Command = []string{ + // "setlocal", + // "EnableDelayedExpansion", "&", + // "for", "/f", "%L", "in", `(%DRONE_SCRIPT%)`, + // "do", "(", + // "echo", "+", "%L", + // ")", "&", + //} + + //dst.Command = []string{"set/p", "_discard=%DRONE_SCRIPT%build_script.cmd"} // & call build_script.cmd + //dst.Command = []string{`set/p_discard=%DRONE_SCRIPT%build_script.cmd & dir & echo ================== & type build_script.cmd & echo ================== `} // & call build_script.cmd + //dst.Command = []string{"build_script.cmd"} + + //dst.Command = []string{"set"} + //dst.Entrypoint = []string{"cmd", "/S", "/c", "%DRONE_WORKSPACE%\\§§build§§.cmd"} + //dst.Entrypoint = []string{"cmd", "/S", "/c", "echo", "%DRONE_WORKSPACE%\\§§build§§.cmd"} + //dst.Entrypoint = []string{"cmd", "/S", "/c"} + //dst.Command = []string{"%DRONE_WORKSPACE%__build__.cmd"} + //dst.Command = []string{" Date: Fri, 14 Jul 2023 01:00:45 -0500 Subject: [PATCH 2/3] WIP wincmd runner Only copy tar on windows and env.SHELL=cmd.exe --- engine/compiler/script.go | 39 +++++++-------- .../compiler/shell/powershell/powershell.go | 2 +- engine/compiler/shell/wincmd/wincmd.go | 48 +++++++++++++++++++ engine/engine.go | 30 ++++++++++++ 4 files changed, 95 insertions(+), 24 deletions(-) create mode 100644 engine/compiler/shell/wincmd/wincmd.go diff --git a/engine/compiler/script.go b/engine/compiler/script.go index df71f0c..73ffafd 100644 --- a/engine/compiler/script.go +++ b/engine/compiler/script.go @@ -8,6 +8,7 @@ import ( "github.com/drone-runners/drone-runner-docker/engine" "github.com/drone-runners/drone-runner-docker/engine/compiler/shell" "github.com/drone-runners/drone-runner-docker/engine/compiler/shell/powershell" + "github.com/drone-runners/drone-runner-docker/engine/compiler/shell/wincmd" "github.com/drone-runners/drone-runner-docker/engine/resource" ) @@ -17,7 +18,7 @@ func setupScript(src *resource.Step, dst *engine.Step, os string) { if len(src.Commands) > 0 { switch os { case "windows": - setupScriptWindows(src, dst) + setupScriptWindowsCmd(src, dst) default: setupScriptPosix(src, dst) } @@ -28,38 +29,30 @@ func setupScript(src *resource.Step, dst *engine.Step, os string) { // windows operating system. // WIP, BROKEN func setupScriptWindowsCmd(src *resource.Step, dst *engine.Step) { - - //dst.Command = []string{ - // "setlocal", - // "EnableDelayedExpansion", "&", - // "for", "/f", "%L", "in", `(%DRONE_SCRIPT%)`, - // "do", "(", - // "echo", "+", "%L", - // ")", "&", - //} - - //dst.Command = []string{"set/p", "_discard=%DRONE_SCRIPT%build_script.cmd"} // & call build_script.cmd - //dst.Command = []string{`set/p_discard=%DRONE_SCRIPT%build_script.cmd & dir & echo ================== & type build_script.cmd & echo ================== `} // & call build_script.cmd - //dst.Command = []string{"build_script.cmd"} - - //dst.Command = []string{"set"} - //dst.Entrypoint = []string{"cmd", "/S", "/c", "%DRONE_WORKSPACE%\\§§build§§.cmd"} - //dst.Entrypoint = []string{"cmd", "/S", "/c", "echo", "%DRONE_WORKSPACE%\\§§build§§.cmd"} - //dst.Entrypoint = []string{"cmd", "/S", "/c"} - //dst.Command = []string{"%DRONE_WORKSPACE%__build__.cmd"} - //dst.Command = []string{" (Join-Path $Env:USERPROFILE '_netrc'); +"@ > (Join-Path $Env:USERPROFILE '.netrc'); } [Environment]::SetEnvironmentVariable("DRONE_NETRC_USERNAME", $null); [Environment]::SetEnvironmentVariable("DRONE_NETRC_PASSWORD", $null); diff --git a/engine/compiler/shell/wincmd/wincmd.go b/engine/compiler/shell/wincmd/wincmd.go new file mode 100644 index 0000000..a394f11 --- /dev/null +++ b/engine/compiler/shell/wincmd/wincmd.go @@ -0,0 +1,48 @@ +// Package wincmd provides functions for converting shell +// commands to cmd scripts. +package wincmd + +import ( + "bytes" +) + +// Script converts a slice of individual shell commands to +// a cmd script. +func Script(commands []string) string { + buf := new(bytes.Buffer) + + // ignore first linebreak + buf.WriteString(optionScript) + + for _, command := range commands { + buf.WriteString("echo +--------------------------------\n") + buf.WriteString("echo + " + command + "\n") + buf.WriteString(command) + buf.WriteString(` +set LastErrorLevel=%ErrorLevel% +if %LastErrorLevel% gtr 0 ( + echo ERROR: %LastErrorLevel% + exit /b %LastErrorLevel% +)` + "\n") + buf.WriteString("echo.\n") + } + + return buf.String() +} + +// optionScript is a helper script this is added to the build +// to set shell options, in this case, to exit on error. +const optionScript = ` +@echo off +if .%DRONE_NETRC_MACHINE%. neq .. ( + type nul > %USERPROFILE%\.netrc + echo machine %DRONE_NETRC_MACHINE% >> %USERPROFILE%\.netrc + echo login %DRONE_NETRC_USERNAME% >> %USERPROFILE%\.netrc + echo password %DRONE_NETRC_PASSWORD% >> %USERPROFILE%\.netrc +) +set DRONE_NETRC_USERNAME= +set DRONE_NETRC_PASSWORD= +set DRONE_NETRC_FILE= +set DRONE_SCRIPT= + +` diff --git a/engine/engine.go b/engine/engine.go index 70d33eb..16a4301 100644 --- a/engine/engine.go +++ b/engine/engine.go @@ -5,6 +5,9 @@ package engine import ( + "archive/tar" + "bufio" + "bytes" "context" "io" "io/ioutil" @@ -18,6 +21,7 @@ import ( "github.com/drone/runner-go/logger" "github.com/drone/runner-go/pipeline/runtime" "github.com/drone/runner-go/registry/auths" + "github.com/sirupsen/logrus" "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/container" @@ -198,6 +202,32 @@ func (e *Docker) Run(ctx context.Context, specv runtime.Spec, stepv runtime.Step if err != nil { return nil, errors.TrimExtraInfo(err) } + + // Copy init script to container only on windows + if spec.Platform.OS == "windows" && step.Envs["SHELL"] == "cmd.exe" { + if step.Name != "clone" { + fileContent := []byte(step.Envs["DRONE_SCRIPT"]) + + hdr := &tar.Header{ + Name: "/drone/drone-build-script.cmd", + Size: int64(len(fileContent)), + } + + tarBuf := new(bytes.Buffer) + tw := tar.NewWriter(tarBuf) + + tw.WriteHeader(hdr) + io.Copy(tw, bytes.NewReader(fileContent)) + tw.Close() + + err = e.client.CopyToContainer(ctx, step.ID, "/", bufio.NewReader(tarBuf), types.CopyToContainerOptions{}) + if err != nil { + logrus.Debugln(hdr.Name + ": " + err.Error()) + } else { + logrus.Debugln("Copied build script: " + hdr.Name) + } + } + } // start the container err = e.start(ctx, step.ID) if err != nil { From ebedc9301e1419a4729f54741029b0834f321831 Mon Sep 17 00:00:00 2001 From: Yuri Date: Tue, 18 Jul 2023 17:38:09 -0500 Subject: [PATCH 3/3] Print error as error --- engine/engine.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/engine/engine.go b/engine/engine.go index 16a4301..9d270c5 100644 --- a/engine/engine.go +++ b/engine/engine.go @@ -222,7 +222,7 @@ func (e *Docker) Run(ctx context.Context, specv runtime.Spec, stepv runtime.Step err = e.client.CopyToContainer(ctx, step.ID, "/", bufio.NewReader(tarBuf), types.CopyToContainerOptions{}) if err != nil { - logrus.Debugln(hdr.Name + ": " + err.Error()) + logrus.Errorln(hdr.Name + ": " + err.Error()) } else { logrus.Debugln("Copied build script: " + hdr.Name) }