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

Proposed by Adam Gandelman on 2013-02-27
Status: Merged
Approved by: Mark Mims on 2013-03-12
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) 2013-02-27 Approve on 2013-03-12
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.
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
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
1=== modified file 'hooks/glance-common'
2--- hooks/glance-common 2012-10-01 22:17:24 +0000
3+++ hooks/glance-common 2013-02-27 19:30:31 +0000
4@@ -17,72 +17,27 @@
5 juju-log "ERROR: Couldn't load $CHARM_DIR/lib/openstack-common." && exit 1
6 fi
7
8-function set_paste_deploy_flavor {
9- local flavor="$1"
10- local config="$2"
11- case $config in
12- "api") local conf=$GLANCE_API_CONF ;;
13- "registry") local conf=$GLANCE_REGISTRY_CONF ;;
14- *) juju-log "ERROR: set_paste_deploy: invalid config=$config" && exit 1 ;;
15- esac
16- if ! grep -q "\[paste_deploy\]" "$conf" ; then
17- juju-log "Updating $conf: Setting new paste_deploy flavor = $flavor"
18- echo -e "\n[paste_deploy]\nflavor = keystone\n" >>$conf && return 0
19- juju-log "ERROR: Could not update paste_deploy flavor in $conf" && return 1
20- fi
21- juju-log "Updating $conf: Setting paste_deploy flavor = $flavor"
22- local tag="[paste_deploy]"
23- sed -i "/$tag/, +1 s/\(flavor = \).*/\1$flavor/g" $conf && return 0
24- juju-log "ERROR: Could not update paste_deploy flavor in $conf" && return 1
25-}
26-
27-function update_pipeline {
28- # updates pipeline middleware definitions in api-paste.ini
29- local pipeline="$1"
30- local new="$2"
31- local config="$3"
32-
33- case $config in
34- "api") local api_conf=$GLANCE_API_CONF ;;
35- "registry") local api_conf=$GLANCE_REGISTRY_CONF ;;
36- *) juju-log "ERROR: update_pipeline: invalid config=$config" && exit 1 ;;
37- esac
38-
39- local tag="\[pipeline:$pipeline\]"
40- if ! grep -q "$tag" $api_conf ; then
41- juju-log "ERROR: update_pipeline: pipeline not found: $pipeline"
42- return 1
43- fi
44- juju-log "Updating pipeline:$pipeline in $api_conf"
45- sed -i "/$tag/, +1 s/\(pipeline = \).*/\1$new/g" $api_conf
46-}
47-
48 function set_or_update {
49- # This handles configuration of both api and registry server
50- # until LP #806241 is resolved. Until then, $3 is either
51- # 'api' or 'registry' to specify which
52- # set or update a key=value config option in glance.conf
53- KEY=$1
54- VALUE=$2
55- case "$3" in
56- "api") CONF=$GLANCE_API_CONF ;;
57- "api-paste") CONF=$GLANCE_API_PASTE_INI ;;
58- "registry") CONF=$GLANCE_REGISTRY_CONF ;;
59- "registry-paste") CONF=$GLANCE_REGISTRY_PASTE_INI ;;
60+ local key="$1"
61+ local value="$2"
62+ local file="$3"
63+ local section="$4"
64+ local conf=""
65+ [[ -z $key ]] && juju-log "ERROR: set_or_update(): value $value missing key" \
66+ && exit 1
67+ [[ -z $value ]] && juju-log "ERROR: set_or_update(): key $key missing value" \
68+ && exit 1
69+ case "$file" in
70+ "api") conf=$GLANCE_API_CONF ;;
71+ "api-paste") conf=$GLANCE_API_PASTE_INI ;;
72+ "registry") conf=$GLANCE_REGISTRY_CONF ;;
73+ "registry-paste") conf=$GLANCE_REGISTRY_PASTE_INI ;;
74 *) juju-log "ERROR: set_or_update(): Invalid or no config file specified." \
75 && exit 1 ;;
76 esac
77- [[ -z $KEY ]] && juju-log "ERROR: set_or_update(): value $VALUE missing key" \
78- && exit 1
79- [[ -z $VALUE ]] && juju-log "ERROR: set_or_update(): key $KEY missing value" \
80- && exit 1
81- cat $CONF | grep "$KEY = $VALUE" >/dev/null \
82- && juju-log "glance: $KEY = $VALUE already set" exit 0
83- if cat $CONF | grep "$KEY =" >/dev/null ; then
84- sed -i "s|\($KEY = \).*|\1$VALUE|" $CONF
85- else
86- echo "$KEY = $VALUE" >>$CONF
87- fi
88+ [[ ! -e $conf ]] && juju-log "ERROR: set_or_update(): File not found $conf" \
89+ && exit 1
90+ cfg_set_or_update "$key" "$value" "$conf" "$section"
91 }
92
93 do_openstack_upgrade() {
94
95=== modified file 'hooks/glance-relations'
96--- hooks/glance-relations 2013-01-30 23:14:25 +0000
97+++ hooks/glance-relations 2013-02-27 19:30:31 +0000
98@@ -222,19 +222,20 @@
99 unset JUJU_REMOTE_UNIT JUJU_RELATION JUJU_RELATION_ID
100 fi
101
102- set_paste_deploy_flavor "keystone" "api" || exit 1
103- set_paste_deploy_flavor "keystone" "registry" || exit 1
104+ set_or_update "flavor" "keystone" "api" "paste_deploy"
105+ set_or_update "flavor" "keystone" "registry" "paste_deploy"
106
107+ local sect="filter:authtoken"
108 for i in api-paste registry-paste ; do
109- set_or_update "service_host" "$keystone_host" $i
110- set_or_update "service_port" "$service_port" $i
111- set_or_update "auth_host" "$keystone_host" $i
112- set_or_update "auth_port" "$auth_port" $i
113- set_or_update "auth_uri" "http://$keystone_host:$service_port/" $i
114- set_or_update "admin_token" "$token" $i
115- set_or_update "admin_tenant_name" "$service_tenant" $i
116- set_or_update "admin_user" "$service_username" $i
117- set_or_update "admin_password" "$service_password" $i
118+ set_or_update "service_host" "$keystone_host" $i $sect
119+ set_or_update "service_port" "$service_port" $i $sect
120+ set_or_update "auth_host" "$keystone_host" $i $sect
121+ set_or_update "auth_port" "$auth_port" $i $sect
122+ set_or_update "auth_uri" "http://$keystone_host:$service_port/" $i $sect
123+ set_or_update "admin_token" "$token" $i $sect
124+ set_or_update "admin_tenant_name" "$service_tenant" $i $sect
125+ set_or_update "admin_user" "$service_username" $i $sect
126+ set_or_update "admin_password" "$service_password" $i $sect
127 done
128 service_ctl all restart
129
130
131=== modified file 'hooks/lib/openstack-common'
132--- hooks/lib/openstack-common 2013-01-11 18:33:16 +0000
133+++ hooks/lib/openstack-common 2013-02-27 19:30:31 +0000
134@@ -314,3 +314,29 @@
135 echo "$found"
136 return 0
137 }
138+
139+function cfg_set_or_update() {
140+ local key="$1"
141+ local value="$2"
142+ local file="$3"
143+ local section="$4"
144+ [[ -z "$section" ]] && section="DEFAULT"
145+ [[ -z $key ]] && juju-log "ERROR: cfg_set_or_update(): value $value missing key" \
146+ && exit 1
147+ [[ -z $value ]] && juju-log "ERROR: cfg_set_or_update(): key $key missing value" \
148+ && exit 1
149+ [[ ! -e $file ]] && juju-log "ERROR: cfg_set_or_update(): File not found $file" \
150+ && exit 1
151+
152+ python -c "
153+import ConfigParser
154+config = ConfigParser.RawConfigParser()
155+config.read('$file')
156+if '$section' != 'DEFAULT' and not config.has_section('$section'):
157+ config.add_section('$section')
158+config.set('$section', '$key', '$value')
159+with open('$file', 'wb') as conf_out:
160+ config.write(conf_out)
161+"
162+ juju-log "Updated config $file, $key = $value in section $section."
163+}
164
165=== modified file 'revision'
166--- revision 2013-01-30 23:14:25 +0000
167+++ revision 2013-02-27 19:30:31 +0000
168@@ -1,1 +1,1 @@
169-79
170+80

Subscribers

People subscribed via source and target branches