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

feat: Commands should fail the build if their exit code is not zero #534

Merged
merged 1 commit into from
Jan 22, 2024

Conversation

Fran-Rg
Copy link
Contributor

@Fran-Rg Fran-Rg commented Jan 17, 2024

Description

When running with some source_path setting a command:

source_path = [
        {
          "commands" = XYZ
        }

It might happen the commands fails and we want to fail the terraform deployment

Motivation and Context

Before packaging we needed to set some auth token for npm resources. In some rare cases the authentication failed but the build carried on.

Breaking Changes

No

How Has This Been Tested?

  • I have updated at least one of the examples/* to demonstrate and validate my change(s)
  • I have tested and validated these changes using one or more of the provided examples/* projects
  • I have executed pre-commit run -a on my pull request

@Fran-Rg Fran-Rg changed the title commands to fail on non returning 0 scripts fix: commands to fail on non returning 0 scripts Jan 17, 2024
@Fran-Rg Fran-Rg changed the title fix: commands to fail on non returning 0 scripts Fix: commands to fail on non returning 0 scripts Jan 17, 2024
Copy link
Member

@antonbabenko antonbabenko left a comment

Choose a reason for hiding this comment

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

I am just double-checking with you, will we also see the text of the error?

@pdecat WDYT?

@antonbabenko antonbabenko changed the title Fix: commands to fail on non returning 0 scripts feat: Commands should fail the build if their exit code is not zero Jan 17, 2024
Copy link
Contributor

@pdecat pdecat left a comment

Choose a reason for hiding this comment

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

Without any testing, the changes look good to me.

However, I'm a bit confused about the closing of the w file descriptor right above.

package.py Outdated
exit_code = p.wait()
if exit_code != 0:
raise RuntimeError(
"Script did not run successfully"
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd trace the exit code for easier debugging:

Suggested change
"Script did not run successfully"
"Script did not run successfully, exit code {}".format(exit_code)

package.py Outdated
@@ -893,7 +893,11 @@ def execute(self, build_plan, zip_stream, query):
pass_fds=(w,))
os.close(w)
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't this be closed after p.wait() has returned?

@Fran-Rg
Copy link
Contributor Author

Fran-Rg commented Jan 18, 2024

I don't understand why the test is failing on Github Action. Works fine locally.

@antonbabenko
Copy link
Member

@Fran-Rg I think you need to fix the conflicts, and then GitHub can restart it automatically. If it doesn't happen by itself, I can hit the restart button manually.

@Fran-Rg
Copy link
Contributor Author

Fran-Rg commented Jan 19, 2024

I ended up reformatting the code as the fd wasn't being used. Outputs get shown on failure

@pdecat
Copy link
Contributor

pdecat commented Jan 19, 2024

I ended up reformatting the code as the fd wasn't being used.

The r and w file descriptors from the pipe were used, but I believe w was closed too early which may have caused issues if the command was too slow to complete.

If we look at this code fragment from v6.8.0:

            elif cmd == 'sh':
                r, w = os.pipe()
                side_ch = os.fdopen(r)
                path, script = action[1:]
                script = "{}\npwd >&{}".format(script, w)

                p = subprocess.Popen(script, shell=True, cwd=path,
                                     pass_fds=(w,))
                os.close(w)
                sh_work_dir = side_ch.read().strip()
                p.wait()
                log.info('WD: %s', sh_work_dir)
                side_ch.close()

w is passed to the sub-process and written to with the pwd >&{} redirection.

The result of pwd is then read from the other end of the pipe with side_ch.read().

@Fran-Rg
Copy link
Contributor Author

Fran-Rg commented Jan 19, 2024

But then the result of pwd is never used unless I'm missing something

@pdecat
Copy link
Contributor

pdecat commented Jan 19, 2024

But then the result of pwd is never used unless I'm missing something

It was just logged in log.info('WD: %s', sh_work_dir)

@Fran-Rg
Copy link
Contributor Author

Fran-Rg commented Jan 19, 2024

I believe this is makes it not powershell friendly since set -e is only linux friendly, $ErrorActionPreference="Stop" needs to be used instead for powershell

@pdecat
Copy link
Contributor

pdecat commented Jan 19, 2024

In my opinion, passing set -e or anything else should be left to the user of the terraform module.

@Fran-Rg
Copy link
Contributor Author

Fran-Rg commented Jan 19, 2024

That is right, I've added a comment. So initially what was causing the exit code not caring about the actual problem was the "pwd" being appended. Since it's for a log only removing it cleared the problem

Copy link
Contributor

@pdecat pdecat left a comment

Choose a reason for hiding this comment

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

Awesome, thanks for adding the unit tests!

I'll try this PR ASAP.

tests/test_package_toml.py Outdated Show resolved Hide resolved
@pdecat
Copy link
Contributor

pdecat commented Jan 20, 2024

Maybe rebase/squash some git commits to cleanup history.

@antonbabenko antonbabenko merged commit eebfc36 into terraform-aws-modules:master Jan 22, 2024
30 checks passed
antonbabenko pushed a commit that referenced this pull request Jan 22, 2024
## [7.1.0](v7.0.0...v7.1.0) (2024-01-22)

### Features

* Commands should fail the build if their exit code is not zero ([#534](#534)) ([eebfc36](eebfc36))
@antonbabenko
Copy link
Member

This PR is included in version 7.1.0 🎉

@antonbabenko
Copy link
Member

There was no need to prepare git history because we always squash commits during PR merge.

Thank you!

@sohaibiftikhar
Copy link

Unfortunately after this change debugging of custom commands becomes a lot harder since the logs from stdout/stderr are printed only on failure. In addition before we had streaming logs due to the pipes. Now the stderr and out are separated out which makes it harder to correlate them.

Also I believe the wrong logger is used? I had to change log.info to self._log.info to see exit code results.

@p0fi
Copy link

p0fi commented Jan 22, 2024

Just for completeness: I believe this PR finally fixes #388

Copy link

I'm going to lock this pull request because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you have found a problem that seems related to this change, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 22, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants