~charmers/charms/trusty/apache2/trunk

« back to all changes in this revision

Viewing changes to hooks/hooks.py

  • Committer: Marco Ceppi
  • Date: 2014-01-16 13:46:52 UTC
  • mfrom: (50.1.1 apache2-cs-jjo)
  • Revision ID: marco@ceppi.net-20140116134652-vaofgo4xqh3np65t
[jjo] support apache 2.4 config filenames

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
###############################################################################
26
26
# Global variables
27
27
###############################################################################
28
 
default_apache2_config_dir = "/etc/apache2/conf.d"
29
 
default_apache2_config = "%s/apache2.conf" % default_apache2_config_dir
30
28
default_apache2_service_config_dir = "/var/run/apache2"
31
29
test_apache2_vhost = "/etc/apache2/sites-available/testvhost"
32
30
service_affecting_packages = ['apache2']
 
31
default_apache22_config_dir = "/etc/apache2/conf.d"
 
32
default_apache24_config_dir = "/etc/apache2/conf-available"
33
33
 
34
34
juju_warning_header = """#
35
35
#    "             "
135
135
        return True
136
136
 
137
137
 
 
138
def is_apache24():
 
139
    return os.path.exists("/usr/sbin/a2enconf")
 
140
 
 
141
 
 
142
def conf_filename(name):
 
143
    """Return an apache2 config filename path, as:
 
144
      2.4: /etc/apache2/conf-available/foo.conf
 
145
      2.2: /etc/apache2/conf.d/foo
 
