From bde3d445b64f5bd2e0a4183ea9e17f3e626adf03 Mon Sep 17 00:00:00 2001 From: Denis Vaumoron Date: Tue, 28 Jan 2025 20:48:34 +0100 Subject: [PATCH] transmit signal only windows (#341) * discard signal on non windows OS * update README (no more detached behavior since discarding signal on non windows OS) Signed-off-by: Denis Vaumoron --- README.md | 9 ---- config/envname/env.go | 20 ++++---- pkg/cmdproxy/proxy.go | 12 ----- pkg/cmdproxy/transmit_others.go | 29 +++++++++++ pkg/cmdproxy/transmit_windows.go | 33 ++++++++++++ versionmanager/proxy/agnostic.go | 9 ---- .../proxy/detach/detached_others.go | 50 ------------------- .../proxy/detach/detached_windows.go | 42 ---------------- versionmanager/proxy/light/light.go | 9 ---- ...ustom_env_others.go => transmit_others.go} | 20 ++------ ...tom_env_windows.go => transmit_windows.go} | 8 +-- versionmanager/proxy/proxy.go | 17 +------ 12 files changed, 82 insertions(+), 176 deletions(-) create mode 100644 pkg/cmdproxy/transmit_others.go create mode 100644 pkg/cmdproxy/transmit_windows.go delete mode 100644 versionmanager/proxy/detach/detached_others.go delete mode 100644 versionmanager/proxy/detach/detached_windows.go rename versionmanager/proxy/light/{custom_env_others.go => transmit_others.go} (65%) rename versionmanager/proxy/light/{custom_env_windows.go => transmit_windows.go} (80%) diff --git a/README.md b/README.md index 215f39f..a392498 100644 --- a/README.md +++ b/README.md @@ -658,15 +658,6 @@ If set to true **tenv** will automatically install missing tool versions needed. -
TENV_DETACHED_PROXY
- -String (Default: false on Windows OS, true on other OS without CI or PIPELINE_WORKSPACE environment variable) - -Enable **tenv** proxies detached behaviour (use a new process group id when launching tool command, allow to avoid killing proxied tool process earlier than expected when proxies redirect interrupt signal already sended to whole process group (issue occurrences known on macOS and Linux)). - -
- -
TENV_FORCE_REMOTE
String (Default: false) diff --git a/config/envname/env.go b/config/envname/env.go index e6663d6..f79dd22 100644 --- a/config/envname/env.go +++ b/config/envname/env.go @@ -48,17 +48,15 @@ const ( AtmosRemoteURL = AtmosPrefix + remoteURL AtmosRemoteUser = AtmosPrefix + remoteUser - tenvPrefix = "TENV_" - TenvArch = tenvPrefix + arch - TenvAutoInstall = tenvPrefix + autoInstall - TenvDetachedProxy = tenvPrefix + "DETACHED_PROXY" - TenvDetachedProxyDefault = TenvDetachedProxy + "_DEFAULT" - TenvForceRemote = tenvPrefix + forceRemote - TenvLog = tenvPrefix + log - TenvQuiet = tenvPrefix + quiet - TenvRemoteConf = tenvPrefix + "REMOTE_CONF" - TenvRootPath = tenvPrefix + rootPath - TenvToken = tenvPrefix + token + tenvPrefix = "TENV_" + TenvArch = tenvPrefix + arch + TenvAutoInstall = tenvPrefix + autoInstall + TenvForceRemote = tenvPrefix + forceRemote + TenvLog = tenvPrefix + log + TenvQuiet = tenvPrefix + quiet + TenvRemoteConf = tenvPrefix + "REMOTE_CONF" + TenvRootPath = tenvPrefix + rootPath + TenvToken = tenvPrefix + token TfenvPrefix = "TFENV_" tfenvTerraformPrefix = TfenvPrefix + "TERRAFORM_" diff --git a/pkg/cmdproxy/proxy.go b/pkg/cmdproxy/proxy.go index 2462890..4a00d7d 100644 --- a/pkg/cmdproxy/proxy.go +++ b/pkg/cmdproxy/proxy.go @@ -130,18 +130,6 @@ func initIO(cmd *exec.Cmd, pExitCode *int, gha bool, getenv configutils.GetenvFu } } -func transmitIncreasingSignal(signalReceiver <-chan os.Signal, process *os.Process) { - first := true - for range signalReceiver { - if first { - _ = process.Signal(os.Interrupt) - first = false - } else { - _ = process.Signal(os.Kill) - } - } -} - func writeMultiline(file *os.File, key string, value string) error { delimiter := "ghadelimeter_" + strconv.Itoa(rand.Int()) //nolint if strings.Contains(key, delimiter) || strings.Contains(value, delimiter) { diff --git a/pkg/cmdproxy/transmit_others.go b/pkg/cmdproxy/transmit_others.go new file mode 100644 index 0000000..3ef83f4 --- /dev/null +++ b/pkg/cmdproxy/transmit_others.go @@ -0,0 +1,29 @@ +//go:build !windows + +/* + * + * Copyright 2024 tofuutils authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package cmdproxy + +import "os" + +func transmitIncreasingSignal(signalReceiver <-chan os.Signal, _ *os.Process) { + for range signalReceiver { //nolint + // discard signals on non Windows OS (already send to whole process group) + } +} diff --git a/pkg/cmdproxy/transmit_windows.go b/pkg/cmdproxy/transmit_windows.go new file mode 100644 index 0000000..da2a0d9 --- /dev/null +++ b/pkg/cmdproxy/transmit_windows.go @@ -0,0 +1,33 @@ +/* + * + * Copyright 2024 tofuutils authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package cmdproxy + +import "os" + +func transmitIncreasingSignal(signalReceiver <-chan os.Signal, process *os.Process) { + first := true + for range signalReceiver { + if first { + _ = process.Signal(os.Interrupt) + first = false + } else { + _ = process.Signal(os.Kill) + } + } +} diff --git a/versionmanager/proxy/agnostic.go b/versionmanager/proxy/agnostic.go index d90a4a1..5410c2c 100644 --- a/versionmanager/proxy/agnostic.go +++ b/versionmanager/proxy/agnostic.go @@ -28,10 +28,8 @@ import ( "github.com/tofuutils/tenv/v4/config" "github.com/tofuutils/tenv/v4/config/cmdconst" - "github.com/tofuutils/tenv/v4/config/envname" cmdproxy "github.com/tofuutils/tenv/v4/pkg/cmdproxy" "github.com/tofuutils/tenv/v4/versionmanager/builder" - detachproxy "github.com/tofuutils/tenv/v4/versionmanager/proxy/detach" ) // Always call os.Exit. @@ -80,12 +78,5 @@ func ExecAgnostic(conf *config.Config, hclParser *hclparse.Parser, cmdArgs []str cmd := exec.CommandContext(ctx, execPath, cmdArgs...) - defaultDetach, err := conf.Getenv.Bool(false, envname.TenvDetachedProxyDefault) - if err != nil { - fmt.Println(msgReadDefaultDetachErr, err) //nolint - } - - detachproxy.InitBehaviorFromEnv(cmd, conf.Getenv, defaultDetach) - cmdproxy.Run(cmd, conf.GithubActions, conf.Getenv) } diff --git a/versionmanager/proxy/detach/detached_others.go b/versionmanager/proxy/detach/detached_others.go deleted file mode 100644 index 39d2f1c..0000000 --- a/versionmanager/proxy/detach/detached_others.go +++ /dev/null @@ -1,50 +0,0 @@ -//go:build !windows - -/* - * - * Copyright 2024 tofuutils authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -package detachproxy - -import ( - "fmt" - "os/exec" - "syscall" - - "github.com/tofuutils/tenv/v4/config/envname" - configutils "github.com/tofuutils/tenv/v4/config/utils" -) - -const msgDisableStart = "Failed to read " + envname.TenvDetachedProxy + " environment variable, use" - -func InitBehaviorFromEnv(cmd *exec.Cmd, getenv configutils.GetenvFunc, defaultDetached bool) { - detached, err := getenv.Bool(defaultDetached, envname.TenvDetachedProxy) - if err != nil { - fmt.Println(msgDisableStart, defaultDetached, ":", err) //nolint - } - - if !detached { - return - } - - if cmd.SysProcAttr == nil { - cmd.SysProcAttr = &syscall.SysProcAttr{} - } - - cmd.SysProcAttr.Setpgid = true - cmd.SysProcAttr.Foreground = true -} diff --git a/versionmanager/proxy/detach/detached_windows.go b/versionmanager/proxy/detach/detached_windows.go deleted file mode 100644 index 0321b0b..0000000 --- a/versionmanager/proxy/detach/detached_windows.go +++ /dev/null @@ -1,42 +0,0 @@ -/* - * - * Copyright 2024 tofuutils authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -package detachproxy - -import ( - "fmt" - "os/exec" - - "github.com/tofuutils/tenv/v4/config/envname" - configutils "github.com/tofuutils/tenv/v4/config/utils" -) - -const ( - msgFailReadErr = msgStart + "failed to read environment variable :" - msgNotApplied = msgStart + "can not apply environment variable" - msgStart = envname.TenvDetachedProxy + " behavior is always disabled on Windows OS, " -) - -func InitBehaviorFromEnv(_ *exec.Cmd, getenv configutils.GetenvFunc, defaultDetached bool) { - switch detached, err := getenv.Bool(defaultDetached, envname.TenvDetachedProxy); { - case err != nil: - fmt.Println(msgFailReadErr, err) //nolint - case detached: - fmt.Println(msgNotApplied) //nolint - } -} diff --git a/versionmanager/proxy/light/light.go b/versionmanager/proxy/light/light.go index 0677009..f58bbe7 100644 --- a/versionmanager/proxy/light/light.go +++ b/versionmanager/proxy/light/light.go @@ -26,7 +26,6 @@ import ( "os/signal" "github.com/tofuutils/tenv/v4/config/cmdconst" - detachproxy "github.com/tofuutils/tenv/v4/versionmanager/proxy/detach" ) func Exec(execName string) { @@ -36,8 +35,6 @@ func Exec(execName string) { // proxy to selected version cmd := exec.Command(cmdconst.TenvName, cmdArgs...) //nolint - defaultDetach := updateDefaultDetachInCmdEnv(cmd) - detachproxy.InitBehaviorFromEnv(cmd, os.Getenv, defaultDetach) cmd.Stderr = os.Stderr cmd.Stdin = os.Stdin cmd.Stdout = os.Stdout @@ -63,9 +60,3 @@ func exitWithErrorMsg(execName string, err error) { fmt.Println("Failure during", execName, "call :", err) //nolint os.Exit(1) } - -func transmitSignal(signalReceiver <-chan os.Signal, process *os.Process) { - for range signalReceiver { - _ = process.Signal(os.Interrupt) - } -} diff --git a/versionmanager/proxy/light/custom_env_others.go b/versionmanager/proxy/light/transmit_others.go similarity index 65% rename from versionmanager/proxy/light/custom_env_others.go rename to versionmanager/proxy/light/transmit_others.go index 0c9ba1b..c45a976 100644 --- a/versionmanager/proxy/light/custom_env_others.go +++ b/versionmanager/proxy/light/transmit_others.go @@ -20,22 +20,10 @@ package lightproxy -import ( - "os" - "os/exec" +import "os" - "github.com/tofuutils/tenv/v4/config/envname" - "github.com/tofuutils/tenv/v4/pkg/tty" -) - -const changeDefaultDetach = envname.TenvDetachedProxyDefault + "=true" - -func updateDefaultDetachInCmdEnv(cmd *exec.Cmd) bool { - if tty.Detect() { - cmd.Env = append(os.Environ(), changeDefaultDetach) - - return true +func transmitSignal(signalReceiver <-chan os.Signal, _ *os.Process) { + for range signalReceiver { //nolint + // discard signals on non Windows OS (already send to whole process group) } - - return false } diff --git a/versionmanager/proxy/light/custom_env_windows.go b/versionmanager/proxy/light/transmit_windows.go similarity index 80% rename from versionmanager/proxy/light/custom_env_windows.go rename to versionmanager/proxy/light/transmit_windows.go index bfdcd87..003e68d 100644 --- a/versionmanager/proxy/light/custom_env_windows.go +++ b/versionmanager/proxy/light/transmit_windows.go @@ -18,8 +18,10 @@ package lightproxy -import "os/exec" +import "os" -func updateDefaultDetachInCmdEnv(_ *exec.Cmd) bool { - return false +func transmitSignal(signalReceiver <-chan os.Signal, process *os.Process) { + for range signalReceiver { + _ = process.Signal(os.Interrupt) + } } diff --git a/versionmanager/proxy/proxy.go b/versionmanager/proxy/proxy.go index 54ae91d..9e05c40 100644 --- a/versionmanager/proxy/proxy.go +++ b/versionmanager/proxy/proxy.go @@ -29,19 +29,13 @@ import ( "github.com/hashicorp/hcl/v2/hclparse" "github.com/tofuutils/tenv/v4/config" - "github.com/tofuutils/tenv/v4/config/envname" - cmdproxy "github.com/tofuutils/tenv/v4/pkg/cmdproxy" + "github.com/tofuutils/tenv/v4/pkg/cmdproxy" "github.com/tofuutils/tenv/v4/pkg/loghelper" "github.com/tofuutils/tenv/v4/versionmanager/builder" "github.com/tofuutils/tenv/v4/versionmanager/lastuse" - detachproxy "github.com/tofuutils/tenv/v4/versionmanager/proxy/detach" ) -const ( - chdirFlagPrefix = "-chdir=" - - msgReadDefaultDetachErr = "Failed to read " + envname.TenvDetachedProxyDefault + " environment variable, default to false :" -) +const chdirFlagPrefix = "-chdir=" // Always call os.Exit. func Exec(conf *config.Config, builderFunc builder.Func, hclParser *hclparse.Parser, execName string, cmdArgs []string) { @@ -67,13 +61,6 @@ func Exec(conf *config.Config, builderFunc builder.Func, hclParser *hclparse.Par cmd := exec.CommandContext(ctx, execPath, cmdArgs...) - defaultDetach, err := conf.Getenv.Bool(false, envname.TenvDetachedProxyDefault) - if err != nil { - fmt.Println(msgReadDefaultDetachErr, err) //nolint - } - - detachproxy.InitBehaviorFromEnv(cmd, conf.Getenv, defaultDetach) - cmdproxy.Run(cmd, conf.GithubActions, conf.Getenv) }