Example #1
0
def _bundle_download(bundle, location):
    """
    Download diagnostics bundle.

    :param bundle: bundle file name.
    :type bundle: string
    :param location: location on a local filesystem.
    :type location: string
    :return: status code
    :rtype: int
    """

    # make sure the requested bundle exists
    bundle_size = 0
    for available_bundle in _get_bundle_list():
        # _get_bundle_list must return a list of tuples
        # where first element is file name and second is its size.
        if len(available_bundle) != 2:
            raise DCOSException(
                'Request to get a list of diagnostic bundles returned an '
                'unexpected response: {}'.format(available_bundle))

        # available_bundle[0] is a file name
        # available_bundle[1] is a file size
        if available_bundle[0] == bundle:
            bundle_size = available_bundle[1]

    url = urllib.parse.urljoin(DIAGNOSTICS_BASE_URL, 'serve/' + bundle)
    bundle_location = os.path.join(os.getcwd(), bundle)
    if location:
        if os.path.isdir(location):
            bundle_location = os.path.join(location, bundle)
        else:
            bundle_location = location

    if bundle_size > BUNDLE_WARN_SIZE:
        if not confirm('Diagnostics bundle size is {}, are you sure you want '
                       'to download it?'.format(sizeof_fmt(bundle_size)),
                       False):
            return 0

    r = _do_request(url, 'GET', stream=True)
    try:
        with open(bundle_location, 'wb') as f:
            for chunk in r.iter_content(1024):
                f.write(chunk)
    except Exception as e:
        raise DCOSException(e)
    emitter.publish('Diagnostics bundle downloaded to ' + bundle_location)
    return 0
Example #2
0
def _bundle_download(bundle, location):
    """
    Download diagnostics bundle.

    :param bundle: bundle file name.
    :type bundle: string
    :param location: location on a local filesystem.
    :type location: string
    :return: status code
    :rtype: int
    """

    # make sure the requested bundle exists
    bundle_size = 0
    for available_bundle in _get_bundle_list():
        # _get_bundle_list must return a list of tuples
        # where first element is file name and second is its size.
        if len(available_bundle) != 2:
            raise DCOSException(
                'Request to get a list of diagnostic bundles returned an '
                'unexpected response: {}'.format(available_bundle))

        # available_bundle[0] is a file name
        # available_bundle[1] is a file size
        if available_bundle[0] == bundle:
            bundle_size = available_bundle[1]

    url = urllib.parse.urljoin(DIAGNOSTICS_BASE_URL, 'serve/' + bundle)
    bundle_location = os.path.join(os.getcwd(), bundle)
    if location:
        if os.path.isdir(location):
            bundle_location = os.path.join(location, bundle)
        else:
            bundle_location = location

    if bundle_size > BUNDLE_WARN_SIZE:
        if not confirm('Diagnostics bundle size is {}, are you sure you want '
                       'to download it?'.format(sizeof_fmt(bundle_size)),
                       False):
            return 0

    r = _do_request(url, 'GET', stream=True)
    try:
        with open(bundle_location, 'wb') as f:
            for chunk in r.iter_content(1024):
                f.write(chunk)
    except Exception as e:
        raise DCOSException(e)
    emitter.publish('Diagnostics bundle downloaded to ' + bundle_location)
    return 0
Example #3
0
def _snapshot_download(snapshot, location):
    """
    Download snapshot and put in the the current directory.

    :param snapshot: snapshot file name.
    :type snapshot: string
    :return: status code
    :rtype: int
    """

    # make sure the requested snapshot exists
    snapshot_size = 0
    for available_snapshot in _get_snapshots_list():
        # _get_snapshot_list must return a list of tuples
        # where first element is file name and second is its size.
        if len(available_snapshot) != 2:
            raise DCOSException(
                'Request to get a list of snapshots returned an '
                'unexpected response: {}'.format(available_snapshot))

        # available_snapshot[0] is a snapshot file name
        # available_snapshot[1] is a snapshot file size
        if available_snapshot[0] == snapshot:
            snapshot_size = available_snapshot[1]

    url = urllib.parse.urljoin(SNAPSHOT_BASE_URL, 'serve/' + snapshot)
    snapshot_location = os.path.join(os.getcwd(), snapshot)
    if location:
        if os.path.isdir(location):
            snapshot_location = os.path.join(location, snapshot)
        else:
            snapshot_location = location

    if snapshot_size > SNAPSHOT_WARN_SIZE:
        if not confirm('Snapshot size is {}, are you sure you want '
                       'to download it?'.format(sizeof_fmt(snapshot_size)),
                       False):
            return 0

    r = _do_request(url, 'GET', stream=True)
    try:
        with open(snapshot_location, 'wb') as f:
            for chunk in r.iter_content(1024):
                f.write(chunk)
    except Exception as e:
        raise DCOSException(e)
    emitter.publish('Snapshot downloaded to ' + snapshot_location)
    return 0