~axino/charms/trusty/ubuntu-repository-cache/test-python3

« back to all changes in this revision

Viewing changes to lib/ubuntu_repository_cache/apache.py.bak

  • Committer: axino
  • Date: 2016-06-03 15:42:06 UTC
  • Revision ID: axino@canonical.com-20160603154206-sot5q63jqd1mtdbi
[axino] upgrade charmhelpers, make code compatible with python3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
'''Functions related to apache use within this charm'''
 
2
 
 
3
import os
 
4
import subprocess
 
5
 
 
6
# pylint can't find the modules  # pylint: disable=F0401
 
7
from charmhelpers import (
 
8
    fetch,
 
9
)
 
10
 
 
11
from charmhelpers.core import (
 
12
    hookenv,
 
13
    host,
 
14
    templating
 
15
)
 
16
import util
 
17
# pylint: enable=F0401
 
18
 
 
19
 
 
20
LOG = hookenv.log
 
21
SERVICE = 'apache2'
 
22
 
 
23
 
 
24
@util.run_as_user('root')
 
25
def install():
 
26
    '''Perform the install/config of apache and disable the default site'''
 
27
 
 
28
    LOG('Installing apache', hookenv.INFO)
 
29
    fetch.apt_install(SERVICE, fatal=True)
 
30
    stop()
 
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'])
 
38
 
 
39
 
 
40
@util.run_as_user('root')
 
41
def start():
 
42
    '''Start the service and remove the override file blocking startup'''
 
43
 
 
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')
 
48
 
 
49
    subprocess.call(['a2ensite', 'archive_ubuntu_com'])
 
50
    if not service_running():
 
51
        host.service_start(SERVICE)
 
52
    else:
 
53
        host.service_reload(SERVICE)
 
54
 
 
55
 
 
56
@util.run_as_user('root')
 
57
def stop():
 
58
    '''Stop the service and block startup at boot'''
 
59
 
 
60
    LOG('Stopping apache', hookenv.INFO)
 
61
    with open('/etc/init/apache2.override', 'w+') as override_file:
 
62
        override_file.write('manual\n')
 
63
 
 
64
    host.service_stop(SERVICE)
 
65
    subprocess.call(['a2dissite', 'archive_ubuntu_com'])
 
66
 
 
67
 
 
68
@util.run_as_user('root')
 
69
def service_running():
 
70
    '''Return True is the service is running'''
 
71
 
 
72
    return host.service_running(SERVICE)
 
73
 
 
74
 
 
75
def create_mpm_workerfile():
 
76
    '''Create the multi-processing module (MPM) configuration'''
 
77
 
 
78
    config = hookenv.config()
 
79
    mpm_context = {
 
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'],
 
88
    }
 
89
    templating.render('apache2/mpm_worker.template',
 
90
                      '/etc/apache2/conf-available/000mpm-worker.conf',
 
91
                      mpm_context)
 
92
    subprocess.check_call(['a2enconf', '000mpm-worker'])
 
93
 
 
94
 
 
95
def create_security():
 
96
    '''Create the security configuration'''
 
97
 
 
98
    config = hookenv.config()
 
99
    security_context = {
 
100
        'server_tokens': config['apache2_server_tokens'],
 
101
        'server_signature': config['apache2_server_signature'],
 
102
        'trace_enabled': config['apache2_trace_enabled'],
 
103
    }
 
104
    templating.render('apache2/security.template',
 
105
                      '/etc/apache2/conf-available/security.conf',
 
106
                      security_context)
 
107
    subprocess.check_call(['a2enconf', 'security'])
 
108
 
 
109
 
 
110
def create_metadata_site():
 
111
    '''Create the site config for serving (or proxying) repository metadata'''
 
112
 
 
113
    config = hookenv.config()
 
114
    apache_context = {}
 
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/' %
 
124
                          uri)
 
125
    apache_context['MetadataProxy'] = metaproxy_conf
 
126
 
 
127
    templating.render('apache2/archive_ubuntu_com.conf',
 
128
                      '/etc/apache2/sites-available/archive_ubuntu_com.conf',
 
129
                      apache_context)
 
130
 
 
131
 
 
132
@util.run_as_user('root')
 
133
def render_configs():
 
134
    '''Render the configuration templates for apache'''
 
135
 
 
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)
 
151
 
 
152
    create_metadata_site()
 
153
    create_mpm_workerfile()
 
154
    create_security()
 
155
 
 
156
 
 
157
def update_checks(nrpe_config):
 
158
    '''Update nagios check for apache serving archive metadata'''
 
159
 
 
160
    nrpe_config.add_check(
 
161
        shortname='apache',
 
162
        description='Apache2 serving archive metadata',
 
163
        check_cmd='check_http --hostname localhost --url "/ubuntu/" '
 
164
                  '--string "dists/" --expect \"200\\ OK\"',
 
165
        )