Skip to content

Review buildkite scripts cleanup process #1487

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

Merged
merged 16 commits into from
Oct 19, 2023

Conversation

mrodm
Copy link
Contributor

@mrodm mrodm commented Oct 4, 2023

No description provided.

@mrodm mrodm self-assigned this Oct 4, 2023
@@ -87,12 +95,11 @@ upload_safe_logs() {
return
fi

local gsUtilLocation=$(google_cloud_auth_safe_logs)
google_cloud_auth_safe_logs
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Calling this function as $(google_cloud_auth_safe_logs) make that the variable that was exported in that function (GOOGLE_APPLICATION_CREDENTIALS) was not available for the rest of the function. It seems that it creates a subshell and that variable does not exist outside of it.

@@ -67,14 +77,12 @@ if [[ "${TARGET}" == "" ]]; then
fi

google_cloud_auth_safe_logs() {
local gsUtilLocation=$(mktemp -d -p . -t ${TMP_FOLDER_TEMPLATE})
local gsUtilLocation=$(mktemp -d -p ${WORKSPACE} -t ${TMP_FOLDER_TEMPLATE})
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ensure that the temporal folder is created under WORKSPACE

Comment on lines 7 to 23
cleanup() {
local error_code=$?

if [ $error_code != 0 ] ; then
if [ -f ${GOOGLE_APPLICATION_CREDENTIALS+x} ]; then
google_cloud_logout_active_account
fi
fi

echo "Deleting temporal files..."
cd ${WORKSPACE}
rm -rf "${TMP_FOLDER_TEMPLATE_BASE}.*"
echo "Done."

exit $error_code
}
trap cleanup EXIT
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I didn't want to move this function to tooling.sh to avoid more refactors in this PR.

Each pipeline (script) has its own cleanup and the one from release pipeline is different.

Copy link
Contributor

Choose a reason for hiding this comment

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

The command trap cleanup EXIT will run every time before exit.
But the variable GOOGLE_APPLICATION_CREDENTIALS can be not defined yet, for example, when the script finishes with an error before google_cloud_auth* func. Perhaps, in this case, we have to look for the google-cloud-credentials.json file in the ${WORKSPACE}/* folder and subfolders instead of using the variable.
@mrodm WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tried to take into account this by using this parameter expansion ${var+x}

 if [ -n "${GOOGLE_APPLICATION_CREDENTIALS+x}" ]; then

In that case, if the variable is not defined the logout is not executed.

I think that it could be a good approach, since that variable is set by the google_cloud_auth just after the login command

gcloud auth activate-service-account --key-file ${keyFile} 2> /dev/null
export GOOGLE_APPLICATION_CREDENTIALS=${secretFileLocation}

In this commit I added two conditions that the variable is defined and the file pointing that variable exists. Maybe it can be just leave that the variable is defined. If the variable is defined, run the logout function

@sharbuz WDYT ?

Comment on lines 10 to 14
if [ $error_code != 0 ] ; then
if [ -f ${GOOGLE_APPLICATION_CREDENTIALS+x} ]; then
google_cloud_logout_active_account
fi
fi
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ensure that in case of failure, the active account is logged out.

Copy link
Contributor

Choose a reason for hiding this comment

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

perfect! we check the variable and if it exists we check the file!

@mrodm mrodm marked this pull request as ready for review October 5, 2023 10:55
@mrodm mrodm requested review from sharbuz and a team October 5, 2023 10:55
Comment on lines 17 to 20
unset ELASTIC_PACKAGE_AWS_ACCESS_KEY
unset ELASTIC_PACKAGE_AWS_SECRET_KEY
unset AWS_ACCESS_KEY_ID
unset AWS_SECRET_ACCESS_KEY
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we update the unset_secrets func to unset _KEY and _KEY_ID secrets to exclude whole this condition in the pre-exit hook?

Copy link
Contributor

Choose a reason for hiding this comment

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

Also, if we already have a great cleanup func that checks the current GCP connection I suppose we can add running of this func in the pre-exit hook right after unset_secrets to avoid plenty of conditions with gcp_logout* calls.
WDYT?

Copy link
Contributor

Choose a reason for hiding this comment

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

look at the example: https://github.com/elastic/package-storage-infra/pull/570
refactored cleanup and unset_secrets funcs in the common.sh and pre-exit hook.

Copy link
Contributor

Choose a reason for hiding this comment

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

Can we update the unset_secrets func to unset _KEY and _KEY_ID secrets to exclude whole this condition in the pre-exit hook?

It's not a good idea :) I've checked it. in this case, we will unset all BUILDKITE_*_KEY and similar variables needed for the pipeline.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This unset_secrets function is the same as in other repositories, and I didn't want to deviate much from the other usages in case it could be moved to a shared location.

Comment on lines +11 to +19
local error_code=$?

if [ $error_code != 0 ] ; then
# if variable is defined, run the logout
if [ -n "${GOOGLE_APPLICATION_CREDENTIALS+x}" ]; then
google_cloud_logout_active_account
fi
fi

Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps it's better to move the cleanup func into tooling.sh script to avoid duplicating the code in the signAndPublishPackage.sh and integration_tests.sh. @mrodm WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I wanted to avoid that in this PR at least, I added a comment about this in this other thread:
#1487 (comment)

@mrodm mrodm requested a review from sharbuz October 11, 2023 10:03
Comment on lines 17 to 20
unset ELASTIC_PACKAGE_AWS_ACCESS_KEY
unset ELASTIC_PACKAGE_AWS_SECRET_KEY
unset AWS_ACCESS_KEY_ID
unset AWS_SECRET_ACCESS_KEY
Copy link
Contributor

Choose a reason for hiding this comment

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

Also, if we already have a great cleanup func that checks the current GCP connection I suppose we can add running of this func in the pre-exit hook right after unset_secrets to avoid plenty of conditions with gcp_logout* calls.
WDYT?

Comment on lines 17 to 20
unset ELASTIC_PACKAGE_AWS_ACCESS_KEY
unset ELASTIC_PACKAGE_AWS_SECRET_KEY
unset AWS_ACCESS_KEY_ID
unset AWS_SECRET_ACCESS_KEY
Copy link
Contributor

Choose a reason for hiding this comment

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

look at the example: https://github.com/elastic/package-storage-infra/pull/570
refactored cleanup and unset_secrets funcs in the common.sh and pre-exit hook.

Comment on lines 17 to 20
unset ELASTIC_PACKAGE_AWS_ACCESS_KEY
unset ELASTIC_PACKAGE_AWS_SECRET_KEY
unset AWS_ACCESS_KEY_ID
unset AWS_SECRET_ACCESS_KEY
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we update the unset_secrets func to unset _KEY and _KEY_ID secrets to exclude whole this condition in the pre-exit hook?

It's not a good idea :) I've checked it. in this case, we will unset all BUILDKITE_*_KEY and similar variables needed for the pipeline.

mrodm and others added 2 commits October 11, 2023 14:54
Co-authored-by: sharbuz <87968844+sharbuz@users.noreply.github.com>
@mrodm
Copy link
Contributor Author

mrodm commented Oct 11, 2023

/test

3 similar comments
@mrodm
Copy link
Contributor Author

mrodm commented Oct 11, 2023

/test

@mrodm
Copy link
Contributor Author

mrodm commented Oct 11, 2023

/test

@mrodm
Copy link
Contributor Author

mrodm commented Oct 11, 2023

/test

@mrodm
Copy link
Contributor Author

mrodm commented Oct 16, 2023

/test

@mrodm mrodm changed the title Update buildkite scripts Review buildkite scripts cleanup process Oct 19, 2023
unset_secrets

# unset the environment variables not managged by "unset_secrets"
if [[ "$BUILDKITE_PIPELINE_SLUG" == "elastic-package" && "$BUILDKITE_STEP_KEY" == "integration-parallel-gcp" ]]; then
Copy link
Member

Choose a reason for hiding this comment

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

There would be any problem with unsetting all these variables always?

Suggested change
if [[ "$BUILDKITE_PIPELINE_SLUG" == "elastic-package" && "$BUILDKITE_STEP_KEY" == "integration-parallel-gcp" ]]; then

Copy link
Contributor Author

Choose a reason for hiding this comment

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

mmm yes , and it probably it would be safer.
Executing those unset commands even if the variable does not exist should not cause any issue.
I'll change it.

@elasticmachine
Copy link
Collaborator

elasticmachine commented Oct 19, 2023

💛 Build succeeded, but was flaky

Failed CI Steps

History

cc @mrodm

@mrodm mrodm merged commit 950abd3 into elastic:main Oct 19, 2023
@mrodm mrodm deleted the update_buildkite_scripts branch October 19, 2023 10:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants