Merge lp:~mthaddon/charms/precise/pgbouncer/remove-template-generate into lp:charms/pgbouncer
- Precise Pangolin (12.04)
- remove-template-generate
- Merge into trunk
Proposed by
Tom Haddon
Status: | Merged |
---|---|
Approved by: | Liam Young |
Approved revision: | 62 |
Merged at revision: | 60 |
Proposed branch: | lp:~mthaddon/charms/precise/pgbouncer/remove-template-generate |
Merge into: | lp:charms/pgbouncer |
Diff against target: |
274 lines (+34/-179) 3 files modified
hooks/nrpe-external-master-relation-changed (+13/-20) scripts/common (+21/-9) scripts/template-generate.py (+0/-150) |
To merge this branch: | bzr merge lp:~mthaddon/charms/precise/pgbouncer/remove-template-generate |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Liam Young (community) | Approve | ||
Review via email: mp+210372@code.launchpad.net |
Commit message
Description of the change
Per https:/
To post a comment you must log in.
Revision history for this message
Tom Haddon (mthaddon) wrote : | # |
Preview Diff
[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1 | === modified file 'hooks/nrpe-external-master-relation-changed' | |||
2 | --- hooks/nrpe-external-master-relation-changed 2013-08-30 16:43:46 +0000 | |||
3 | +++ hooks/nrpe-external-master-relation-changed 2014-03-11 10:30:01 +0000 | |||
4 | @@ -7,14 +7,19 @@ | |||
5 | 7 | . scripts/common | 7 | . scripts/common |
6 | 8 | 8 | ||
7 | 9 | unit_name=${JUJU_UNIT_NAME////-} | 9 | unit_name=${JUJU_UNIT_NAME////-} |
9 | 10 | nagios_hostname="$(config-get nagios_context)-${unit_name}" | 10 | export nagios_hostname="$(config-get nagios_context)-${unit_name}" |
10 | 11 | nagios_logdir='/var/log/nagios' | 11 | nagios_logdir='/var/log/nagios' |
11 | 12 | nagios_check_script=/usr/local/lib/nagios/plugins/check-pgbouncer.py | 12 | nagios_check_script=/usr/local/lib/nagios/plugins/check-pgbouncer.py |
14 | 13 | nagios_servicegroup=$(config-get nagios_context) | 13 | export nagios_servicegroup=$(config-get nagios_context) |
15 | 14 | address=$(config-get listen_addr) | 14 | export address=$(config-get listen_addr) |
16 | 15 | if [ "$address" = "*" ]; then | 15 | if [ "$address" = "*" ]; then |
18 | 16 | address=$(unit-get private-address) | 16 | export address=$(unit-get private-address) |
19 | 17 | fi | 17 | fi |
20 | 18 | export listen_port=$(config-get listen_port) | ||
21 | 19 | export wait_warn=$(config-get wait_warn) | ||
22 | 20 | export wait_crit=$(config-get wait_crit) | ||
23 | 21 | export conn_warn=$(config-get conn_warn) | ||
24 | 22 | export conn_crit=$(config-get conn_crit) | ||
25 | 18 | 23 | ||
26 | 19 | nagios_uid=$(getent passwd nagios | awk -F: '{ print $3}') | 24 | nagios_uid=$(getent passwd nagios | awk -F: '{ print $3}') |
27 | 20 | nagios_gid=$(getent passwd nagios | awk -F: '{ print $4}') | 25 | nagios_gid=$(getent passwd nagios | awk -F: '{ print $4}') |
28 | @@ -38,33 +43,21 @@ | |||
29 | 38 | rm -f $FILE | 43 | rm -f $FILE |
30 | 39 | done | 44 | done |
31 | 40 | 45 | ||
32 | 41 | # Export Juju configs and relations. | ||
33 | 42 | juju_configs=$(/bin/mktemp --suffix=.juju) | ||
34 | 43 | config-get --format=json -o $juju_configs | ||
35 | 44 | |||
36 | 45 | # Insert variables | ||
37 | 46 | sed -i -e "s/}$/,\"nagios_hostname\": \"$nagios_hostname\" }/" $juju_configs | ||
38 | 47 | sed -i -e "s/}$/,\"address\": \"$address\" }/" $juju_configs | ||
39 | 48 | sed -i -e "s/}$/,\"nagios_servicegroup\": \"$nagios_servicegroup\" }/" $juju_configs | ||
40 | 49 | |||
41 | 50 | # NRPE check files | 46 | # NRPE check files |
42 | 51 | nrpe_check_pgbouncer_connection_count="/etc/nagios/nrpe.d/check_pgbouncer_connection_count.cfg" | 47 | nrpe_check_pgbouncer_connection_count="/etc/nagios/nrpe.d/check_pgbouncer_connection_count.cfg" |
43 | 52 | nrpe_check_pgbouncer_pool_waittime="/etc/nagios/nrpe.d/check_pgbouncer_pool_waittime.cfg" | 48 | nrpe_check_pgbouncer_pool_waittime="/etc/nagios/nrpe.d/check_pgbouncer_pool_waittime.cfg" |
44 | 53 | nrpe_check_pgbouncer_connection_count_template="${CHARM_DIR}/templates/nrpe_check_pgbouncer_connection_count.tmpl" | 49 | nrpe_check_pgbouncer_connection_count_template="${CHARM_DIR}/templates/nrpe_check_pgbouncer_connection_count.tmpl" |
45 | 54 | nrpe_check_pgbouncer_pool_waittime_template="${CHARM_DIR}/templates/nrpe_check_pgbouncer_pool_waittime.tmpl" | 50 | nrpe_check_pgbouncer_pool_waittime_template="${CHARM_DIR}/templates/nrpe_check_pgbouncer_pool_waittime.tmpl" |
48 | 55 | scripts/template-generate.py -j $juju_configs $nrpe_check_pgbouncer_connection_count_template > $nrpe_check_pgbouncer_connection_count | 51 | cheetah fill --env --stdout $nrpe_check_pgbouncer_connection_count_template > $nrpe_check_pgbouncer_connection_count |
49 | 56 | scripts/template-generate.py -j $juju_configs $nrpe_check_pgbouncer_pool_waittime_template > $nrpe_check_pgbouncer_pool_waittime | 52 | cheetah fill --env --stdout $nrpe_check_pgbouncer_pool_waittime_template > $nrpe_check_pgbouncer_pool_waittime |
50 | 57 | 53 | ||
51 | 58 | # Exported service files | 54 | # Exported service files |
52 | 59 | nrpe_check_pgbouncer_connection_count_service_file="/var/lib/nagios/export/service__${nagios_servicegroup}-${nagios_hostname}_check_pgbouncer_connection_count.cfg" | 55 | nrpe_check_pgbouncer_connection_count_service_file="/var/lib/nagios/export/service__${nagios_servicegroup}-${nagios_hostname}_check_pgbouncer_connection_count.cfg" |
53 | 60 | nrpe_check_pgbouncer_pool_waittime_service_file="/var/lib/nagios/export/service__${nagios_servicegroup}-${nagios_hostname}_check_pgbouncer_pool_waittime.cfg" | 56 | nrpe_check_pgbouncer_pool_waittime_service_file="/var/lib/nagios/export/service__${nagios_servicegroup}-${nagios_hostname}_check_pgbouncer_pool_waittime.cfg" |
54 | 61 | nrpe_check_pgbouncer_connection_count_service_file_template="${CHARM_DIR}/templates/nrpe_check_pgbouncer_connection_count_service_file.tmpl" | 57 | nrpe_check_pgbouncer_connection_count_service_file_template="${CHARM_DIR}/templates/nrpe_check_pgbouncer_connection_count_service_file.tmpl" |
55 | 62 | nrpe_check_pgbouncer_pool_waittime_service_file_template="${CHARM_DIR}/templates/nrpe_check_pgbouncer_pool_waittime_service_file.tmpl" | 58 | nrpe_check_pgbouncer_pool_waittime_service_file_template="${CHARM_DIR}/templates/nrpe_check_pgbouncer_pool_waittime_service_file.tmpl" |
61 | 63 | scripts/template-generate.py -j $juju_configs $nrpe_check_pgbouncer_connection_count_service_file_template > $nrpe_check_pgbouncer_connection_count_service_file | 59 | cheetah fill --env --stdout $nrpe_check_pgbouncer_connection_count_service_file_template > $nrpe_check_pgbouncer_connection_count_service_file |
62 | 64 | scripts/template-generate.py -j $juju_configs $nrpe_check_pgbouncer_pool_waittime_service_file_template > $nrpe_check_pgbouncer_pool_waittime_service_file | 60 | cheetah fill --env --stdout $nrpe_check_pgbouncer_pool_waittime_service_file_template > $nrpe_check_pgbouncer_pool_waittime_service_file |
58 | 65 | |||
59 | 66 | # Clean up | ||
60 | 67 | rm -f $juju_configs | ||
63 | 68 | 61 | ||
64 | 69 | # Create nagios user in pgbouncer | 62 | # Create nagios user in pgbouncer |
65 | 70 | nagios_home=$(getent passwd nagios | awk -F: '{ print $6 }') | 63 | nagios_home=$(getent passwd nagios | awk -F: '{ print $6 }') |
66 | 71 | 64 | ||
67 | === modified file 'scripts/common' | |||
68 | --- scripts/common 2013-07-26 04:08:19 +0000 | |||
69 | +++ scripts/common 2014-03-11 10:30:01 +0000 | |||
70 | @@ -24,12 +24,8 @@ | |||
71 | 24 | local pgbouncer_config="/etc/pgbouncer/pgbouncer.ini" | 24 | local pgbouncer_config="/etc/pgbouncer/pgbouncer.ini" |
72 | 25 | local general_template="templates/pgbouncer.ini.general.tmpl" | 25 | local general_template="templates/pgbouncer.ini.general.tmpl" |
73 | 26 | 26 | ||
74 | 27 | # Export Juju configs and relations. | ||
75 | 28 | local juju_configs=$(/bin/mktemp --suffix=.juju) | ||
76 | 29 | config-get --format=json -o $juju_configs | ||
77 | 30 | |||
78 | 31 | # Copy existing config file to preserve permissions. | 27 | # Copy existing config file to preserve permissions. |
80 | 32 | local pgbouncer_config_new=$(/bin/mktemp --suffix=.juju) | 28 | local pgbouncer_config_new=$(/bin/mktemp --suffix=.juju) |
81 | 33 | cp -a $pgbouncer_config $pgbouncer_config_new | 29 | cp -a $pgbouncer_config $pgbouncer_config_new |
82 | 34 | 30 | ||
83 | 35 | # Generate the "databases" section first from list of Juju relations. | 31 | # Generate the "databases" section first from list of Juju relations. |
84 | @@ -39,15 +35,31 @@ | |||
85 | 39 | 35 | ||
86 | 40 | # Generate the general "pgbouncer" section with configuration options | 36 | # Generate the general "pgbouncer" section with configuration options |
87 | 41 | # from Juju's configs. | 37 | # from Juju's configs. |
91 | 42 | # Need to use cheetah cli. Setup vars and excuete cheetah-compile??? | 38 | export admin_users=$(config-get admin_users) |
92 | 43 | scripts/template-generate.py -j $juju_configs $general_template \ | 39 | export auth_file=$(config-get auth_file) |
93 | 44 | >> $pgbouncer_config_new | 40 | export auth_type=$(config-get auth_type) |
94 | 41 | export client_login_timeout=$(config-get client_login_timeout) | ||
95 | 42 | export default_pool_size=$(config-get default_pool_size) | ||
96 | 43 | export ignore_startup_parameters=$(config-get ignore_startup_parameters) | ||
97 | 44 | export listen_addr=$(config-get listen_addr) | ||
98 | 45 | export listen_port=$(config-get listen_port) | ||
99 | 46 | export logfile=$(config-get logfile) | ||
100 | 47 | export max_client_conn=$(config-get max_client_conn) | ||
101 | 48 | export pidfile=$(config-get pidfile) | ||
102 | 49 | export pool_mode=$(config-get pool_mode) | ||
103 | 50 | export reserve_pool_size=$(config-get reserve_pool_size) | ||
104 | 51 | export server_check_delay=$(config-get server_check_delay) | ||
105 | 52 | export server_connect_timeout=$(config-get server_connect_timeout) | ||
106 | 53 | export server_idle_timeout=$(config-get server_idle_timeout) | ||
107 | 54 | export server_lifetime=$(config-get server_lifetime) | ||
108 | 55 | export server_login_retry=$(config-get server_login_retry) | ||
109 | 56 | cheetah fill --env --stdout $general_template >> $pgbouncer_config_new | ||
110 | 45 | 57 | ||
111 | 46 | # Looks good, let's make it live. | 58 | # Looks good, let's make it live. |
112 | 47 | mv $pgbouncer_config_new $pgbouncer_config | 59 | mv $pgbouncer_config_new $pgbouncer_config |
113 | 48 | 60 | ||
114 | 49 | # Clean up | 61 | # Clean up |
116 | 50 | rm -f $pgbouncer_userlist_new $juju_configs $pgbouncer_config_new | 62 | rm -f $pgbouncer_userlist_new $pgbouncer_config_new |
117 | 51 | 63 | ||
118 | 52 | juju-log "reloading pgbouncer" | 64 | juju-log "reloading pgbouncer" |
119 | 53 | service pgbouncer reload || exit 1 | 65 | service pgbouncer reload || exit 1 |
120 | 54 | 66 | ||
121 | === removed file 'scripts/template-generate.py' | |||
122 | --- scripts/template-generate.py 2012-10-03 16:59:12 +0000 | |||
123 | +++ scripts/template-generate.py 1970-01-01 00:00:00 +0000 | |||
124 | @@ -1,150 +0,0 @@ | |||
125 | 1 | #!/usr/bin/python | ||
126 | 2 | |||
127 | 3 | # Copyright 2012 Canonical Ltd. All rights reserved. | ||
128 | 4 | # Author: Haw Loeung <haw.loeung@canonical.com> | ||
129 | 5 | |||
130 | 6 | # Script used to generate file using python-cheetah template. | ||
131 | 7 | |||
132 | 8 | from optparse import OptionParser | ||
133 | 9 | from Cheetah.Template import Template | ||
134 | 10 | import json | ||
135 | 11 | import sys | ||
136 | 12 | |||
137 | 13 | |||
138 | 14 | ############################################################################### | ||
139 | 15 | # Supporting functions | ||
140 | 16 | ############################################################################### | ||
141 | 17 | |||
142 | 18 | #------------------------------------------------------------------------------ | ||
143 | 19 | # config_get: Returns a dictionary containing all of the config information | ||
144 | 20 | # Optional parameter: scope | ||
145 | 21 | # scope: limits the scope of the returned configuration to the | ||
146 | 22 | # desired config item. | ||
147 | 23 | #------------------------------------------------------------------------------ | ||
148 | 24 | def config_get(scope=None): | ||
149 | 25 | try: | ||
150 | 26 | config_cmd_line = ['config-get'] | ||
151 | 27 | if scope is not None: | ||
152 | 28 | config_cmd_line.append(scope) | ||
153 | 29 | config_cmd_line.append('--format=json') | ||
154 | 30 | config_data = json.loads(subprocess.check_output(config_cmd_line)) | ||
155 | 31 | except Exception, e: | ||
156 | 32 | subprocess.call(['juju-log', str(e)]) | ||
157 | 33 | config_data = None | ||
158 | 34 | finally: | ||
159 | 35 | return(config_data) | ||
160 | 36 | |||
161 | 37 | #------------------------------------------------------------------------------ | ||
162 | 38 | # relation_json: Returns json-formatted relation data | ||
163 | 39 | # Optional parameters: scope, relation_id | ||
164 | 40 | # scope: limits the scope of the returned data to the | ||
165 | 41 | # desired item. | ||
166 | 42 | # unit_name: limits the data ( and optionally the scope ) | ||
167 | 43 | # to the specified unit | ||
168 | 44 | # relation_id: specify relation id for out of context usage. | ||
169 | 45 | #------------------------------------------------------------------------------ | ||
170 | 46 | def relation_json(scope=None, unit_name=None, relation_id=None): | ||
171 | 47 | try: | ||
172 | 48 | relation_cmd_line = ['relation-get', '--format=json'] | ||
173 | 49 | if relation_id is not None: | ||
174 | 50 | relation_cmd_line.extend(('-r', relation_id)) | ||
175 | 51 | if scope is not None: | ||
176 | 52 | relation_cmd_line.append(scope) | ||
177 | 53 | else: | ||
178 | 54 | relation_cmd_line.append('-') | ||
179 | 55 | relation_cmd_line.append(unit_name) | ||
180 | 56 | relation_data = subprocess.check_output(relation_cmd_line) | ||
181 | 57 | except Exception, e: | ||
182 | 58 | subprocess.call(['juju-log', str(e)]) | ||
183 | 59 | relation_data = None | ||
184 | 60 | finally: | ||
185 | 61 | return(relation_data) | ||
186 | 62 | |||
187 | 63 | #------------------------------------------------------------------------------ | ||
188 | 64 | # relation_get: Returns a dictionary containing the relation information | ||
189 | 65 | # Optional parameters: scope, relation_id | ||
190 | 66 | # scope: limits the scope of the returned data to the | ||
191 | 67 | # desired item. | ||
192 | 68 | # unit_name: limits the data ( and optionally the scope ) | ||
193 | 69 | # to the specified unit | ||
194 | 70 | # relation_id: specify relation id for out of context usage. | ||
195 | 71 | #------------------------------------------------------------------------------ | ||
196 | 72 | def relation_get(scope=None, unit_name=None, relation_id=None): | ||
197 | 73 | try: | ||
198 | 74 | relation_data = json.loads(relation_json()) | ||
199 | 75 | except Exception, e: | ||
200 | 76 | subprocess.call(['juju-log', str(e)]) | ||
201 | 77 | relation_data = None | ||
202 | 78 | finally: | ||
203 | 79 | return(relation_data) | ||
204 | 80 | |||
205 | 81 | #------------------------------------------------------------------------------ | ||
206 | 82 | # relation_ids: Returns a list of relation ids | ||
207 | 83 | # optional parameters: relation_type | ||
208 | 84 | # relation_type: return relations only of this type | ||
209 | 85 | #------------------------------------------------------------------------------ | ||
210 | 86 | def relation_ids(relation_types=['db-proxy','backend-db-admin']): | ||
211 | 87 | # accept strings or iterators | ||
212 | 88 | if isinstance(relation_types, basestring): | ||
213 | 89 | reltypes = [relation_types,] | ||
214 | 90 | else: | ||
215 | 91 | reltypes = relation_types | ||
216 | 92 | relids = [] | ||
217 | 93 | for reltype in reltypes: | ||
218 | 94 | relid_cmd_line = ['relation-ids', '--format=json',reltype] | ||
219 | 95 | relids.extend(json.loads(subprocess.check_output(relid_cmd_line))) | ||
220 | 96 | return relids | ||
221 | 97 | |||
222 | 98 | #------------------------------------------------------------------------------ | ||
223 | 99 | # relation_get_all: Returns a dictionary containing the relation information | ||
224 | 100 | # optional parameters: relation_type | ||
225 | 101 | # relation_type: limits the scope of the returned data to the | ||
226 | 102 | # desired item. | ||
227 | 103 | #------------------------------------------------------------------------------ | ||
228 | 104 | def relation_get_all(): | ||
229 | 105 | reldata = {} | ||
230 | 106 | try: | ||
231 | 107 | relids = relation_ids() | ||
232 | 108 | for relid in relids: | ||
233 | 109 | units_cmd_line = ['relation-list','--format=json','-r',relid] | ||
234 | 110 | units = json.loads(subprocess.check_output(units_cmd_line)) | ||
235 | 111 | for unit in units: | ||
236 | 112 | reldata[unit] = json.loads(relation_json(relation_id=relid,unit_name=unit)) | ||
237 | 113 | reldata[unit]['relation-id'] = relid | ||
238 | 114 | reldata[unit]['name'] = unit.replace("/","_") | ||
239 | 115 | except Exception, e: | ||
240 | 116 | subprocess.call(['juju-log', str(e)]) | ||
241 | 117 | reldata = [] | ||
242 | 118 | finally: | ||
243 | 119 | return(reldata) | ||
244 | 120 | |||
245 | 121 | #------------------------------------------------------------------------------ | ||
246 | 122 | # template_generate: Generate the cheetah template | ||
247 | 123 | # template: Template file | ||
248 | 124 | # searchlist: searchlist dictionary of configuration | ||
249 | 125 | #------------------------------------------------------------------------------ | ||
250 | 126 | def template_generate(template, searchlist): | ||
251 | 127 | """ Generate file using specified python-cheetah template file """ | ||
252 | 128 | |||
253 | 129 | if not template: | ||
254 | 130 | print "ERROR: No template provided." | ||
255 | 131 | return -1 | ||
256 | 132 | |||
257 | 133 | tmpl = Template(file=template, searchList=searchlist) | ||
258 | 134 | print tmpl | ||
259 | 135 | |||
260 | 136 | |||
261 | 137 | def main(): | ||
262 | 138 | parser = OptionParser() | ||
263 | 139 | parser.add_option("-j", "--json", dest="json", | ||
264 | 140 | help="JSON file to read in for template searchlist") | ||
265 | 141 | |||
266 | 142 | (options, args) = parser.parse_args() | ||
267 | 143 | searchlist = dict(json.loads(open(options.json).read()).items() + relation_get_all().items()) | ||
268 | 144 | |||
269 | 145 | for template in args: | ||
270 | 146 | template_generate(template, searchlist) | ||
271 | 147 | |||
272 | 148 | |||
273 | 149 | if __name__ == '__main__': | ||
274 | 150 | sys.exit(main()) |
This change has been tested in a number of live environments, including a production service.