Merge lp:~mthaddon/charms/precise/pgbouncer/remove-template-generate into lp:charms/pgbouncer

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
Reviewer Review Type Date Requested Status
Liam Young (community) Approve
Review via email: mp+210372@code.launchpad.net

Description of the change

Per https://bugs.launchpad.net/charms/+bug/1046318/comments/1 remove template-generate and use cheetah command line instead

To post a comment you must log in.
Revision history for this message
Tom Haddon (mthaddon) wrote :

This change has been tested in a number of live environments, including a production service.

Revision history for this message
Liam Young (gnuoy) wrote :

LGTM

review: Approve

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

Subscribers

People subscribed via source and target branches

to all changes: