~kwmonroe/charms/trusty/apache2/timeouts

« back to all changes in this revision

Viewing changes to hooks/hooks.py

  • Committer: Tim Van Steenburgh
  • Date: 2015-05-15 13:47:16 UTC
  • mfrom: (56.3.24 apache2-website-interface)
  • Revision ID: tim.van.steenburgh@canonical.com-20150515134716-94riimzd27o5581z
[abentley] Support apache-website interface.

Show diffs side-by-side

added added

removed removed

Lines of Context:
596
596
        if is_apache24():
597
597
            enable_module('lbmethod_byrequests')
598
598
 
599
 
    if config_data['enable_modules']:
600
 
        module_list = config_data['enable_modules'].split()
601
 
        for module in module_list:
602
 
            enable_module(module)
 
599
    disabled_modules = config_data['disable_modules'].split()
 
600
    apache_websites = ApacheWebsites.from_config(
 
601
        relations_of_type("apache-website"), disabled_modules)
 
602
    enabled_modules = config_data.get('enable_modules', '').split()
 
603
    enabled_modules = apache_websites.list_enabled_modules(enabled_modules)
 
604
    for module in enabled_modules:
 
605
        enable_module(module)
603
606
 
604
607
    if config_data['disable_modules']:
605
 
        module_list = config_data['disable_modules'].split()
606
 
        for module in module_list:
 
608
        for module in disabled_modules:
607
609
            disable_module(module)
608
610
 
 
611
    apache_websites.disable_sites()
 
612
    apache_websites.write_configs()
 
613
    apache_websites.enable_sites()
 
614
    apache_websites.configure_extra_ports()
 
615
    all_ports = apache_websites.list_enabled_ports()
609
616
    enable_mpm(config_data)
610
617
    # XXX we only configure the worker mpm?
611
618
    create_mpm_workerfile()
612
619
    create_security()
613
620
 
614
621
    ports = {'http': 80, 'https': 443}
615
 
    all_ports = set()
616
622
    for protocol, port in ports.iteritems():
617
623
        if create_vhost(
618
624
                port,
710
716
    # Disable the default website because we don't want people to see the
711
717
    # "It works!" page on production services and remove the
712
718
    # conf.d/other-vhosts-access-log conf.
713
 
    if os.path.exists(site_filename("000-default", True)):
714
 
        run(["/usr/sbin/a2dissite", "000-default"])
 
719
    ensure_disabled(["000-default"])
715
720
    conf_disable("other-vhosts-access-log")
716
721
    if os.path.exists(conf_filename("other-vhosts-access-log")):
717
722
        os.unlink(conf_filename("other-vhosts-access-log"))
734
739
    ship_logrotate_conf()
735
740
 
736
741
 
 
742
def ensure_disabled(sites):
 
743
    to_disable = [s for s in sites if os.path.exists(site_filename(s, True))]
 
744
    if len(to_disable) == 0:
 
745
        return
 
746
    run(["/usr/sbin/a2dissite"] + to_disable)
 
747
 
 
748
 
 
749
def ensure_removed(filename):
 
750
    try:
 
751
        os.unlink(filename)
 
752
    except OSError as e:
 
753
        if e.errno != errno.ENOENT:
 
754
            raise
 
755
 
 
756
 
 
757
class ApacheWebsites:
 
758
 
 
759
    @classmethod
 
760
    def from_config(cls, relations, disabled_modules):
 
761
        """Return an ApacheWebsites with information about all sites."""
 
762
        if relations is None:
 
763
            relations = []
 
764
        self_relations = {}
 
765
        for relation in relations:
 
766
            self_relation = {'domain': relation.get('domain')}
 
767
            enabled = bool(relation.get('enabled', 'False').lower() == 'true')
 
768
            site_modules = relation.get('site_modules', '').split()
 
769
            for module in site_modules:
 
770
                if module in disabled_modules:
 
771
                    enabled = False
 
772
                    log('site {} requires disabled_module {}'.format(
 
773
                        relation['__relid__'], module))
 
774
                break
 
775
            self_relation['site_modules'] = site_modules
 
776
            self_relation['enabled'] = enabled
 
777
            self_relation['site_config'] = relation.get('site_config')
 
778
            self_relation['ports'] = [
 
779
                int(p) for p in relation.get('ports', '').split()]
 
780
            self_relations[relation['__relid__']] = self_relation
 
781
        return cls(self_relations)
 
782
 
 
783
    def __init__(self, relations):
 
784
        self.relations = relations
 
785
 
 
786
    def write_configs(self):
 
787
        for key, relation in self.relations.items():
 
788
            config_file = site_filename(key)
 
789
            site_config = relation['site_config']
 
790
            if site_config is None:
 
791
                ensure_removed(config_file)
 
792
            else:
 
793
                with open(config_file, 'w') as output:
 
794
                    output.write(site_config)
 
795
 
 
796
    def iter_enabled_sites(self):
 
797
        return ((k, v) for k, v in self.relations.items() if v['enabled'])
 
798
 
 
799
    def enable_sites(self):
 
800
        enabled_sites = [k for k, v in self.iter_enabled_sites()]
 
801
        enabled_sites.sort()
 
802
        if len(enabled_sites) == 0:
 
803
            return
 
804
        subprocess.check_call(['/usr/sbin/a2ensite'] + enabled_sites)
 
805
 
 
806
    def disable_sites(self):
 
807
        disabled_sites = [k for k, v in self.relations.items()
 
808
                          if not v['enabled']]
 
809
        disabled_sites.sort()
 
810
        if len(disabled_sites) == 0:
 
811
            return
 
812
        ensure_disabled(disabled_sites)
 
813
 
 
814
    def list_enabled_modules(self, enabled_modules):
 
815
        enabled_modules = set(enabled_modules)
 
816
        for key, relation in self.iter_enabled_sites():
 
817
            enabled_modules.update(relation['site_modules'])
 
818
        return enabled_modules
 
819
 
 
820
    def list_enabled_ports(self):
 
821
        enabled_ports = set()
 
822
        for key, relation in self.iter_enabled_sites():
 
823
            enabled_ports.update(relation['ports'])
 
824
        return enabled_ports
 
825
 
 
826
    def configure_extra_ports(self):
 
827
        extra_ports = self.list_enabled_ports()
 
828
        extra_ports.discard(80)
 
829
        extra_ports.discard(443)
 
830
        extra_ports_conf = conf_filename('extra_ports')
 
831
        if len(extra_ports) > 0:
 
832
            with file(extra_ports_conf, 'w') as f:
 
833
                for port in sorted(extra_ports):
 
834
                    f.write('Listen {}\n'.format(port))
 
835
            conf_enable('extra_ports')
 
836
        else:
 
837
            conf_disable('extra_ports')
 
838
            ensure_removed(extra_ports_conf)
 
839
 
 
840
 
737
841
def update_vhost_config_relation():
738
842
    """
739
843
    Update the vhost file and include the certificate in the relation
884
988
        config_changed()
885
989
    elif hook_name == "website-relation-joined":
886
990
        website_interface("joined")
 
991
    elif hook_name == 'apache-website-relation-changed':
 
992
        config_changed()
887
993
    elif hook_name in ("nrpe-external-master-relation-changed",
888
994
                       "local-monitors-relation-changed"):
889
995
        update_nrpe_checks()