-
Notifications
You must be signed in to change notification settings - Fork 15
[GPII-3841]: Add display_universal_image_info task #344
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems useful, good idea 👍
shared/rakefiles/entrypoint.rake
Outdated
sh "#{@exekube_cmd} sh -c ' \ | ||
UNIVERSAL_CI_URL=\"https://ci.gpii.net/\"; | ||
UNIVERSAL_REPO=\"https://github.com/gpii/universal\"; | ||
RELEASE_JOB_URL=\"$UNIVERSAL_CI_URL/job/docker-gpii-universal-master-release\"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we put braces around variables here: ${UNIVERSAL}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do you think they are needed here?
I can think of only one common case when variable expansion braces are needed – var="${another_var}and_not_a_var"
. Well, another not so common case maybe working with arrays or some string manipulations. Otherwise they contribute to nothing, imo.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess this is a personal taste, but specially in the cases like "$UNIVERSAL_CI_URL/job/...
I would use them, as it's not very obvious imho when this will be treated as a variable ${UNIVERSAL}
, ${UNIVERSAL_CI_URL}
or variable ${UNIVERSAL_CI_URL/job/...}
- depending on which characters exaclty given shell considers a word separators. Adding {}
make the intention very clear.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using capital case for variables has the same effect on code readability as using braces with low case variables.
Not sure I agree with this for humans, but I disagree for computers -- braces have special meaning to the parser whereas capitalized variable names do not.
Both is probably overkill
Why?
What do you guys think on this topic?
I think Stepan's comment is nitpicky and I would not have suggested it. However, I understand his motivation and I would make his suggested change if I had received it on one of my PRs.
Longer-term, I think the solution to debates about code style is to automate code style checking and/or fixing (a la terraform fmt
). I don't think we're ready to deal with that, especially since we have a fair amount of shell scripts mixed with terraform code, which complicates the tooling. (I've mentioned this before, e.g. #93 (comment).)
But anyway, here is what I got from dropping this shell code into shellcheck
:
(EDIT: Oops, forgot to fix escaped quotes the first time. Output below is updated.)
In sergey.sh line 18:
RELEASE_BUILD_LIMIT=$(($RELEASE_BUILD-$LOOKUP_BUILDS));
^-- SC2004: $/${} is unnecessary on arithmetic variables.
^-- SC2004: $/${} is unnecessary on arithmetic variables.
In sergey.sh line 20:
SHA_FOUND=$(curl $RELEASE_JOB_URL/$RELEASE_BUILD/consoleText 2> /dev/null | grep -so "$PREFERENCES_IMAGE_SHA" || true);
^-- SC2086: Double quote to prevent globbing and word splitting.
In sergey.sh line 22:
UPSTREAM_JOB_NUMBER=$(curl $RELEASE_JOB_URL/$RELEASE_BUILD/api/json 2> /dev/null | jq -r ".actions[] | select (.causes[0].upstreamBuild != null) | .causes[0].upstreamBuild");
^-- SC2086: Double quote to prevent globbing and word splitting.
In sergey.sh line 23:
GITHUB_LINK="$UNIVERSAL_REPO/commit/$(curl $UPSTREAM_JOB_URL/$UPSTREAM_JOB_NUMBER/api/json 2> /dev/null | jq -r ".actions[] | select (.lastBuiltRevision.SHA1 != null) | .lastBuiltRevision.SHA1")";
^-- SC2086: Double quote to prevent globbing and word splitting.
In sergey.sh line 32:
RELEASE_BUILD=$(($RELEASE_BUILD-1));
^-- SC2004: $/${} is unnecessary on arithmetic variables.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mrtyler Thanks for review!
braces have special meaning to the parser whereas capitalized variable names do not
Yes, I think we all know that here, context of original discussion was limited to affect of braces on code readability, since code works just fine with them or without them.
Longer-term, I think the solution to debates about code style is to automate code style checking and/or fixing (a la terraform fmt). I don't think we're ready to deal with that
Agree and agree.
But anyway, here is what I got from dropping this shell code into shellcheck
Cool, let me address some of those warnings!
shared/rakefiles/entrypoint.rake
Outdated
@@ -108,6 +108,55 @@ task :display_cluster_info => [:set_vars] do | |||
puts | |||
end | |||
|
|||
desc "Display gpii/universal image SHA and link to GitHub commit that triggered the build" | |||
task :display_universal_image_info => [:set_vars] do | |||
sh "#{@exekube_cmd} sh -c ' \ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider setting -e
flag to stop on errors? Or add some error handling, as there's none for most parts of this script - curl calls, an unexpected structure of outputs... (I think -e might be easy way out in this case ;))
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are condition checks to handle most common failures (unable to get data from k8s, unable to generate commit link), but yeah, setting -e
flag makes sense.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually nevermind, it is not going to work properly with set -e
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair enough, I think that it's generally dangerous to rely on output of given commands, not even checking for return codes. Like relying on empty output of curl | jq
in a while condition.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see your point, but I think since this is just utility task, and nothing terrible going to happen even when it fails, current level of error handling is good enough.
Good idea @natarajaya of using the CI to match the images with the commits. But I think that something is wrong here. I tried this code and I got:
If I go to the last job: https://ci.gpii.net/job/docker-gpii-universal-master/158/ My guess is that the script is using the wrong build number. Bear in mind that the build number can be different in between the jobs of a multijob: the parent job number is 158 but the |
Thanks @amatas, nice catch! This is fixed now. I also followed your advice and added this task to CI for staging and prod. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. This looks more useful than the documentation I was writing to describe the process. :)
shared/rakefiles/entrypoint.rake
Outdated
echo | ||
echo \"Unable to retrieve data from K8s cluster!\"; | ||
echo \"This is most likely because you are not authenticated!\"; | ||
echo \"Try running \\\`rake sh[\\\"rake configure_kubectl\\\"]\\\` and then try again.\"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we use rake dependencies so that this happens automatically?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, let me move the task to xk_util
and make an alias similarly to display_cluster_state
.
shared/rakefiles/entrypoint.rake
Outdated
fi | ||
|
||
echo | ||
echo \"Deployed gpii/universal image SHA:\"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would mention that it's the gpii/universal image in use by preferences, in case there happens to be a discrepancy between preferences and other components that use gpii/universal.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense!
What will happen when this task fails, will it fail the build? If so, it seems quite risky to make our pipeline dependant on availability of GPII Jenkins server. |
This is fixed. |
|
||
PREFERENCES_IMAGE_SHA=$(kubectl -n gpii get deployment preferences -o json 2> /dev/null | jq -r \".spec.template.spec.containers[0].image\" | grep -o \"sha256:.*\"); | ||
if [ \"$?\" != \"0\" ]; then | ||
echo |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since I'm looking at this code again, why do most lines end with ;
but some do not?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sh
collapses everything into a single string, ;
are required to separate commands that need it from each other. Alternative would be to escape newlines. I think this is another sign that bash code does not belong to rake and we should do something about it in future.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this is true, then does line 143 combine with line 144 and become echo exit 1
(which will not exit)?
I guess if it worked in testing then the inconsistency isn't a big deal.
shared/rakefiles/xk_util.rake
Outdated
echo \"Preferences image SHA:\"; | ||
echo \"$PREFERENCES_IMAGE_SHA\"; | ||
RELEASE_BUILD=$(curl $RELEASE_JOB_URL/lastBuild/api/json 2> /dev/null | jq -r \".id\"); | ||
RELEASE_BUILD_LIMIT=$(($RELEASE_BUILD-$LOOKUP_BUILDS)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would add spaces around -
to make it clear this is arithmetic subtraction, not joining strings together.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, shellcheck
suggests the same.
shared/rakefiles/xk_util.rake
Outdated
SHA_FOUND=$(curl $RELEASE_JOB_URL/$RELEASE_BUILD/consoleText 2> /dev/null | grep -so \"$PREFERENCES_IMAGE_SHA\" || true); | ||
if [ \"$SHA_FOUND\" == \"$PREFERENCES_IMAGE_SHA\" ]; then | ||
UPSTREAM_JOB_NUMBER=$(curl $RELEASE_JOB_URL/$RELEASE_BUILD/api/json 2> /dev/null | jq -r \".actions[] | select (.causes[0].upstreamBuild != null) | .causes[0].upstreamBuild\"); | ||
GITHUB_LINK=\"$UNIVERSAL_REPO/commit/$(curl $UPSTREAM_JOB_URL/$UPSTREAM_JOB_NUMBER/api/json 2> /dev/null | jq -r \".actions[] | select (.lastBuiltRevision.SHA1 != null) | .lastBuiltRevision.SHA1\")\"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might want to use curl -f
here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense.
lgtm |
This should be helpful for developers, while we think on better long-term approach.