<s> from collections import OrderedDict , namedtuple <EOL> import cPickle as pickle <EOL> import datetime <EOL> import json <EOL> import logging <EOL> import os <EOL> import re <EOL> import shutil <EOL> import socket <EOL> import subprocess <EOL> import sysctl <EOL> import time <EOL> import urllib <EOL> import xmlrpclib <EOL> from django . contrib . auth import login , get_backends <EOL> from django . core . servers . basehttp import FileWrapper <EOL> from django . core . urlresolvers import reverse <EOL> from django . db import transaction <EOL> from django . http import ( <EOL> HttpResponse , <EOL> HttpResponseRedirect , <EOL> StreamingHttpResponse , <EOL> ) <EOL> from django . shortcuts import render , redirect <EOL> from django . utils . translation import ugettext as _ <EOL> from django . views . decorators . cache import never_cache <EOL> from freenasOS import Configuration <EOL> from freenasOS . Exceptions import UpdateManifestNotFound <EOL> from freenasOS . Update import ( <EOL> ActivateClone , <EOL> CheckForUpdates , <EOL> DeleteClone , <EOL> ) <EOL> from freenasUI . account . models import bsdUsers <EOL> from freenasUI . common . system import ( <EOL> get_sw_name , <EOL> get_sw_version , <EOL> send_mail <EOL> ) <EOL> from freenasUI . common . pipesubr import pipeopen <EOL> from freenasUI . common . ssl import ( <EOL> export_certificate , <EOL> export_certificate_chain , <EOL> export_privatekey , <EOL> ) <EOL> from freenasUI . freeadmin . apppool import appPool <EOL> from freenasUI . freeadmin . views import JsonResp <EOL> from freenasUI . middleware . exceptions import MiddlewareError <EOL> from freenasUI . middleware . notifier import notifier <EOL> from freenasUI . middleware . zfs import zpool_list <EOL> from freenasUI . network . models import GlobalConfiguration <EOL> from freenasUI . storage . models import Volume <EOL> from freenasUI . system import forms , models <EOL> from freenasUI . system . utils import ( <EOL> CheckUpdateHandler , <EOL> UpdateHandler , <EOL> VerifyHandler , <EOL> debug_get_settings , <EOL> debug_generate , <EOL> get_changelog , <EOL> parse_changelog , <EOL> run_updated , <EOL> ) <EOL> GRAPHS_DIR = '<STR_LIT>' <EOL> VERSION_FILE = '<STR_LIT>' <EOL> PGFILE = '<STR_LIT>' <EOL> INSTALLFILE = '<STR_LIT>' <EOL> BOOTENV_DELETE_PROGRESS = '<STR_LIT>' <EOL> RE_DD = re . compile ( r"<STR_LIT>" , re . M | re . S ) <EOL> PERFTEST_SIZE = <NUM_LIT> * <NUM_LIT> * <NUM_LIT> * <NUM_LIT> <EOL> log = logging . getLogger ( '<STR_LIT>' ) <EOL> def _system_info ( request = None ) : <EOL> __ , hostname , __ = os . uname ( ) [ <NUM_LIT:0> : <NUM_LIT:3> ] <EOL> platform = sysctl . filter ( '<STR_LIT>' ) [ <NUM_LIT:0> ] . value <EOL> physmem = '<STR_LIT>' % ( <EOL> sysctl . filter ( '<STR_LIT>' ) [ <NUM_LIT:0> ] . value / <NUM_LIT> , <EOL> ) <EOL> date = time . strftime ( '<STR_LIT>' ) + '<STR_LIT:\n>' <EOL> uptime = subprocess . check_output ( <EOL> "<STR_LIT>" , <EOL> shell = True <EOL> ) <EOL> loadavg = "<STR_LIT>" % os . getloadavg ( ) <EOL> try : <EOL> freenas_build = get_sw_version ( ) <EOL> except : <EOL> freenas_build = "<STR_LIT>" <EOL> return { <EOL> '<STR_LIT>' : hostname , <EOL> '<STR_LIT>' : platform , <EOL> '<STR_LIT>' : physmem , <EOL> '<STR_LIT:date>' : date , <EOL> '<STR_LIT>' : uptime , <EOL> '<STR_LIT>' : loadavg , <EOL> '<STR_LIT>' : freenas_build , <EOL> } <EOL> def system_info ( request ) : <EOL> sysinfo = _system_info ( request ) <EOL> sysinfo [ '<STR_LIT>' ] = appPool . get_system_info ( request ) <EOL> return render ( request , '<STR_LIT>' , sysinfo ) <EOL> def bootenv_datagrid ( request ) : <EOL> bootzvolstats = notifier ( ) . zpool_status ( '<STR_LIT>' ) <EOL> bootme = notifier ( ) . zpool_parse ( '<STR_LIT>' ) <EOL> zlist = zpool_list ( name = '<STR_LIT>' ) <EOL> try : <EOL> advanced = models . Advanced . objects . order_by ( '<STR_LIT>' ) [ <NUM_LIT:0> ] <EOL> except : <EOL> advanced = models . Advanced . objects . create ( ) <EOL> return render ( request , '<STR_LIT>' , { <EOL> '<STR_LIT>' : reverse ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : reverse ( '<STR_LIT>' , kwargs = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } ) , <EOL> '<STR_LIT>' : reverse ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : bootme , <EOL> '<STR_LIT>' : bootzvolstats , <EOL> '<STR_LIT>' : advanced , <EOL> '<STR_LIT>' : zlist , <EOL> } ) <EOL> def bootenv_datagrid_actions ( request ) : <EOL> onclick = '''<STR_LIT>''' <EOL> onselectafter = '''<STR_LIT>''' <EOL> actions = { <EOL> _ ( '<STR_LIT>' ) : { <EOL> '<STR_LIT>' : onclick % ( _ ( '<STR_LIT>' ) , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : _ ( '<STR_LIT>' ) , <EOL> } , <EOL> _ ( '<STR_LIT>' ) : { <EOL> '<STR_LIT>' : onclick % ( _ ( '<STR_LIT>' ) , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : onselectafter % ( <EOL> '<STR_LIT>' <EOL> ) , <EOL> '<STR_LIT>' : _ ( '<STR_LIT>' ) , <EOL> } , <EOL> _ ( '<STR_LIT>' ) : { <EOL> '<STR_LIT>' : """<STR_LIT>""" , <EOL> '<STR_LIT>' : """<STR_LIT>""" , <EOL> '<STR_LIT>' : _ ( '<STR_LIT>' ) , <EOL> } , <EOL> _ ( '<STR_LIT>' ) : { <EOL> '<STR_LIT>' : onclick % ( _ ( '<STR_LIT>' ) , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : onselectafter % ( <EOL> '<STR_LIT>' <EOL> ) , <EOL> '<STR_LIT>' : _ ( '<STR_LIT>' ) , <EOL> } , <EOL> _ ( '<STR_LIT>' ) : { <EOL> '<STR_LIT>' : onclick % ( _ ( '<STR_LIT>' ) , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : _ ( '<STR_LIT>' ) , <EOL> } , <EOL> } <EOL> return HttpResponse ( <EOL> json . dumps ( actions ) , <EOL> content_type = '<STR_LIT:application/json>' , <EOL> ) <EOL> def bootenv_datagrid_structure ( request ) : <EOL> structure = OrderedDict ( ( <EOL> ( '<STR_LIT:name>' , { '<STR_LIT:label>' : _ ( '<STR_LIT:Name>' ) } ) , <EOL> ( '<STR_LIT>' , { '<STR_LIT:label>' : _ ( '<STR_LIT>' ) } ) , <EOL> ( '<STR_LIT>' , { '<STR_LIT:label>' : _ ( '<STR_LIT>' ) } ) , <EOL> ) ) <EOL> return HttpResponse ( <EOL> json . dumps ( structure ) , <EOL> content_type = '<STR_LIT:application/json>' , <EOL> ) <EOL> def bootenv_activate ( request , name ) : <EOL> if request . method == '<STR_LIT:POST>' : <EOL> active = ActivateClone ( name ) <EOL> if active is not False : <EOL> return JsonResp ( <EOL> request , <EOL> message = _ ( '<STR_LIT>' ) , <EOL> ) <EOL> return JsonResp ( <EOL> request , <EOL> message = _ ( '<STR_LIT>' ) , <EOL> ) <EOL> return render ( request , '<STR_LIT>' , { <EOL> '<STR_LIT:name>' : name , <EOL> } ) <EOL> def bootenv_add ( request , source = None ) : <EOL> if request . method == '<STR_LIT:POST>' : <EOL> form = forms . BootEnvAddForm ( request . POST , source = source ) <EOL> if form . is_valid ( ) : <EOL> form . save ( ) <EOL> return JsonResp ( <EOL> request , <EOL> message = _ ( '<STR_LIT>' ) , <EOL> ) <EOL> return JsonResp ( request , form = form ) <EOL> else : <EOL> form = forms . BootEnvAddForm ( source = source ) <EOL> return render ( request , '<STR_LIT>' , { <EOL> '<STR_LIT>' : form , <EOL> '<STR_LIT:source>' : source , <EOL> } ) <EOL> def bootenv_scrub ( request ) : <EOL> if request . method == "<STR_LIT:POST>" : <EOL> try : <EOL> notifier ( ) . zfs_scrub ( '<STR_LIT>' ) <EOL> return JsonResp ( request , message = _ ( "<STR_LIT>" ) ) <EOL> except Exception , e : <EOL> return JsonResp ( request , error = True , message = repr ( e ) ) <EOL> return render ( request , '<STR_LIT>' ) <EOL> def bootenv_scrub_interval ( request ) : <EOL> assert request . method == '<STR_LIT:POST>' <EOL> interval = request . POST . get ( '<STR_LIT>' ) <EOL> if not interval . isdigit ( ) : <EOL> return JsonResp ( <EOL> request , <EOL> error = True , <EOL> message = _ ( '<STR_LIT>' ) , <EOL> ) <EOL> try : <EOL> advanced = models . Advanced . objects . order_by ( '<STR_LIT>' ) [ <NUM_LIT:0> ] <EOL> except : <EOL> advanced = models . Advanced . objects . create ( ) <EOL> advanced . adv_boot_scrub = int ( interval ) <EOL> advanced . save ( ) <EOL> notifier ( ) . restart ( "<STR_LIT>" ) <EOL> return JsonResp ( <EOL> request , <EOL> message = _ ( '<STR_LIT>' ) , <EOL> ) <EOL> def bootenv_delete ( request , name ) : <EOL> if request . method == '<STR_LIT:POST>' : <EOL> delete = DeleteClone ( name ) <EOL> if delete is not False : <EOL> return JsonResp ( <EOL> request , <EOL> message = _ ( '<STR_LIT>' ) , <EOL> ) <EOL> return JsonResp ( <EOL> request , <EOL> message = _ ( '<STR_LIT>' ) , <EOL> ) <EOL> return render ( request , '<STR_LIT>' , { <EOL> '<STR_LIT:name>' : name , <EOL> } ) <EOL> def bootenv_deletebulk ( request ) : <EOL> names = request . GET . get ( '<STR_LIT>' ) <EOL> if '<STR_LIT:/>' in names or '<STR_LIT:U+0020>' in names : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> names = names . split ( '<STR_LIT:U+002C>' ) <EOL> if request . method == '<STR_LIT:POST>' : <EOL> failed = False <EOL> for i , name in enumerate ( names ) : <EOL> with open ( BOOTENV_DELETE_PROGRESS , '<STR_LIT:w>' ) as f : <EOL> f . write ( json . dumps ( { <EOL> '<STR_LIT>' : name , <EOL> '<STR_LIT:index>' : i , <EOL> '<STR_LIT>' : len ( names ) , <EOL> } ) ) <EOL> delete = DeleteClone ( name ) <EOL> if delete is False : <EOL> failed = True <EOL> if os . path . exists ( BOOTENV_DELETE_PROGRESS ) : <EOL> os . unlink ( BOOTENV_DELETE_PROGRESS ) <EOL> if failed is False : <EOL> return JsonResp ( <EOL> request , <EOL> message = _ ( '<STR_LIT>' ) , <EOL> ) <EOL> return JsonResp ( <EOL> request , <EOL> message = _ ( '<STR_LIT>' ) , <EOL> ) <EOL> return render ( request , '<STR_LIT>' , { <EOL> '<STR_LIT>' : names , <EOL> '<STR_LIT>' : request . GET . get ( '<STR_LIT>' ) , <EOL> } ) <EOL> def bootenv_deletebulk_progress ( request ) : <EOL> if not os . path . exists ( BOOTENV_DELETE_PROGRESS ) : <EOL> return HttpResponse ( <EOL> json . dumps ( { '<STR_LIT>' : True } ) , <EOL> content_type = '<STR_LIT:application/json>' , <EOL> ) <EOL> with open ( BOOTENV_DELETE_PROGRESS , '<STR_LIT:r>' ) as f : <EOL> data = f . read ( ) <EOL> try : <EOL> data = json . loads ( data ) <EOL> return HttpResponse ( <EOL> json . dumps ( { <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : int ( ( data [ '<STR_LIT:index>' ] / float ( data [ '<STR_LIT>' ] ) ) * <NUM_LIT> ) , <EOL> '<STR_LIT>' : data [ '<STR_LIT>' ] , <EOL> } ) , <EOL> content_type = '<STR_LIT:application/json>' , <EOL> ) <EOL> except : <EOL> log . warn ( "<STR_LIT>" ) <EOL> return HttpResponse ( <EOL> json . dumps ( { '<STR_LIT>' : True } ) , <EOL> content_type = '<STR_LIT:application/json>' , <EOL> ) <EOL> def bootenv_rename ( request , name ) : <EOL> if request . method == '<STR_LIT:POST>' : <EOL> form = forms . BootEnvRenameForm ( request . POST , name = name ) <EOL> if form . is_valid ( ) : <EOL> form . save ( ) <EOL> return JsonResp ( <EOL> request , <EOL> message = _ ( '<STR_LIT>' ) , <EOL> ) <EOL> return JsonResp ( request , form = form ) <EOL> else : <EOL> form = forms . BootEnvRenameForm ( name = name ) <EOL> return render ( request , '<STR_LIT>' , { <EOL> '<STR_LIT>' : form , <EOL> '<STR_LIT:name>' : name , <EOL> } ) <EOL> def bootenv_pool_attach ( request ) : <EOL> label = request . GET . get ( '<STR_LIT:label>' ) <EOL> if request . method == '<STR_LIT:POST>' : <EOL> form = forms . BootEnvPoolAttachForm ( request . POST , label = label ) <EOL> if form . is_valid ( ) : <EOL> form . done ( ) <EOL> return JsonResp ( <EOL> request , <EOL> message = _ ( '<STR_LIT>' ) , <EOL> ) <EOL> return JsonResp ( request , form = form ) <EOL> else : <EOL> form = forms . BootEnvPoolAttachForm ( label = label ) <EOL> return render ( request , '<STR_LIT>' , { <EOL> '<STR_LIT>' : form , <EOL> '<STR_LIT:label>' : label , <EOL> } ) <EOL> def bootenv_pool_detach ( request , label ) : <EOL> if request . method == '<STR_LIT:POST>' : <EOL> notifier ( ) . zfs_detach_disk ( '<STR_LIT>' , label ) <EOL> return JsonResp ( <EOL> request , <EOL> message = _ ( "<STR_LIT>" ) ) <EOL> return render ( request , '<STR_LIT>' , { <EOL> '<STR_LIT:label>' : label , <EOL> } ) <EOL> def bootenv_pool_replace ( request , label ) : <EOL> if request . method == '<STR_LIT:POST>' : <EOL> form = forms . BootEnvPoolReplaceForm ( request . POST , label = label ) <EOL> if form . is_valid ( ) : <EOL> form . done ( ) <EOL> return JsonResp ( <EOL> request , <EOL> message = _ ( '<STR_LIT>' ) , <EOL> ) <EOL> return JsonResp ( request , form = form ) <EOL> else : <EOL> form = forms . BootEnvPoolReplaceForm ( label = label ) <EOL> return render ( request , '<STR_LIT>' , { <EOL> '<STR_LIT>' : form , <EOL> } ) <EOL> def config_restore ( request ) : <EOL> if request . method == "<STR_LIT:POST>" : <EOL> request . session [ '<STR_LIT>' ] = True <EOL> notifier ( ) . config_restore ( ) <EOL> user = bsdUsers . objects . filter ( bsdusr_uid = <NUM_LIT:0> ) [ <NUM_LIT:0> ] <EOL> backend = get_backends ( ) [ <NUM_LIT:0> ] <EOL> user . backend = "<STR_LIT>" % ( backend . __module__ , <EOL> backend . __class__ . __name__ ) <EOL> login ( request , user ) <EOL> return render ( request , '<STR_LIT>' ) <EOL> return render ( request , '<STR_LIT>' ) <EOL> def config_upload ( request ) : <EOL> if request . method == "<STR_LIT:POST>" : <EOL> form = forms . ConfigUploadForm ( request . POST , request . FILES ) <EOL> variables = { <EOL> '<STR_LIT>' : form , <EOL> } <EOL> if form . is_valid ( ) : <EOL> success , errmsg = notifier ( ) . config_upload ( request . FILES [ '<STR_LIT>' ] ) <EOL> if not success : <EOL> form . _errors [ '<STR_LIT>' ] = form . error_class ( [ errmsg ] ) <EOL> return JsonResp ( request , form = form ) <EOL> else : <EOL> request . session [ '<STR_LIT>' ] = True <EOL> return render ( request , '<STR_LIT>' , variables ) <EOL> return render ( request , '<STR_LIT>' , variables ) <EOL> else : <EOL> FIRMWARE_DIR = '<STR_LIT>' <EOL> if os . path . exists ( FIRMWARE_DIR ) : <EOL> if os . path . islink ( FIRMWARE_DIR ) : <EOL> os . unlink ( FIRMWARE_DIR ) <EOL> if os . path . isdir ( FIRMWARE_DIR ) : <EOL> shutil . rmtree ( FIRMWARE_DIR + '<STR_LIT:/>' ) <EOL> os . mkdir ( FIRMWARE_DIR ) <EOL> os . chmod ( FIRMWARE_DIR , <NUM_LIT:0> <NUM_LIT> ) <EOL> form = forms . ConfigUploadForm ( ) <EOL> return render ( request , '<STR_LIT>' , { <EOL> '<STR_LIT>' : form , <EOL> } ) <EOL> def config_save ( request ) : <EOL> hostname = GlobalConfiguration . objects . all ( ) . order_by ( '<STR_LIT>' ) [ <NUM_LIT:0> ] . gc_hostname <EOL> filename = '<STR_LIT>' <EOL> wrapper = FileWrapper ( file ( filename ) ) <EOL> freenas_build = "<STR_LIT>" <EOL> try : <EOL> with open ( VERSION_FILE ) as d : <EOL> freenas_build = d . read ( ) . strip ( ) <EOL> except : <EOL> pass <EOL> response = StreamingHttpResponse ( <EOL> wrapper , content_type = '<STR_LIT>' <EOL> ) <EOL> response [ '<STR_LIT>' ] = os . path . getsize ( filename ) <EOL> response [ '<STR_LIT>' ] = '<STR_LIT>' % ( <EOL> hostname . encode ( '<STR_LIT:utf-8>' ) , <EOL> freenas_build , <EOL> time . strftime ( '<STR_LIT>' ) ) <EOL> return response <EOL> def reporting ( request ) : <EOL> return render ( request , '<STR_LIT>' ) <EOL> def home ( request ) : <EOL> tabs = appPool . hook_app_tabs ( '<STR_LIT>' , request ) <EOL> tabs = sorted ( tabs , key = lambda y : y [ '<STR_LIT>' ] if '<STR_LIT>' in y else <NUM_LIT:0> ) <EOL> return render ( request , '<STR_LIT>' , { <EOL> '<STR_LIT>' : request . GET . get ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : tabs , <EOL> } ) <EOL> def varlogmessages ( request , lines ) : <EOL> if lines is None : <EOL> lines = <NUM_LIT:3> <EOL> msg = os . popen ( '<STR_LIT>' % int ( lines ) ) . read ( ) . strip ( ) <EOL> msg = msg . replace ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> return render ( request , '<STR_LIT>' , { <EOL> '<STR_LIT>' : msg , <EOL> } , content_type = '<STR_LIT>' ) <EOL> def top ( request ) : <EOL> top_pipe = os . popen ( '<STR_LIT>' ) <EOL> try : <EOL> top_output = top_pipe . read ( ) <EOL> finally : <EOL> top_pipe . close ( ) <EOL> return render ( request , '<STR_LIT>' , { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : top_output , <EOL> } , content_type = '<STR_LIT>' ) <EOL> def reboot_dialog ( request ) : <EOL> if request . method == "<STR_LIT:POST>" : <EOL> if notifier ( ) . zpool_scrubbing ( ) : <EOL> if '<STR_LIT>' not in request . session : <EOL> request . session [ '<STR_LIT>' ] = True <EOL> return render ( request , '<STR_LIT>' ) <EOL> request . session [ '<STR_LIT>' ] = True <EOL> return JsonResp ( <EOL> request , <EOL> message = _ ( "<STR_LIT>" ) , <EOL> events = [ '<STR_LIT>' % reverse ( '<STR_LIT>' ) ] <EOL> ) <EOL> return render ( request , '<STR_LIT>' ) <EOL> def reboot ( request ) : <EOL> """<STR_LIT>""" <EOL> if not request . session . get ( "<STR_LIT>" ) : <EOL> return HttpResponseRedirect ( '<STR_LIT:/>' ) <EOL> request . session . pop ( "<STR_LIT>" ) <EOL> return render ( request , '<STR_LIT>' , { <EOL> '<STR_LIT>' : get_sw_name ( ) , <EOL> '<STR_LIT>' : get_sw_version ( ) , <EOL> } ) <EOL> def reboot_run ( request ) : <EOL> notifier ( ) . restart ( "<STR_LIT>" ) <EOL> return HttpResponse ( '<STR_LIT:OK>' ) <EOL> def shutdown_dialog ( request ) : <EOL> if request . method == "<STR_LIT:POST>" : <EOL> if notifier ( ) . zpool_scrubbing ( ) : <EOL> if '<STR_LIT>' not in request . session : <EOL> request . session [ '<STR_LIT>' ] = True <EOL> return render ( request , '<STR_LIT>' ) <EOL> request . session [ '<STR_LIT>' ] = True <EOL> return JsonResp ( <EOL> request , <EOL> message = _ ( "<STR_LIT>" ) , <EOL> events = [ '<STR_LIT>' % reverse ( '<STR_LIT>' ) ] ) <EOL> return render ( request , '<STR_LIT>' ) <EOL> def shutdown ( request ) : <EOL> """<STR_LIT>""" <EOL> if not request . session . get ( "<STR_LIT>" ) : <EOL> return HttpResponseRedirect ( '<STR_LIT:/>' ) <EOL> request . session . pop ( "<STR_LIT>" ) <EOL> return render ( request , '<STR_LIT>' , { <EOL> '<STR_LIT>' : get_sw_name ( ) , <EOL> '<STR_LIT>' : get_sw_version ( ) , <EOL> } ) <EOL> def shutdown_run ( request ) : <EOL> notifier ( ) . stop ( "<STR_LIT>" ) <EOL> return HttpResponse ( '<STR_LIT:OK>' ) <EOL> def testmail ( request ) : <EOL> try : <EOL> kwargs = dict ( instance = models . Email . objects . order_by ( '<STR_LIT>' ) [ <NUM_LIT:0> ] ) <EOL> except IndexError : <EOL> kwargs = { } <EOL> fromwizard = False <EOL> data = request . POST . copy ( ) <EOL> for key , value in data . items ( ) : <EOL> if key . startswith ( '<STR_LIT>' ) : <EOL> fromwizard = True <EOL> data [ key . replace ( '<STR_LIT>' , '<STR_LIT>' ) ] = value <EOL> form = forms . EmailForm ( data , ** kwargs ) <EOL> if not form . is_valid ( ) : <EOL> return JsonResp ( request , form = form ) <EOL> if fromwizard : <EOL> allfield = '<STR_LIT>' <EOL> else : <EOL> allfield = '<STR_LIT>' <EOL> if fromwizard : <EOL> email = request . POST . get ( '<STR_LIT>' ) <EOL> errmsg = _ ( '<STR_LIT>' ) <EOL> else : <EOL> email = bsdUsers . objects . get ( bsdusr_username = '<STR_LIT:root>' ) . bsdusr_email <EOL> errmsg = _ ( '<STR_LIT>' ) <EOL> if not email : <EOL> form . errors [ allfield ] = form . error_class ( [ errmsg ] ) <EOL> return JsonResp ( <EOL> request , <EOL> form = form , <EOL> ) <EOL> sid = transaction . savepoint ( ) <EOL> form . save ( ) <EOL> error = False <EOL> if request . is_ajax ( ) : <EOL> sw_name = get_sw_name ( ) <EOL> error , errmsg = send_mail ( <EOL> subject = _ ( '<STR_LIT>' ) % sw_name , <EOL> text = _ ( '<STR_LIT>' ) % sw_name , <EOL> to = [ email ] , <EOL> timeout = <NUM_LIT:10> ) <EOL> if error : <EOL> errmsg = _ ( "<STR_LIT>" ) % errmsg <EOL> else : <EOL> errmsg = _ ( '<STR_LIT>' ) <EOL> transaction . savepoint_rollback ( sid ) <EOL> form . errors [ allfield ] = form . error_class ( [ errmsg ] ) <EOL> return JsonResp ( <EOL> request , <EOL> form = form , <EOL> ) <EOL> class DojoFileStore ( object ) : <EOL> def __init__ ( self , path , dirsonly = False , root = "<STR_LIT:/>" , filterVolumes = True ) : <EOL> self . root = os . path . abspath ( str ( root ) ) <EOL> self . filterVolumes = filterVolumes <EOL> if self . filterVolumes : <EOL> self . mp = [ <EOL> os . path . abspath ( ( u'<STR_LIT>' % v . vol_name ) . encode ( '<STR_LIT:utf8>' ) ) <EOL> for v in Volume . objects . filter ( vol_fstype = '<STR_LIT>' ) <EOL> ] <EOL> self . path = os . path . join ( self . root , path . replace ( "<STR_LIT:..>" , "<STR_LIT>" ) ) <EOL> self . path = os . path . abspath ( self . path ) <EOL> if self . path . startswith ( '<STR_LIT>' ) : <EOL> self . path = self . path [ <NUM_LIT:1> : ] <EOL> self . path = self . path . encode ( '<STR_LIT:utf8>' ) <EOL> self . dirsonly = dirsonly <EOL> if self . dirsonly : <EOL> self . _lookupurl = '<STR_LIT>' <EOL> else : <EOL> self . _lookupurl = '<STR_LIT>' <EOL> def items ( self ) : <EOL> if self . path == self . root : <EOL> return self . children ( self . path ) <EOL> node = self . _item ( self . path , self . path ) <EOL> if node [ '<STR_LIT>' ] : <EOL> node [ '<STR_LIT>' ] = self . children ( self . path ) <EOL> return node <EOL> def children ( self , entry ) : <EOL> _children = [ ] <EOL> for _entry in sorted ( os . listdir ( entry ) ) : <EOL> if _entry . startswith ( "<STR_LIT:.>" ) or _entry == '<STR_LIT>' : <EOL> continue <EOL> full_path = os . path . join ( self . path , _entry ) <EOL> if self . filterVolumes and len ( <EOL> [ <EOL> f for f in self . mp if ( <EOL> full_path . startswith ( f + '<STR_LIT:/>' ) or full_path == f or <EOL> full_path . startswith ( '<STR_LIT>' ) <EOL> ) <EOL> ] <EOL> ) > <NUM_LIT:0> : <EOL> _children . append ( self . _item ( self . path , _entry ) ) <EOL> if self . dirsonly : <EOL> _children = [ child for child in _children if child [ '<STR_LIT>' ] ] <EOL> return _children <EOL> def _item ( self , path , entry ) : <EOL> full_path = os . path . join ( path , entry ) <EOL> if full_path . startswith ( self . root ) : <EOL> path = full_path . replace ( self . root , "<STR_LIT:/>" , <NUM_LIT:1> ) <EOL> else : <EOL> path = full_path <EOL> if path . startswith ( "<STR_LIT>" ) : <EOL> path = path [ <NUM_LIT:1> : ] <EOL> isdir = os . path . isdir ( full_path ) <EOL> item = { <EOL> '<STR_LIT:name>' : os . path . basename ( full_path ) , <EOL> '<STR_LIT>' : isdir , <EOL> '<STR_LIT:path>' : path , <EOL> } <EOL> if isdir : <EOL> item [ '<STR_LIT>' ] = True <EOL> item [ '<STR_LIT>' ] = os . path . abspath ( <EOL> reverse ( self . _lookupurl , kwargs = { <EOL> '<STR_LIT:path>' : path , <EOL> } ) + '<STR_LIT>' % urllib . quote_plus ( self . root ) , <EOL> ) <EOL> item [ '<STR_LIT:id>' ] = item [ '<STR_LIT>' ] <EOL> return item <EOL> def directory_browser ( request , path = '<STR_LIT:/>' ) : <EOL> """<STR_LIT>""" <EOL> directories = DojoFileStore ( <EOL> path , <EOL> dirsonly = True , <EOL> root = request . GET . get ( "<STR_LIT:root>" , "<STR_LIT:/>" ) , <EOL> ) . items ( ) <EOL> context = directories <EOL> content = json . dumps ( context ) <EOL> return HttpResponse ( content , content_type = '<STR_LIT:application/json>' ) <EOL> def file_browser ( request , path = '<STR_LIT:/>' ) : <EOL> """<STR_LIT>""" <EOL> directories = DojoFileStore ( <EOL> path , <EOL> dirsonly = False , <EOL> root = request . GET . get ( "<STR_LIT:root>" , "<STR_LIT:/>" ) , <EOL> ) . items ( ) <EOL> context = directories <EOL> content = json . dumps ( context ) <EOL> return HttpResponse ( content , content_type = '<STR_LIT:application/json>' ) <EOL> def manualupdate_progress ( request ) : <EOL> data = { } <EOL> if os . path . exists ( PGFILE ) : <EOL> with open ( PGFILE , '<STR_LIT:r>' ) as f : <EOL> last = f . readlines ( ) <EOL> if last : <EOL> step , percent = last [ - <NUM_LIT:1> ] . split ( "<STR_LIT:|>" ) <EOL> data [ '<STR_LIT>' ] = int ( step ) <EOL> percent = percent . strip ( ) <EOL> if percent : <EOL> data [ '<STR_LIT>' ] = int ( percent ) <EOL> else : <EOL> data [ '<STR_LIT>' ] = True <EOL> elif os . path . exists ( INSTALLFILE ) : <EOL> data = { <EOL> '<STR_LIT>' : <NUM_LIT:3> , <EOL> '<STR_LIT>' : True , <EOL> } <EOL> content = json . dumps ( data ) <EOL> return HttpResponse ( content , content_type = '<STR_LIT:application/json>' ) <EOL> def initialwizard_progress ( request ) : <EOL> data = { } <EOL> if os . path . exists ( forms . WIZARD_PROGRESSFILE ) : <EOL> with open ( forms . WIZARD_PROGRESSFILE , '<STR_LIT:rb>' ) as f : <EOL> data = f . read ( ) <EOL> try : <EOL> data = pickle . loads ( data ) <EOL> except : <EOL> data = { } <EOL> content = json . dumps ( data ) <EOL> return HttpResponse ( content , content_type = '<STR_LIT:application/json>' ) <EOL> def restart_httpd ( request ) : <EOL> """<STR_LIT>""" <EOL> notifier ( ) . restart ( "<STR_LIT:http>" ) <EOL> return HttpResponse ( '<STR_LIT:OK>' ) <EOL> def restart_httpd_all ( request ) : <EOL> """<STR_LIT>""" <EOL> notifier ( ) . restart ( "<STR_LIT:http>" ) <EOL> notifier ( ) . restart ( "<STR_LIT>" ) <EOL> return HttpResponse ( '<STR_LIT:OK>' ) <EOL> def reload_httpd ( request ) : <EOL> """<STR_LIT>""" <EOL> notifier ( ) . reload ( "<STR_LIT:http>" ) <EOL> return HttpResponse ( '<STR_LIT:OK>' ) <EOL> def debug ( request ) : <EOL> _n = notifier ( ) <EOL> if request . method == '<STR_LIT:GET>' : <EOL> if not _n . is_freenas ( ) and _n . failover_licensed ( ) : <EOL> try : <EOL> s = _n . failover_rpc ( ) <EOL> s . ping ( ) <EOL> except socket . error : <EOL> return render ( request , '<STR_LIT>' ) <EOL> return render ( request , '<STR_LIT>' ) <EOL> debug_generate ( ) <EOL> return render ( request , '<STR_LIT>' ) <EOL> def debug_download ( request ) : <EOL> mntpt , direc , dump = debug_get_settings ( ) <EOL> gc = GlobalConfiguration . objects . all ( ) . order_by ( '<STR_LIT>' ) [ <NUM_LIT:0> ] <EOL> _n = notifier ( ) <EOL> if not _n . is_freenas ( ) and _n . failover_licensed ( ) : <EOL> debug_file = '<STR_LIT>' % direc <EOL> extension = '<STR_LIT>' <EOL> hostname = '<STR_LIT>' <EOL> else : <EOL> debug_file = dump <EOL> extension = '<STR_LIT>' <EOL> hostname = '<STR_LIT>' % gc . gc_hostname . encode ( '<STR_LIT:utf-8>' ) <EOL> wrapper = FileWrapper ( file ( debug_file ) ) <EOL> response = StreamingHttpResponse ( <EOL> wrapper , <EOL> content_type = '<STR_LIT>' , <EOL> ) <EOL> response [ '<STR_LIT>' ] = os . path . getsize ( debug_file ) <EOL> response [ '<STR_LIT>' ] = '<STR_LIT>' % ( <EOL> hostname , <EOL> time . strftime ( '<STR_LIT>' ) , <EOL> extension ) <EOL> return response <EOL> def backup ( request ) : <EOL> backups = models . Backup . objects . all ( ) . order_by ( '<STR_LIT>' ) <EOL> if len ( backups ) < <NUM_LIT:1> or backups [ <NUM_LIT:0> ] . bak_acknowledged : <EOL> if request . method == '<STR_LIT:POST>' : <EOL> backup_form = forms . BackupForm ( request . POST ) <EOL> if backup_form . is_valid ( ) : <EOL> backup = models . Backup ( ) <EOL> backup . bak_started_at = datetime . datetime . now ( ) <EOL> backup . save ( ) <EOL> transaction . commit ( ) <EOL> args = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : backup_form . cleaned_data [ '<STR_LIT>' ] , <EOL> '<STR_LIT:username>' : backup_form . cleaned_data [ '<STR_LIT>' ] , <EOL> '<STR_LIT:password>' : backup_form . cleaned_data [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : backup_form . cleaned_data [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : backup_form . cleaned_data [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : backup_form . cleaned_data [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : backup_form . cleaned_data [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : backup . id <EOL> } <EOL> notifier ( ) . start ( '<STR_LIT>' ) <EOL> response = notifier ( ) . call_backupd ( args ) <EOL> if response [ '<STR_LIT:status>' ] != '<STR_LIT:OK>' : <EOL> return JsonResp ( request , error = True , message = '<STR_LIT>' ) <EOL> return render ( request , '<STR_LIT>' ) <EOL> else : <EOL> return JsonResp ( request , form = backup_form ) <EOL> backup_form = forms . BackupForm ( ) <EOL> return render ( request , '<STR_LIT>' , { <EOL> '<STR_LIT>' : backup_form <EOL> } ) <EOL> elif backups [ <NUM_LIT:0> ] . bak_finished or backups [ <NUM_LIT:0> ] . bak_failed : <EOL> if request . method == '<STR_LIT:POST>' : <EOL> backups [ <NUM_LIT:0> ] . bak_acknowledged = True <EOL> backups [ <NUM_LIT:0> ] . save ( ) <EOL> return JsonResp ( request , message = '<STR_LIT>' ) <EOL> return render ( request , '<STR_LIT>' , { '<STR_LIT>' : backups [ <NUM_LIT:0> ] } ) <EOL> else : <EOL> return render ( request , '<STR_LIT>' ) <EOL> def backup_progress ( request ) : <EOL> backup = models . Backup . objects . all ( ) . order_by ( '<STR_LIT>' ) . first ( ) <EOL> if backup . bak_finished : <EOL> data = { '<STR_LIT:status>' : '<STR_LIT>' , '<STR_LIT:message>' : backup . bak_status } <EOL> return HttpResponse ( json . dumps ( data ) , content_type = '<STR_LIT:application/json>' ) <EOL> if backup . bak_failed : <EOL> data = { '<STR_LIT:status>' : '<STR_LIT:error>' , '<STR_LIT:message>' : backup . bak_status } <EOL> return HttpResponse ( json . dumps ( data ) , content_type = '<STR_LIT:application/json>' ) <EOL> response = notifier ( ) . call_backupd ( { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> if response [ '<STR_LIT:status>' ] != '<STR_LIT:OK>' : <EOL> data = { '<STR_LIT:status>' : '<STR_LIT:error>' , '<STR_LIT:message>' : '<STR_LIT>' } <EOL> return HttpResponse ( json . dumps ( data ) , content_type = '<STR_LIT:application/json>' ) <EOL> data = { <EOL> '<STR_LIT:status>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : response [ '<STR_LIT>' ] , <EOL> '<STR_LIT:message>' : response [ '<STR_LIT:message>' ] <EOL> } <EOL> return HttpResponse ( json . dumps ( data ) , content_type = '<STR_LIT:application/json>' ) <EOL> def backup_abort ( request ) : <EOL> backups = models . Backup . objects . all ( ) . order_by ( '<STR_LIT>' ) <EOL> if len ( backups ) < <NUM_LIT:1> or backups [ <NUM_LIT:0> ] . bak_finished : <EOL> pass <EOL> if request . method == '<STR_LIT:POST>' : <EOL> response = notifier ( ) . call_backupd ( { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> if response [ '<STR_LIT:status>' ] != '<STR_LIT:OK>' : <EOL> return redirect ( '<STR_LIT>' ) <EOL> return redirect ( '<STR_LIT>' ) <EOL> class UnixTransport ( xmlrpclib . Transport ) : <EOL> def make_connection ( self , addr ) : <EOL> self . sock = socket . socket ( socket . AF_UNIX , socket . SOCK_STREAM ) <EOL> self . sock . connect ( addr ) <EOL> self . sock . settimeout ( <NUM_LIT:5> ) <EOL> return self . sock <EOL> def single_request ( self , host , handler , request_body , verbose = <NUM_LIT:0> ) : <EOL> self . make_connection ( host ) <EOL> try : <EOL> self . sock . send ( request_body + "<STR_LIT:\n>" ) <EOL> p , u = self . getparser ( ) <EOL> while <NUM_LIT:1> : <EOL> data = self . sock . recv ( <NUM_LIT> ) <EOL> if not data : <EOL> break <EOL> p . feed ( data ) <EOL> self . sock . close ( ) <EOL> p . close ( ) <EOL> return u . close ( ) <EOL> except xmlrpclib . Fault : <EOL> raise <EOL> except Exception : <EOL> self . close ( ) <EOL> raise <EOL> class MyServer ( xmlrpclib . ServerProxy ) : <EOL> def __init__ ( self , addr ) : <EOL> self . __handler = "<STR_LIT:/>" <EOL> self . __host = addr <EOL> self . __transport = UnixTransport ( ) <EOL> self . __encoding = None <EOL> self . __verbose = <NUM_LIT:0> <EOL> self . __allow_none = <NUM_LIT:0> <EOL> def __request ( self , methodname , params ) : <EOL> request = xmlrpclib . dumps ( <EOL> params , <EOL> methodname , <EOL> encoding = self . __encoding , <EOL> allow_none = self . __allow_none , <EOL> ) <EOL> response = self . __transport . request ( <EOL> self . __host , <EOL> self . __handler , <EOL> request , <EOL> verbose = self . __verbose <EOL> ) <EOL> if len ( response ) == <NUM_LIT:1> : <EOL> response = response [ <NUM_LIT:0> ] <EOL> return response <EOL> def __getattr__ ( self , name ) : <EOL> return xmlrpclib . _Method ( self . __request , name ) <EOL> @ never_cache <EOL> def terminal ( request ) : <EOL> sid = int ( request . POST . get ( "<STR_LIT:s>" , <NUM_LIT:0> ) ) <EOL> jid = request . POST . get ( "<STR_LIT>" , <NUM_LIT:0> ) <EOL> shell = request . POST . get ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> k = request . POST . get ( "<STR_LIT:k>" ) <EOL> w = int ( request . POST . get ( "<STR_LIT:w>" , <NUM_LIT> ) ) <EOL> h = int ( request . POST . get ( "<STR_LIT:h>" , <NUM_LIT> ) ) <EOL> multiplex = MyServer ( "<STR_LIT>" ) <EOL> alive = False <EOL> for i in range ( <NUM_LIT:3> ) : <EOL> try : <EOL> alive = multiplex . proc_keepalive ( sid , jid , shell , w , h ) <EOL> break <EOL> except Exception , e : <EOL> notifier ( ) . restart ( "<STR_LIT>" ) <EOL> time . sleep ( <NUM_LIT:0.5> ) <EOL> try : <EOL> if alive : <EOL> if k : <EOL> multiplex . proc_write ( <EOL> sid , <EOL> xmlrpclib . Binary ( bytearray ( k . encode ( '<STR_LIT:utf-8>' ) ) ) <EOL> ) <EOL> time . sleep ( <NUM_LIT> ) <EOL> content_data = '<STR_LIT>' + multiplex . proc_dump ( sid ) <EOL> response = HttpResponse ( content_data , content_type = '<STR_LIT>' ) <EOL> return response <EOL> else : <EOL> response = HttpResponse ( '<STR_LIT>' ) <EOL> response . status_code = <NUM_LIT> <EOL> return response <EOL> except ( KeyError , ValueError , IndexError , xmlrpclib . Fault ) , e : <EOL> response = HttpResponse ( '<STR_LIT>' % e ) <EOL> response . status_code = <NUM_LIT> <EOL> return response <EOL> def terminal_paste ( request ) : <EOL> return render ( request , "<STR_LIT>" ) <EOL> def update_index ( request ) : <EOL> try : <EOL> update = models . Update . objects . order_by ( '<STR_LIT>' ) [ <NUM_LIT:0> ] <EOL> except IndexError : <EOL> update = models . Update . objects . create ( ) <EOL> return render ( request , '<STR_LIT>' , { <EOL> '<STR_LIT>' : update , <EOL> '<STR_LIT>' : Configuration . Configuration ( ) . UpdateServerURL ( ) , <EOL> } ) <EOL> def update_save ( request ) : <EOL> assert request . method == '<STR_LIT:POST>' <EOL> try : <EOL> update = models . Update . objects . order_by ( '<STR_LIT>' ) [ <NUM_LIT:0> ] <EOL> except IndexError : <EOL> update = models . Update . objects . create ( ) <EOL> if request . POST . get ( '<STR_LIT>' ) : <EOL> if request . POST . get ( '<STR_LIT>' ) == '<STR_LIT:true>' : <EOL> update . upd_autocheck = True <EOL> else : <EOL> update . upd_autocheck = False <EOL> update . save ( ) <EOL> notifier ( ) . restart ( "<STR_LIT>" ) <EOL> if request . POST . get ( '<STR_LIT:train>' ) : <EOL> update . upd_train = request . POST . get ( '<STR_LIT:train>' ) <EOL> update . save ( ) <EOL> return HttpResponse ( <EOL> json . dumps ( True ) , <EOL> content_type = '<STR_LIT:application/json>' , <EOL> ) <EOL> def update_apply ( request ) : <EOL> try : <EOL> updateobj = models . Update . objects . order_by ( '<STR_LIT>' ) [ <NUM_LIT:0> ] <EOL> except IndexError : <EOL> updateobj = models . Update . objects . create ( ) <EOL> if request . method == '<STR_LIT:POST>' : <EOL> uuid = request . GET . get ( '<STR_LIT>' ) <EOL> if not uuid : <EOL> if not notifier ( ) . is_freenas ( ) and notifier ( ) . failover_licensed ( ) : <EOL> s = notifier ( ) . failover_rpc ( ) <EOL> uuid = s . updated ( False , True ) <EOL> if uuid is False : <EOL> raise MiddlewareError ( _ ( '<STR_LIT>' ) ) <EOL> return HttpResponse ( uuid , status = <NUM_LIT> ) <EOL> running = UpdateHandler . is_running ( ) <EOL> if running is not False : <EOL> return HttpResponse ( running , status = <NUM_LIT> ) <EOL> returncode , uuid = run_updated ( <EOL> str ( updateobj . get_train ( ) ) , <EOL> str ( notifier ( ) . get_update_location ( ) ) , <EOL> download = False , <EOL> apply = True , <EOL> ) <EOL> if returncode != <NUM_LIT:0> : <EOL> raise MiddlewareError ( _ ( '<STR_LIT>' ) ) <EOL> return HttpResponse ( uuid , status = <NUM_LIT> ) <EOL> else : <EOL> failover = False <EOL> if not notifier ( ) . is_freenas ( ) and notifier ( ) . failover_licensed ( ) : <EOL> failover = True <EOL> s = notifier ( ) . failover_rpc ( ) <EOL> rv = s . updated_handler ( uuid ) <EOL> def exit ( ) : <EOL> pass <EOL> rv [ '<STR_LIT>' ] = exit <EOL> handler = namedtuple ( '<STR_LIT>' , rv . keys ( ) ) ( ** rv ) <EOL> else : <EOL> handler = UpdateHandler ( uuid = uuid ) <EOL> if handler . error is not False : <EOL> raise MiddlewareError ( handler . error ) <EOL> if not handler . finished : <EOL> return HttpResponse ( handler . uuid , status = <NUM_LIT> ) <EOL> handler . exit ( ) <EOL> if failover : <EOL> try : <EOL> s . reboot ( ) <EOL> except : <EOL> pass <EOL> return render ( request , '<STR_LIT>' ) <EOL> else : <EOL> if handler . reboot : <EOL> request . session [ '<STR_LIT>' ] = True <EOL> return render ( request , '<STR_LIT>' ) <EOL> else : <EOL> return JsonResp ( <EOL> request , <EOL> message = _ ( '<STR_LIT>' ) , <EOL> ) <EOL> else : <EOL> if not notifier ( ) . is_freenas ( ) and notifier ( ) . failover_licensed ( ) : <EOL> try : <EOL> s = notifier ( ) . failover_rpc ( ) <EOL> return render ( <EOL> request , <EOL> '<STR_LIT>' , <EOL> s . update_check ( ) , <EOL> ) <EOL> except socket . error : <EOL> return render ( request , '<STR_LIT>' ) <EOL> handler = CheckUpdateHandler ( ) <EOL> update = CheckForUpdates ( <EOL> diff_handler = handler . diff_call , <EOL> handler = handler . call , <EOL> train = updateobj . get_train ( ) , <EOL> cache_dir = notifier ( ) . get_update_location ( ) , <EOL> ) <EOL> changelog = None <EOL> if update : <EOL> changelogpath = '<STR_LIT>' % ( <EOL> notifier ( ) . get_update_location ( ) <EOL> ) <EOL> conf = Configuration . Configuration ( ) <EOL> sys_mani = conf . SystemManifest ( ) <EOL> if sys_mani : <EOL> sequence = sys_mani . Sequence ( ) <EOL> else : <EOL> sequence = '<STR_LIT>' <EOL> if os . path . exists ( changelogpath ) : <EOL> with open ( changelogpath , '<STR_LIT:r>' ) as f : <EOL> changelog = parse_changelog ( <EOL> f . read ( ) , <EOL> start = sequence , <EOL> end = update . Sequence ( ) <EOL> ) <EOL> return render ( request , '<STR_LIT>' , { <EOL> '<STR_LIT>' : update , <EOL> '<STR_LIT>' : handler , <EOL> '<STR_LIT>' : changelog , <EOL> } ) <EOL> def update_check ( request ) : <EOL> try : <EOL> updateobj = models . Update . objects . order_by ( '<STR_LIT>' ) [ <NUM_LIT:0> ] <EOL> except IndexError : <EOL> updateobj = models . Update . objects . create ( ) <EOL> if request . method == '<STR_LIT:POST>' : <EOL> uuid = request . GET . get ( '<STR_LIT>' ) <EOL> if not uuid : <EOL> if request . POST . get ( '<STR_LIT>' ) == '<STR_LIT:1>' : <EOL> apply_ = True <EOL> else : <EOL> apply_ = False <EOL> if not notifier ( ) . is_freenas ( ) and notifier ( ) . failover_licensed ( ) : <EOL> s = notifier ( ) . failover_rpc ( ) <EOL> uuid = s . updated ( True , apply_ ) <EOL> if uuid is False : <EOL> raise MiddlewareError ( _ ( '<STR_LIT>' ) ) <EOL> return HttpResponse ( uuid , status = <NUM_LIT> ) <EOL> running = UpdateHandler . is_running ( ) <EOL> if running is not False : <EOL> return HttpResponse ( running , status = <NUM_LIT> ) <EOL> returncode , uuid = run_updated ( <EOL> str ( updateobj . get_train ( ) ) , <EOL> str ( notifier ( ) . get_update_location ( ) ) , <EOL> download = True , <EOL> apply = apply_ , <EOL> ) <EOL> if returncode != <NUM_LIT:0> : <EOL> raise MiddlewareError ( _ ( '<STR_LIT>' ) ) <EOL> return HttpResponse ( uuid , status = <NUM_LIT> ) <EOL> else : <EOL> failover = False <EOL> if not notifier ( ) . is_freenas ( ) and notifier ( ) . failover_licensed ( ) : <EOL> failover = True <EOL> s = notifier ( ) . failover_rpc ( ) <EOL> rv = s . updated_handler ( uuid ) <EOL> def exit ( ) : <EOL> pass <EOL> rv [ '<STR_LIT>' ] = exit <EOL> handler = namedtuple ( '<STR_LIT>' , rv . keys ( ) ) ( ** rv ) <EOL> else : <EOL> handler = UpdateHandler ( uuid = uuid ) <EOL> if handler . error is not False : <EOL> raise MiddlewareError ( handler . error ) <EOL> if not handler . finished : <EOL> return HttpResponse ( handler . uuid , status = <NUM_LIT> ) <EOL> handler . exit ( ) <EOL> if handler . apply : <EOL> if failover : <EOL> try : <EOL> s . reboot ( ) <EOL> except : <EOL> pass <EOL> return render ( request , '<STR_LIT>' ) <EOL> if handler . reboot : <EOL> request . session [ '<STR_LIT>' ] = True <EOL> return render ( request , '<STR_LIT>' ) <EOL> else : <EOL> return JsonResp ( <EOL> request , <EOL> message = _ ( '<STR_LIT>' ) , <EOL> ) <EOL> else : <EOL> return JsonResp ( <EOL> request , <EOL> message = _ ( '<STR_LIT>' ) , <EOL> ) <EOL> else : <EOL> if not notifier ( ) . is_freenas ( ) and notifier ( ) . failover_licensed ( ) : <EOL> try : <EOL> s = notifier ( ) . failover_rpc ( ) <EOL> return render ( <EOL> request , <EOL> '<STR_LIT>' , <EOL> s . update_check ( ) , <EOL> ) <EOL> except socket . error : <EOL> return render ( request , '<STR_LIT>' ) <EOL> handler = CheckUpdateHandler ( ) <EOL> try : <EOL> update = CheckForUpdates ( <EOL> diff_handler = handler . diff_call , <EOL> handler = handler . call , <EOL> train = updateobj . get_train ( ) , <EOL> ) <EOL> network = True <EOL> except UpdateManifestNotFound : <EOL> network = False <EOL> update = False <EOL> if update : <EOL> conf = Configuration . Configuration ( ) <EOL> sys_mani = conf . SystemManifest ( ) <EOL> if sys_mani : <EOL> sequence = sys_mani . Sequence ( ) <EOL> else : <EOL> sequence = '<STR_LIT>' <EOL> changelog = get_changelog ( updateobj . get_train ( ) , start = sequence , end = update . Sequence ( ) ) <EOL> else : <EOL> changelog = None <EOL> return render ( request , '<STR_LIT>' , { <EOL> '<STR_LIT>' : update , <EOL> '<STR_LIT>' : network , <EOL> '<STR_LIT>' : handler , <EOL> '<STR_LIT>' : changelog , <EOL> } ) <EOL> def update_progress ( request ) : <EOL> if not notifier ( ) . is_freenas ( ) and notifier ( ) . failover_licensed ( ) : <EOL> s = notifier ( ) . failover_rpc ( ) <EOL> rv = s . updated_handler ( None ) <EOL> load = rv [ '<STR_LIT:data>' ] <EOL> else : <EOL> load = UpdateHandler ( ) . load ( ) <EOL> return HttpResponse ( <EOL> json . dumps ( load ) , <EOL> content_type = '<STR_LIT:application/json>' , <EOL> ) <EOL> def update_verify ( request ) : <EOL> if request . method == '<STR_LIT:POST>' : <EOL> handler = VerifyHandler ( ) <EOL> try : <EOL> log . debug ( "<STR_LIT>" ) <EOL> error_flag , ed , warn_flag , wl = Configuration . do_verify ( handler . verify_handler ) <EOL> except Exception , e : <EOL> log . debug ( "<STR_LIT>" % e ) <EOL> handler . error = unicode ( e ) <EOL> handler . finished = True <EOL> handler . dump ( ) <EOL> log . debug ( "<STR_LIT>" ) <EOL> if handler . error is not False : <EOL> handler . exit ( ) <EOL> raise MiddlewareError ( handler . error ) <EOL> handler . exit ( ) <EOL> if error_flag or warn_flag : <EOL> checksums = None <EOL> wrongtype = None <EOL> notfound = None <EOL> perms = None <EOL> if ed [ '<STR_LIT>' ] : <EOL> checksums = ed [ '<STR_LIT>' ] <EOL> if ed [ '<STR_LIT>' ] : <EOL> notfound = ed [ '<STR_LIT>' ] <EOL> if ed [ '<STR_LIT>' ] : <EOL> wrongtype = ed [ '<STR_LIT>' ] <EOL> if warn_flag : <EOL> perms = wl <EOL> return render ( request , '<STR_LIT>' , { <EOL> '<STR_LIT:error>' : True , <EOL> '<STR_LIT>' : checksums , <EOL> '<STR_LIT>' : notfound , <EOL> '<STR_LIT>' : wrongtype , <EOL> '<STR_LIT>' : perms , <EOL> } ) <EOL> else : <EOL> return render ( request , '<STR_LIT>' , { <EOL> '<STR_LIT:success>' : True , <EOL> } ) <EOL> else : <EOL> return render ( request , '<STR_LIT>' ) <EOL> def verify_progress ( request ) : <EOL> handler = VerifyHandler ( ) <EOL> return HttpResponse ( <EOL> json . dumps ( handler . load ( ) ) , <EOL> content_type = '<STR_LIT:application/json>' , <EOL> ) <EOL> def CA_import ( request ) : <EOL> if request . method == "<STR_LIT:POST>" : <EOL> form = forms . CertificateAuthorityImportForm ( request . POST ) <EOL> if form . is_valid ( ) : <EOL> form . save ( ) <EOL> return JsonResp ( <EOL> request , <EOL> message = _ ( "<STR_LIT>" ) <EOL> ) <EOL> else : <EOL> form = forms . CertificateAuthorityImportForm ( ) <EOL> return render ( request , "<STR_LIT>" , { <EOL> '<STR_LIT>' : form <EOL> } ) <EOL> def CA_create_internal ( request ) : <EOL> if request . method == "<STR_LIT:POST>" : <EOL> form = forms . CertificateAuthorityCreateInternalForm ( request . POST ) <EOL> if form . is_valid ( ) : <EOL> form . save ( ) <EOL> return JsonResp ( <EOL> request , <EOL> message = _ ( "<STR_LIT>" ) <EOL> ) <EOL> else : <EOL> form = forms . CertificateAuthorityCreateInternalForm ( ) <EOL> return render ( request , "<STR_LIT>" , { <EOL> '<STR_LIT>' : form <EOL> } ) <EOL> def CA_create_intermediate ( request ) : <EOL> if request . method == "<STR_LIT:POST>" : <EOL> form = forms . CertificateAuthorityCreateIntermediateForm ( request . POST ) <EOL> if form . is_valid ( ) : <EOL> form . save ( ) <EOL> return JsonResp ( <EOL> request , <EOL> message = _ ( "<STR_LIT>" ) <EOL> ) <EOL> else : <EOL> form = forms . CertificateAuthorityCreateIntermediateForm ( ) <EOL> return render ( request , "<STR_LIT>" , { <EOL> '<STR_LIT>' : form <EOL> } ) <EOL> def CA_edit ( request , id ) : <EOL> ca = models . CertificateAuthority . objects . get ( pk = id ) <EOL> if request . method == "<STR_LIT:POST>" : <EOL> form = forms . CertificateAuthorityEditForm ( request . POST , instance = ca ) <EOL> if form . is_valid ( ) : <EOL> form . save ( ) <EOL> return JsonResp ( <EOL> request , <EOL> message = _ ( "<STR_LIT>" ) <EOL> ) <EOL> else : <EOL> form = forms . CertificateAuthorityEditForm ( instance = ca ) <EOL> return render ( request , "<STR_LIT>" , { <EOL> '<STR_LIT>' : form <EOL> } ) <EOL> def buf_generator ( buf ) : <EOL> for line in buf : <EOL> yield line <EOL> def CA_export_certificate ( request , id ) : <EOL> ca = models . CertificateAuthority . objects . get ( pk = id ) <EOL> try : <EOL> if ca . cert_chain : <EOL> cert = export_certificate_chain ( ca . cert_certificate ) <EOL> else : <EOL> cert = export_certificate ( ca . cert_certificate ) <EOL> except Exception as e : <EOL> raise MiddlewareError ( e ) <EOL> response = StreamingHttpResponse ( <EOL> buf_generator ( cert ) , content_type = '<STR_LIT>' <EOL> ) <EOL> response [ '<STR_LIT>' ] = len ( cert ) <EOL> response [ '<STR_LIT>' ] = '<STR_LIT>' % ca <EOL> return response <EOL> def CA_export_privatekey ( request , id ) : <EOL> ca = models . CertificateAuthority . objects . get ( pk = id ) <EOL> if not ca . cert_privatekey : <EOL> return HttpResponse ( '<STR_LIT>' ) <EOL> key = export_privatekey ( ca . cert_privatekey ) <EOL> response = StreamingHttpResponse ( <EOL> buf_generator ( key ) , content_type = '<STR_LIT>' <EOL> ) <EOL> response [ '<STR_LIT>' ] = len ( key ) <EOL> response [ '<STR_LIT>' ] = '<STR_LIT>' % ca <EOL> return response <EOL> def certificate_import ( request ) : <EOL> if request . method == "<STR_LIT:POST>" : <EOL> form = forms . CertificateImportForm ( request . POST ) <EOL> if form . is_valid ( ) : <EOL> form . save ( ) <EOL> return JsonResp ( <EOL> request , <EOL> message = _ ( "<STR_LIT>" ) <EOL> ) <EOL> else : <EOL> form = forms . CertificateImportForm ( ) <EOL> return render ( request , "<STR_LIT>" , { <EOL> '<STR_LIT>' : form <EOL> } ) <EOL> def certificate_create_internal ( request ) : <EOL> if request . method == "<STR_LIT:POST>" : <EOL> form = forms . CertificateCreateInternalForm ( request . POST ) <EOL> if form . is_valid ( ) : <EOL> form . save ( ) <EOL> return JsonResp ( <EOL> request , <EOL> message = _ ( "<STR_LIT>" ) <EOL> ) <EOL> else : <EOL> form = forms . CertificateCreateInternalForm ( ) <EOL> return render ( request , "<STR_LIT>" , { <EOL> '<STR_LIT>' : form <EOL> } ) <EOL> def certificate_create_CSR ( request ) : <EOL> if request . method == "<STR_LIT:POST>" : <EOL> form = forms . CertificateCreateCSRForm ( request . POST ) <EOL> if form . is_valid ( ) : <EOL> form . save ( ) <EOL> return JsonResp ( <EOL> request , <EOL> message = _ ( "<STR_LIT>" ) <EOL> ) <EOL> else : <EOL> form = forms . CertificateCreateCSRForm ( ) <EOL> return render ( request , "<STR_LIT>" , { <EOL> '<STR_LIT>' : form <EOL> } ) <EOL> def certificate_edit ( request , id ) : <EOL> cert = models . Certificate . objects . get ( pk = id ) <EOL> if request . method == "<STR_LIT:POST>" : <EOL> form = forms . CertificateEditForm ( request . POST , instance = cert ) <EOL> if form . is_valid ( ) : <EOL> form . save ( ) <EOL> return JsonResp ( <EOL> request , <EOL> message = _ ( "<STR_LIT>" ) <EOL> ) <EOL> else : <EOL> form = forms . CertificateEditForm ( instance = cert ) <EOL> return render ( request , "<STR_LIT>" , { <EOL> '<STR_LIT>' : form <EOL> } ) <EOL> def CSR_edit ( request , id ) : <EOL> cert = models . Certificate . objects . get ( pk = id ) <EOL> if request . method == "<STR_LIT:POST>" : <EOL> form = forms . CertificateCSREditForm ( request . POST , instance = cert ) <EOL> if form . is_valid ( ) : <EOL> form . save ( ) <EOL> return JsonResp ( <EOL> request , <EOL> message = _ ( "<STR_LIT>" ) <EOL> ) <EOL> else : <EOL> form = forms . CertificateCSREditForm ( instance = cert ) <EOL> return render ( request , "<STR_LIT>" , { <EOL> '<STR_LIT>' : form <EOL> } ) <EOL> def certificate_export_certificate ( request , id ) : <EOL> c = models . Certificate . objects . get ( pk = id ) <EOL> cert = export_certificate ( c . cert_certificate ) <EOL> response = StreamingHttpResponse ( <EOL> buf_generator ( cert ) , content_type = '<STR_LIT>' <EOL> ) <EOL> response [ '<STR_LIT>' ] = len ( cert ) <EOL> response [ '<STR_LIT>' ] = '<STR_LIT>' % c <EOL> return response <EOL> def certificate_export_privatekey ( request , id ) : <EOL> c = models . Certificate . objects . get ( pk = id ) <EOL> if not c . cert_privatekey : <EOL> return HttpResponse ( '<STR_LIT>' ) <EOL> key = export_privatekey ( c . cert_privatekey ) <EOL> response = StreamingHttpResponse ( <EOL> buf_generator ( key ) , content_type = '<STR_LIT>' <EOL> ) <EOL> response [ '<STR_LIT>' ] = len ( key ) <EOL> response [ '<STR_LIT>' ] = '<STR_LIT>' % c <EOL> return response <EOL> def certificate_export_certificate_and_privatekey ( request , id ) : <EOL> c = models . Certificate . objects . get ( pk = id ) <EOL> export_certificate ( c . cert_certificate ) <EOL> export_privatekey ( c . cert_privatekey ) <EOL> response = StreamingHttpResponse ( <EOL> buf_generator ( combined ) , content_type = '<STR_LIT>' <EOL> ) <EOL> response [ '<STR_LIT>' ] = len ( combined ) <EOL> response [ '<STR_LIT>' ] = '<STR_LIT>' % c <EOL> return response <EOL> def certificate_to_json ( certtype ) : <EOL> try : <EOL> data = { <EOL> '<STR_LIT>' : certtype . cert_root_path , <EOL> '<STR_LIT>' : certtype . cert_type , <EOL> '<STR_LIT>' : certtype . cert_certificate , <EOL> '<STR_LIT>' : certtype . cert_privatekey , <EOL> '<STR_LIT>' : certtype . cert_CSR , <EOL> '<STR_LIT>' : certtype . cert_key_length , <EOL> '<STR_LIT>' : certtype . cert_digest_algorithm , <EOL> '<STR_LIT>' : certtype . cert_lifetime , <EOL> '<STR_LIT>' : certtype . cert_country , <EOL> '<STR_LIT>' : certtype . cert_state , <EOL> '<STR_LIT>' : certtype . cert_city , <EOL> '<STR_LIT>' : certtype . cert_organization , <EOL> '<STR_LIT>' : certtype . cert_email , <EOL> '<STR_LIT>' : certtype . cert_serial , <EOL> '<STR_LIT>' : certtype . cert_internal , <EOL> '<STR_LIT>' : certtype . cert_certificate_path , <EOL> '<STR_LIT>' : certtype . cert_privatekey_path , <EOL> '<STR_LIT>' : certtype . cert_CSR_path , <EOL> '<STR_LIT>' : certtype . cert_issuer , <EOL> '<STR_LIT>' : certtype . cert_ncertificates , <EOL> '<STR_LIT>' : certtype . cert_DN , <EOL> '<STR_LIT>' : certtype . cert_from , <EOL> '<STR_LIT>' : certtype . cert_until , <EOL> '<STR_LIT>' : certtype . cert_type_existing , <EOL> '<STR_LIT>' : certtype . cert_type_internal , <EOL> '<STR_LIT>' : certtype . cert_type_CSR , <EOL> '<STR_LIT>' : certtype . CA_type_existing , <EOL> '<STR_LIT>' : certtype . CA_type_internal , <EOL> '<STR_LIT>' : certtype . CA_type_intermediate , <EOL> } <EOL> except Exception as e : <EOL> log . debug ( "<STR_LIT>" , e ) <EOL> try : <EOL> data [ '<STR_LIT>' ] = "<STR_LIT:%s>" % certtype . cert_signedby <EOL> except : <EOL> data [ '<STR_LIT>' ] = None <EOL> content = json . dumps ( data ) <EOL> return HttpResponse ( content , content_type = '<STR_LIT:application/json>' ) <EOL> def CA_info ( request , id ) : <EOL> return certificate_to_json ( <EOL> models . CertificateAuthority . objects . get ( pk = int ( id ) ) <EOL> ) <EOL> def certificate_info ( request , id ) : <EOL> return certificate_to_json ( <EOL> models . Certificate . objects . get ( pk = int ( id ) ) <EOL> ) </s>
