~bjornt/charms/trusty/quantum-gateway-hardcode-eth1/trunk

« back to all changes in this revision

Viewing changes to hooks/charmhelpers/fetch/__init__.py

[gnuoy,r=james-page,t=james-page] Add support for specifying external port by mac address

Also resync charm-helpers

Show diffs side-by-side

added added

removed removed

Lines of Context:
13
13
    log,
14
14
)
15
15
import apt_pkg
 
16
import os
16
17
 
17
18
CLOUD_ARCHIVE = """# Ubuntu Cloud Archive
18
19
deb http://ubuntu-cloud.archive.canonical.com/ubuntu {} main
43
44
    'precise-havana/updates': 'precise-updates/havana',
44
45
    'precise-updates/havana': 'precise-updates/havana',
45
46
    'havana/proposed': 'precise-proposed/havana',
46
 
    'precies-havana/proposed': 'precise-proposed/havana',
 
47
    'precise-havana/proposed': 'precise-proposed/havana',
47
48
    'precise-proposed/havana': 'precise-proposed/havana',
 
49
    # Icehouse
 
50
    'icehouse': 'precise-updates/icehouse',
 
51
    'precise-icehouse': 'precise-updates/icehouse',
 
52
    'precise-icehouse/updates': 'precise-updates/icehouse',
 
53
    'precise-updates/icehouse': 'precise-updates/icehouse',
 
54
    'icehouse/proposed': 'precise-proposed/icehouse',
 
55
    'precise-icehouse/proposed': 'precise-proposed/icehouse',
 
56
    'precise-proposed/icehouse': 'precise-proposed/icehouse',
48
57
}
49
58
 
50
59
 
66
75
 
67
76
def apt_install(packages, options=None, fatal=False):
68
77
    """Install one or more packages"""
69
 
    options = options or []
70
 
    cmd = ['apt-get', '-y']
 
78
    if options is None:
 
79
        options = ['--option=Dpkg::Options::=--force-confold']
 
80
 
 
81
    cmd = ['apt-get', '--assume-yes']
71
82
    cmd.extend(options)
72
83
    cmd.append('install')
73
84
    if isinstance(packages, basestring):
76
87
        cmd.extend(packages)
77
88
    log("Installing {} with options: {}".format(packages,
78
89
                                                options))
 
90
    env = os.environ.copy()
 
91
    if 'DEBIAN_FRONTEND' not in env:
 
92
        env['DEBIAN_FRONTEND'] = 'noninteractive'
 
93
 
79
94
    if fatal:
80
 
        subprocess.check_call(cmd)
 
95
        subprocess.check_call(cmd, env=env)
81
96
    else:
82
 
        subprocess.call(cmd)
 
97
        subprocess.call(cmd, env=env)
83
98
 
84
99
 
85
100
def apt_update(fatal=False):
93
108
 
94
109
def apt_purge(packages, fatal=False):
95
110
    """Purge one or more packages"""
96
 
    cmd = ['apt-get', '-y', 'purge']
 
111
    cmd = ['apt-get', '--assume-yes', 'purge']
97
112
    if isinstance(packages, basestring):
98
113
        cmd.append(packages)
99
114
    else:
120
135
 
121
136
 
122
137
def add_source(source, key=None):
 
138
    if source is None:
 
139
        log('Source is not present. Skipping')
 
140
        return
 
141
 
123
142
    if (source.startswith('ppa:') or
124
 
        source.startswith('http:') or
 
143
        source.startswith('http') or
125
144
        source.startswith('deb ') or
126
 
        source.startswith('cloud-archive:')):
 
145
            source.startswith('cloud-archive:')):
127
146
        subprocess.check_call(['add-apt-repository', '--yes', source])
128
147
    elif source.startswith('cloud:'):
129
148
        apt_install(filter_installed_packages(['ubuntu-cloud-keyring']),
130
149
                    fatal=True)
131
150
        pocket = source.split(':')[-1]
132
151
        if pocket not in CLOUD_ARCHIVE_POCKETS:
133
 
            raise SourceConfigError('Unsupported cloud: source option %s' % pocket)
 
152
            raise SourceConfigError(
 
153
                'Unsupported cloud: source option %s' %
 
154
                pocket)
134
155
        actual_pocket = CLOUD_ARCHIVE_POCKETS[pocket]
135
156
        with open('/etc/apt/sources.list.d/cloud-archive.list', 'w') as apt:
136
157
            apt.write(CLOUD_ARCHIVE.format(actual_pocket))
139
160
        with open('/etc/apt/sources.list.d/proposed.list', 'w') as apt:
140
161
            apt.write(PROPOSED_POCKET.format(release))
141
162
    if key:
142
 
        subprocess.check_call(['apt-key', 'import', key])
 
163
        subprocess.check_call(['apt-key', 'adv', '--keyserver',
 
164
                               'keyserver.ubuntu.com', '--recv',
 
165
                               key])
143
166
 
144
167
 
145
168
class SourceConfigError(Exception):
220
243
 
221
244
 
222
245
class BaseFetchHandler(object):
 
246
 
223
247
    """Base class for FetchHandler implementations in fetch plugins"""
 
248
 
224
249
    def can_handle(self, source):
225
250
        """Returns True if the source can be handled. Otherwise returns
226
251
        a string explaining why it cannot"""
248
273
    for handler_name in fetch_handlers:
249
274
        package, classname = handler_name.rsplit('.', 1)
250
275
        try:
251
 
            handler_class = getattr(importlib.import_module(package), classname)
 
276
            handler_class = getattr(
 
277
                importlib.import_module(package),
 
278
                classname)
252
279
            plugin_list.append(handler_class())
253
280
        except (ImportError, AttributeError):
254
281
            # Skip missing plugins so that they can be ommitted from
255
282
            # installation if desired
256
 
            log("FetchHandler {} not found, skipping plugin".format(handler_name))
 
283
            log("FetchHandler {} not found, skipping plugin".format(
 
284
                handler_name))
257
285
    return plugin_list