Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Envman functions #819

Merged
merged 12 commits into from
Oct 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 8 additions & 16 deletions cli/run_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -450,11 +450,11 @@ func runStep(
fmt.Errorf("Failed to install Step dependency, error: %s", err)
}

if err := tools.EnvmanInitAtPath(configs.InputEnvstorePath); err != nil {
if err := tools.EnvmanInit(configs.InputEnvstorePath, true); err != nil {
return 1, []envmanModels.EnvironmentItemModel{}, err
}

if err := tools.ExportEnvironmentsList(configs.InputEnvstorePath, environments); err != nil {
if err := tools.EnvmanAddEnvs(configs.InputEnvstorePath, environments); err != nil {
return 1, []envmanModels.EnvironmentItemModel{}, err
}

Expand Down Expand Up @@ -618,13 +618,13 @@ func activateAndRunSteps(

//
// Preparing the step
if err := tools.EnvmanInitAtPath(configs.InputEnvstorePath); err != nil {
if err := tools.EnvmanInit(configs.InputEnvstorePath, true); err != nil {
runResultCollector.registerStepRunResults(&buildRunResults, stepStartTime, stepmanModels.StepModel{}, stepInfoPtr, stepIdxPtr,
"", models.StepRunStatusCodePreparationFailed, 1, err, isLastStep, true, map[string]string{}, stepStartedProperties)
continue
}

if err := tools.ExportEnvironmentsList(configs.InputEnvstorePath, *environments); err != nil {
if err := tools.EnvmanAddEnvs(configs.InputEnvstorePath, *environments); err != nil {
runResultCollector.registerStepRunResults(&buildRunResults, stepStartTime, stepmanModels.StepModel{}, stepInfoPtr, stepIdxPtr,
"", models.StepRunStatusCodePreparationFailed, 1, err, isLastStep, true, map[string]string{}, stepStartedProperties)
continue
Expand Down Expand Up @@ -706,18 +706,10 @@ func activateAndRunSteps(
// Run step
bitrise.PrintRunningStepHeader(stepInfoPtr, mergedStep, idx)
if mergedStep.RunIf != nil && *mergedStep.RunIf != "" {
outStr, err := tools.EnvmanJSONPrint(configs.InputEnvstorePath)
envList, err := tools.EnvmanReadEnvList(configs.InputEnvstorePath)
if err != nil {
runResultCollector.registerStepRunResults(&buildRunResults, stepStartTime, mergedStep, stepInfoPtr, stepIdxPtr,
*mergedStep.RunIf, models.StepRunStatusCodePreparationFailed, 1, fmt.Errorf("EnvmanJSONPrint failed, err: %s", err),
isLastStep, false, map[string]string{}, stepStartedProperties)
continue
}

envList, err := envmanModels.NewEnvJSONList(outStr)
if err != nil {
runResultCollector.registerStepRunResults(&buildRunResults, stepStartTime, mergedStep, stepInfoPtr, stepIdxPtr,
*mergedStep.RunIf, models.StepRunStatusCodePreparationFailed, 1, fmt.Errorf("CreateFromJSON failed, err: %s", err),
*mergedStep.RunIf, models.StepRunStatusCodePreparationFailed, 1, fmt.Errorf("EnvmanReadEnvList failed, err: %s", err),
isLastStep, false, map[string]string{}, stepStartedProperties)
continue
}
Expand Down Expand Up @@ -977,8 +969,8 @@ func runWorkflowWithConfiguration(
return models.BuildRunResultsModel{}, fmt.Errorf("Failed to add env, err: %s", err)
}

if err := tools.EnvmanInit(); err != nil {
return models.BuildRunResultsModel{}, errors.New("Failed to run envman init")
if err := tools.EnvmanInit(configs.OutputEnvstorePath, false); err != nil {
return models.BuildRunResultsModel{}, fmt.Errorf("Failed to run envman init: %s", err)
}

// App level environment
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/bitrise-io/bitrise
go 1.16

require (
github.com/bitrise-io/envman v0.0.0-20220401145857-d11e00a5dc55
github.com/bitrise-io/envman v0.0.0-20221010094751-a03ce30a5316
github.com/bitrise-io/go-utils v1.0.2
github.com/bitrise-io/go-utils/v2 v2.0.0-alpha.10
github.com/bitrise-io/goinp v0.0.0-20211005113137-305e91b481f4
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03
github.com/bitrise-io/colorstring v0.0.0-20180614154802-a8cd70115192 h1:vSHYT6kCL/iOT9BVuUPm0tVcbW57r5zldLWg0aB7qbQ=
github.com/bitrise-io/colorstring v0.0.0-20180614154802-a8cd70115192/go.mod h1:CIHVcxZUvsG99XUJV6JlR7okNsMMGY81jMvPC20W+O0=
github.com/bitrise-io/envman v0.0.0-20211026063720-03283f9c3f32/go.mod h1:L4WQyg88d87Z4dxNwrYEa0Cwd9/W0gSfXsibw30r8Vw=
github.com/bitrise-io/envman v0.0.0-20220401145857-d11e00a5dc55 h1:uU2j30ndTLUdtkAw/RicxczXlOiSh2Ng/if9D/1xtp8=
github.com/bitrise-io/envman v0.0.0-20220401145857-d11e00a5dc55/go.mod h1:L4WQyg88d87Z4dxNwrYEa0Cwd9/W0gSfXsibw30r8Vw=
github.com/bitrise-io/envman v0.0.0-20221010094751-a03ce30a5316 h1:gRQReCvmNxUVvm71SJYpx8d6wz4ln6ChapNkDStco58=
github.com/bitrise-io/envman v0.0.0-20221010094751-a03ce30a5316/go.mod h1:L4WQyg88d87Z4dxNwrYEa0Cwd9/W0gSfXsibw30r8Vw=
github.com/bitrise-io/go-utils v0.0.0-20200224122728-e212188d99b4/go.mod h1:tTEsKvbz1LbzuN/KpVFHXnLtcAPdEgIdM41s0lL407s=
github.com/bitrise-io/go-utils v0.0.0-20210505121718-07411d72e36e/go.mod h1:nhdaDQFvaMny1CugVV6KjK92/q97ENo0RuKSW5I4fbA=
github.com/bitrise-io/go-utils v1.0.1/go.mod h1:ZY1DI+fEpZuFpO9szgDeICM4QbqoWVt0RSY3tRI1heY=
Expand Down
2 changes: 1 addition & 1 deletion plugins/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func runPlugin(plugin Plugin, args []string, envs PluginConfig, input []byte) er

pluginEnvstorePath := filepath.Join(pluginWorkDir, "envstore.yml")

if err := tools.EnvmanInitAtPath(pluginEnvstorePath); err != nil {
if err := tools.EnvmanInit(pluginEnvstorePath, true); err != nil {
return err
}

Expand Down
11 changes: 3 additions & 8 deletions tools/timeoutcmd/timeoutcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,9 @@ func (c *Command) SetHangTimeout(timeout time.Duration) {
}
}

// AppendEnv appends and env to the command's env list.
func (c *Command) AppendEnv(env string) {
if c.cmd.Env != nil {
c.cmd.Env = append(c.cmd.Env, env)
return
}

c.cmd.Env = append(os.Environ(), env)
// SetEnv sets the command's env list.
func (c *Command) SetEnv(env []string) {
c.cmd.Env = env
}

// SetStandardIO sets the input and outputs of the command.
Expand Down
123 changes: 43 additions & 80 deletions tools/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,18 @@ import (
"io/ioutil"
"net/http"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"time"

"github.com/bitrise-io/bitrise/configs"
"github.com/bitrise-io/bitrise/tools/errorfinder"
"github.com/bitrise-io/bitrise/tools/filterwriter"
"github.com/bitrise-io/bitrise/tools/timeoutcmd"
envman "github.com/bitrise-io/envman/cli"
envmanEnv "github.com/bitrise-io/envman/env"
envmanModels "github.com/bitrise-io/envman/models"
"github.com/bitrise-io/go-utils/command"
"github.com/bitrise-io/go-utils/errorutil"
"github.com/bitrise-io/go-utils/pathutil"
log "github.com/sirupsen/logrus"
"golang.org/x/sys/unix"
Expand Down Expand Up @@ -265,43 +264,17 @@ func StepmanShareStart(collection string) error {
// --- Envman

// EnvmanInit ...
func EnvmanInit() error {
logLevel := log.GetLevel().String()
args := []string{"--loglevel", logLevel, "init"}
return command.RunCommand("envman", args...)
}

// EnvmanInitAtPath ...
func EnvmanInitAtPath(envstorePth string) error {
logLevel := log.GetLevel().String()
args := []string{"--loglevel", logLevel, "--path", envstorePth, "init", "--clear"}
return command.RunCommand("envman", args...)
func EnvmanInit(envStorePth string, clear bool) error {
return envman.InitEnvStore(envStorePth, clear)
}

// EnvmanAdd ...
func EnvmanAdd(envstorePth, key, value string, expand, skipIfEmpty, sensitive bool) error {
logLevel := log.GetLevel().String()
args := []string{"--loglevel", logLevel, "--path", envstorePth, "add", "--key", key, "--append"}
if !expand {
args = append(args, "--no-expand")
}
if skipIfEmpty {
args = append(args, "--skip-if-empty")
}

if configs.IsSecretEnvsFiltering && sensitive {
args = append(args, "--sensitive")
}

envman := exec.Command("envman", args...)
envman.Stdin = strings.NewReader(value)
envman.Stdout = os.Stdout
envman.Stderr = os.Stderr
return envman.Run()
func EnvmanAdd(envStorePth, key, value string, expand, skipIfEmpty, sensitive bool) error {
return envman.AddEnv(envStorePth, key, value, expand, false, skipIfEmpty, sensitive)
}

// ExportEnvironmentsList ...
func ExportEnvironmentsList(envstorePth string, envsList []envmanModels.EnvironmentItemModel) error {
// EnvmanAddEnvs ...
func EnvmanAddEnvs(envstorePth string, envsList []envmanModels.EnvironmentItemModel) error {
for _, env := range envsList {
key, value, err := env.GetKeyValuePair()
if err != nil {
Expand Down Expand Up @@ -335,50 +308,29 @@ func ExportEnvironmentsList(envstorePth string, envsList []envmanModels.Environm
return nil
}

// EnvmanClear ...
func EnvmanClear(envstorePth string) error {
logLevel := log.GetLevel().String()
args := []string{"--loglevel", logLevel, "--path", envstorePth, "clear"}
out, err := command.New("envman", args...).RunAndReturnTrimmedCombinedOutput()
if err != nil {
errorMsg := err.Error()
if errorutil.IsExitStatusError(err) && out != "" {
errorMsg = out
}
return fmt.Errorf("failed to clear envstore (%s), error: %s", envstorePth, errorMsg)
}
return nil
// EnvmanReadEnvList ...
func EnvmanReadEnvList(envStorePth string) (envmanModels.EnvsJSONListModel, error) {
return envman.ReadEnvsJSONList(envStorePth, true, false, &envmanEnv.DefaultEnvironmentSource{})
}

// GetSecretValues filters out built in configuration parameters from the secret envs
func GetSecretValues(secrets []envmanModels.EnvironmentItemModel) []string {
var secretValues []string
for _, secret := range secrets {
key, value, err := secret.GetKeyValuePair()
if err != nil || len(value) < 1 || IsBuiltInFlagTypeKey(key) {
if err != nil {
log.Warnf("Error getting key-value pair from secret (%v): %s", secret, err)
}
continue
}
secretValues = append(secretValues, value)
}

return secretValues
// EnvmanClear ...
func EnvmanClear(envStorePth string) error {
return envman.ClearEnvs(envStorePth)
}

// EnvmanRun runs a command through envman.
func EnvmanRun(envstorePth,
func EnvmanRun(envStorePth,
workDirPth string,
cmdArgs []string,
timeout time.Duration,
noOutputTimeout time.Duration,
secrets []string,
stdInPayload []byte,
) (int, error) {
logLevel := log.GetLevel().String()
args := []string{"--loglevel", logLevel, "--path", envstorePth, "run"}
args = append(args, cmdArgs...)
envs, err := envman.ReadAndEvaluateEnvs(envStorePth, &envmanEnv.DefaultEnvironmentSource{})
if err != nil {
return 1, err
}
tothszabi marked this conversation as resolved.
Show resolved Hide resolved

var inReader io.Reader
var outWriter io.Writer
Expand All @@ -400,13 +352,19 @@ func EnvmanRun(envstorePth,
inReader = bytes.NewReader(stdInPayload)
}

cmd := timeoutcmd.New(workDirPth, "envman", args...)
name := cmdArgs[0]
var args []string
if len(cmdArgs) > 1 {
args = cmdArgs[1:]
}

cmd := timeoutcmd.New(workDirPth, name, args...)
cmd.SetTimeout(timeout)
cmd.SetHangTimeout(noOutputTimeout)
cmd.SetStandardIO(inReader, outWriter, errWriter)
cmd.AppendEnv("PWD=" + workDirPth)
cmd.SetEnv(append(envs, "PWD="+workDirPth))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this better than the existing AppendEnv() call?

Copy link
Contributor Author

@godrei godrei Oct 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AppendEnv is working with a single environment variable + checking if the command has Env set or not is unnecessary because it will always be nil, and the envs we set already contain the os.Environ.

Although we don't need both functions, so I removed cmd.AppendEnv in 73afe9d


err := cmd.Start()
err = cmd.Start()

// flush the writer anyway if the process is finished
if configs.IsSecretFiltering {
Expand All @@ -419,19 +377,24 @@ func EnvmanRun(envstorePth,
return timeoutcmd.ExitStatus(err), errorFinder.WrapError(err)
}

// EnvmanJSONPrint ...
func EnvmanJSONPrint(envstorePth string) (string, error) {
logLevel := log.GetLevel().String()
args := []string{"--loglevel", logLevel, "--path", envstorePth, "print", "--format", "json", "--expand"}

var outBuffer bytes.Buffer
var errBuffer bytes.Buffer
// ------------------
// --- Utility

if err := command.RunCommandWithWriters(io.Writer(&outBuffer), io.Writer(&errBuffer), "envman", args...); err != nil {
return outBuffer.String(), fmt.Errorf("Error: %s, details: %s", err, errBuffer.String())
// GetSecretValues filters out built in configuration parameters from the secret envs
func GetSecretValues(secrets []envmanModels.EnvironmentItemModel) []string {
var secretValues []string
for _, secret := range secrets {
key, value, err := secret.GetKeyValuePair()
if err != nil || len(value) < 1 || IsBuiltInFlagTypeKey(key) {
if err != nil {
log.Warnf("Error getting key-value pair from secret (%v): %s", secret, err)
}
continue
}
secretValues = append(secretValues, value)
}

return outBuffer.String(), nil
return secretValues
}

// MoveFile ...
Expand Down
14 changes: 8 additions & 6 deletions tools/tools_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"runtime"
"testing"

"github.com/bitrise-io/envman/models"

"github.com/bitrise-io/bitrise/configs"
"github.com/bitrise-io/go-utils/command"
"github.com/bitrise-io/go-utils/pathutil"
Expand Down Expand Up @@ -95,19 +97,19 @@ func TestEnvmanJSONPrint(t *testing.T) {

envstorePth := filepath.Join(testDirPth, "envstore.yml")

require.Equal(t, nil, EnvmanInitAtPath(envstorePth))
require.Equal(t, nil, EnvmanInit(envstorePth, true))

outStr, err := EnvmanJSONPrint(envstorePth)
out, err := EnvmanReadEnvList(envstorePth)
require.NoError(t, err)
require.NotEqual(t, "", outStr)
require.Equal(t, models.EnvsJSONListModel{}, out)

// Not initialized envstore -- Err should filled, output empty
testDirPth, err = pathutil.NormalizedOSTempDirPath("test_env_store")
require.NoError(t, err)

envstorePth = filepath.Join("test_env_store", "envstore.yml")
envstorePth = filepath.Join(testDirPth, "envstore.yml")

outStr, err = EnvmanJSONPrint(envstorePth)
out, err = EnvmanReadEnvList(envstorePth)
require.NotEqual(t, nil, err)
require.Equal(t, "", outStr)
require.Nil(t, out)
}
Loading