def install(self, source, dest=None, checksum=None, hash_type='sha1'):
        """
        Download and install an archive file, with optional checksum validation.

        The checksum can also be given on the :param:`source` URL's fragment.
        For example::

            handler.install('http://example.com/file.tgz#sha1=deadbeef')

        :param str source: URL pointing to an archive file.
        :param str dest: Local destination path to install to.  If not given,
                         installs to `$CHARM_DIR/archives/archive_file_name`.
        :param str checksum: If given, validate the archive file after download.
        :param str hash_type: Algorithm used to generate :param:`checksum`.
                              Can be any hash alrgorithm supported by :mod:`hashlib`,
                              such as md5, sha1, sha256, sha512, etc.
        """
        url_parts = self.parse_url(source)
        dest_dir = os.path.join(os.environ.get('CHARM_DIR'), 'fetched')
        if not os.path.exists(dest_dir):
            mkdir(dest_dir, perms=0755)
        dld_file = os.path.join(dest_dir, os.path.basename(url_parts.path))
        try:
            self.download(source, dld_file)
        except urllib2.URLError as e:
            raise UnhandledSource(e.reason)
        except OSError as e:
            raise UnhandledSource(e.strerror)
        options = urlparse.parse_qs(url_parts.fragment)
        for key, value in options.items():
            if key in hashlib.algorithms:
                check_hash(dld_file, value, key)
        if checksum:
            check_hash(dld_file, checksum, hash_type)
        return extract(dld_file, dest)
Example #2
0
 def test_check_hash(self, file_hash):
     file_hash.return_value = 'good-hash'
     self.assertRaises(host.ChecksumError, host.check_hash, 'file',
                       'bad-hash')
     host.check_hash('file', 'good-hash', 'sha256')
     self.assertEqual(file_hash.call_args_list, [
         call('file', 'md5'),
         call('file', 'sha256'),
     ])
Example #3
0
 def download_platform(self):
     hookenv.status_set('maintenance', 'Downloading platform')
     url = self.get_platform_package_url()
     package = self.get_platform_package_name()
     log("Platform package url: " + url, INFO)
     aufh = ArchiveUrlFetchHandler()
     dest_file = "/tmp/" + package
     aufh.download(url, dest_file)
     fetch.apt_update()
     checksum = self.config['package-checksum']
     if checksum:
         hash_type = self.config['hash-type']
         if not hash_type:
             hash_type = 'md5'
         host.check_hash(dest_file, checksum, hash_type)
     return dest_file
    def install(self, source, dest=None, checksum=None, hash_type='sha1'):
        """
        Download and install an archive file, with optional checksum validation.

        The checksum can also be given on the `source` URL's fragment.
        For example::

            handler.install('http://example.com/file.tgz#sha1=deadbeef')

        :param str source: URL pointing to an archive file.
        :param str dest: Local destination path to install to. If not given,
            installs to `$CHARM_DIR/archives/archive_file_name`.
        :param str checksum: If given, validate the archive file after download.
        :param str hash_type: Algorithm used to generate `checksum`.
            Can be any hash alrgorithm supported by :mod:`hashlib`,
            such as md5, sha1, sha256, sha512, etc.

        """
        url_parts = self.parse_url(source)
        dest_dir = os.path.join(os.environ.get('CHARM_DIR'), 'fetched')
        if not os.path.exists(dest_dir):
            mkdir(dest_dir, perms=0o755)
        dld_file = os.path.join(dest_dir, os.path.basename(url_parts.path))
        try:
            self.download(source, dld_file)
        except URLError as e:
            raise UnhandledSource(e.reason)
        except OSError as e:
            raise UnhandledSource(e.strerror)
        options = parse_qs(url_parts.fragment)
        for key, value in options.items():
            if not six.PY3:
                algorithms = hashlib.algorithms
            else:
                algorithms = hashlib.algorithms_available
            if key in algorithms:
                if len(value) != 1:
                    raise TypeError("Expected 1 hash value, not %d" %
                                    len(value))
                expected = value[0]
                check_hash(dld_file, expected, key)
        if checksum:
            check_hash(dld_file, checksum, hash_type)
        return extract(dld_file, dest)
    def download_plugin(self, plugin_name):
        hookenv.status_set('maintenance', 'Downloading plugin ' + plugin_name)
        url = self.get_plugin_package_url(plugin_name)
        package = self.get_plugin_package_name(plugin_name)
        log("Plugin package url: " + url, INFO)
        aufh =  ArchiveUrlFetchHandler()
        dest_file = "/tmp/" + package
        aufh.download(url, dest_file)
        fetch.apt_update()

        hash_key = plugin_name + '-hash' 
        checksum = ''
        if hash_key in self.config.keys(): 
           checksum = self.config[hash_key]
        if checksum:
            hash_type = self.config['hash-type']
            if not hash_type:
                hash_type = 'md5'
            host.check_hash(dest_file, checksum, hash_type)
        return dest_file
 def download_and_validate(self, url, hashsum, validate="sha1"):
     tempfile, headers = urlretrieve(url)
     check_hash(tempfile, hashsum, validate)
     return tempfile
Example #7
0
 def download_and_validate(self, url, hashsum, validate="sha1"):
     tempfile, headers = urlretrieve(url)
     check_hash(tempfile, hashsum, validate)
     return tempfile