Пример #1
0
def _initiate_push(request, callback_view, remote_host_id, obj_type,
                   push_obj_id):
    """
    Kicks off data push
    :param Request request: request object
    :param function callback_view: initiating view (e.g. initiate_push_datafile)
    :param int remote_host_id: database id of remote host
    :param class obj_type: the type of data
    :param int push_obj_id: the data object id
    :return: status message, host list or OAuth2 redirects
    :rtype: HttpResponse
    """

    # If the remote_host_id is not given, render a view to show a list of
    # acceptable hosts
    if remote_host_id is None:
        args = {'obj_type': obj_type, 'push_obj_id': push_obj_id}
        c = {
            'cert_signing_services_url': reverse(get_signing_services,
                                                 kwargs=args),
            'accessible_hosts_url': reverse(get_accessible_hosts, kwargs=args)
        }
        return render(request, 'host_list.html', c)

    try:
        remote_host = RemoteHost.objects.get(pk=remote_host_id)
        credential = get_credential(request, remote_host)

        ssh_client = credential.get_client_for_host(remote_host)
        if request.GET.get('path', None) is not None:
            destination = request.GET.get('path')
        else:
            args = {'remote_host_id': remote_host_id}
            c = {
                'remote_path_verify_url':
                reverse(validate_remote_path, kwargs=args),
                'remote_destination_name':
                remote_host.nickname
            }
            return render(request, 'destination_selector.html', c)

        destination_ok, message = can_copy(ssh_client, obj_type, push_obj_id,
                                           destination)
        if not destination_ok:
            return render_error_message(request,
                                        'Invalid destination: %s' % message)

    except NoSuitableCredential:
        callback_args = {
            'remote_host_id': remote_host_id,
            obj_type + '_id': push_obj_id
        }
        callback_url = reverse(callback_view, kwargs=callback_args)
        redirect_args = {'remote_host_id': remote_host_id}
        redirect_url = reverse(
            authorize_remote_access,
            kwargs=redirect_args) + '?next=%s' % callback_url
        return redirect(redirect_url)

    push_to_priority = settings.DEFAULT_TASK_PRIORITY + 1

    if obj_type == 'experiment':
        tasks.push_experiment_to_host.apply_async(args=[
            request.user.pk, credential.pk, remote_host_id, push_obj_id,
            destination
        ],
                                                  priority=push_to_priority)
    elif obj_type == 'dataset':
        tasks.push_dataset_to_host.apply_async(args=[
            request.user.pk, credential.pk, remote_host_id, push_obj_id,
            destination
        ],
                                               priority=push_to_priority)
    elif obj_type == 'datafile':
        tasks.push_datafile_to_host.apply_async(args=[
            request.user.pk, credential.pk, remote_host_id, push_obj_id,
            destination
        ],
                                                priority=push_to_priority)

    success_message = ('The requested item will be pushed to %s. <strong>You '
                       'will be notified by email once this has been '
                       'completed.</strong>'
                       '<br/>'
                       'Data will be pushed to '
                       '<pre>%s</pre>')
    success_message %= (remote_host.nickname, destination)
    return render_success_message(request, success_message)
