Merge lp:~gandelman-a/charms/precise/glance/cfgparser into lp:~charmers/charms/precise/glance/trunk

Proposed by Adam Gandelman
Status: Merged
Approved by: Mark Mims
Approved revision: 31
Merged at revision: 32
Proposed branch: lp:~gandelman-a/charms/precise/glance/cfgparser
Merge into: lp:~charmers/charms/precise/glance/trunk
Diff against target: 170 lines (+56/-74)
4 files modified
hooks/glance-common (+17/-62)
hooks/glance-relations (+12/-11)
hooks/lib/openstack-common (+26/-0)
revision (+1/-1)
To merge this branch: bzr merge lp:~gandelman-a/charms/precise/glance/cfgparser
Reviewer Review Type Date Requested Status
Mark Mims (community) Approve
Review via email: mp+150887@code.launchpad.net

Description of the change

This should fix issues where certain config files need to updated differently depending on the Openstack version being deployed.

Adds an openstack-common function to update a config file using the python standard ConfigParser. Charms are free to wrap this when updating config and we can move away from using difficult-to-maintain sed statements. All openstack projects have standardized on the same config format, so in theory we could migrate all charms to use this same common function instead of each wrapping it (though I think a total python rewrite would be a better use of time :)

One drawback is that we lose any comments that may have existed as documentation in the package-provided config.

To post a comment you must log in.
Revision history for this message
Mark Mims (mark-mims) wrote :

lgtm

Dude, ConfigParser wrappers in the bash charm-helpers would rock! You know, for the next time you're bored or something :)

filed https://bugs.launchpad.net/charm-tools/+bug/1154225

review: Approve
Revision history for this message
Mark Mims (mark-mims) wrote :

damn, you're such a charming guy... how'd we let you not be a member of ~charmers anymore?

