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)
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({})
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)
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)
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)
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({})
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({})
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({})
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)
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({})
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)
def volume_delete(volume_id): client = Client() client.remove_volume(volume_id) return utils.jsonify({})