-
Notifications
You must be signed in to change notification settings - Fork 478
/
views.py
672 lines (551 loc) · 19.8 KB
/
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
#+
# Copyright 2010 iXsystems, Inc.
# All rights reserved
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted providing that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
# IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
#####################################################################
import logging
import os
import re
import shutil
import signal
import socket
import string
import subprocess
import time
import urllib
import xmlrpclib
from django.contrib.auth import login, get_backends
from django.contrib.auth.models import User
from django.core.servers.basehttp import FileWrapper
from django.core.urlresolvers import reverse
from django.db import transaction
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render
from django.template.loader import render_to_string
from django.utils import simplejson
from django.utils.translation import ugettext as _
from django.views.decorators.cache import never_cache
from freenasUI.account.models import bsdUsers
from freenasUI.common.system import get_sw_name, get_sw_version, send_mail
from freenasUI.common.pipesubr import pipeopen
from freenasUI.freeadmin.views import JsonResp
from freenasUI.middleware.notifier import notifier
from freenasUI.network.models import GlobalConfiguration
from freenasUI.storage.models import MountPoint
from freenasUI.system import forms, models
GRAPHS_DIR = '/var/db/graphs'
VERSION_FILE = '/etc/version'
DEBUG_TEMP = '/tmp/debug.txt'
PGFILE = '/tmp/.extract_progress'
DDFILE = '/tmp/.upgrade_dd'
RE_DD = re.compile(r"^(\d+) bytes", re.M | re.S)
log = logging.getLogger('system.views')
def _system_info(request=None):
# OS, hostname, release
__, hostname, __ = os.uname()[0:3]
platform = subprocess.check_output(['sysctl', '-n', 'hw.model'])
physmem = str(int(int(
subprocess.check_output(['sysctl', '-n', 'hw.physmem'])
) / 1048576)) + 'MB'
# All this for a timezone, because time.asctime() doesn't add it in.
date = time.strftime('%a %b %d %H:%M:%S %Z %Y') + '\n'
uptime = subprocess.check_output(
"env -u TZ uptime | awk -F', load averages:' '{ print $1 }'",
shell=True
)
loadavg = "%.2f, %.2f, %.2f" % os.getloadavg()
freenas_build = "Unrecognized build (%s missing?)" % VERSION_FILE
try:
with open(VERSION_FILE) as d:
freenas_build = d.read()
except:
pass
if request:
host = request.META.get("HTTP_HOST")
else:
host = None
return {
'hostname': hostname,
'platform': platform,
'physmem': physmem,
'date': date,
'uptime': uptime,
'loadavg': loadavg,
'freenas_build': freenas_build,
'host': host,
}
def system_info(request):
sysinfo = _system_info(request)
return render(request, 'system/system_info.html', sysinfo)
def config_restore(request):
if request.method == "POST":
request.session['allow_reboot'] = True
notifier().config_restore()
user = User.objects.all()[0]
backend = get_backends()[0]
user.backend = "%s.%s" % (backend.__module__,
backend.__class__.__name__)
login(request, user)
return render(request, 'system/config_ok2.html')
return render(request, 'system/config_restore.html')
def config_upload(request):
if request.method == "POST":
form = forms.ConfigUploadForm(request.POST, request.FILES)
variables = {
'form': form,
}
if form.is_valid():
if not notifier().config_upload(request.FILES['config']):
form._errors['__all__'] = \
form.error_class([
_('The uploaded file is not valid.'),
])
else:
request.session['allow_reboot'] = True
return render(request, 'system/config_ok.html', variables)
if 'iframe' in request.GET:
return HttpResponse('<html><body><textarea>' +
render_to_string('system/config_upload.html',
variables) +
'</textarea></boby></html>')
else:
return render(request, 'system/config_upload.html', variables)
else:
FIRMWARE_DIR = '/var/tmp/firmware'
if os.path.exists(FIRMWARE_DIR):
if os.path.islink(FIRMWARE_DIR):
os.unlink(FIRMWARE_DIR)
if os.path.isdir(FIRMWARE_DIR):
shutil.rmtree(FIRMWARE_DIR + '/')
os.mkdir(FIRMWARE_DIR)
os.chmod(FIRMWARE_DIR, 01777)
form = forms.ConfigUploadForm()
return render(request, 'system/config_upload.html', {
'form': form,
})
def config_save(request):
hostname = GlobalConfiguration.objects.all().order_by('-id')[0].gc_hostname
filename = '/data/freenas-v1.db'
wrapper = FileWrapper(file(filename))
freenas_build = "UNKNOWN"
try:
with open(VERSION_FILE) as d:
freenas_build = d.read().strip()
except:
pass
response = HttpResponse(wrapper, content_type='application/octet-stream')
response['Content-Length'] = os.path.getsize(filename)
response['Content-Disposition'] = \
'attachment; filename=%s-%s-%s.db' % (
hostname.encode('utf-8'),
freenas_build,
time.strftime('%Y%m%d%H%M%S'))
return response
def reporting(request):
return render(request, 'system/reporting.html')
def settings(request):
try:
settings = models.Settings.objects.order_by("-id")[0]
except:
settings = None
try:
email = models.Email.objects.order_by("-id")[0]
except:
email = None
try:
ssl = models.SSL.objects.order_by("-id")[0]
except:
ssl = None
try:
advanced = models.Advanced.objects.order_by("-id")[0]
except:
advanced = None
return render(request, 'system/settings.html', {
'settings': settings,
'email': email,
'ssl': ssl,
'advanced': advanced,
})
def varlogmessages(request, lines):
if lines is None:
lines = 3
msg = os.popen('tail -n %s /var/log/messages' % int(lines)).read().strip()
return render(request, 'system/status/msg.xml', {
'msg': msg,
}, content_type='text/xml')
def top(request):
top_pipe = os.popen('top')
try:
top_output = top_pipe.read()
finally:
top_pipe.close()
return render(request, 'system/status/top.xml', {
'focused_tab': 'system',
'top': top_output,
}, content_type='text/xml')
def reboot_dialog(request):
if request.method == "POST":
request.session['allow_reboot'] = True
return JsonResp(
request,
message=_("Reboot is being issued"),
events=['window.location="%s"' % reverse('system_reboot')]
)
return render(request, 'system/reboot_dialog.html')
def reboot(request):
""" reboots the system """
if not request.session.get("allow_reboot"):
return HttpResponseRedirect('/')
request.session.pop("allow_reboot")
return render(request, 'system/reboot.html', {
'sw_name': get_sw_name(),
'sw_version': get_sw_version(),
})
def reboot_run(request):
notifier().restart("system")
return HttpResponse('OK')
def shutdown_dialog(request):
if request.method == "POST":
request.session['allow_shutdown'] = True
return JsonResp(
request,
message=_("Shutdown is being issued"),
events=['window.location="%s"' % reverse('system_shutdown')])
return render(request, 'system/shutdown_dialog.html')
def shutdown(request):
""" shuts down the system and powers off the system """
if not request.session.get("allow_shutdown"):
return HttpResponseRedirect('/')
request.session.pop("allow_shutdown")
return render(request, 'system/shutdown.html', {
'sw_name': get_sw_name(),
'sw_version': get_sw_version(),
})
def shutdown_run(request):
notifier().stop("system")
return HttpResponse('OK')
def testmail(request):
try:
kwargs = dict(instance=models.Email.objects.order_by('-id')[0])
except IndexError:
kwargs = {}
form = forms.EmailForm(request.POST, **kwargs)
if not form.is_valid():
return JsonResp(request, form=form)
email = bsdUsers.objects.get(bsdusr_username='root').bsdusr_email
if not email:
return JsonResp(
request,
error=True,
message=_(
"You must configure the root email (Accounts->Users->root)"
),
)
sid = transaction.savepoint()
form.save()
error = False
if request.is_ajax():
sw_name = get_sw_name()
error, errmsg = send_mail(subject=_('Test message from %s'
% (sw_name)),
text=_('This is a message test from %s'
% (sw_name, )))
if error:
errmsg = _("Your test email could not be sent: %s") % errmsg
else:
errmsg = _('Your test email has been sent!')
transaction.savepoint_rollback(sid)
return JsonResp(request, error=error, message=errmsg)
def clearcache(request):
error = False
errmsg = ''
os.system(
"(/usr/local/bin/python "
"/usr/local/www/freenasUI/tools/cachetool.py expire >/dev/null 2>&1 &&"
" /usr/local/bin/python /usr/local/www/freenasUI/tools/cachetool.py "
"fill >/dev/null 2>&1) &")
return HttpResponse(simplejson.dumps({
'error': error,
'errmsg': errmsg,
}))
class DojoFileStore(object):
def __init__(self, path, dirsonly=False, root="/", filterVolumes=True):
self.root = os.path.abspath(str(root))
self.filterVolumes = filterVolumes
if self.filterVolumes:
self.mp = [
os.path.abspath(mp.mp_path.encode('utf8'))
for mp in MountPoint.objects.filter(
mp_volume__vol_fstype__in=('ZFS', 'UFS')
)
]
self.path = os.path.join(self.root, path.replace("..", ""))
self.path = os.path.abspath(self.path)
# POSIX allows one or two initial slashes, but treats three or more
# as single slash.
if self.path.startswith('//'):
self.path = self.path[1:]
self.path = self.path.encode('utf8')
self.dirsonly = dirsonly
if self.dirsonly:
self._lookupurl = 'system_dirbrowser'
else:
self._lookupurl = 'system_filebrowser'
def items(self):
if self.path == self.root:
return self.children(self.path)
node = self._item(self.path, self.path)
if node['directory']:
node['children'] = self.children(self.path)
return node
def children(self, entry):
_children = []
for _entry in sorted(os.listdir(entry)):
#FIXME: better extendable way to exclude files
if _entry.startswith(".") or _entry == 'md_size':
continue
full_path = os.path.join(self.path, _entry)
if self.filterVolumes and len(
[
f for f in self.mp if (
full_path.startswith(f + '/') or full_path == f or
full_path.startswith('/mnt')
)
]
) > 0:
_children.append(self._item(self.path, _entry))
if self.dirsonly:
_children = [child for child in _children if child['directory']]
return _children
def _item(self, path, entry):
full_path = os.path.join(path, entry)
if full_path.startswith(self.root):
path = full_path.replace(self.root, "/", 1)
else:
path = full_path
if path.startswith("//"):
path = path[1:]
isdir = os.path.isdir(full_path)
item = {
'name': os.path.basename(full_path),
'directory': isdir,
'path': path,
}
if isdir:
item['children'] = True
item['$ref'] = os.path.abspath(
reverse(self._lookupurl, kwargs={
'path': path + '?root=' + urllib.quote_plus(self.root),
})
)
item['id'] = item['$ref']
return item
def directory_browser(request, path='/'):
""" This view provides the ajax driven directory browser callback """
#if not path.startswith('/'):
# path = '/%s' % path
directories = DojoFileStore(
path,
dirsonly=True,
root=request.GET.get("root", "/"),
).items()
context = directories
content = simplejson.dumps(context)
return HttpResponse(content, mimetype='application/json')
def file_browser(request, path='/'):
""" This view provides the ajax driven directory browser callback """
#if not path.startswith('/'):
# path = '/%s' % path
directories = DojoFileStore(
path,
dirsonly=False,
root=request.GET.get("root", "/"),
).items()
context = directories
content = simplejson.dumps(context)
return HttpResponse(content, mimetype='application/json')
def firmware_progress(request):
data = {}
if os.path.exists(PGFILE):
with open(PGFILE, 'r') as f:
last = f.readlines()
if last:
step, percent = last[-1].split("|")
data['step'] = int(step)
percent = percent.strip()
if percent:
data['percent'] = int(percent)
else:
data['indeterminate'] = True
elif os.path.exists(DDFILE):
with open(DDFILE, 'r') as f:
pid = f.readline()
if pid:
pid = int(pid.strip())
try:
os.kill(pid, signal.SIGINFO)
time.sleep(0.5)
with open(DDFILE, 'r') as f2:
line = f2.read()
reg = RE_DD.findall(line)
if reg:
current = int(reg[-1])
size = os.stat("/var/tmp/firmware/firmware.img").st_size
percent = int((current / size) * 100)
data = {
'step': 3,
'percent': percent,
}
except OSError:
pass
content = simplejson.dumps(data)
return HttpResponse(content, mimetype='application/json')
def restart_httpd(request):
""" restart httpd """
notifier().restart("http")
return HttpResponse('OK')
def reload_httpd(request):
""" restart httpd """
notifier().reload("http")
return HttpResponse('OK')
def debug(request):
"""Save freenas-debug output to DEBUG_TEMP"""
if request.method == "POST":
form = forms.DebugForm(request.POST)
if form.is_valid():
opts = ["/usr/local/bin/freenas-debug"]
opts += form.get_options()
p1 = pipeopen(string.join(opts, ' '), allowfork=True)
debug = p1.communicate()[0]
with open(DEBUG_TEMP, 'w') as f:
f.write(debug)
return render(request, 'system/debug.html')
else:
form = forms.DebugForm()
return render(request, 'system/debug_form.html', {
'form': form,
})
def debug_save(request):
hostname = GlobalConfiguration.objects.all().order_by('-id')[0].gc_hostname
wrapper = FileWrapper(file(DEBUG_TEMP))
response = HttpResponse(wrapper, content_type='application/octet-stream')
response['Content-Length'] = os.path.getsize(DEBUG_TEMP)
response['Content-Disposition'] = \
'attachment; filename=debug-%s-%s.txt' % (
hostname.encode('utf-8'),
time.strftime('%Y%m%d%H%M%S'),
)
return response
class UnixTransport(xmlrpclib.Transport):
def make_connection(self, addr):
self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
self.sock.connect(addr)
self.sock.settimeout(5)
return self.sock
def single_request(self, host, handler, request_body, verbose=0):
# issue XML-RPC request
self.make_connection(host)
try:
self.sock.send(request_body + "\n")
p, u = self.getparser()
while 1:
data = self.sock.recv(1024)
if not data:
break
p.feed(data)
self.sock.close()
p.close()
return u.close()
except xmlrpclib.Fault:
raise
except Exception:
# All unexpected errors leave connection in
# a strange state, so we clear it.
self.close()
raise
class MyServer(xmlrpclib.ServerProxy):
def __init__(self, addr):
self.__handler = "/"
self.__host = addr
self.__transport = UnixTransport()
self.__encoding = None
self.__verbose = 0
self.__allow_none = 0
def __request(self, methodname, params):
# call a method on the remote server
request = xmlrpclib.dumps(
params,
methodname,
encoding=self.__encoding,
allow_none=self.__allow_none,
)
response = self.__transport.request(
self.__host,
self.__handler,
request,
verbose=self.__verbose
)
if len(response) == 1:
response = response[0]
return response
def __getattr__(self, name):
# magic method dispatcher
return xmlrpclib._Method(self.__request, name)
@never_cache
def terminal(request):
sid = int(request.POST.get("s", 0))
k = request.POST.get("k")
w = int(request.POST.get("w", 80))
h = int(request.POST.get("h", 24))
multiplex = MyServer("/var/run/webshell.sock")
alive = False
for i in range(3):
try:
alive = multiplex.proc_keepalive(sid, w, h)
break
except:
notifier().restart("webshell")
time.sleep(0.5)
try:
if alive:
if k:
multiplex.proc_write(
sid,
xmlrpclib.Binary(bytearray(k.encode('utf-8')))
)
time.sleep(0.002)
content_data = '<?xml version="1.0" encoding="UTF-8"?>' + \
multiplex.proc_dump(sid)
response = HttpResponse(content_data, content_type='text/xml')
return response
else:
response = HttpResponse('Disconnected')
response.status_code = 400
return response
except (KeyError, ValueError, IndexError, xmlrpclib.Fault), e:
response = HttpResponse('Invalid parameters: %s' % e)
response.status_code = 400
return response
def terminal_paste(request):
return render(request, "system/terminal_paste.html")