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
=== modified file 'hooks/nrpe-external-master-relation-changed'
--- hooks/nrpe-external-master-relation-changed 2013-08-30 16:43:46 +0000
+++ hooks/nrpe-external-master-relation-changed 2014-03-11 10:30:01 +0000
@@ -7,14 +7,19 @@
7. scripts/common7. scripts/common
88
9unit_name=${JUJU_UNIT_NAME////-}9unit_name=${JUJU_UNIT_NAME////-}
10nagios_hostname="$(config-get nagios_context)-${unit_name}" 10export nagios_hostname="$(config-get nagios_context)-${unit_name}"
11nagios_logdir='/var/log/nagios'11nagios_logdir='/var/log/nagios'
12nagios_check_script=/usr/local/lib/nagios/plugins/check-pgbouncer.py12nagios_check_script=/usr/local/lib/nagios/plugins/check-pgbouncer.py
13nagios_servicegroup=$(config-get nagios_context)13export nagios_servicegroup=$(config-get nagios_context)
14address=$(config-get listen_addr)14export address=$(config-get listen_addr)
15if [ "$address" = "*" ]; then15if [ "$address" = "*" ]; then
16 address=$(unit-get private-address)16 export address=$(unit-get private-address)
17fi17fi
18export listen_port=$(config-get listen_port)
19export wait_warn=$(config-get wait_warn)
20export wait_crit=$(config-get wait_crit)
21export conn_warn=$(config-get conn_warn)
22export conn_crit=$(config-get conn_crit)
1823
19nagios_uid=$(getent passwd nagios | awk -F: '{ print $3}')24nagios_uid=$(getent passwd nagios | awk -F: '{ print $3}')
20nagios_gid=$(getent passwd nagios | awk -F: '{ print $4}')25nagios_gid=$(getent passwd nagios | awk -F: '{ print $4}')
@@ -38,33 +43,21 @@
38 rm -f $FILE43 rm -f $FILE
39done44done
4045
41# Export Juju configs and relations.
42juju_configs=$(/bin/mktemp --suffix=.juju)
43config-get --format=json -o $juju_configs
44
45# Insert variables
46sed -i -e "s/}$/,\"nagios_hostname\": \"$nagios_hostname\" }/" $juju_configs
47sed -i -e "s/}$/,\"address\": \"$address\" }/" $juju_configs
48sed -i -e "s/}$/,\"nagios_servicegroup\": \"$nagios_servicegroup\" }/" $juju_configs
49
50# NRPE check files46# NRPE check files
51nrpe_check_pgbouncer_connection_count="/etc/nagios/nrpe.d/check_pgbouncer_connection_count.cfg"47nrpe_check_pgbouncer_connection_count="/etc/nagios/nrpe.d/check_pgbouncer_connection_count.cfg"
52nrpe_check_pgbouncer_pool_waittime="/etc/nagios/nrpe.d/check_pgbouncer_pool_waittime.cfg"48nrpe_check_pgbouncer_pool_waittime="/etc/nagios/nrpe.d/check_pgbouncer_pool_waittime.cfg"
53nrpe_check_pgbouncer_connection_count_template="${CHARM_DIR}/templates/nrpe_check_pgbouncer_connection_count.tmpl"49nrpe_check_pgbouncer_connection_count_template="${CHARM_DIR}/templates/nrpe_check_pgbouncer_connection_count.tmpl"
54nrpe_check_pgbouncer_pool_waittime_template="${CHARM_DIR}/templates/nrpe_check_pgbouncer_pool_waittime.tmpl"50nrpe_check_pgbouncer_pool_waittime_template="${CHARM_DIR}/templates/nrpe_check_pgbouncer_pool_waittime.tmpl"
55scripts/template-generate.py -j $juju_configs $nrpe_check_pgbouncer_connection_count_template > $nrpe_check_pgbouncer_connection_count51cheetah fill --env --stdout $nrpe_check_pgbouncer_connection_count_template > $nrpe_check_pgbouncer_connection_count
56scripts/template-generate.py -j $juju_configs $nrpe_check_pgbouncer_pool_waittime_template > $nrpe_check_pgbouncer_pool_waittime52cheetah fill --env --stdout $nrpe_check_pgbouncer_pool_waittime_template > $nrpe_check_pgbouncer_pool_waittime
5753
58# Exported service files54# Exported service files
59nrpe_check_pgbouncer_connection_count_service_file="/var/lib/nagios/export/service__${nagios_servicegroup}-${nagios_hostname}_check_pgbouncer_connection_count.cfg"55nrpe_check_pgbouncer_connection_count_service_file="/var/lib/nagios/export/service__${nagios_servicegroup}-${nagios_hostname}_check_pgbouncer_connection_count.cfg"
60nrpe_check_pgbouncer_pool_waittime_service_file="/var/lib/nagios/export/service__${nagios_servicegroup}-${nagios_hostname}_check_pgbouncer_pool_waittime.cfg"56nrpe_check_pgbouncer_pool_waittime_service_file="/var/lib/nagios/export/service__${nagios_servicegroup}-${nagios_hostname}_check_pgbouncer_pool_waittime.cfg"
61nrpe_check_pgbouncer_connection_count_service_file_template="${CHARM_DIR}/templates/nrpe_check_pgbouncer_connection_count_service_file.tmpl"57nrpe_check_pgbouncer_connection_count_service_file_template="${CHARM_DIR}/templates/nrpe_check_pgbouncer_connection_count_service_file.tmpl"
62nrpe_check_pgbouncer_pool_waittime_service_file_template="${CHARM_DIR}/templates/nrpe_check_pgbouncer_pool_waittime_service_file.tmpl"58nrpe_check_pgbouncer_pool_waittime_service_file_template="${CHARM_DIR}/templates/nrpe_check_pgbouncer_pool_waittime_service_file.tmpl"
63scripts/template-generate.py -j $juju_configs $nrpe_check_pgbouncer_connection_count_service_file_template > $nrpe_check_pgbouncer_connection_count_service_file59cheetah fill --env --stdout $nrpe_check_pgbouncer_connection_count_service_file_template > $nrpe_check_pgbouncer_connection_count_service_file
64scripts/template-generate.py -j $juju_configs $nrpe_check_pgbouncer_pool_waittime_service_file_template > $nrpe_check_pgbouncer_pool_waittime_service_file60cheetah fill --env --stdout $nrpe_check_pgbouncer_pool_waittime_service_file_template > $nrpe_check_pgbouncer_pool_waittime_service_file
65
66# Clean up
67rm -f $juju_configs
6861
69# Create nagios user in pgbouncer62# Create nagios user in pgbouncer
70nagios_home=$(getent passwd nagios | awk -F: '{ print $6 }')63nagios_home=$(getent passwd nagios | awk -F: '{ print $6 }')
7164
=== modified file 'scripts/common'
--- scripts/common 2013-07-26 04:08:19 +0000
+++ scripts/common 2014-03-11 10:30:01 +0000
@@ -24,12 +24,8 @@
24 local pgbouncer_config="/etc/pgbouncer/pgbouncer.ini"24 local pgbouncer_config="/etc/pgbouncer/pgbouncer.ini"
25 local general_template="templates/pgbouncer.ini.general.tmpl"25 local general_template="templates/pgbouncer.ini.general.tmpl"
2626
27 # Export Juju configs and relations.
28 local juju_configs=$(/bin/mktemp --suffix=.juju)
29 config-get --format=json -o $juju_configs
30
31 # Copy existing config file to preserve permissions.27 # Copy existing config file to preserve permissions.
32 local pgbouncer_config_new=$(/bin/mktemp --suffix=.juju)28 local pgbouncer_config_new=$(/bin/mktemp --suffix=.juju)
33 cp -a $pgbouncer_config $pgbouncer_config_new29 cp -a $pgbouncer_config $pgbouncer_config_new
3430
35 # Generate the "databases" section first from list of Juju relations.31 # Generate the "databases" section first from list of Juju relations.
@@ -39,15 +35,31 @@
3935
40 # Generate the general "pgbouncer" section with configuration options36 # Generate the general "pgbouncer" section with configuration options
41 # from Juju's configs.37 # from Juju's configs.
42 # Need to use cheetah cli. Setup vars and excuete cheetah-compile???38 export admin_users=$(config-get admin_users)
43 scripts/template-generate.py -j $juju_configs $general_template \39 export auth_file=$(config-get auth_file)
44 >> $pgbouncer_config_new40 export auth_type=$(config-get auth_type)
41 export client_login_timeout=$(config-get client_login_timeout)
42 export default_pool_size=$(config-get default_pool_size)
43 export ignore_startup_parameters=$(config-get ignore_startup_parameters)
44 export listen_addr=$(config-get listen_addr)
45 export listen_port=$(config-get listen_port)
46 export logfile=$(config-get logfile)
47 export max_client_conn=$(config-get max_client_conn)
48 export pidfile=$(config-get pidfile)
49 export pool_mode=$(config-get pool_mode)
50 export reserve_pool_size=$(config-get reserve_pool_size)
51 export server_check_delay=$(config-get server_check_delay)
52 export server_connect_timeout=$(config-get server_connect_timeout)
53 export server_idle_timeout=$(config-get server_idle_timeout)
54 export server_lifetime=$(config-get server_lifetime)
55 export server_login_retry=$(config-get server_login_retry)
56 cheetah fill --env --stdout $general_template >> $pgbouncer_config_new
4557
46 # Looks good, let's make it live.58 # Looks good, let's make it live.
47 mv $pgbouncer_config_new $pgbouncer_config59 mv $pgbouncer_config_new $pgbouncer_config
4860
49 # Clean up61 # Clean up
50 rm -f $pgbouncer_userlist_new $juju_configs $pgbouncer_config_new62 rm -f $pgbouncer_userlist_new $pgbouncer_config_new
5163
52 juju-log "reloading pgbouncer"64 juju-log "reloading pgbouncer"
53 service pgbouncer reload || exit 165 service pgbouncer reload || exit 1
5466
=== removed file 'scripts/template-generate.py'
--- scripts/template-generate.py 2012-10-03 16:59:12 +0000
+++ scripts/template-generate.py 1970-01-01 00:00:00 +0000
@@ -1,150 +0,0 @@
1#!/usr/bin/python
2
3# Copyright 2012 Canonical Ltd. All rights reserved.
4# Author: Haw Loeung <haw.loeung@canonical.com>
5
6# Script used to generate file using python-cheetah template.
7
8from optparse import OptionParser
9from Cheetah.Template import Template
10import json
11import sys
12
13
14###############################################################################
15# Supporting functions
16###############################################################################
17
18#------------------------------------------------------------------------------
19# config_get: Returns a dictionary containing all of the config information
20# Optional parameter: scope
21# scope: limits the scope of the returned configuration to the
22# desired config item.
23#------------------------------------------------------------------------------
24def config_get(scope=None):
25 try:
26 config_cmd_line = ['config-get']
27 if scope is not None:
28 config_cmd_line.append(scope)
29 config_cmd_line.append('--format=json')
30 config_data = json.loads(subprocess.check_output(config_cmd_line))
31 except Exception, e:
32 subprocess.call(['juju-log', str(e)])
33 config_data = None
34 finally:
35 return(config_data)
36
37#------------------------------------------------------------------------------
38# relation_json: Returns json-formatted relation data
39# Optional parameters: scope, relation_id
40# scope: limits the scope of the returned data to the
41# desired item.
42# unit_name: limits the data ( and optionally the scope )
43# to the specified unit
44# relation_id: specify relation id for out of context usage.
45#------------------------------------------------------------------------------
46def relation_json(scope=None, unit_name=None, relation_id=None):
47 try:
48 relation_cmd_line = ['relation-get', '--format=json']
49 if relation_id is not None:
50 relation_cmd_line.extend(('-r', relation_id))
51 if scope is not None:
52 relation_cmd_line.append(scope)
53 else:
54 relation_cmd_line.append('-')
55 relation_cmd_line.append(unit_name)
56 relation_data = subprocess.check_output(relation_cmd_line)
57 except Exception, e:
58 subprocess.call(['juju-log', str(e)])
59 relation_data = None
60 finally:
61 return(relation_data)
62
63#------------------------------------------------------------------------------
64# relation_get: Returns a dictionary containing the relation information
65# Optional parameters: scope, relation_id
66# scope: limits the scope of the returned data to the
67# desired item.
68# unit_name: limits the data ( and optionally the scope )
69# to the specified unit
70# relation_id: specify relation id for out of context usage.
71#------------------------------------------------------------------------------
72def relation_get(scope=None, unit_name=None, relation_id=None):
73 try:
74 relation_data = json.loads(relation_json())
75 except Exception, e:
76 subprocess.call(['juju-log', str(e)])
77 relation_data = None
78 finally:
79 return(relation_data)
80
81#------------------------------------------------------------------------------
82# relation_ids: Returns a list of relation ids
83# optional parameters: relation_type
84# relation_type: return relations only of this type
85#------------------------------------------------------------------------------
86def relation_ids(relation_types=['db-proxy','backend-db-admin']):
87 # accept strings or iterators
88 if isinstance(relation_types, basestring):
89 reltypes = [relation_types,]
90 else:
91 reltypes = relation_types
92 relids = []
93 for reltype in reltypes:
94 relid_cmd_line = ['relation-ids', '--format=json',reltype]
95 relids.extend(json.loads(subprocess.check_output(relid_cmd_line)))
96 return relids
97
98#------------------------------------------------------------------------------
99# relation_get_all: Returns a dictionary containing the relation information
100# optional parameters: relation_type
101# relation_type: limits the scope of the returned data to the
102# desired item.
103#------------------------------------------------------------------------------
104def relation_get_all():
105 reldata = {}
106 try:
107 relids = relation_ids()
108 for relid in relids:
109 units_cmd_line = ['relation-list','--format=json','-r',relid]
110 units = json.loads(subprocess.check_output(units_cmd_line))
111 for unit in units:
112 reldata[unit] = json.loads(relation_json(relation_id=relid,unit_name=unit))
113 reldata[unit]['relation-id'] = relid
114 reldata[unit]['name'] = unit.replace("/","_")
115 except Exception, e:
116 subprocess.call(['juju-log', str(e)])
117 reldata = []
118 finally:
119 return(reldata)
120
121#------------------------------------------------------------------------------
122# template_generate: Generate the cheetah template
123# template: Template file
124# searchlist: searchlist dictionary of configuration
125#------------------------------------------------------------------------------
126def template_generate(template, searchlist):
127 """ Generate file using specified python-cheetah template file """
128
129 if not template:
130 print "ERROR: No template provided."
131 return -1
132
133 tmpl = Template(file=template, searchList=searchlist)
134 print tmpl
135
136
137def main():
138 parser = OptionParser()
139 parser.add_option("-j", "--json", dest="json",
140 help="JSON file to read in for template searchlist")
141
142 (options, args) = parser.parse_args()
143 searchlist = dict(json.loads(open(options.json).read()).items() + relation_get_all().items())
144
145 for template in args:
146 template_generate(template, searchlist)
147
148
149if __name__ == '__main__':
150 sys.exit(main())

Subscribers

People subscribed via source and target branches

to all changes: