Menu

[r6348]: / trunk / gui / system / views.py  Maximize  Restore  History

Download this file

290 lines (239 with data), 9.8 kB

  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
#+
# Copyright 2010 iXsystems
# 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.
#
# $FreeBSD$
#####################################################################
from datetime import datetime
import tempfile
import os
import commands
from django.contrib.auth import login, get_backends
from django.contrib.auth.models import User
from django.http import HttpResponse
from django.shortcuts import render_to_response
from django.template.loader import render_to_string
from django.template import RequestContext
from django.utils import simplejson
from django.utils.translation import ugettext as _
from freenasUI.system import forms
from freenasUI.system import models
from freenasUI.middleware.notifier import notifier
from freenasUI.common.system import get_freenas_version
def _system_info():
hostname = commands.getoutput("hostname")
uname1 = os.uname()[0]
uname2 = os.uname()[2]
platform = os.popen("sysctl -n hw.model").read()
physmem = str(int(int(os.popen("sysctl -n hw.physmem").read()) / 1048576)) + "MB"
date = os.popen('env -u TZ date').read()
uptime = commands.getoutput("env -u TZ uptime | awk -F', load averages:' '{ print $1 }'")
loadavg = "%.2f, %.2f, %.2f" % os.getloadavg()
try:
d = open('/etc/version.freenas', 'r')
freenas_build = d.read()
d.close()
except:
freenas_build = "Unrecognized build (/etc/version.freenas missing?)"
return {
'hostname': hostname,
'uname1': uname1,
'uname2': uname2,
'platform': platform,
'physmem': physmem,
'date': date,
'uptime': uptime,
'loadavg': loadavg,
'freenas_build': freenas_build,
}
def system_info(request):
sysinfo = _system_info()
variables = RequestContext(request, {
})
variables.update(sysinfo)
return render_to_response('system/system_info.html', variables)
def config(request):
variables = RequestContext(request, {
})
return render_to_response('system/config.html', variables)
def config_restore(request):
variables = RequestContext(request)
if request.method == "POST":
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_to_response('system/config_ok2.html', variables)
return render_to_response('system/config_restore.html', variables)
def config_upload(request):
if request.method == "POST":
form = forms.ConfigUploadForm(request.POST, request.FILES)
variables = RequestContext(request, {
'form': form,
})
if form.is_valid():
import sqlite3
sqlite = request.FILES['config'].read()
f = tempfile.NamedTemporaryFile()
f.write(sqlite)
f.flush()
try:
conn = sqlite3.connect(f.name)
cur = conn.cursor()
cur.execute("""SELECT name FROM sqlite_master
WHERE type='table'
ORDER BY name;""")
except sqlite3.DatabaseError:
f.close()
form._errors['__all__'] = form.error_class([_("The uploaded file is not valid."),])
else:
db = open('/data/freenas-v1.db', 'w')
db.write(sqlite)
db.close()
f.close()
user = User.objects.all()[0]
backend = get_backends()[0]
user.backend = "%s.%s" % (backend.__module__, backend.__class__.__name__)
login(request, user)
return render_to_response('system/config_ok.html', variables)
if request.GET.has_key("iframe"):
return HttpResponse("<html><body><textarea>"+render_to_string('system/config_upload.html', variables)+"</textarea></boby></html>")
else:
return render_to_response('system/config_upload.html', variables)
else:
os.system("rm -rf /var/tmp/firmware")
os.system("/bin/ln -s /var/tmp/ /var/tmp/firmware")
form = forms.ConfigUploadForm()
variables = RequestContext(request, {
'form': form,
})
return render_to_response('system/config_upload.html', variables)
def config_save(request):
from django.core.servers.basehttp import FileWrapper
filename = '/data/freenas-v1.db'
wrapper = FileWrapper(file(filename))
response = HttpResponse(wrapper, content_type='application/octet-stream')
response['Content-Length'] = os.path.getsize(filename)
response['Content-Disposition'] = 'attachment; filename=freenas-%s.db' % datetime.now().strftime("%Y-%m-%d")
return response
def reporting(request):
graphs = {}
try:
graphs['hourly'] = None or [file for file in os.listdir( os.path.join('/var/db/graphs/', 'hourly/') )],
except OSError:
pass
try:
graphs['daily'] = None or [file for file in os.listdir( os.path.join('/var/db/graphs/', 'daily/') )],
except OSError:
pass
try:
graphs['weekly'] = None or [file for file in os.listdir( os.path.join('/var/db/graphs/', 'weekly/') )],
except OSError:
pass
try:
graphs['monthly'] = None or [file for file in os.listdir( os.path.join('/var/db/graphs/', 'monthly/') )],
except OSError:
pass
try:
graphs['yearly'] = None or [file for file in os.listdir( os.path.join('/var/db/graphs/', 'yearly/') )],
except OSError:
pass
variables = RequestContext(request, {
'graphs': graphs,
})
return render_to_response('system/reporting.html', variables)
def settings(request):
settings = models.Settings.objects.order_by("-id")[0].id
email = models.Email.objects.order_by("-id")[0].id
ssl = models.SSL.objects.order_by("-id")[0].id
advanced = models.Advanced.objects.order_by("-id")[0].id
variables = RequestContext(request, {
'settings': settings,
'email': email,
'ssl': ssl,
'advanced': advanced,
})
return render_to_response('system/settings.html', variables)
def advanced(request):
extra_context = {}
advanced = forms.AdvancedForm(data = models.Advanced.objects.order_by("-id").values()[0], auto_id=False)
if request.method == 'POST':
advanced = forms.AdvancedForm(request.POST, auto_id=False)
if advanced.is_valid():
advanced.save()
extra_context['saved'] = True
extra_context.update({
'advanced': advanced,
})
variables = RequestContext(request, extra_context)
return render_to_response('system/advanced.html', variables)
def varlogmessages(request, lines):
if lines == None:
lines = 3
msg = os.popen('tail -n %s /var/log/messages' % int(lines)).read().strip()
variables = RequestContext(request, {
'msg': msg,
})
return render_to_response('system/status/msg.xml', variables, mimetype='text/xml')
def top(request):
top = os.popen('top').read()
variables = RequestContext(request, {
'focused_tab' : 'system',
'top': top,
})
return render_to_response('system/status/top.xml', variables, mimetype='text/xml')
def reboot(request):
""" reboots the system """
notifier().restart("system")
variables = RequestContext(request, {
'freenas_version': get_freenas_version(),
})
return render_to_response('system/reboot.html', variables)
def shutdown(request):
""" shuts down the system and powers off the system """
notifier().stop("system")
variables = RequestContext(request, {
'freenas_version': get_freenas_version(),
})
return render_to_response('system/shutdown.html', variables)
def testmail(request):
error = False
errmsg = ''
if request.is_ajax():
from common.system import send_mail
error, errmsg = send_mail(subject="Test message from FreeNAS",
text="This is a message test from FreeNAS")
return HttpResponse(simplejson.dumps({
'error': error,
'errmsg': 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,
}))
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.