~oddbloke/cloud-init/fix-gce-az

« back to all changes in this revision

Viewing changes to cloudinit/distros/__init__.py

  • Committer: Daniel Watkins
  • Date: 2015-07-22 12:06:34 UTC
  • Revision ID: daniel.watkins@canonical.com-20150722120634-wsg8rwzcaanhc2pn
Make full data source available to code that handles mirror selection.

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
import abc
28
28
import os
29
29
import re
 
30
import stat
30
31
 
31
32
from cloudinit import importer
32
33
from cloudinit import log as logging
89
90
        self._write_hostname(writeable_hostname, self.hostname_conf_fn)
90
91
        self._apply_hostname(writeable_hostname)
91
92
 
 
93
    def uses_systemd(self):
 
94
        try:
 
95
            res = os.lstat('/run/systemd/system')
 
96
            return stat.S_ISDIR(res.st_mode)
 
97
        except:
 
98
            return False
 
99
 
92
100
    @abc.abstractmethod
93
101
    def package_command(self, cmd, args=None, pkgs=None):
94
102
        raise NotImplementedError()
109
117
            arch = self.get_primary_arch()
110
118
        return _get_arch_package_mirror_info(mirror_info, arch)
111
119
 
112
 
    def get_package_mirror_info(self, arch=None,
113
 
                                availability_zone=None):
 
120
    def get_package_mirror_info(self, arch=None, data_source=None):
114
121
        # This resolves the package_mirrors config option
115
122
        # down to a single dict of {mirror_name: mirror_url}
116
123
        arch_info = self._get_arch_package_mirror_info(arch)
117
 
        return _get_package_mirror_info(availability_zone=availability_zone,
 
124
        return _get_package_mirror_info(data_source=data_source,
118
125
                                        mirror_info=arch_info)
119
126
 
120
127
    def apply_network(self, settings, bring_up=True):
548
555
                LOG.info("Added user '%s' to group '%s'" % (member, name))
549
556
 
550
557
 
551
 
def _get_package_mirror_info(mirror_info, availability_zone=None,
 
558
def _get_package_mirror_info(mirror_info, data_source=None,
552
559
                             mirror_filter=util.search_for_mirror):
553
560
    # given a arch specific 'mirror_info' entry (from package_mirrors)
554
561
    # search through the 'search' entries, and fallback appropriately
556
563
    if not mirror_info:
557
564
        mirror_info = {}
558
565
 
559
 
    ec2_az_re = ("^[a-z][a-z]-(%s)-[1-9][0-9]*[a-z]$" %
560
 
        "north|northeast|east|southeast|south|southwest|west|northwest")
 
566
    # ec2 availability zones are named cc-direction-[0-9][a-d] (us-east-1b)
 
567
    # the region is us-east-1. so region = az[0:-1]
 
568
    directions_re = '|'.join([
 
569
        'central', 'east', 'north', 'northeast', 'northwest',
 
570
        'south', 'southeast', 'southwest', 'west'])
 
571
    ec2_az_re = ("^[a-z][a-z]-(%s)-[1-9][0-9]*[a-z]$" % directions_re)
561
572
 
562
573
    subst = {}
563
 
    if availability_zone:
564
 
        subst['availability_zone'] = availability_zone
 
574
    if data_source and data_source.availability_zone:
 
575
        subst['availability_zone'] = data_source.availability_zone
565
576
 
566
 
    if availability_zone and re.match(ec2_az_re, availability_zone):
567
 
        subst['ec2_region'] = "%s" % availability_zone[0:-1]
 
577
        if re.match(ec2_az_re, data_source.availability_zone):
 
578
            subst['ec2_region'] = "%s" % data_source.availability_zone[0:-1]
568
579
 
569
580
    results = {}
570
581
    for (name, mirror) in mirror_info.get('failsafe', {}).items():