Пример #2
0
def _initiate_push(request, callback_view, remote_host_id, obj_type,
                   push_obj_id):
    """
    Kicks off data push
    :param Request request: request object
    :param function callback_view: initiating view (e.g. initiate_push_datafile)
    :param int remote_host_id: database id of remote host
    :param class obj_type: the type of data
    :param int push_obj_id: the data object id
    :return: status message, host list or OAuth2 redirects
    :rtype: HttpResponse
    """

    # If the remote_host_id is not given, render a view to show a list of
    # acceptable hosts
    if remote_host_id is None:
        args = {'obj_type': obj_type, 'push_obj_id': push_obj_id}
        c = {
            'cert_signing_services_url': reverse(get_signing_services,
                                                 kwargs=args),
            'accessible_hosts_url': reverse(get_accessible_hosts, kwargs=args)
        }
        return render(request, 'host_list.html', c)

    remote_host = RemoteHost.objects.get(pk=remote_host_id)

    destination = request.GET.get("path", None)
    if destination is None:
        args = {"remote_host_id": remote_host_id}
        c = {
            'remote_path_verify_url': reverse(validate_remote_path,
                                              kwargs=args),
            'remote_destination_name': remote_host.nickname
        }
        return render(request, 'destination_selector.html', c)

    try:
        credential = get_credential(request, remote_host)
    except:
        callback_args = {
            'remote_host_id': remote_host_id,
            obj_type + '_id': push_obj_id
        }
        callback_url = reverse(callback_view, kwargs=callback_args)
        redirect_args = {'remote_host_id': remote_host_id}
        redirect_url = reverse(authorize_remote_access, kwargs=redirect_args) + \
                       "?next=%s" % callback_url
        return redirect(redirect_url)

    try:
        ssh = credential.get_client_for_host(remote_host)
        sftp = ssh.open_sftp()
        destination_ok, message = can_copy(sftp, obj_type, push_obj_id,
                                           destination)
        sftp.close()
        ssh.close()
    except Exception as e:
        return render_error_message(request,
                                    "Can't connect to this host: %s" % str(e))

    if not destination_ok:
        return render_error_message(request,
                                    "Invalid destination: %s" % message)

    priority = settings.DEFAULT_TASK_PRIORITY + 1

    params = [
        request.user.pk, credential.pk, remote_host_id, push_obj_id,
        destination
    ]

    if obj_type == "experiment":
        tasks.push_experiment_to_host.apply_async(args=params,
                                                  priority=priority)
    elif obj_type == "dataset":
        tasks.push_dataset_to_host.apply_async(args=params, priority=priority)
    elif obj_type == "datafile":
        tasks.push_datafile_to_host.apply_async(args=params, priority=priority)

    # Log PushTo event
    if getattr(settings, "ENABLE_EVENTLOG", False):
        from tardis.apps.eventlog.utils import log
        log(action="PUSH_TO",
            extra={
                "id": push_obj_id,
                "type": obj_type
            },
            request=request)

    success_message = ('The requested item will be pushed to %s. <strong>You '
                       'will be notified by email once this has been '
                       'completed.</strong>'
                       '<br/>'
                       'Data will be pushed to '
                       '<pre>%s</pre>')
    success_message %= (remote_host.nickname, destination)
    return render_success_message(request, success_message)
Пример #3
0
def _initiate_push(
        request, callback_view, remote_host_id, obj_type, push_obj_id
):
    """
    Kicks off data push
    :param request: request object
    :param callback_view: initiating view (e.g. initiate_push_datafile)
    :param remote_host_id: database id of remote host
    :param obj_type: the type of data
    :param push_obj_id: the data object id
    :param destination: a path
    :return: status message, host list or OAuth2 redirects
    """

    # If the remote_host_id is not given, render a view to show a list of
    # acceptable hosts
    if remote_host_id is None:
        args = {'obj_type': obj_type, 'push_obj_id': push_obj_id}
        c = {
            'cert_signing_services_url': reverse(
                get_signing_services,
                kwargs=args),
            'accessible_hosts_url': reverse(
                get_accessible_hosts,
                kwargs=args)
        }
        return HttpResponse(render(request, 'host_list.html', c))

    try:
        remote_host = RemoteHost.objects.get(pk=remote_host_id)
        credential = get_credential(request, remote_host)

        ssh_client = credential.get_client_for_host(remote_host)
        if request.GET.get('path', None) is not None:
            destination = request.GET.get('path')
        else:
            args = {
                'remote_host_id': remote_host_id
            }
            c = {
                'remote_path_verify_url': reverse(validate_remote_path,
                                                  kwargs=args),
                'remote_destination_name': remote_host.nickname
            }
            return HttpResponse(
                render(request, 'destination_selector.html', c))

        destination_ok, message = can_copy(ssh_client, obj_type, push_obj_id,
                                           destination)
        if not destination_ok:
            return render_error_message(request,
                                        'Invalid destination: %s' % message)

    except NoSuitableCredential:
        callback_args = {
            'remote_host_id': remote_host_id,
            obj_type + '_id': push_obj_id
        }
        callback_url = reverse(callback_view, kwargs=callback_args)
        redirect_args = {'remote_host_id': remote_host_id}
        redirect_url = reverse(
            authorize_remote_access,
            kwargs=redirect_args) + '?next=%s' % callback_url
        return redirect(redirect_url)

    if obj_type == 'experiment':
        tasks.push_experiment_to_host.delay(
            request.user.pk, credential.pk, remote_host_id, push_obj_id,
            destination)
    elif obj_type == 'dataset':
        tasks.push_dataset_to_host.delay(
            request.user.pk, credential.pk, remote_host_id, push_obj_id,
            destination)
    elif obj_type == 'datafile':
        tasks.push_datafile_to_host.delay(
            request.user.pk, credential.pk, remote_host_id, push_obj_id,
            destination)

    success_message = ('The requested item will be pushed to %s. <strong>You '
                       'will be notified by email once this has been '
                       'completed.</strong>'
                       '<br/>'
                       'Data will be pushed to '
                       '<pre>%s</pre>')
    success_message %= (remote_host.nickname, destination)
    return render_success_message(
        request,
        success_message)