Beispiel #1
0
def treemenu(request, plugin_id):
    """
    This is how we inject nodes to the Tree Menu

    The FreeNAS GUI will access this view, expecting for a JSON
    that describes a node and possible some children.
    """

    (mineos_key, mineos_secret) = utils.get_mineos_oauth_creds()
    url = utils.get_rpc_url(request)
    trans = OAuthTransport(url, key=mineos_key, secret=mineos_secret)
    server = jsonrpclib.Server(url, transport=trans)
    jail = json.loads(server.plugins.jail.info(plugin_id))[0]
    jail_name = jail['fields']['jail_host']
    number = jail_name.rsplit('_', 1)
    name = "MineOS"
    if len(number) == 2:
        try:
            number = int(number)
            if number > 1:
                name = "MineOS (%d)" % number
        except:
            pass

    plugin = {
        'name': name,
        'append_to': 'plugins',
        'icon': reverse('treemenu_icon', kwargs={'plugin_id': plugin_id}),
        'type': 'pluginsfcgi',
        'url': reverse('mineos_edit', kwargs={'plugin_id': plugin_id}),
        'kwargs': {'plugin_name': 'mineos', 'plugin_id': plugin_id },
    }

    return HttpResponse(json.dumps([plugin]), content_type='application/json')
Beispiel #2
0
def edit(request, plugin_id):
    (mineos_key, mineos_secret) = utils.get_mineos_oauth_creds()
    url = utils.get_rpc_url(request)
    trans = OAuthTransport(url, key=mineos_key, secret=mineos_secret)
    """
    Get the MineOS object
    If it does not exist create a new entry
    """
    try:
        mineos = models.MineOS.objects.order_by('-id')[0]
    except IndexError:
        mineos = models.MineOS.objects.create()

    try:
        server = jsonrpclib.Server(url, transport=trans)
        jail_path = server.plugins.jail.path(plugin_id)
        jail = json.loads(server.plugins.jail.info(plugin_id))[0]['fields']
        jail_ipv4 = jail['jail_ipv4'].split('/')[0]
        if mineos.mineos_ssl:
            mineos_scheme = "https"
        else:
            mineos_scheme = "http"
        mineos_port = mineos.mineos_port
        auth = server.plugins.is_authenticated(
            request.COOKIES.get("sessionid", ""))
        assert auth
    except Exception as e:
        raise

    if request.method == "GET":
        form = forms.MineOSForm(instance=mineos, jail_path=jail_path)
        return render(
            request, "edit.html", {
                'form': form,
                'ipv4': jail_ipv4,
                'scheme': mineos_scheme,
                'port': mineos_port
            })

    if not request.POST:
        return JsonResponse(request, error=True, message="A problem occurred.")

    form = forms.MineOSForm(request.POST, instance=mineos, jail_path=jail_path)
    if form.is_valid():
        form.save()

        cmd = "%s restart" % utils.mineos_control
        pipe = Popen(cmd,
                     stdin=PIPE,
                     stdout=PIPE,
                     stderr=PIPE,
                     shell=True,
                     close_fds=True)

        return JsonResponse(request,
                            error=True,
                            message="MineOS settings successfully saved.")

    return JsonResponse(request, form=form)
Beispiel #3
0
def edit(request, plugin_id):
    (mineos_key, mineos_secret) = utils.get_mineos_oauth_creds()
    url = utils.get_rpc_url(request)
    trans = OAuthTransport(url, key=mineos_key, secret=mineos_secret)

    """
    Get the MineOS object
    If it does not exist create a new entry
    """
    try:
        mineos = models.MineOS.objects.order_by('-id')[0]
    except IndexError:
        mineos = models.MineOS.objects.create()

    try:
        server = jsonrpclib.Server(url, transport=trans)
        jail_path = server.plugins.jail.path(plugin_id)
        jail = json.loads(server.plugins.jail.info(plugin_id))[0]['fields']
        jail_ipv4 = jail['jail_ipv4'].split('/')[0]
        if mineos.mineos_ssl:
                mineos_scheme = "https"
        else:
                mineos_scheme = "http"
        mineos_port = mineos.mineos_port
        auth = server.plugins.is_authenticated(
            request.COOKIES.get("sessionid", "")
            )
        assert auth
    except Exception as e:
        raise

    if request.method == "GET":
        form = forms.MineOSForm(instance=mineos, jail_path=jail_path)
        return render(request, "edit.html", {
            'form': form,
            'ipv4': jail_ipv4,
            'scheme': mineos_scheme,
            'port': mineos_port
        })

    if not request.POST:
        return JsonResponse(request, error=True, message="A problem occurred.")

    form = forms.MineOSForm(request.POST, instance=mineos, jail_path=jail_path)
    if form.is_valid():
        form.save()

        cmd = "%s restart" % utils.mineos_control
        pipe = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE, shell=True, close_fds=True)

        return JsonResponse(request, error=True, message="MineOS settings successfully saved.")

    return JsonResponse(request, form=form)
