~openstack-charmers-archive/charms/trusty/ceph-radosgw/next

« back to all changes in this revision

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

  • Committer: Christopher Glass
  • Date: 2014-09-20 07:26:18 UTC
  • mfrom: (25.1.1 ceph-radosgw)
  • Revision ID: christopher.glass@canonical.com-20140920072618-9lyk6yt44nv8js33
Merging lp:~gnuoy/charms/trusty/ceph-radosgw/next-charmhelper-sync [r=tribaal]

The ceph-radosgw charm was in need of a charm-helpers refresh (to get the apt-cache
in-memory indexing).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
import os
2
2
import urllib2
 
3
from urllib import urlretrieve
3
4
import urlparse
 
5
import hashlib
4
6
 
5
7
from charmhelpers.fetch import (
6
8
    BaseFetchHandler,
12
14
)
13
15
from charmhelpers.core.host import mkdir
14
16
 
15
 
 
 
17
"""
 
18
This class is a plugin for charmhelpers.fetch.install_remote.
 
19
 
 
20
It grabs, validates and installs remote archives fetched over "http", "https", "ftp" or "file" protocols. The contents of the archive are installed in $CHARM_DIR/fetched/.
 
21
 
 
22
Example usage:
 
23
install_remote("https://example.com/some/archive.tar.gz")
 
24
# Installs the contents of archive.tar.gz in $CHARM_DIR/fetched/.
 
25
 
 
26
See charmhelpers.fetch.archiveurl.get_archivehandler for supported archive types.
 
27
"""
16
28
class ArchiveUrlFetchHandler(BaseFetchHandler):
17
29
    """Handler for archives via generic URLs"""
18
30
    def can_handle(self, source):
61
73
        except OSError as e:
62
74
            raise UnhandledSource(e.strerror)
63
75
        return extract(dld_file)
 
76
 
 
77
    # Mandatory file validation via Sha1 or MD5 hashing.
 
78
    def download_and_validate(self, url, hashsum, validate="sha1"):
 
79
        if validate == 'sha1' and len(hashsum) != 40:
 
80
            raise ValueError("HashSum must be = 40 characters when using sha1"
 
81
                             " validation")
 
82
        if validate == 'md5' and len(hashsum) != 32:
 
83
            raise ValueError("HashSum must be = 32 characters when using md5"
 
84
                             " validation")
 
85
        tempfile, headers = urlretrieve(url)
 
86
        self.validate_file(tempfile, hashsum, validate)
 
87
        return tempfile
 
88
 
 
89
    # Predicate method that returns status of hash matching expected hash.
 
90
    def validate_file(self, source, hashsum, vmethod='sha1'):
 
91
        if vmethod != 'sha1' and vmethod != 'md5':
 
92
            raise ValueError("Validation Method not supported")
 
93
 
 
94
        if vmethod == 'md5':
 
95
            m = hashlib.md5()
 
96
        if vmethod == 'sha1':
 
97
            m = hashlib.sha1()
 
98
        with open(source) as f:
 
99
            for line in f:
 
100
                m.update(line)
 
101
        if hashsum != m.hexdigest():
 
102
            msg = "Hash Mismatch on {} expected {} got {}"
 
103
            raise ValueError(msg.format(source, hashsum, m.hexdigest()))