I'll go ahead an merge this too

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'hooks/glance-common'
--- hooks/glance-common 2012-10-01 22:17:24 +0000
+++ hooks/glance-common 2013-02-27 19:30:31 +0000
@@ -17,72 +17,27 @@
17 juju-log "ERROR: Couldn't load $CHARM_DIR/lib/openstack-common." && exit 117 juju-log "ERROR: Couldn't load $CHARM_DIR/lib/openstack-common." && exit 1
18fi18fi
1919
20function set_paste_deploy_flavor {
21 local flavor="$1"
22 local config="$2"
23 case $config in
24 "api") local conf=$GLANCE_API_CONF ;;
25 "registry") local conf=$GLANCE_REGISTRY_CONF ;;
26 *) juju-log "ERROR: set_paste_deploy: invalid config=$config" && exit 1 ;;
27 esac
28 if ! grep -q "\[paste_deploy\]" "$conf" ; then
29 juju-log "Updating $conf: Setting new paste_deploy flavor = $flavor"
30 echo -e "\n[paste_deploy]\nflavor = keystone\n" >>$conf && return 0
31 juju-log "ERROR: Could not update paste_deploy flavor in $conf" && return 1
32 fi
33 juju-log "Updating $conf: Setting paste_deploy flavor = $flavor"
34 local tag="[paste_deploy]"
35 sed -i "/$tag/, +1 s/\(flavor = \).*/\1$flavor/g" $conf && return 0
36 juju-log "ERROR: Could not update paste_deploy flavor in $conf" && return 1
37}
38
39function update_pipeline {
40 # updates pipeline middleware definitions in api-paste.ini
41 local pipeline="$1"
42 local new="$2"
43 local config="$3"
44
45 case $config in
46 "api") local api_conf=$GLANCE_API_CONF ;;
47 "registry") local api_conf=$GLANCE_REGISTRY_CONF ;;
48 *) juju-log "ERROR: update_pipeline: invalid config=$config" && exit 1 ;;
49 esac
50
51 local tag="\[pipeline:$pipeline\]"
52 if ! grep -q "$tag" $api_conf ; then
53 juju-log "ERROR: update_pipeline: pipeline not found: $pipeline"
54 return 1
55 fi
56 juju-log "Updating pipeline:$pipeline in $api_conf"
57 sed -i "/$tag/, +1 s/\(pipeline = \).*/\1$new/g" $api_conf
58}
59
60function set_or_update {20function set_or_update {
61 # This handles configuration of both api and registry server21 local key="$1"
62 # until LP #806241 is resolved. Until then, $3 is either22 local value="$2"
63 # 'api' or 'registry' to specify which23 local file="$3"
64 # set or update a key=value config option in glance.conf24 local section="$4"
65 KEY=$125 local conf=""
66 VALUE=$226 [[ -z $key ]] && juju-log "ERROR: set_or_update(): value $value missing key" \
67 case "$3" in27 && exit 1
68 "api") CONF=$GLANCE_API_CONF ;;28 [[ -z $value ]] && juju-log "ERROR: set_or_update(): key $key missing value" \
69 "api-paste") CONF=$GLANCE_API_PASTE_INI ;;29 && exit 1
70 "registry") CONF=$GLANCE_REGISTRY_CONF ;;30 case "$file" in
71 "registry-paste") CONF=$GLANCE_REGISTRY_PASTE_INI ;;31 "api") conf=$GLANCE_API_CONF ;;
32 "api-paste") conf=$GLANCE_API_PASTE_INI ;;
33 "registry") conf=$GLANCE_REGISTRY_CONF ;;
34 "registry-paste") conf=$GLANCE_REGISTRY_PASTE_INI ;;
72 *) juju-log "ERROR: set_or_update(): Invalid or no config file specified." \35 *) juju-log "ERROR: set_or_update(): Invalid or no config file specified." \
73 && exit 1 ;;36 && exit 1 ;;
74 esac37 esac
75 [[ -z $KEY ]] && juju-log "ERROR: set_or_update(): value $VALUE missing key" \38 [[ ! -e $conf ]] && juju-log "ERROR: set_or_update(): File not found $conf" \
76 && exit 139 && exit 1
77 [[ -z $VALUE ]] && juju-log "ERROR: set_or_update(): key $KEY missing value" \40 cfg_set_or_update "$key" "$value" "$conf" "$section"
78 && exit 1
79 cat $CONF | grep "$KEY = $VALUE" >/dev/null \
80 && juju-log "glance: $KEY = $VALUE already set" exit 0
81 if cat $CONF | grep "$KEY =" >/dev/null ; then
82 sed -i "s|\($KEY = \).*|\1$VALUE|" $CONF
83 else
84 echo "$KEY = $VALUE" >>$CONF
85 fi
86}41}
8742
88do_openstack_upgrade() {43do_openstack_upgrade() {
8944
=== modified file 'hooks/glance-relations'
--- hooks/glance-relations 2013-01-30 23:14:25 +0000
+++ hooks/glance-relations 2013-02-27 19:30:31 +0000
@@ -222,19 +222,20 @@
222 unset JUJU_REMOTE_UNIT JUJU_RELATION JUJU_RELATION_ID222 unset JUJU_REMOTE_UNIT JUJU_RELATION JUJU_RELATION_ID
223 fi223 fi
224224
225 set_paste_deploy_flavor "keystone" "api" || exit 1225 set_or_update "flavor" "keystone" "api" "paste_deploy"
226 set_paste_deploy_flavor "keystone" "registry" || exit 1226 set_or_update "flavor" "keystone" "registry" "paste_deploy"
227227
228 local sect="filter:authtoken"
228 for i in api-paste registry-paste ; do229 for i in api-paste registry-paste ; do
229 set_or_update "service_host" "$keystone_host" $i230 set_or_update "service_host" "$keystone_host" $i $sect
230 set_or_update "service_port" "$service_port" $i231 set_or_update "service_port" "$service_port" $i $sect
231 set_or_update "auth_host" "$keystone_host" $i232 set_or_update "auth_host" "$keystone_host" $i $sect
232 set_or_update "auth_port" "$auth_port" $i233 set_or_update "auth_port" "$auth_port" $i $sect
233 set_or_update "auth_uri" "http://$keystone_host:$service_port/" $i234 set_or_update "auth_uri" "http://$keystone_host:$service_port/" $i $sect
234 set_or_update "admin_token" "$token" $i235 set_or_update "admin_token" "$token" $i $sect
235 set_or_update "admin_tenant_name" "$service_tenant" $i236 set_or_update "admin_tenant_name" "$service_tenant" $i $sect
236 set_or_update "admin_user" "$service_username" $i237 set_or_update "admin_user" "$service_username" $i $sect
237 set_or_update "admin_password" "$service_password" $i238 set_or_update "admin_password" "$service_password" $i $sect
238 done239 done
239 service_ctl all restart240 service_ctl all restart
240241
241242
=== modified file 'hooks/lib/openstack-common'
--- hooks/lib/openstack-common 2013-01-11 18:33:16 +0000
+++ hooks/lib/openstack-common 2013-02-27 19:30:31 +0000
@@ -314,3 +314,29 @@
314 echo "$found"314 echo "$found"
315 return 0315 return 0
316}316}
317
318function cfg_set_or_update() {
319 local key="$1"
320 local value="$2"
321 local file="$3"
322 local section="$4"
323 [[ -z "$section" ]] && section="DEFAULT"
324 [[ -z $key ]] && juju-log "ERROR: cfg_set_or_update(): value $value missing key" \
325 && exit 1
326 [[ -z $value ]] && juju-log "ERROR: cfg_set_or_update(): key $key missing value" \
327 && exit 1
328 [[ ! -e $file ]] && juju-log "ERROR: cfg_set_or_update(): File not found $file" \
329 && exit 1
330
331 python -c "
332import ConfigParser
333config = ConfigParser.RawConfigParser()
334config.read('$file')
335if '$section' != 'DEFAULT' and not config.has_section('$section'):
336 config.add_section('$section')
337config.set('$section', '$key', '$value')
338with open('$file', 'wb') as conf_out:
339 config.write(conf_out)
340"
341 juju-log "Updated config $file, $key = $value in section $section."
342}
317343
=== modified file 'revision'
--- revision 2013-01-30 23:14:25 +0000
+++ revision 2013-02-27 19:30:31 +0000
@@ -1,1 +1,1 @@
179180

Subscribers

People subscribed via source and target branches