~arosales/charms/trusty/spark/fix-readme-example-jar

« back to all changes in this revision

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

  • Committer: amir sanjar
  • Date: 2014-10-10 09:17:48 UTC
  • Revision ID: amir.sanjar@canonical.com-20141010091748-ey66r7rayvytugm0
adding yarn support - part 1

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
import importlib
 
2
from tempfile import NamedTemporaryFile
2
3
import time
3
4
from yaml import safe_load
4
5
from charmhelpers.core.host import (
13
14
    config,
14
15
    log,
15
16
)
16
 
import apt_pkg
17
17
import os
18
18
 
19
19
 
117
117
 
118
118
def filter_installed_packages(packages):
119
119
    """Returns a list of packages that require installation"""
120
 
    apt_pkg.init()
121
 
 
122
 
    # Tell apt to build an in-memory cache to prevent race conditions (if
123
 
    # another process is already building the cache).
124
 
    apt_pkg.config.set("Dir::Cache::pkgcache", "")
125
 
 
126
 
    cache = apt_pkg.Cache()
 
120
    cache = apt_cache()
127
121
    _pkgs = []
128
122
    for package in packages:
129
123
        try:
136
130
    return _pkgs
137
131
 
138
132
 
 
133
def apt_cache(in_memory=True):
 
134
    """Build and return an apt cache"""
 
135
    import apt_pkg
 
136
    apt_pkg.init()
 
137
    if in_memory:
 
138
        apt_pkg.config.set("Dir::Cache::pkgcache", "")
 
139
        apt_pkg.config.set("Dir::Cache::srcpkgcache", "")
 
140
    return apt_pkg.Cache()
 
141
 
 
142
 
139
143
def apt_install(packages, options=None, fatal=False):
140
144
    """Install one or more packages"""
141
145
    if options is None:
201
205
 
202
206
 
203
207
def add_source(source, key=None):
 
208
    """Add a package source to this system.
 
209
 
 
210
    @param source: a URL or sources.list entry, as supported by
 
211
    add-apt-repository(1). Examples:
 
212
        ppa:charmers/example
 
213
        deb https://stub:key@private.example.com/ubuntu trusty main
 
214
 
 
215
    In addition:
 
216
        'proposed:' may be used to enable the standard 'proposed'
 
217
        pocket for the release.
 
218
        'cloud:' may be used to activate official cloud archive pockets,
 
219
        such as 'cloud:icehouse'
 
220
 
 
221
    @param key: A key to be added to the system's APT keyring and used
 
222
    to verify the signatures on packages. Ideally, this should be an
 
223
    ASCII format GPG public key including the block headers. A GPG key
 
224
    id may also be used, but be aware that only insecure protocols are
 
225
    available to retrieve the actual public key from a public keyserver
 
226
    placing your Juju environment at risk. ppa and cloud archive keys
 
227
    are securely added automtically, so sould not be provided.
 
228
    """
204
229
    if source is None:
205
230
        log('Source is not present. Skipping')
206
231
        return
225
250
        release = lsb_release()['DISTRIB_CODENAME']
226
251
        with open('/etc/apt/sources.list.d/proposed.list', 'w') as apt:
227
252
            apt.write(PROPOSED_POCKET.format(release))
 
253
    else:
 
254
        raise SourceConfigError("Unknown source: {!r}".format(source))
 
255
 
228
256
    if key:
229
 
        subprocess.check_call(['apt-key', 'adv', '--keyserver',
230
 
                               'hkp://keyserver.ubuntu.com:80', '--recv',
231
 
                               key])
 
257
        if '-----BEGIN PGP PUBLIC KEY BLOCK-----' in key:
 
258
            with NamedTemporaryFile() as key_file:
 
259
                key_file.write(key)
 
260
                key_file.flush()
 
261
                key_file.seek(0)
 
262
                subprocess.check_call(['apt-key', 'add', '-'], stdin=key_file)
 
263
        else:
 
264
            # Note that hkp: is in no way a secure protocol. Using a
 
265
            # GPG key id is pointless from a security POV unless you
 
266
            # absolutely trust your network and DNS.
 
267
            subprocess.check_call(['apt-key', 'adv', '--keyserver',
 
268
                                   'hkp://keyserver.ubuntu.com:80', '--recv',
 
269
                                   key])
232
270
 
233
271
 
234
272
def configure_sources(update=False,
235
273
                      sources_var='install_sources',
236
274
                      keys_var='install_keys'):
237
275
    """
238
 
    Configure multiple sources from charm configuration
 
276
    Configure multiple sources from charm configuration.
 
277
 
 
278
    The lists are encoded as yaml fragments in the configuration.
 
279
    The frament needs to be included as a string. Sources and their
 
280
    corresponding keys are of the types supported by add_source().
239
281
 
240
282
    Example config:
241
 
        install_sources:
 
283
        install_sources: |
242
284
          - "ppa:foo"
243
285
          - "http://example.com/repo precise main"
244
 
        install_keys:
 
286
        install_keys: |
245
287
          - null
246
288
          - "a1b2c3d4"
247
289
 
248
290
    Note that 'null' (a.k.a. None) should not be quoted.
249
291
    """
250
 
    sources = safe_load(config(sources_var))
251
 
    keys = config(keys_var)
252
 
    if keys is not None:
253
 
        keys = safe_load(keys)
254
 
    if isinstance(sources, basestring) and (
255
 
            keys is None or isinstance(keys, basestring)):
256
 
        add_source(sources, keys)
 
292
    sources = safe_load((config(sources_var) or '').strip()) or []
 
293
    keys = safe_load((config(keys_var) or '').strip()) or None
 
294
 
 
295
    if isinstance(sources, basestring):
 
296
        sources = [sources]
 
297
 
 
298
    if keys is None:
 
299
        for source in sources:
 
300
            add_source(source, None)
257
301
    else:
258
 
        if not len(sources) == len(keys):
259
 
            msg = 'Install sources and keys lists are different lengths'
260
 
            raise SourceConfigError(msg)
261
 
        for src_num in range(len(sources)):
262
 
            add_source(sources[src_num], keys[src_num])
 
302
        if isinstance(keys, basestring):
 
303
            keys = [keys]
 
304
 
 
305
        if len(sources) != len(keys):
 
306
            raise SourceConfigError(
 
307
                'Install sources and keys lists are different lengths')
 
308
        for source, key in zip(sources, keys):
 
309
            add_source(source, key)
263
310
    if update:
264
311
        apt_update(fatal=True)
265
312