Ejemplo n.º 1
0
def task_get(volume_id):
    client = Client()
    volume = client.get_volume(volume_id)

    if not volume:
        return utils.jsonify({
            'error': VOLUME_NOT_FOUND,
            'error_msg': VOLUME_NOT_FOUND_MSG,
        }, 404)

    tasks = []

    for task in reversed(volume.get_tasks()):
        task_data = {
            'id': task.id,
            'volume': volume.id,
            'volume_name': volume.name,
            'type': task.type,
            'state': task.state,
            'time': task.time,
            'has_log': False,
        }

        if task.log_path:
            if os.path.isfile(task.log_path):
                task_data['has_log'] = True

        if task.snapshot_id:
            task_data['snapshot_id'] = task.snapshot_id

        tasks.append(task_data)

    return utils.jsonify(tasks)
Ejemplo n.º 2
0
def restore_post(volume_id, snapshot_id):
    client = Client()

    volume = client.get_volume(volume_id)

    if not volume:
        return utils.jsonify({
            'error': VOLUME_NOT_FOUND,
            'error_msg': VOLUME_NOT_FOUND_MSG,
        }, 404)

    snapshot = volume.get_snapshot(snapshot_id)

    if not snapshot:
        return utils.jsonify({
            'error': SNAPSHOT_NOT_FOUND,
            'error_msg': SNAPSHOT_NOT_FOUND_MSG,
        }, 404)

    source_paths = flask.request.json['source_paths']
    destination_path = os.path.join(
        volume.source_path, flask.request.json['destination_path'])
    source_objects = []

    for path in source_paths:
        source_objects.append(Object(os.path.join(snapshot.path, path)))

    volume.restore_object(source_objects, destination_path)

    return utils.jsonify({})
Ejemplo n.º 3
0
def log_get(type, volume_id, type_id):
    client = Client()
    volume = client.get_volume(volume_id)
    data = None

    if not volume:
        return utils.jsonify({
            'error': VOLUME_NOT_FOUND,
            'error_msg': VOLUME_NOT_FOUND_MSG,
        }, 404)

    if type == 'snapshot':
        snapshot_id = type_id
        snapshot = volume.get_snapshot(int(snapshot_id))

        if not snapshot:
            return utils.jsonify({
                'error': SNAPSHOT_NOT_FOUND,
                'error_msg': SNAPSHOT_NOT_FOUND_MSG,
            }, 404)

        if snapshot.log_size() > MAX_TEXT_SIZE:
            syntax = ''
        else:
            syntax = 'sh'

        data = {
            'id': snapshot.id,
            'syntax': syntax,
            'data': snapshot.log_read(),
        }

    elif type == 'task':
        task_id = type_id
        task = volume.get_task(task_id.encode())

        if not task:
            return utils.jsonify({
                'error': TASK_NOT_FOUND,
                'error_msg': TASK_NOT_FOUND_MSG,
            }, 404)

        if task.log_size() > MAX_TEXT_SIZE:
            syntax = ''
        else:
            syntax = 'sh'

        data = {
            'id': task.id,
            'syntax': syntax,
            'data': task.log_read(),
        }

    if not data:
        return utils.jsonify({
            'error': FILE_NOT_FOUND,
            'error_msg': FILE_NOT_FOUND_MSG,
        }, 404)

    return utils.jsonify(data)
Ejemplo n.º 4
0
def event_get(last_event=None):
    if server.interrupt:
        return flask.abort(503)

    if not last_event:
        events = [
            {
                'id': uuid.uuid4().hex,
                'type': 'time',
                'time': int(time.time() * 1000),
            },
        ]
        return utils.jsonify(events)

    run_time = 0
    while run_time <= 30 and not server.interrupt:
        events = []

        for event in Event.get_events(last_event):
            event_data = {
                'id': event.id,
                'type': event.type,
                'time': event.time,
            }
            if event.volume_id:
                event_data['volume_id'] = event.volume_id
            events.append(event_data)

        if len(events):
            return utils.jsonify(events)

        run_time += 0.1
        time.sleep(0.1)

    return utils.jsonify([])