146
    """
 
147
    # Check for conf.d presence (instead of parsing apache2ctl -V, etc)
 
148
    if is_apache24():
 
149
        return "{}/{}.conf".format(default_apache24_config_dir, name)
 
150
    else:
 
151
        return "{}/{}".format(default_apache22_config_dir, name)
 
152
 
 
153
 
 
154
def conf_enable(name):
 
155
    "Enable apache2 config without reloading service"
 
156
    if is_apache24():
 
157
        return subprocess.call(['/usr/sbin/a2enconf', name]) == 0
 
158
    # no-op otherwise
 
159
    return True
 
160
 
 
161
 
 
162
def conf_disable(name):
 
163
    "Disable apache2 config without reloading service"
 
164
    if is_apache24():
 
165
        return subprocess.call(['/usr/sbin/a2disconf', name]) == 0
 
166
    # no-op otherwise
 
167
    return True
 
168
 
 
169
 
138
170
###############################################################################
139
171
# Hook functions
140
172
###############################################################################
150
182
        result["servername"] = unit_get("public-address")
151
183
    return result
152
184
 
 
185
 
153
186
def install_hook():
154
187
    if not os.path.exists(default_apache2_service_config_dir):
155
188
        os.mkdir(default_apache2_service_config_dir, 0600)
156
189
    apt_get_install("python-jinja2")
157
190
    install_status = apt_get_install("apache2")
158
191
    if install_status == 0:
159
 
        ensure_package_status(service_affecting_packages, config_get('package_status'))
 
192
        ensure_package_status(service_affecting_packages,
 
193
                              config_get('package_status'))
160
194
    ensure_extra_packages()
161
195
    return install_status
162
196
 
269
303
    template_env = Environment(loader=FileSystemLoader(os.path.join(
270
304
        os.environ['CHARM_DIR'], 'data')))
271
305
    for balancer_name in unit_dict.keys():
272
 
        balancer_host_file = '%s/%s.balancer' % (default_apache2_config_dir,
273
 
                                                 balancer_name)
 
306
        balancer_host_file = conf_filename('{}.balancer'.format(balancer_name))
274
307
        templ_vars = {
275
308
            'balancer_name': balancer_name,
276
309
            'balancer_addresses': unit_dict[balancer_name],
282
315
                                                templ_vars))
283
316
        with open(balancer_host_file, 'w') as balancer_config:
284
317
            balancer_config.write(str(template))
 
318
        conf_enable('{}.balancer'.format(balancer_name))
285
319
 
286
320
 
287
321
def update_nrpe_checks():
299
333
 
300
334
def create_mpm_workerfile():
301
335
    config_data = config_get()
302
 
    mpm_workerfile = '/etc/apache2/conf.d/000mpm-worker'
 
336
    mpm_workerfile = conf_filename('000mpm-worker')
303
337
    from jinja2 import Environment, FileSystemLoader
304
338
    template_env = Environment(loader=FileSystemLoader(os.path.join(
305
339
        os.environ['CHARM_DIR'], 'data')))
318
352
        template_env.get_template('mpm_worker.template').render(templ_vars)
319
353
    with open(mpm_workerfile, 'w') as mpm_config:
320
354
        mpm_config.write(str(template))
 
355
    conf_enable('000mpm-worker')
321
356
 
322
357
 
323
358
def create_security():
324
359
    config_data = config_get()
325
 
    securityfile = '/etc/apache2/conf.d/security'
 
360
    securityfile = conf_filename('security')
326
361
    from jinja2 import Environment, FileSystemLoader
327
362
    template_env = Environment(loader=FileSystemLoader(os.path.join(
328
363
        os.environ['CHARM_DIR'], 'data')))
336
371
        template_env.get_template('security.template').render(templ_vars)
337
372
    with open(securityfile, 'w') as security_config:
338
373
        security_config.write(str(template))
 
374
    conf_enable('security')
339
375
 
340
376
 
341
377
def ship_logrotate_conf():
360
396
    relationship_data = {}
361
397
    config_data = config_get()
362
398
 
363
 
    ensure_package_status(service_affecting_packages, config_data['package_status'])
 
399
    ensure_package_status(service_affecting_packages,
 
400
                          config_data['package_status'])
364
401
    ensure_extra_packages()
365
402
 
366
403
    relationship_data.update(get_reverseproxy_data(relation='reverseproxy'))
461
498
                    log("Writing chain certificates file from"
462
499
                        "config ssl_chain: %s" % chain_file)
463
500
                    with open(chain_file, 'w') as f:
464
 
                        f.write(str(base64.b64decode(config_data['ssl_chain'])))
 
501
                        f.write(str(base64.b64decode(
 
502
                            config_data['ssl_chain'])))
465
503
                # Use chain certificates shipped out with charm.
466
504
                else:
467
505
                    source = os.path.join(os.environ['CHARM_DIR'], 'data',
478
516
            os.chown(key_file, pwd.getpwnam('root').pw_uid,
479
517
                     grp.getgrnam('ssl-cert').gr_gid)
480
518
 
481
 
    apache_syslog_conf = "/etc/apache2/conf.d/syslog"
 
519
    apache_syslog_conf = conf_filename("syslog")
482
520
    rsyslog_apache_conf = "/etc/rsyslog.d/45-apache2.conf"
483
521
    if config_data['use_rsyslog']:
484
522
        shutil.copy2("data/syslog-apache.conf", apache_syslog_conf)
 
523
        conf_enable("syslog")
485
524
        shutil.copy2("data/syslog-rsyslog.conf", rsyslog_apache_conf)
486
525
        # Fix permissions of access.log and error.log to allow syslog user to
487
526
        # write to
490
529
        os.chown("/var/log/apache2/error.log", pwd.getpwnam('syslog').pw_uid,
491
530
                 pwd.getpwnam('syslog').pw_gid)
492
531
    else:
 
532
        conf_disable("syslog")
493
533
        if os.path.exists(apache_syslog_conf):
494
534
            os.unlink(apache_syslog_conf)
495
535
        if os.path.exists(rsyslog_apache_conf):
501
541
    # conf.d/other-vhosts-access-log conf.
502
542
    if os.path.exists("/etc/apache2/sites-enabled/000-default"):
503
543
        run(["/usr/sbin/a2dissite", "000-default"])
504
 
    if os.path.exists("/etc/apache2/conf.d/other-vhosts-access-log"):
505
 
        os.unlink("/etc/apache2/conf.d/other-vhosts-access-log")
 
544
    conf_disable("other-vhosts-access-log")
 
545
    if os.path.exists(conf_filename("other-vhosts-access-log")):
 
546
        os.unlink(conf_filename("other-vhosts-access-log"))
506
547
 
507
548
    if service_apache2("check"):
508
549
        if config_data["config_change_command"] in ["reload", "restart"]: