Merge ~ahasenack/ubuntu/+source/base-files:eoan-base-file-handle-empty-cloud-id-1841597 into ubuntu/+source/base-files:ubuntu/devel

Proposed by Andreas Hasenack
Status: Merged
Approved by: Andreas Hasenack
Approved revision: f1d54c80e8841754b37eb7b17caf5d93157166e1
Merged at revision: f1d54c80e8841754b37eb7b17caf5d93157166e1
Proposed branch: ~ahasenack/ubuntu/+source/base-files:eoan-base-file-handle-empty-cloud-id-1841597
Merge into: ubuntu/+source/base-files:ubuntu/devel
Diff against target: 56 lines (+19/-5)
2 files modified
debian/changelog (+7/-0)
motd/50-motd-news (+12/-5)
Reviewer Review Type Date Requested Status
Christian Ehrhardt  (community) Approve
Canonical Server Pending
Review via email: mp+371905@code.launchpad.net

Description of the change

Set cloud-id to unknown if the tool exited with a non-zero status, or printed an empty string.

I wanted to use set -o pipefail, so I could check the exit status of cloud-id in this line below, but dash doesn't have this option:

cloud_id=$(/usr/bin/cloud-id 2>/dev/null | cut -c -40 | tr -c -d '[:alnum:]')

Therefore I have to call cloud-id first, check $?, and then proceed if that is zero, and this is this branch. I checked if shell chars in the cloud-id output would break the script, and looks like they don't, at least in this test where I replaced /usr/bin/cloud-id with a script of mine, outputting all sorts of nasty things:

++ /usr/bin/cloud-id
$(id)'d_id='"\'\'';id nasty things
$(id)' ']'e 0 -o -z '"\'\'';id nasty things
$(id)'o '"\'\'';id nasty things
++ cut -c -40
++ tr -c -d '[:alnum:]'
+ cloud_id=idnastythingsid
+ read up idle
+ uptime=uptime/15283.00/15247.00
+ USER_AGENT='curl/7.65.3-1ubuntu1 Ubuntu/Eoan/Ermine/(development/branch) GNU/Linux/4.15.0-58-generic/x86_64 Intel(R)/Core(TM)/i7-7600U/CPU/@/2.80GHz uptime/15283.00/15247.00 cloud_id/idnastythingsid'

I also got a suggestion to use head or tail -n 1 in the cloud-id output, but I think it's best to have the tr call delete the \n and join multiple lines if that ever happens, which it does today. I rather not overoptimize this client-side branch, and deal with this on the server if I have to, unless it becomes a real problem.

Just to remember, the server side will do this kind of parsing:
+ RewriteEngine on
+ # list in the regexp all the clouds which should get motd support
+ RewriteCond "%{HTTP_USER_AGENT}" "cloud_id/(aws|azure|gce)\b"
+ RewriteRule "^/$" "/index-%1.txt" [L]

Specific for those 3 clouds at the moment, spelled like that. And if the corresponding index-$cloud.txt file doesn't exist, the 404 document is index.txt, which exists as that is the common motd message content.

To post a comment you must log in.
Revision history for this message
Christian Ehrhardt  (paelzer) wrote :

Two minor comments inline - one a potential rework here one a cloud-init bug/feature if you like it, but generally LGTM already +1

review: Approve
Revision history for this message
Andreas Hasenack (ahasenack) :
Revision history for this message
Andreas Hasenack (ahasenack) wrote :

I pushed a new alternative, using a tempfile for the cloud-id data, please take another look

Revision history for this message
Christian Ehrhardt  (paelzer) :
Revision history for this message
Andreas Hasenack (ahasenack) wrote :

Lastly I renamed CLOUD_ID (the tempfile) to just CLOUD, to avoid confusion with the variable cloud_id.

Tagging and uploading f1d54c80e8841754b37eb7b17caf5d93157166e1

$ git push pkg upload/10.2ubuntu6
Enumerating objects: 14, done.
Counting objects: 100% (14/14), done.
Delta compression using up to 2 threads
Compressing objects: 100% (9/9), done.
Writing objects: 100% (9/9), 1.30 KiB | 222.00 KiB/s, done.
Total 9 (delta 6), reused 0 (delta 0)
To ssh://git.launchpad.net/~usd-import-team/ubuntu/+source/base-files
 * [new tag] upload/10.2ubuntu6 -> upload/10.2ubuntu6