Ejemplo n.º 5
0
def object_get(volume_id, snapshot_id, path=None):
    client = Client()
    volume = client.get_volume(volume_id)

    if not volume:
        return utils.jsonify({
            'error': VOLUME_NOT_FOUND,
            'error_msg': VOLUME_NOT_FOUND_MSG,
        }, 404)

    if snapshot_id == 'origin':
        bucket = volume.get_origin()
    else:
        bucket = volume.get_snapshot(int(snapshot_id))

    if not bucket:
        return utils.jsonify({
            'error': SNAPSHOT_NOT_FOUND,
            'error_msg': SNAPSHOT_NOT_FOUND_MSG,
        }, 404)

    objects_dict = {}
    objects_name = []

    try:
        bucket_objects = bucket.get_objects(path)
    except OSError, error:
        return utils.jsonify({
            'error': PATH_NOT_FOUND,
            'error_msg': error.strerror,
        }, 404)
Ejemplo n.º 6
0
def volume_get(volume_id=None):
    client = Client()

    if volume_id:
        volume = client.get_volume(volume_id)
        return utils.jsonify(_get_volume_object(volume))

    volumes = []
    for volume in client.get_volumes():
        volumes.append(_get_volume_object(volume))

    return utils.jsonify(volumes)
Ejemplo n.º 7
0
def snapshot_post(volume_id):
    client = Client()
    volume = client.get_volume(volume_id)

    if not volume:
        return utils.jsonify({
            'error': VOLUME_NOT_FOUND,
            'error_msg': VOLUME_NOT_FOUND_MSG,
        }, 404)

    snapshot_task = volume.create_snapshot()

    return utils.jsonify({})
Ejemplo n.º 8
0
def volume_put_post(volume_id=None):
    client = Client()

    if volume_id:
        volume = client.get_volume(volume_id)
    else:
        volume = client.add_volume(os.path.normpath(
            flask.request.json['path']))
        if volume.source_path:
            return utils.jsonify({})

    if 'name' in flask.request.json:
        volume.name = flask.request.json['name']
    if 'source_path' in flask.request.json:
        volume.source_path = os.path.normpath(
            flask.request.json['source_path'])
    if 'path' in flask.request.json:
        volume.path = os.path.normpath(flask.request.json['path'])
    if 'excludes' in flask.request.json:
        volume.excludes = flask.request.json['excludes']
        for i, exclude in enumerate(volume.excludes):
            volume.excludes[i] = os.path.normpath(exclude)

    if 'schedule' in flask.request.json:
        volume.schedule = flask.request.json['schedule']
    if 'min_free_space' in flask.request.json:
        volume.min_free_space = flask.request.json['min_free_space']
    if 'snapshot_limit' in flask.request.json:
        volume.snapshot_limit = flask.request.json['snapshot_limit']
    if 'bandwidth_limit' in flask.request.json:
        volume.bandwidth_limit = flask.request.json['bandwidth_limit']

    if 'email' in flask.request.json:
        volume.email = flask.request.json['email']
    if 'email_host' in flask.request.json:
        volume.email_host = flask.request.json['email_host']
    if 'email_user' in flask.request.json:
        volume.email_user = flask.request.json['email_user']
    if 'email_pass' in flask.request.json:
        volume.email_pass = flask.request.json['email_pass']
    if 'email_ssl' in flask.request.json:
        volume.email_ssl = flask.request.json['email_ssl']

    volume.commit()

    if 'email_send_test' in flask.request.json and \
            flask.request.json['email_send_test']:
        msg = Messenger(volume)
        msg.send('Test notification.')

    return utils.jsonify({})
Ejemplo n.º 9
0
def task_put(volume_id, task_id):
    client = Client()
    volume = client.get_volume(volume_id)

    if not volume:
        return utils.jsonify({
            'error': VOLUME_NOT_FOUND,
            'error_msg': VOLUME_NOT_FOUND_MSG,
        }, 404)

    task = Task(id=task_id.encode())

    if 'abort' in flask.request.json and flask.request.json['abort']:
        task.abort()

    return utils.jsonify({})
Ejemplo n.º 10
0
def text_get(volume_id, snapshot_id, path=None):
    client = Client()
    volume = client.get_volume(volume_id)

    if not volume:
        return utils.jsonify({
            'error': VOLUME_NOT_FOUND,
            'error_msg': VOLUME_NOT_FOUND_MSG,
        }, 404)

    if snapshot_id == 'origin':
        bucket = volume.get_origin()
    else:
        bucket = volume.get_snapshot(int(snapshot_id))

    if not bucket:
        return utils.jsonify({
            'error': SNAPSHOT_NOT_FOUND,
            'error_msg': SNAPSHOT_NOT_FOUND_MSG,
        }, 404)

    object = bucket.get_object(path)
    object.get_syntax()

    syntax = object.syntax
    if object.syntax and object.size > MAX_TEXT_SIZE:
        syntax = ''

    object_data = {
        'id': object.name,
        'type': object.type,
        'syntax': syntax,
        'data': object.read(),
    }

    if not object_data:
        return utils.jsonify({
            'error': FILE_NOT_FOUND,
            'error_msg': FILE_NOT_FOUND_MSG,
        }, 404)

    return utils.jsonify(object_data)