Beispiel #4
0
def start(request, plugin_id):
    (mineos_key, mineos_secret) = utils.get_mineos_oauth_creds()

    url = utils.get_rpc_url(request)
    trans = OAuthTransport(url, key=mineos_key, secret=mineos_secret)

    server = jsonrpclib.Server(url, transport=trans)
    auth = server.plugins.is_authenticated(
        request.COOKIES.get("sessionid", "")
        )
    jail_path = server.plugins.jail.path(plugin_id)
    assert auth

    linprocfs_path = '/usr/compat/linux/proc'
    linprocfs = _linprocfs_mounted(linprocfs_path)
    linprocfs_path = linprocfs_path.lstrip('/')
    linprocfs_path = os.path.join(jail_path, linprocfs_path)

    if linprocfs is False:
        return HttpResponse(simplejson.dumps({
            'error': True,
            'message': 'Please run "mount -t linprocfs linprocfs ' + linprocfs_path + '" first.',
        }), content_type='application/json')

    try:
        mineos = models.MineOS.objects.order_by('-id')[0]
        mineos.enable = True
        mineos.save()
    except IndexError:
        mineos = models.MineOS.objects.create(enable=True)

    try:
        form = forms.MineOSForm(mineos.__dict__, instance=mineos, jail_path=jail_path)
        form.is_valid()
        form.save()
    except ValueError:
        return HttpResponse(simplejson.dumps({
            'error': True,
            'message': ('MineOS data did not validate, configure '
                'it first.'),
            }), content_type='application/json')

    cmd = "%s onestart" % utils.mineos_control
    pipe = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE, shell=True, close_fds=True)

    out = pipe.communicate()[0]
    return HttpResponse(simplejson.dumps({
        'error': False,
        'message': out,
        }), content_type='application/json')
Beispiel #5
0
def start(request, plugin_id):
    (mineos_key, mineos_secret) = utils.get_mineos_oauth_creds()

    url = utils.get_rpc_url(request)
    trans = OAuthTransport(url, key=mineos_key, secret=mineos_secret)

    server = jsonrpclib.Server(url, transport=trans)
    auth = server.plugins.is_authenticated(request.COOKIES.get(
        "sessionid", ""))
    jail_path = server.plugins.jail.path(plugin_id)
    assert auth

    _linprocfs_mount(server, plugin_id)

    try:
        mineos = models.MineOS.objects.order_by('-id')[0]
        mineos.enable = True
        mineos.save()
    except IndexError:
        mineos = models.MineOS.objects.create(enable=True)

    try:
        form = forms.MineOSForm(mineos.__dict__,
                                instance=mineos,
                                jail_path=jail_path)
        form.is_valid()
        form.save()
    except ValueError:
        return HttpResponse(simplejson.dumps({
            'error':
            True,
            'message': ('MineOS data did not validate, configure '
                        'it first.'),
        }),
                            content_type='application/json')

    cmd = "%s onestart" % utils.mineos_control
    pipe = Popen(cmd,
                 stdin=PIPE,
                 stdout=PIPE,
                 stderr=PIPE,
                 shell=True,
                 close_fds=True)

    out = pipe.communicate()[0]
    return HttpResponse(simplejson.dumps({
        'error': False,
        'message': out,
    }),
                        content_type='application/json')
Beispiel #6
0
def stop(request, plugin_id):
    (mineos_key,
    mineos_secret) = utils.get_mineos_oauth_creds()
    url = utils.get_rpc_url(request)
    trans = OAuthTransport(url, key=mineos_key,
        secret=mineos_secret)

    server = jsonrpclib.Server(url, transport=trans)
    auth = server.plugins.is_authenticated(
        request.COOKIES.get("sessionid", "")
        )
    jail_path = server.plugins.jail.path(plugin_id)
    assert auth

    _linprocfs_mount(server, plugin_id)

    try:
        mineos = models.MineOS.objects.order_by('-id')[0]
        mineos.enable = False
        mineos.save()
    except IndexError:
        mineos = models.MineOS.objects.create(enable=False)

    try:
        form = forms.MineOSForm(mineos.__dict__,
            instance=mineos,
            jail_path=jail_path)
        form.is_valid()
        form.save()
    except ValueError:
        pass

    cmd = "%s onestop" % utils.mineos_control
    pipe = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE,
        shell=True, close_fds=True)

    out = pipe.communicate()[0]
    linprocfs_path = '/usr/compat/linux/proc'
    linprocfs = _linprocfs_check(linprocfs_path)
    while linprocfs is True:
        server.fs.umount(plugin_id, linprocfs_path)
        linprocfs = _linprocfs_check(linprocfs_path)
    return HttpResponse(simplejson.dumps({
        'error': False,
        'message': out,
        }), content_type='application/json')
Beispiel #7
0
def treemenu(request, plugin_id):
    """
    This is how we inject nodes to the Tree Menu

    The FreeNAS GUI will access this view, expecting for a JSON
    that describes a node and possible some children.
    """

    (mineos_key, mineos_secret) = utils.get_mineos_oauth_creds()
    url = utils.get_rpc_url(request)
    trans = OAuthTransport(url, key=mineos_key, secret=mineos_secret)
    server = jsonrpclib.Server(url, transport=trans)
    jail = json.loads(server.plugins.jail.info(plugin_id))[0]
    jail_name = jail['fields']['jail_host']
    number = jail_name.rsplit('_', 1)
    name = "MineOS"
    if len(number) == 2:
        try:
            number = int(number)
            if number > 1:
                name = "MineOS (%d)" % number
        except:
            pass

    plugin = {
        'name': name,
        'append_to': 'plugins',
        'icon': reverse('treemenu_icon', kwargs={'plugin_id': plugin_id}),
        'type': 'pluginsfcgi',
        'url': reverse('mineos_edit', kwargs={'plugin_id': plugin_id}),
        'kwargs': {
            'plugin_name': 'mineos',
            'plugin_id': plugin_id
        },
    }

    return HttpResponse(json.dumps([plugin]), content_type='application/json')