retspen / webvirtmgr

WebVirtMgr panel for manage virtual machine
http://retspen.github.io
2.05k stars 537 forks source link

Django 1.7+ compatibility #592

Open wildstray opened 8 years ago

wildstray commented 8 years ago

I upgraded OS to Debian 8.2 "Jessie" so python-django was updated to 1.7.7. Since that moment I'm experiencing many major issues. 1) Startup script In /etc/supervisor/conf.d/webvirtmgr.conf I had to change command to:

command=/usr/bin/gunicorn webvirtmgr.wsgi:application

2) Web console In /opt/webvirtmgr/console/webvirtmgr-console I had to add after os.environ.setdefault("DJANGO_SETTINGS_MODULE", "webvirtmgr.settings"):

import django
django.setup()

3) I had to migrate db (commands valid for Debian/Ubuntu installation type):

sudo -u webvirtmgr python /opt/webvirtmgr/manage.py makemigrations
sudo -u webvirtmgr python /opt/webvirtmgr/manage.py migrate

Now webvirtmgr is "semi-usable" "semi-working"... console random fails for some guests (and not with some others, I've no explanations), this is the traceback:

ERROR:django.request:Internal Server Error: /console/
Traceback (most recent call last):
  File "/usr/lib/python2.7/dist-packages/django/core/handlers/base.py", line 111, in get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/opt/webvirtmgr/console/views.py", line 63, in console
    response.set_cookie('token', token)
AttributeError: 'str' object has no attribute 'set_cookie'

Likewise, graphs (hostdetails, instdetails) are not working anymore due to cookies (functions get_cookie, set_cookie and direct access to cookies) as it seems that object WSGIRequest doesn't expose them anymore.

ERROR:django.request:Internal Server Error: /info/hostusage/1/
Traceback (most recent call last):
  File "/usr/lib/python2.7/dist-packages/django/core/handlers/base.py", line 111, in get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/opt/webvirtmgr/hostdetail/views.py", line 42, in hostusage
    cookies['cpu'] = request._cookies['cpu']
AttributeError: 'WSGIRequest' object has no attribute '_cookies'

I'm using last webvirtmgr version from git...

wildstray commented 8 years ago

Just solved the console issue deleting all records of instance_instance table of /opt/webvirtmgr/webvirtmgr.sqlite3

 echo "delete from instance_instance;" | sqlite3 /opt/webvirtmgr/webvirtmgr.sqlite3

Now all server console connects (but it isn't stable as it was with django 1.5.5+, I'm experiencing disconnections...). Unluckily this does not solve issues with 'WSGIRequest' object has no attribute '_get_cookies'...

wildstray commented 8 years ago

...using request.session instead of request._get_cookies and response.cookie might be a solution?

wildstray commented 8 years ago

A little patch (far from beeing perfect... eg. CPU usage for guest seems to be always 0 also if there is load...)

--- /opt/webvirtmgr.old/instance/views.py       2015-11-27 08:36:15.000000000 +0100
+++ /opt/webvirtmgr/instance/views.py   2016-01-13 13:44:30.838477614 +0100
@@ -26,25 +26,24 @@
     if not request.user.is_authenticated():
         return HttpResponseRedirect(reverse('login'))

-    cookies = {}
     datasets = {}
     datasets_rd = []
     datasets_wr = []
     json_blk = []
-    cookie_blk = {}
+    session_blk = {}
     blk_error = False
     datasets_rx = []
     datasets_tx = []
     json_net = []
-    cookie_net = {}
+    session_net = {}
+    network = {}
+    disk = {}
     net_error = False
     points = 5
     curent_time = time.strftime("%H:%M:%S")
     compute = Compute.objects.get(id=host_id)
-    cookies = request._get_cookies()
     response = HttpResponse()
     response['Content-Type'] = "text/javascript"
-
     try:
         conn = wvmInstance(compute.hostname,
                            compute.login,
@@ -55,13 +54,12 @@
         blk_usage = conn.disk_usage()
         net_usage = conn.net_usage()
         conn.close()
-
-        if cookies.get('cpu') == '{}' or not cookies.get('cpu') or not cpu_usage:
+        if request.session.get('cpu') == {} or not request.session.get('cpu') or not cpu_usage:
             datasets['cpu'] = [0]
             datasets['timer'] = [curent_time]
         else:
-            datasets['cpu'] = eval(cookies.get('cpu'))
-            datasets['timer'] = eval(cookies.get('timer'))
+            datasets['cpu'] = request.session.get('cpu')
+            datasets['timer'] = request.session.get('timer')

         datasets['timer'].append(curent_time)
         datasets['cpu'].append(int(cpu_usage['cpu']))
@@ -85,11 +83,11 @@
         }

         for blk in blk_usage:
-            if cookies.get('hdd') == '{}' or not cookies.get('hdd') or not blk_usage:
+            if request.session.get('hdd') == {} or not request.session.get('hdd') or not blk_usage:
                 datasets_wr.append(0)
                 datasets_rd.append(0)
             else:
-                datasets['hdd'] = eval(cookies.get('hdd'))
+                datasets['hdd'] = request.session.get('hdd')
                 try:
                     datasets_rd = datasets['hdd'][blk['dev']][0]
                     datasets_wr = datasets['hdd'][blk['dev']][1]
@@ -126,17 +124,17 @@
                 }

             json_blk.append({'dev': blk['dev'], 'data': disk})
-            cookie_blk[blk['dev']] = [datasets_rd, datasets_wr]
+            session_blk[blk['dev']] = [datasets_rd, datasets_wr]

         for net in net_usage:
-            if cookies.get('net') == '{}' or not cookies.get('net') or not net_usage:
+            if request.session.get('net') == {} or not request.session.get('net') or not net_usage:
                 datasets_rx.append(0)
                 datasets_tx.append(0)
             else:
-                datasets['net'] = eval(cookies.get('net'))
+                datasets['net'] = request.session.get('net')
                 try:
-                    datasets_rx = datasets['net'][net['dev']][0]
-                    datasets_tx = datasets['net'][net['dev']][1]
+                    datasets_rx = datasets['net'][str(net['dev'])][0]
+                    datasets_tx = datasets['net'][str(net['dev'])][1]
                 except:
                     net_error = True

@@ -170,13 +168,14 @@
                 }

             json_net.append({'dev': net['dev'], 'data': network})
-            cookie_net[net['dev']] = [datasets_rx, datasets_tx]
+            session_net[net['dev']] = [datasets_rx, datasets_tx]
+
+        request.session['cpu'] = datasets['cpu']
+        request.session['timer'] = datasets['timer']
+        request.session['hdd'] = session_blk
+        request.session['net'] = session_net

         data = json.dumps({'cpu': cpu, 'hdd': json_blk, 'net': json_net})
-        response.cookies['cpu'] = datasets['cpu']
-        response.cookies['timer'] = datasets['timer']
-        response.cookies['hdd'] = cookie_blk
-        response.cookies['net'] = cookie_net
         response.write(data)
     except libvirtError:
         data = json.dumps({'error': 'Error 500'})
--- /opt/webvirtmgr.old/hostdetail/views.py     2016-01-12 18:59:30.123400812 +0100
+++ /opt/webvirtmgr/hostdetail/views.py 2016-01-13 02:46:20.394806269 +0100
@@ -21,7 +21,6 @@

     points = 5
     datasets = {}
-    cookies = {}
     compute = Compute.objects.get(id=host_id)
     curent_time = time.strftime("%H:%M:%S")

@@ -36,34 +35,10 @@
     except libvirtError:
         cpu_usage = 0
         mem_usage = 0
-    """
-    try:
-        cookies['cpu'] = request._cookies['cpu']
-        cookies['mem'] = request._cookies['mem']
-        cookies['timer'] = request._cookies['timer']
-    except KeyError:
-        cookies['cpu'] = None
-        cookies['mem'] = None
-
-    if not cookies['cpu'] and not cookies['mem']:
-        datasets['cpu'] = [0]
-        datasets['mem'] = [0]
-        datasets['timer'] = [curent_time]
-    else:
-        datasets['cpu'] = eval(cookies['cpu'])
-        datasets['mem'] = eval(cookies['mem'])
-        datasets['timer'] = eval(cookies['timer'])
-    """
-    print request.__dict__
-    cookies = request._get_cookies()
-    if cookies.get('cpu') == '{}' or not cookies.get('cpu') or not cpu_usage:
-        datasets['cpu'] = [0]
-        datasets['mem'] = [0]
-        datasets['timer'] = [curent_time]
-    else:
-       datasets['cpu'] = eval(cookies['cpu'])
-       datasets['mem'] = eval(cookies['mem'])
-       datasets['timer'] = eval(cookies['timer'])
+
+    datasets['cpu'] = request.session.get('cpu', [0])
+    datasets['mem'] = request.session.get('mem', [0])
+    datasets['timer'] = request.session.get('timer', [curent_time])

     datasets['timer'].append(curent_time)
     datasets['cpu'].append(int(cpu_usage['usage']))
@@ -102,12 +77,13 @@
         ]
     }

+    request.session['cpu'] = datasets['cpu']
+    request.session['timer'] = datasets['timer']
+    request.session['mem'] = datasets['mem']
+
     data = json.dumps({'cpu': cpu, 'memory': memory})
     response = HttpResponse()
     response['Content-Type'] = "text/javascript"
-    response.cookies['cpu'] = datasets['cpu']
-    response.cookies['timer'] = datasets['timer']
-    response.cookies['mem'] = datasets['mem']
     response.write(data)
     return response
wildstray commented 8 years ago
--- /opt/webvirtmgr.old/console/webvirtmgr-console      2016-01-14 12:39:28.887877598 +0100
+++ /opt/webvirtmgr/console/webvirtmgr-console  2016-01-13 02:05:01.018446547 +0100
@@ -16,6 +16,9 @@
 # if VENV_PATH not in sys.path:
 # sys.path.append(VENV_PATH)

+import django
+django.setup()
+
 import Cookie
 import socket
 from webvirtmgr.settings import WS_PORT, WS_HOST, WS_CERT
purplesrl commented 8 years ago

https://github.com/retspen/webvirtmgr/issues/613

rapgro commented 7 years ago

"Per our supported versions policy, Django 1.5 and 1.6 are no longer receiving security updates." since at least CVE-2015-5145. See also CVE-2015-5144.