Ejemplo n.º 11
0
def snapshot_delete(volume_id, snapshot_id):
    client = Client()
    volume = client.get_volume(volume_id)

    if not volume:
        return utils.jsonify({
            'error': VOLUME_NOT_FOUND,
            'error_msg': VOLUME_NOT_FOUND_MSG,
        }, 404)

    snapshot = volume.get_snapshot(snapshot_id)

    if not snapshot:
        return utils.jsonify({
            'error': SNAPSHOT_NOT_FOUND,
            'error_msg': SNAPSHOT_NOT_FOUND_MSG,
        }, 404)

    volume.remove_snapshot(snapshot)

    return utils.jsonify({})
Ejemplo n.º 12
0
def snapshot_get(volume_id):
    client = Client()
    volume = client.get_volume(volume_id)

    if not volume:
        return utils.jsonify({
            'error': VOLUME_NOT_FOUND,
            'error_msg': VOLUME_NOT_FOUND_MSG,
        }, 404)

    snapshots = []
    for snapshot in reversed(volume.get_snapshots()):
        snapshots.append({
            'id': snapshot.id,
            'volume': volume.id,
            'state': snapshot.state,
            'sent': snapshot.sent,
            'received': snapshot.received,
            'speed': snapshot.speed,
            'runtime': snapshot.runtime,
            'has_log': True,
        })

    return utils.jsonify(snapshots)
Ejemplo n.º 13
0
def path_get(path=None):
    path = '/' + (path or '')
    paths = []

    if path != '/':
        paths.append({
            'name': '..',
            'path': os.path.abspath(os.path.join(path, os.pardir)),
        })

    try:
        path_list = os.listdir(path)
    except OSError, error:
        return utils.jsonify({
            'error': PATH_NOT_FOUND,
            'error_msg': error.strerror,
        }, 404)
Ejemplo n.º 14
0
def volume_delete(volume_id):
    client = Client()
    client.remove_volume(volume_id)

    return utils.jsonify({})
Ejemplo n.º 15
0
def debug_dump_tasks_get():
    return utils.jsonify(server.app_db.get('tasks'))
Ejemplo n.º 16
0
def debug_dump_events_get():
    return utils.jsonify(server.app_db.get('events'))
Ejemplo n.º 17
0
def debug_dump_system_get():
    return utils.jsonify(server.app_db.get('system'))
Ejemplo n.º 18
0
def task_delete(volume_id, task_id):
    client = Client()
    task = Task(id=task_id.encode())
    task.remove()

    return utils.jsonify({})
Ejemplo n.º 19
0
    try:
        bucket_objects = bucket.get_objects(path)
    except OSError, error:
        return utils.jsonify({
            'error': PATH_NOT_FOUND,
            'error_msg': error.strerror,
        }, 404)

    for object in bucket_objects:
        objects_dict[object.name] = {
            'id': object.name,
            'type': object.type,
            'syntax': object.syntax,
            'size': object.size,
            'time': object.time,
        }
        objects_name.append(object.name)

    objects = []
    for object_name in sorted(objects_name):
        object = objects_dict[object_name]
        if object['type'] == DIR_MIME_TYPE:
            objects.append(object)

    for object_name in sorted(objects_name):
        object = objects_dict[object_name]
        if object['type'] != DIR_MIME_TYPE:
            objects.append(object)

    return utils.jsonify(objects)
Ejemplo n.º 20
0
@server.app.route('/path/<path:path>', methods=['GET'])
def path_get(path=None):
    path = '/' + (path or '')
    paths = []

    if path != '/':
        paths.append({
            'name': '..',
            'path': os.path.abspath(os.path.join(path, os.pardir)),
        })

    try:
        path_list = os.listdir(path)
    except OSError, error:
        return utils.jsonify({
            'error': PATH_NOT_FOUND,
            'error_msg': error.strerror,
        }, 404)

    for name in sorted(path_list):
        full_path = os.path.join(path, name)
        if not os.path.isdir(full_path):
            continue

        paths.append({
            'name': name,
            'path': full_path,
        })

    return utils.jsonify(paths)