Merge lp:~johnsca/layer-ibm-base/fix-multi-call into lp:~ibmcharmers/layer-ibm-base/trunk

Proposed by Cory Johns
Status: Merged
Merged at revision: 4
Proposed branch: lp:~johnsca/layer-ibm-base/fix-multi-call
Merge into: lp:~ibmcharmers/layer-ibm-base/trunk
Diff against target: 133 lines (+36/-51)
2 files modified
layer.yaml (+2/-1)
reactive/ibm-base.sh (+34/-50)
To merge this branch: bzr merge lp:~johnsca/layer-ibm-base/fix-multi-call
Reviewer Review Type Date Requested Status
Kevin W Monroe Approve
Review via email: mp+292845@code.launchpad.net

Description of the change

Change how fetch_curl_resource() is gated using the newer config states to prevent it from being called multiple times.

When curl_url is set, having not be set previously, it will enable fetch_curl_resource() to complete but then that will also trigger curl_url_changed() which will call fetch_curl_resource() a second time under the assumption that the source was changed and it needs to be reinstalled. If curl_opts is set at the same time, it could lead to fetch_curl_resource() being called three different times.

This change relies on the fact that the first time the curl_url is set, either config.changed.curl_url or config.new.curl_url (depending on if we go with https://github.com/juju-solutions/layer-basic/pull/61 or not, or if we're in the first hook or not) in addition to config.set.curl_url.

To post a comment you must log in.
5. By Cory Johns

Work around formatting issue with built yaml

The yaml output from `charm build` drops the spaces before the comment.
However, this causes PyYaml to fail to parse the file. This should be
fixed upstream in charm-tools, but moving the comment to the next line
is an easy work-around.

Revision history for this message
Kevin W Monroe (kwmonroe) wrote :

LGTM, thanks Cory!

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'layer.yaml'
2--- layer.yaml 2016-03-23 20:36:38 +0000
3+++ layer.yaml 2016-05-03 17:29:14 +0000
4@@ -3,6 +3,7 @@
5 - 'layer:apt'
6 - 'layer:leadership'
7 options:
8- basic: # Setting options for the basic layer.
9+ basic:
10+ # Setting options for the basic layer.
11 packages:
12 - curl
13
14=== modified file 'reactive/ibm-base.sh'
15--- reactive/ibm-base.sh 2016-03-23 21:04:54 +0000
16+++ reactive/ibm-base.sh 2016-05-03 17:29:14 +0000
17@@ -15,7 +15,7 @@
18 local PROG=""
19
20 if [ ! -r ${FILE} ]; then
21- status-set active "ibm-base: could not read ${FILE}"
22+ status-set blocked "ibm-base: could not read ${FILE}"
23 juju-log "Could not verify the downloaded resource. File could not be read: ${FILE}"
24 fi
25
26@@ -31,18 +31,17 @@
27 PROG='sha512sum'
28 ;;
29 *)
30- status-set active "ibm-base: checksum type must be md5, sha215, or sha512"
31+ status-set blocked "ibm-base: checksum type must be md5, sha215, or sha512"
32 juju-log "Could not verify the downloaded resource ${FILE}. Unknown checksum type: ${TYPE}"
33 return 1
34 esac
35
36 CALCULATED_SUM=`${PROG} ${FILE} | awk '{print $1}'`
37 if [ "${CALCULATED_SUM}" = "${EXPECTED_SUM}" ]; then
38- status-set active "ibm-base: checksum verified"
39 juju-log "Checksum verified for ${FILE}."
40 return 0
41 else
42- status-set active "ibm-base: checksums did not match"
43+ status-set blocked "ibm-base: checksums did not match"
44 juju-log "Checksum mismatch for ${FILE}. Expected ${EXPECTED_SUM}, got ${CALCULATED_SUM}"
45 return 1
46 fi
47@@ -50,57 +49,42 @@
48
49
50 # Fetch curl resources if a URL is configured
51-@when_not 'ibm-base.curl.resource.fetched'
52+@when 'config.set.curl_url'
53+@when_any 'config.new.curl_url' 'config.changed.curl_url' 'config.new.curl_opts' 'config.changed.curl_opts'
54 function fetch_curl_resource() {
55 local ARCHIVE_DIR="${CHARM_DIR}/files/archives"
56 local CURL_URL=$(config-get 'curl_url')
57 local CURL_OPTS=$(config-get 'curl_opts')
58
59- if [ -n "${CURL_URL}" ]; then
60- status-set maintenance "ibm-base: fetching resource(s)"
61-
62- mkdir -p ${ARCHIVE_DIR}
63- cd ${ARCHIVE_DIR}
64- # Multiple URLs may be separated by a space, so loop.
65- for URL_STRING in ${CURL_URL}
66- do
67- # For each URL_STRING, set the url, checksum type, and checksum value.
68- local URL=${URL_STRING%%\?*} # string before the first '?'
69- local FILE_NAME=${URL##*\/} # string after the last '/'
70- local SUM_STRING=${URL_STRING#*\?} # string after the first '?'
71- local SUM_TYPE=${SUM_STRING%%\=*} # string before the first '='
72- local SUM_VALUE=${SUM_STRING#*\=} # string after the first '='
73-
74- if [ -z ${FILE_NAME} ]; then
75- FILE_NAME="juju-${RANDOM}"
76- fi
77- curl --silent --show-error ${CURL_OPTS} -o ${FILE_NAME} ${URL}
78-
79- # Verify our resource checksum. If this fails, let verify_resource log
80- # the reason and exit successfully. Exiting non-zero would fail the hook,
81- # so return 0 and simply inform the user that verification failed.
82- verify_curl_resource ${FILE_NAME} ${SUM_TYPE} ${SUM_VALUE} || return 0
83- done
84- cd -
85-
86- set_state 'ibm-base.curl.resource.fetched'
87- status-set active "ibm-base: curl resource(s) fetched"
88- juju-log 'Curl resource fetched'
89- fi
90-}
91-
92-
93-# Handle curl configuration changes
94-@when 'ibm-base.curl.resource.fetched' 'config.changed.curl_url'
95-function curl_url_changed() {
96- juju-log 'Curl URL changed'
97- fetch_curl_resource
98-}
99-
100-@when 'ibm-base.curl.resource.fetched' 'config.changed.curl_opts'
101-function curl_opts_changed() {
102- juju-log 'Curl options changed'
103- fetch_curl_resource
104+ status-set maintenance "ibm-base: fetching resource(s)"
105+
106+ mkdir -p ${ARCHIVE_DIR}
107+ cd ${ARCHIVE_DIR}
108+ # Multiple URLs may be separated by a space, so loop.
109+ for URL_STRING in ${CURL_URL}
110+ do
111+ # For each URL_STRING, set the url, checksum type, and checksum value.
112+ local URL=${URL_STRING%%\?*} # string before the first '?'
113+ local FILE_NAME=${URL##*\/} # string after the last '/'
114+ local SUM_STRING=${URL_STRING#*\?} # string after the first '?'
115+ local SUM_TYPE=${SUM_STRING%%\=*} # string before the first '='
116+ local SUM_VALUE=${SUM_STRING#*\=} # string after the first '='
117+
118+ if [ -z ${FILE_NAME} ]; then
119+ FILE_NAME="juju-${RANDOM}"
120+ fi
121+ curl --silent --show-error ${CURL_OPTS} -o ${FILE_NAME} ${URL}
122+
123+ # Verify our resource checksum. If this fails, let verify_resource log
124+ # the reason and exit successfully. Exiting non-zero would fail the hook,
125+ # so return 0 and simply inform the user that verification failed.
126+ verify_curl_resource ${FILE_NAME} ${SUM_TYPE} ${SUM_VALUE} || return 0
127+ done
128+ cd -
129+
130+ set_state 'ibm-base.curl.resource.fetched'
131+ status-set active "ibm-base: curl resource(s) fetched"
132+ juju-log 'Curl resource fetched'
133 }
134
135

Subscribers

People subscribed via source and target branches

to all changes: