1
'''Functions related to apache use within this charm'''
6
# pylint can't find the modules # pylint: disable=F0401
7
from charmhelpers import (
11
from charmhelpers.core import (
17
# pylint: enable=F0401
24
@util.run_as_user('root')
26
'''Perform the install/config of apache and disable the default site'''
28
LOG('Installing apache', hookenv.INFO)
29
fetch.apt_install(SERVICE, fatal=True)
31
subprocess.check_call(['a2enmod', 'expires'])
32
subprocess.check_call(['a2enmod', 'proxy'])
33
subprocess.check_call(['a2enmod', 'proxy_http'])
34
subprocess.check_call(['a2enmod', 'headers'])
35
subprocess.check_call(['a2dismod', 'mpm_event'])
36
subprocess.check_call(['a2enmod', 'mpm_worker'])
37
subprocess.check_call(['a2dissite', '000-default'])
40
@util.run_as_user('root')
42
'''Start the service and remove the override file blocking startup'''
44
LOG('Starting apache', hookenv.INFO)
45
override_path = '/etc/init/apache2.override'
46
if os.path.exists(override_path):
47
os.remove('/etc/init/apache2.override')
49
subprocess.call(['a2ensite', 'archive_ubuntu_com'])
50
if not service_running():
51
host.service_start(SERVICE)
53
host.service_reload(SERVICE)
56
@util.run_as_user('root')
58
'''Stop the service and block startup at boot'''
60
LOG('Stopping apache', hookenv.INFO)
61
with open('/etc/init/apache2.override', 'w+') as override_file:
62
override_file.write('manual\n')
64
host.service_stop(SERVICE)
65
subprocess.call(['a2dissite', 'archive_ubuntu_com'])
68
@util.run_as_user('root')
69
def service_running():
70
'''Return True is the service is running'''
72
return host.service_running(SERVICE)
75
def create_mpm_workerfile():
76
'''Create the multi-processing module (MPM) configuration'''
78
config = hookenv.config()
80
'startservers': config['apache2_mpm_startservers'],
81
'minsparethreads': config['apache2_mpm_minsparethreads'],
82
'maxsparethreads': config['apache2_mpm_maxsparethreads'],
83
'threadlimit': config['apache2_mpm_threadlimit'],
84
'threadsperchild': config['apache2_mpm_threadsperchild'],
85
'serverlimit': config['apache2_mpm_serverlimit'],
86
'maxrequestworkers': config['apache2_mpm_maxrequestworkers'],
87
'maxconnectionsperchild': config['apache2_mpm_maxconnectionsperchild'],
89
templating.render('apache2/mpm_worker.template',
90
'/etc/apache2/conf-available/000mpm-worker.conf',
92
subprocess.check_call(['a2enconf', '000mpm-worker'])
95
def create_security():
96
'''Create the security configuration'''
98
config = hookenv.config()
100
'server_tokens': config['apache2_server_tokens'],
101
'server_signature': config['apache2_server_signature'],
102
'trace_enabled': config['apache2_trace_enabled'],
104
templating.render('apache2/security.template',
105
'/etc/apache2/conf-available/security.conf',
107
subprocess.check_call(['a2enconf', 'security'])
110
def create_metadata_site():
111
'''Create the site config for serving (or proxying) repository metadata'''
113
config = hookenv.config()
115
apache_context['DocumentRoot'] = config['apache-root']
116
apache_context['Sync_Host'] = config['sync-host']
117
metaproxy_conf = '# No failover configured'
118
failover_host = util.get_failover_host()
119
if util.in_failover() and failover_host:
120
uri = 'http://%s:80' % failover_host
121
metaproxy_conf = ('ProxyRemote http://archive.ubuntu.com/ %s\n'
122
'\tProxyPass / http://archive.ubuntu.com/\n'
123
'\tProxyPassReverse / http://archive.ubuntu.com/' %
125
apache_context['MetadataProxy'] = metaproxy_conf
127
templating.render('apache2/archive_ubuntu_com.conf',
128
'/etc/apache2/sites-available/archive_ubuntu_com.conf',
132
@util.run_as_user('root')
133
def render_configs():
134
'''Render the configuration templates for apache'''
136
LOG('Rendering apache2 configuration templates')
137
config = hookenv.config()
138
if config.changed('apache2_mpm_type'):
139
if config['apache2_mpm_type'] == 'worker':
140
LOG('Using apache2-mpm-worker')
141
fetch.apt_install('apache2-mpm-worker', fatal=True)
142
subprocess.check_call(['a2dismod', 'mpm_prefork'])
143
subprocess.check_call(['a2enmod', 'mpm_worker'])
144
fetch.apt_purge('apache2-mpm-prefork', fatal=True)
145
elif config['apache2_mpm_type'] == 'prefork':
146
LOG('Using apache2-mpm-prefork')
147
fetch.apt_install('apache2-mpm-prefork', fatal=True)
148
subprocess.check_call(['a2dismod', 'mpm_worker'])
149
subprocess.check_call(['a2enmod', 'mpm_prefork'])
150
fetch.apt_purge('apache2-mpm-worker', fatal=True)
152
create_metadata_site()
153
create_mpm_workerfile()
157
def update_checks(nrpe_config):
158
'''Update nagios check for apache serving archive metadata'''
160
nrpe_config.add_check(
162
description='Apache2 serving archive metadata',
163
check_cmd='check_http --hostname localhost --url "/ubuntu/" '
164
'--string "dists/" --expect \"200\\ OK\"',