Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env python
# This script adds a EPICS archiver appliance MySQL connection pool to a Tomcat context.xml
# The location of context.xml is determined by the ${CATALINA_HOME} environment variable
# The parameters for the connection pool are determined by the ${MYSQL_HOST},
# ${MYSQL_DATABASE}, ${MYSQL_USER} and ${MYSQL_PASSWORD} environment variables.
try:
import sys
import os
import xml.dom.minidom
mysql_host = os.getenv('MYSQL_HOST')
if not mysql_host:
print 'The environment variable MYSQL_HOST is not defined. Assuming localhost'
mysql_host = 'localhost'
mysql_database = os.getenv('MYSQL_DATABASE')
if not mysql_database:
print 'The environment variable MYSQL_DATABASE is not defined.'
sys.exit(1)
mysql_user = os.getenv('MYSQL_USER')
if not mysql_user:
print 'The environment variable MYSQL_USER is not defined.'
sys.exit(1)
mysql_password = os.getenv('MYSQL_PASSWORD')
if not mysql_password:
print 'The environment variable MYSQL_PASSWORD is not defined.'
sys.exit(1)
catalina_home = os.getenv("CATALINA_HOME")
if not catalina_home:
print "We determine the location of context.xml using the environment variable CATALINA_HOME which does not seem to be set."
sys.exit(1)
context_xml = catalina_home + '/conf/context.xml'
print "Setting MySQL connection parameters in ", context_xml
context_dom = xml.dom.minidom.parse(context_xml)
connpool = context_dom.createElement('Resource')
connpool.setAttribute('name', "jdbc/archappl")
connpool.setAttribute('auth', "Container")
connpool.setAttribute('type', "javax.sql.DataSource")
connpool.setAttribute(
'factory', "org.apache.tomcat.jdbc.pool.DataSourceFactory")
connpool.setAttribute('testWhileIdle', "true")
connpool.setAttribute('testOnBorrow', "true")
connpool.setAttribute('testOnReturn', "false")
connpool.setAttribute('validationQuery', "SELECT 1")
connpool.setAttribute('validationInterval', "30000")
connpool.setAttribute('timeBetweenEvictionRunsMillis', "30000")
connpool.setAttribute('maxActive', "10")
connpool.setAttribute('minIdle', "2")
connpool.setAttribute('maxWait', "10000")
connpool.setAttribute('initialSize', "2")
connpool.setAttribute('removeAbandonedTimeout', "60")
connpool.setAttribute('removeAbandoned', "true")
connpool.setAttribute('logAbandoned', "true")
connpool.setAttribute('minEvictableIdleTimeMillis', "30000")
connpool.setAttribute('jmxEnabled', "true")
connpool.setAttribute('driverClassName', "com.mysql.jdbc.Driver")
connpool.setAttribute('url', "jdbc:mysql://" +
mysql_host + ":3306/" + mysql_database)
connpool.setAttribute('username', mysql_user)
connpool.setAttribute('password', mysql_password)
context_dom.getElementsByTagName('Context')[0].appendChild(connpool)
with open(context_xml, 'w') as file:
file.write(context_dom.toprettyxml())
except Exception as err:
print "ERROR: ", err