Skip to content

Commit

Permalink
Merge pull request #726 from aquaproj/feat/suppport-experimental-x-sy…
Browse files Browse the repository at this point in the history
…s-exec

feat: support AQUA_EXPERIMENTAL_X_SYS_EXEC
  • Loading branch information
suzuki-shunsuke authored May 11, 2022
2 parents b6bd86d + 51af70c commit 087af46
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ require (
github.com/mholt/archiver/v3 v3.5.1
github.com/sirupsen/logrus v1.8.1
github.com/suzuki-shunsuke/flute v1.0.1
github.com/suzuki-shunsuke/go-error-with-exit-code v1.0.0
github.com/suzuki-shunsuke/go-findconfig v1.1.0
github.com/suzuki-shunsuke/go-timeout v1.0.0
github.com/suzuki-shunsuke/logrus-error v0.1.4
github.com/urfave/cli/v2 v2.6.0
golang.org/x/oauth2 v0.0.0-20220411215720-9780585627b5
Expand Down
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ github.com/nwaples/rardecode v1.1.0/go.mod h1:5DzqNKiOdpKKBH87u8VlvAnPZMXcGRhxWk
github.com/pierrec/lz4/v4 v4.1.2 h1:qvY3YFXRQE/XB8MlLzJH7mSzBs74eA2gg52YTk6jUPM=
github.com/pierrec/lz4/v4 v4.1.2/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v0.0.0-20151028094244-d8ed2627bdf0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
Expand Down Expand Up @@ -253,10 +254,14 @@ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/suzuki-shunsuke/flute v1.0.1 h1:qzxSX2WQ8ih378zeCrjDMW5mVCsYyEmjicYuCy4aiLk=
github.com/suzuki-shunsuke/flute v1.0.1/go.mod h1:lQKcCgbjiIzH4lar2V2UX1IdEwAB5KTfTckgWN1rtFg=
github.com/suzuki-shunsuke/go-cliutil v0.0.0-20181211154308-176f852d9bca/go.mod h1:Vq3NkhgmA9DT/2UZ08x/3A34xxvzQ/vTMABnTWKoMbY=
github.com/suzuki-shunsuke/go-error-with-exit-code v1.0.0 h1:oVXrrYNGBq4POyITQNWKzwsYz7B2nUcqtDbeX4BfeEc=
github.com/suzuki-shunsuke/go-error-with-exit-code v1.0.0/go.mod h1:kDFtLeftDiIUUHXGI3xq5eJ+uAOi50FPrxPENTHktJ0=
github.com/suzuki-shunsuke/go-findconfig v1.1.0 h1:kWZHF93LPZPLrvA/GToPZPKEFzgky3tNTrYEfMpr7f8=
github.com/suzuki-shunsuke/go-findconfig v1.1.0/go.mod h1:u/0Zz6/GDE6G0gofzVhR9UPOIKLSUoDMjUoFWqOoVlg=
github.com/suzuki-shunsuke/go-jsoneq v0.1.2 h1:A4czEbmFqSELTbrEtXVo4dSgfz2e2Z0y6G3OpExUML8=
github.com/suzuki-shunsuke/go-jsoneq v0.1.2/go.mod h1:ETXAwfruZTqMMKDxc9CYoS34CNSsnzcdcVIAW3+RujI=
github.com/suzuki-shunsuke/go-timeout v1.0.0 h1:SWiJc8HuLWK4N01x76XUg75rbbwDiHiFoyPbwHcNI4Q=
github.com/suzuki-shunsuke/go-timeout v1.0.0/go.mod h1:ZX3QIig4tvTVXnZUJE3ivC9qy5TJy9Uk5sIcvwjwSNE=
github.com/suzuki-shunsuke/gomic v0.6.0 h1:oSmoXR1nmt7X05TssjBTcX9BCTNnFCcNlA5yshaTjsk=
github.com/suzuki-shunsuke/gomic v0.6.0/go.mod h1:/+uhQJ1H5f9ythus/MbXOotAZDmkN1QOjKlsSiH2v+I=
github.com/suzuki-shunsuke/logrus-error v0.1.4 h1:nWo98uba1fANHdZ9Y5pJ2RKs/PpVjrLzRp5m+mRb9KE=
Expand Down
40 changes: 35 additions & 5 deletions pkg/controller/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"io"
"os"
"os/exec"
"path/filepath"
"time"

Expand All @@ -13,6 +14,9 @@ import (
"github.com/aquaproj/aqua/pkg/installpackage"
"github.com/aquaproj/aqua/pkg/util"
"github.com/sirupsen/logrus"
"github.com/suzuki-shunsuke/go-error-with-exit-code/ecerror"
"github.com/suzuki-shunsuke/go-timeout/timeout"
"github.com/suzuki-shunsuke/logrus-error/logerr"
"golang.org/x/sys/unix"
)

Expand All @@ -22,6 +26,7 @@ type Controller struct {
stderr io.Writer
which which.Controller
packageInstaller installpackage.Installer
enabledXSysExec bool
}

func New(pkgInstaller installpackage.Installer, which which.Controller) *Controller {
Expand All @@ -31,6 +36,7 @@ func New(pkgInstaller installpackage.Installer, which which.Controller) *Control
stderr: os.Stderr,
packageInstaller: pkgInstaller,
which: which,
enabledXSysExec: os.Getenv("AQUA_EXPERIMENTAL_X_SYS_EXEC") == "true",
}
}

Expand Down Expand Up @@ -79,14 +85,38 @@ var errFailedToStartProcess = errors.New("it failed to start the process")

func (ctrl *Controller) execCommand(ctx context.Context, exePath string, args []string, logE *logrus.Entry) error {
logE = logE.WithField("exe_path", exePath)

for i := 0; i < 10; i++ {
logE.Debug("execute the command")
if err := unix.Exec(exePath, append([]string{filepath.Base(exePath)}, args...), os.Environ()); err != nil {
logE.WithField("retry_count", i+1).Debug("the process isn't started. retry")
if err := wait(ctx, 10*time.Millisecond); err != nil { //nolint:gomnd
return err
if ctrl.enabledXSysExec {
if err := unix.Exec(exePath, append([]string{filepath.Base(exePath)}, args...), os.Environ()); err != nil {
logE.WithField("retry_count", i+1).Debug("the process isn't started. retry")
if err := wait(ctx, 10*time.Millisecond); err != nil { //nolint:gomnd
return err
}
continue
}
return nil
}
cmd := exec.Command(exePath, args...)
cmd.Stdin = ctrl.stdin
cmd.Stdout = ctrl.stdout
cmd.Stderr = ctrl.stderr
runner := timeout.NewRunner(0)
if err := runner.Run(ctx, cmd); err != nil {
exitCode := cmd.ProcessState.ExitCode()
// https://pkg.go.dev/os#ProcessState.ExitCode
// > ExitCode returns the exit code of the exited process,
// > or -1 if the process hasn't exited or was terminated by a signal.
if exitCode == -1 && ctx.Err() == nil {
logE.WithField("retry_count", i+1).Debug("the process isn't started. retry")
if err := wait(ctx, 10*time.Millisecond); err != nil { //nolint:gomnd
return err
}
continue
}
continue
logerr.WithError(logE, err).WithField("exit_code", exitCode).Debug("command was executed but it failed")
return ecerror.Wrap(err, exitCode)
}
return nil
}
Expand Down

0 comments on commit 087af46

Please sign in to comment.