$ dput ubuntu ../base-files_10.2ubuntu6_source.changes
Checking signature on .changes
gpg: ../base-files_10.2ubuntu6_source.changes: Valid signature from AC983EB5BF6BCBA9
Checking signature on .dsc
gpg: ../base-files_10.2ubuntu6.dsc: Valid signature from AC983EB5BF6BCBA9
Uploading to ubuntu (via ftp to upload.ubuntu.com):
  Uploading base-files_10.2ubuntu6.dsc: done.
  Uploading base-files_10.2ubuntu6.tar.xz: done.
  Uploading base-files_10.2ubuntu6_source.buildinfo: done.
  Uploading base-files_10.2ubuntu6_source.changes: done.
Successfully uploaded packages.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
diff --git a/debian/changelog b/debian/changelog
index 5054e04..c5dd058 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -1,3 +1,10 @@
1base-files (10.2ubuntu6) eoan; urgency=medium
2
3 * motd/50-motd-news: set cloud-id to unknown if the tool exited with a
4 non-zero status, or printed an empty string (LP: #1841597)
5
6 -- Andreas Hasenack <andreas@canonical.com> Tue, 27 Aug 2019 15:31:30 -0300
7
1base-files (10.2ubuntu5) eoan; urgency=medium8base-files (10.2ubuntu5) eoan; urgency=medium
29
3 * motd/50-motd-news: add cloud_id to user-agent string (LP: #1840946)10 * motd/50-motd-news: add cloud_id to user-agent string (LP: #1840946)
diff --git a/motd/50-motd-news b/motd/50-motd-news
index 026429f..838eeb0 100644
--- a/motd/50-motd-news
+++ b/motd/50-motd-news
@@ -67,7 +67,8 @@ fi
67# Generate our temp files, clean up when done67# Generate our temp files, clean up when done
68NEWS=$(mktemp) || exit 168NEWS=$(mktemp) || exit 1
69ERR=$(mktemp) || exit 169ERR=$(mktemp) || exit 1
70trap "rm -f $NEWS $ERR" HUP INT QUIT ILL TRAP KILL BUS TERM70CLOUD=$(mktemp) || exit 1
71trap "rm -f $NEWS $ERR $CLOUD" HUP INT QUIT ILL TRAP KILL BUS TERM
7172
72# Construct a user agent, similar to Firefox/Chrome/Safari/IE to73# Construct a user agent, similar to Firefox/Chrome/Safari/IE to
73# ensure a proper, tailored, accurate message of the day74# ensure a proper, tailored, accurate message of the day
@@ -84,10 +85,16 @@ codename="$DISTRIB_CODENAME"
84platform="$(uname -o)/$(uname -r)/$(uname -m)"85platform="$(uname -o)/$(uname -r)/$(uname -m)"
85arch="$(uname -m)"86arch="$(uname -m)"
86cpu="$(grep -m1 "^model name" /proc/cpuinfo | sed -e "s/.*: //" -e "s:\s\+:/:g")"87cpu="$(grep -m1 "^model name" /proc/cpuinfo | sed -e "s/.*: //" -e "s:\s\+:/:g")"
88cloud_id="unknown"
87if [ -x /usr/bin/cloud-id ]; then89if [ -x /usr/bin/cloud-id ]; then
88 cloud_id=$(/usr/bin/cloud-id 2>/dev/null | cut -c -40 | tr -c -d '[:alnum:]')90 /usr/bin/cloud-id > "$CLOUD" 2>/dev/null
89else91 if [ "$?" -eq "0" ]; then
90 cloud_id="unknown"92 # sanitize it a bit, just in case
93 cloud_id=$(cut -c -40 "${CLOUD}" | tr -c -d '[:alnum:]')
94 if [ -z "${cloud_id}" ]; then
95 cloud_id="unknown"
96 fi
97 fi
91fi98fi
9299
93# Some messages may only be pertinent before or after some amount of uptime100# Some messages may only be pertinent before or after some amount of uptime
@@ -124,5 +131,5 @@ for u in $URLS; do
124 : > "$CACHE"131 : > "$CACHE"
125 fi132 fi
126done133done
127rm -f "$NEWS" "$ERR"134rm -f "$NEWS" "$ERR" "$CLOUD"
128exit 0135exit 0

Subscribers

People subscribed via source and target branches