<s> from __future__ import absolute_import , print_function <EOL> import os <EOL> import re <EOL> import time <EOL> import socket <EOL> import subprocess <EOL> import ConfigParser <EOL> from flask import Blueprint , request , session , g , redirect , url_for , abort , render_template , flash , jsonify <EOL> import lwp <EOL> import lwp . lxclite as lxc <EOL> from lwp . utils import query_db , if_logged_in , get_bucket_token , hash_passwd , read_config_file , cgroup_ext <EOL> from lwp . views . auth import AUTH <EOL> try : <EOL> config = read_config_file ( ) <EOL> USE_BUCKET = config . getboolean ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> BUCKET_HOST = config . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> BUCKET_PORT = config . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> except ConfigParser . NoOptionError : <EOL> USE_BUCKET = False <EOL> print ( "<STR_LIT>" ) <EOL> storage_repos = config . items ( '<STR_LIT>' ) <EOL> mod = Blueprint ( '<STR_LIT>' , __name__ ) <EOL> @ mod . route ( '<STR_LIT:/>' ) <EOL> @ mod . route ( '<STR_LIT>' ) <EOL> @ if_logged_in ( ) <EOL> def home ( ) : <EOL> """<STR_LIT>""" <EOL> listx = lxc . listx ( ) <EOL> containers_all = [ ] <EOL> for status in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> containers_by_status = [ ] <EOL> for container in listx [ status ] : <EOL> container_info = { <EOL> '<STR_LIT:name>' : container , <EOL> '<STR_LIT>' : lwp . get_container_settings ( container , status ) , <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : get_bucket_token ( container ) <EOL> } <EOL> containers_by_status . append ( container_info ) <EOL> containers_all . append ( { <EOL> '<STR_LIT:status>' : status . lower ( ) , <EOL> '<STR_LIT>' : containers_by_status <EOL> } ) <EOL> clonable_containers = listx [ '<STR_LIT>' ] <EOL> return render_template ( '<STR_LIT>' , containers = lxc . ls ( ) , containers_all = containers_all , dist = lwp . name_distro ( ) , <EOL> host = socket . gethostname ( ) , templates = lwp . get_templates_list ( ) , storage_repos = storage_repos , <EOL> auth = AUTH , clonable_containers = clonable_containers ) <EOL> @ mod . route ( '<STR_LIT>' ) <EOL> @ if_logged_in ( ) <EOL> def about ( ) : <EOL> """<STR_LIT>""" <EOL> return render_template ( '<STR_LIT>' , containers = lxc . ls ( ) , version = lwp . check_version ( ) ) <EOL> @ mod . route ( '<STR_LIT>' , methods = [ '<STR_LIT:POST>' , '<STR_LIT:GET>' ] ) <EOL> @ if_logged_in ( ) <EOL> def edit ( container = None ) : <EOL> """<STR_LIT>""" <EOL> host_memory = lwp . host_memory_usage ( ) <EOL> cfg = lwp . get_container_settings ( container ) <EOL> if request . method == '<STR_LIT:POST>' : <EOL> form = request . form . copy ( ) <EOL> if form [ '<STR_LIT>' ] != get_bucket_token ( container ) : <EOL> g . db . execute ( "<STR_LIT>" , [ container , form [ '<STR_LIT>' ] ] ) <EOL> g . db . commit ( ) <EOL> flash ( u'<STR_LIT>' % container , '<STR_LIT:success>' ) <EOL> form [ '<STR_LIT>' ] = '<STR_LIT>' if '<STR_LIT>' in form else '<STR_LIT>' <EOL> form [ '<STR_LIT>' ] = '<STR_LIT:1>' if '<STR_LIT>' in form else '<STR_LIT:0>' <EOL> if int ( form [ '<STR_LIT>' ] ) == host_memory [ '<STR_LIT>' ] : <EOL> form [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> if int ( form [ '<STR_LIT>' ] ) == host_memory [ '<STR_LIT>' ] * <NUM_LIT:2> : <EOL> form [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> for option in form . keys ( ) : <EOL> if option in cfg . keys ( ) and form [ option ] != cfg [ option ] : <EOL> if re . match ( cgroup_ext [ option ] [ <NUM_LIT:1> ] , form [ option ] ) : <EOL> lwp . push_config_value ( cgroup_ext [ option ] [ <NUM_LIT:0> ] , form [ option ] , container = container ) <EOL> flash ( cgroup_ext [ option ] [ <NUM_LIT:2> ] , '<STR_LIT:success>' ) <EOL> else : <EOL> flash ( '<STR_LIT>' . format ( option ) , '<STR_LIT:error>' ) <EOL> cfg = lwp . get_container_settings ( container ) <EOL> info = lxc . info ( container ) <EOL> infos = { '<STR_LIT:status>' : info [ '<STR_LIT:state>' ] , '<STR_LIT>' : info [ '<STR_LIT>' ] , '<STR_LIT>' : lwp . memory_usage ( container ) } <EOL> regex = { } <EOL> for k , v in cgroup_ext . items ( ) : <EOL> regex [ k ] = v [ <NUM_LIT:1> ] <EOL> return render_template ( '<STR_LIT>' , containers = lxc . ls ( ) , container = container , infos = infos , <EOL> settings = cfg , host_memory = host_memory , storage_repos = storage_repos , regex = regex , <EOL> clonable_containers = lxc . listx ( ) [ '<STR_LIT>' ] ) <EOL> @ mod . route ( '<STR_LIT>' , methods = [ '<STR_LIT:POST>' , '<STR_LIT:GET>' ] ) <EOL> @ if_logged_in ( ) <EOL> def lxc_net ( ) : <EOL> """<STR_LIT>""" <EOL> if session [ '<STR_LIT>' ] != '<STR_LIT>' : <EOL> return abort ( <NUM_LIT> ) <EOL> if request . method == '<STR_LIT:POST>' : <EOL> if lxc . running ( ) == [ ] : <EOL> cfg = lwp . get_net_settings ( ) <EOL> ip_regex = '<STR_LIT>' <EOL> form = { } <EOL> for key in [ '<STR_LIT>' , '<STR_LIT:address>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> form [ key ] = request . form . get ( key , None ) <EOL> form [ '<STR_LIT>' ] = request . form . get ( '<STR_LIT>' , None ) <EOL> if form [ '<STR_LIT>' ] != cfg [ '<STR_LIT>' ] : <EOL> lwp . push_net_value ( '<STR_LIT>' , '<STR_LIT:true>' if form [ '<STR_LIT>' ] else '<STR_LIT:false>' ) <EOL> if form [ '<STR_LIT>' ] and form [ '<STR_LIT>' ] != cfg [ '<STR_LIT>' ] and re . match ( '<STR_LIT>' , form [ '<STR_LIT>' ] ) : <EOL> lwp . push_net_value ( '<STR_LIT>' , form [ '<STR_LIT>' ] ) <EOL> if form [ '<STR_LIT:address>' ] and form [ '<STR_LIT:address>' ] != cfg [ '<STR_LIT:address>' ] and re . match ( '<STR_LIT>' % ip_regex , form [ '<STR_LIT:address>' ] ) : <EOL> lwp . push_net_value ( '<STR_LIT>' , form [ '<STR_LIT:address>' ] ) <EOL> if form [ '<STR_LIT>' ] and form [ '<STR_LIT>' ] != cfg [ '<STR_LIT>' ] and re . match ( '<STR_LIT>' % ip_regex , form [ '<STR_LIT>' ] ) : <EOL> lwp . push_net_value ( '<STR_LIT>' , form [ '<STR_LIT>' ] ) <EOL> if form [ '<STR_LIT>' ] and form [ '<STR_LIT>' ] != cfg [ '<STR_LIT>' ] and re . match ( '<STR_LIT>' % ip_regex , form [ '<STR_LIT>' ] ) : <EOL> lwp . push_net_value ( '<STR_LIT>' , form [ '<STR_LIT>' ] ) <EOL> if form [ '<STR_LIT>' ] and form [ '<STR_LIT>' ] != cfg [ '<STR_LIT>' ] and re . match ( '<STR_LIT>' % ( ip_regex , ip_regex ) , form [ '<STR_LIT>' ] ) : <EOL> lwp . push_net_value ( '<STR_LIT>' , form [ '<STR_LIT>' ] ) <EOL> if form [ '<STR_LIT>' ] and form [ '<STR_LIT>' ] != cfg [ '<STR_LIT>' ] and re . match ( '<STR_LIT>' , form [ '<STR_LIT>' ] ) : <EOL> lwp . push_net_value ( '<STR_LIT>' , form [ '<STR_LIT>' ] ) <EOL> if lwp . net_restart ( ) == <NUM_LIT:0> : <EOL> flash ( u'<STR_LIT>' , '<STR_LIT:success>' ) <EOL> else : <EOL> flash ( u'<STR_LIT>' , '<STR_LIT:error>' ) <EOL> else : <EOL> flash ( u'<STR_LIT>' , '<STR_LIT>' ) <EOL> return render_template ( '<STR_LIT>' , containers = lxc . ls ( ) , cfg = lwp . get_net_settings ( ) , running = lxc . running ( ) ) <EOL> @ mod . route ( '<STR_LIT>' , methods = [ '<STR_LIT:POST>' , '<STR_LIT:GET>' ] ) <EOL> @ if_logged_in ( ) <EOL> def lwp_users ( ) : <EOL> """<STR_LIT>""" <EOL> if session [ '<STR_LIT>' ] != '<STR_LIT>' : <EOL> return abort ( <NUM_LIT> ) <EOL> if AUTH != '<STR_LIT>' : <EOL> return abort ( <NUM_LIT> , '<STR_LIT>' ) <EOL> try : <EOL> trash = request . args . get ( '<STR_LIT>' ) <EOL> except KeyError : <EOL> trash = <NUM_LIT:0> <EOL> su_users = query_db ( "<STR_LIT>" , [ ] , one = True ) <EOL> if request . args . get ( '<STR_LIT>' ) == session . get ( '<STR_LIT>' ) and int ( trash ) == <NUM_LIT:1> and request . args . get ( '<STR_LIT>' ) and request . args . get ( '<STR_LIT:username>' ) : <EOL> nb_users = query_db ( "<STR_LIT>" , [ ] , one = True ) <EOL> if nb_users [ '<STR_LIT>' ] > <NUM_LIT:1> : <EOL> if su_users [ '<STR_LIT>' ] <= <NUM_LIT:1> : <EOL> su_user = query_db ( "<STR_LIT>" , [ ] , one = True ) <EOL> if su_user [ '<STR_LIT:username>' ] == request . args . get ( '<STR_LIT:username>' ) : <EOL> flash ( u'<STR_LIT>' % request . args . get ( '<STR_LIT:username>' ) , '<STR_LIT:error>' ) <EOL> return redirect ( url_for ( '<STR_LIT>' ) ) <EOL> g . db . execute ( "<STR_LIT>" , [ request . args . get ( '<STR_LIT>' ) , <EOL> request . args . get ( '<STR_LIT:username>' ) ] ) <EOL> g . db . commit ( ) <EOL> flash ( u'<STR_LIT>' % request . args . get ( '<STR_LIT:username>' ) , '<STR_LIT:success>' ) <EOL> return redirect ( url_for ( '<STR_LIT>' ) ) <EOL> flash ( u'<STR_LIT>' , '<STR_LIT:error>' ) <EOL> return redirect ( url_for ( '<STR_LIT>' ) ) <EOL> if request . method == '<STR_LIT:POST>' : <EOL> users = query_db ( '<STR_LIT>' ) <EOL> if request . form [ '<STR_LIT>' ] == '<STR_LIT:True>' : <EOL> if not request . form [ '<STR_LIT:username>' ] in [ user [ '<STR_LIT:username>' ] for user in users ] : <EOL> if re . match ( '<STR_LIT>' , request . form [ '<STR_LIT:username>' ] ) and request . form [ '<STR_LIT>' ] : <EOL> if request . form [ '<STR_LIT>' ] == request . form [ '<STR_LIT>' ] : <EOL> if request . form [ '<STR_LIT:name>' ] : <EOL> if re . match ( '<STR_LIT>' , request . form [ '<STR_LIT:name>' ] ) : <EOL> g . db . execute ( "<STR_LIT>" , <EOL> [ request . form [ '<STR_LIT:name>' ] , request . form [ '<STR_LIT:username>' ] , <EOL> hash_passwd ( request . form [ '<STR_LIT>' ] ) ] ) <EOL> g . db . commit ( ) <EOL> else : <EOL> flash ( u'<STR_LIT>' , '<STR_LIT:error>' ) <EOL> else : <EOL> g . db . execute ( "<STR_LIT>" , <EOL> [ request . form [ '<STR_LIT:username>' ] , hash_passwd ( request . form [ '<STR_LIT>' ] ) ] ) <EOL> g . db . commit ( ) <EOL> flash ( u'<STR_LIT>' % request . form [ '<STR_LIT:username>' ] , '<STR_LIT:success>' ) <EOL> else : <EOL> flash ( u'<STR_LIT>' , '<STR_LIT:error>' ) <EOL> else : <EOL> flash ( u'<STR_LIT>' , '<STR_LIT:error>' ) <EOL> else : <EOL> flash ( u'<STR_LIT>' , '<STR_LIT:error>' ) <EOL> elif request . form [ '<STR_LIT>' ] == '<STR_LIT:False>' : <EOL> if request . form [ '<STR_LIT>' ] == request . form [ '<STR_LIT>' ] : <EOL> if re . match ( '<STR_LIT>' , request . form [ '<STR_LIT:name>' ] ) : <EOL> if su_users [ '<STR_LIT>' ] <= <NUM_LIT:1> : <EOL> su = '<STR_LIT>' <EOL> else : <EOL> try : <EOL> su = request . form [ '<STR_LIT>' ] <EOL> except KeyError : <EOL> su = '<STR_LIT>' <EOL> if not request . form [ '<STR_LIT:name>' ] : <EOL> g . db . execute ( "<STR_LIT>" , [ su , request . form [ '<STR_LIT:username>' ] ] ) <EOL> g . db . commit ( ) <EOL> elif request . form [ '<STR_LIT:name>' ] and not request . form [ '<STR_LIT>' ] and not request . form [ '<STR_LIT>' ] : <EOL> g . db . execute ( "<STR_LIT>" , <EOL> [ request . form [ '<STR_LIT:name>' ] , su , request . form [ '<STR_LIT:username>' ] ] ) <EOL> g . db . commit ( ) <EOL> elif request . form [ '<STR_LIT:name>' ] and request . form [ '<STR_LIT>' ] and request . form [ '<STR_LIT>' ] : <EOL> g . db . execute ( "<STR_LIT>" , <EOL> [ request . form [ '<STR_LIT:name>' ] , hash_passwd ( request . form [ '<STR_LIT>' ] ) , su , <EOL> request . form [ '<STR_LIT:username>' ] ] ) <EOL> g . db . commit ( ) <EOL> elif request . form [ '<STR_LIT>' ] and request . form [ '<STR_LIT>' ] : <EOL> g . db . execute ( "<STR_LIT>" , <EOL> [ hash_passwd ( request . form [ '<STR_LIT>' ] ) , su , request . form [ '<STR_LIT:username>' ] ] ) <EOL> g . db . commit ( ) <EOL> flash ( u'<STR_LIT>' , '<STR_LIT:success>' ) <EOL> else : <EOL> flash ( u'<STR_LIT>' , '<STR_LIT:error>' ) <EOL> else : <EOL> flash ( u'<STR_LIT>' , '<STR_LIT:error>' ) <EOL> else : <EOL> flash ( u'<STR_LIT>' , '<STR_LIT:error>' ) <EOL> users = query_db ( "<STR_LIT>" ) <EOL> nb_users = query_db ( "<STR_LIT>" , [ ] , one = True ) <EOL> su_users = query_db ( "<STR_LIT>" , [ ] , one = True ) <EOL> return render_template ( '<STR_LIT>' , containers = lxc . ls ( ) , users = users , nb_users = nb_users , su_users = su_users ) <EOL> @ mod . route ( '<STR_LIT>' , methods = [ '<STR_LIT:POST>' , '<STR_LIT:GET>' ] ) <EOL> @ if_logged_in ( ) <EOL> def lwp_tokens ( ) : <EOL> """<STR_LIT>""" <EOL> if session [ '<STR_LIT>' ] != '<STR_LIT>' : <EOL> return abort ( <NUM_LIT> ) <EOL> if request . method == '<STR_LIT:POST>' : <EOL> if request . form [ '<STR_LIT:action>' ] == '<STR_LIT>' : <EOL> token = request . form [ '<STR_LIT>' ] <EOL> description = request . form [ '<STR_LIT:description>' ] <EOL> username = session [ '<STR_LIT:username>' ] <EOL> g . db . execute ( "<STR_LIT>" , [ username , token , <EOL> description ] ) <EOL> g . db . commit ( ) <EOL> flash ( u'<STR_LIT>' % token , '<STR_LIT:success>' ) <EOL> if request . args . get ( '<STR_LIT:action>' ) == '<STR_LIT>' : <EOL> token = request . args [ '<STR_LIT>' ] <EOL> g . db . execute ( "<STR_LIT>" , [ token ] ) <EOL> g . db . commit ( ) <EOL> flash ( u'<STR_LIT>' % token , '<STR_LIT:success>' ) <EOL> tokens = query_db ( "<STR_LIT>" ) <EOL> return render_template ( '<STR_LIT>' , containers = lxc . ls ( ) , tokens = tokens ) <EOL> @ mod . route ( '<STR_LIT>' ) <EOL> @ if_logged_in ( ) <EOL> def checkconfig ( ) : <EOL> """<STR_LIT>""" <EOL> if session [ '<STR_LIT>' ] != '<STR_LIT>' : <EOL> return abort ( <NUM_LIT> ) <EOL> return render_template ( '<STR_LIT>' , containers = lxc . ls ( ) , cfg = lxc . checkconfig ( ) ) <EOL> @ mod . route ( '<STR_LIT>' , methods = [ '<STR_LIT:GET>' ] ) <EOL> @ if_logged_in ( ) <EOL> def action ( ) : <EOL> """<STR_LIT>""" <EOL> act = request . args [ '<STR_LIT:action>' ] <EOL> name = request . args [ '<STR_LIT:name>' ] <EOL> if act == '<STR_LIT:start>' : <EOL> try : <EOL> if lxc . start ( name ) == <NUM_LIT:0> : <EOL> time . sleep ( <NUM_LIT:1> ) <EOL> flash ( u'<STR_LIT>' % name , '<STR_LIT:success>' ) <EOL> else : <EOL> flash ( u'<STR_LIT>' % name , '<STR_LIT:error>' ) <EOL> except lxc . ContainerAlreadyRunning : <EOL> flash ( u'<STR_LIT>' % name , '<STR_LIT:error>' ) <EOL> elif act == '<STR_LIT>' : <EOL> try : <EOL> if lxc . stop ( name ) == <NUM_LIT:0> : <EOL> flash ( u'<STR_LIT>' % name , '<STR_LIT:success>' ) <EOL> else : <EOL> flash ( u'<STR_LIT>' % name , '<STR_LIT:error>' ) <EOL> except lxc . ContainerNotRunning : <EOL> flash ( u'<STR_LIT>' % name , '<STR_LIT:error>' ) <EOL> elif act == '<STR_LIT>' : <EOL> try : <EOL> if lxc . freeze ( name ) == <NUM_LIT:0> : <EOL> flash ( u'<STR_LIT>' % name , '<STR_LIT:success>' ) <EOL> else : <EOL> flash ( u'<STR_LIT>' % name , '<STR_LIT:error>' ) <EOL> except lxc . ContainerNotRunning : <EOL> flash ( u'<STR_LIT>' % name , '<STR_LIT:error>' ) <EOL> elif act == '<STR_LIT>' : <EOL> try : <EOL> if lxc . unfreeze ( name ) == <NUM_LIT:0> : <EOL> flash ( u'<STR_LIT>' % name , '<STR_LIT:success>' ) <EOL> else : <EOL> flash ( u'<STR_LIT>' % name , '<STR_LIT:error>' ) <EOL> except lxc . ContainerNotRunning : <EOL> flash ( u'<STR_LIT>' % name , '<STR_LIT:error>' ) <EOL> elif act == '<STR_LIT>' : <EOL> if session [ '<STR_LIT>' ] != '<STR_LIT>' : <EOL> return abort ( <NUM_LIT> ) <EOL> try : <EOL> if lxc . destroy ( name ) == <NUM_LIT:0> : <EOL> flash ( u'<STR_LIT>' % name , '<STR_LIT:success>' ) <EOL> else : <EOL> flash ( u'<STR_LIT>' % name , '<STR_LIT:error>' ) <EOL> except lxc . ContainerDoesntExists : <EOL> flash ( u'<STR_LIT>' % name , '<STR_LIT:error>' ) <EOL> elif act == '<STR_LIT>' and name == '<STR_LIT:host>' : <EOL> if session [ '<STR_LIT>' ] != '<STR_LIT>' : <EOL> return abort ( <NUM_LIT> ) <EOL> msg = '<STR_LIT>' <EOL> try : <EOL> subprocess . check_call ( '<STR_LIT>' % msg , shell = True ) <EOL> flash ( u'<STR_LIT>' , '<STR_LIT:success>' ) <EOL> except subprocess . CalledProcessError : <EOL> flash ( u'<STR_LIT>' , '<STR_LIT:error>' ) <EOL> elif act == '<STR_LIT>' : <EOL> pass <EOL> try : <EOL> if request . args [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> return redirect ( url_for ( '<STR_LIT>' , container = name ) ) <EOL> else : <EOL> return redirect ( url_for ( '<STR_LIT>' ) ) <EOL> except KeyError : <EOL> return redirect ( url_for ( '<STR_LIT>' ) ) <EOL> @ mod . route ( '<STR_LIT>' , methods = [ '<STR_LIT:GET>' , '<STR_LIT:POST>' ] ) <EOL> @ if_logged_in ( ) <EOL> def create_container ( ) : <EOL> """<STR_LIT>""" <EOL> if session [ '<STR_LIT>' ] != '<STR_LIT>' : <EOL> return abort ( <NUM_LIT> ) <EOL> if request . method == '<STR_LIT:POST>' : <EOL> name = request . form [ '<STR_LIT:name>' ] <EOL> template = request . form [ '<STR_LIT>' ] <EOL> command = request . form [ '<STR_LIT>' ] <EOL> if re . match ( '<STR_LIT>' , name ) : <EOL> storage_method = request . form [ '<STR_LIT>' ] <EOL> if storage_method == '<STR_LIT:default>' : <EOL> try : <EOL> if lxc . create ( name , template = template , xargs = command ) == <NUM_LIT:0> : <EOL> flash ( u'<STR_LIT>' % name , '<STR_LIT:success>' ) <EOL> else : <EOL> flash ( u'<STR_LIT>' % name , '<STR_LIT:error>' ) <EOL> except lxc . ContainerAlreadyExists : <EOL> flash ( u'<STR_LIT>' % name , '<STR_LIT:error>' ) <EOL> except subprocess . CalledProcessError : <EOL> flash ( u'<STR_LIT>' % name , '<STR_LIT:error>' ) <EOL> elif storage_method == '<STR_LIT>' : <EOL> directory = request . form [ '<STR_LIT>' ] <EOL> if re . match ( '<STR_LIT>' , directory ) and directory != '<STR_LIT>' : <EOL> try : <EOL> if lxc . create ( name , template = template , storage = '<STR_LIT>' % directory , xargs = command ) == <NUM_LIT:0> : <EOL> flash ( u'<STR_LIT>' % name , '<STR_LIT:success>' ) <EOL> else : <EOL> flash ( u'<STR_LIT>' % name , '<STR_LIT:error>' ) <EOL> except lxc . ContainerAlreadyExists : <EOL> flash ( u'<STR_LIT>' % name , '<STR_LIT:error>' ) <EOL> except subprocess . CalledProcessError : <EOL> flash ( u'<STR_LIT>' % name , '<STR_LIT:error>' ) <EOL> elif storage_method == '<STR_LIT>' : <EOL> try : <EOL> if lxc . create ( name , template = template , storage = '<STR_LIT>' , xargs = command ) == <NUM_LIT:0> : <EOL> flash ( u'<STR_LIT>' % name , '<STR_LIT:success>' ) <EOL> else : <EOL> flash ( u'<STR_LIT>' % name , '<STR_LIT:error>' ) <EOL> except lxc . ContainerAlreadyExists : <EOL> flash ( u'<STR_LIT>' % name , '<STR_LIT:error>' ) <EOL> except subprocess . CalledProcessError : <EOL> flash ( u'<STR_LIT>' % name , '<STR_LIT:error>' ) <EOL> elif storage_method == '<STR_LIT>' : <EOL> zfs = request . form [ '<STR_LIT>' ] <EOL> if re . match ( '<STR_LIT>' , zfs ) and zfs != '<STR_LIT>' : <EOL> try : <EOL> if lxc . create ( name , template = template , storage = '<STR_LIT>' % zfs , xargs = command ) == <NUM_LIT:0> : <EOL> flash ( u'<STR_LIT>' % name , '<STR_LIT:success>' ) <EOL> else : <EOL> flash ( u'<STR_LIT>' % name , '<STR_LIT:error>' ) <EOL> except lxc . ContainerAlreadyExists : <EOL> flash ( u'<STR_LIT>' % name , '<STR_LIT:error>' ) <EOL> except subprocess . CalledProcessError : <EOL> flash ( u'<STR_LIT>' % name , '<STR_LIT:error>' ) <EOL> elif storage_method == '<STR_LIT>' : <EOL> lvname = request . form [ '<STR_LIT>' ] <EOL> vgname = request . form [ '<STR_LIT>' ] <EOL> fstype = request . form [ '<STR_LIT>' ] <EOL> fssize = request . form [ '<STR_LIT>' ] <EOL> storage_options = '<STR_LIT>' <EOL> if re . match ( '<STR_LIT>' , lvname ) and lvname != '<STR_LIT>' : <EOL> storage_options += '<STR_LIT>' % lvname <EOL> if re . match ( '<STR_LIT>' , vgname ) and vgname != '<STR_LIT>' : <EOL> storage_options += '<STR_LIT>' % vgname <EOL> if re . match ( '<STR_LIT>' , fstype ) and fstype != '<STR_LIT>' : <EOL> storage_options += '<STR_LIT>' % fstype <EOL> if re . match ( '<STR_LIT>' , fssize ) and fssize != '<STR_LIT>' : <EOL> storage_options += '<STR_LIT>' % fssize <EOL> try : <EOL> if lxc . create ( name , template = template , storage = storage_options , xargs = command ) == <NUM_LIT:0> : <EOL> flash ( u'<STR_LIT>' % name , '<STR_LIT:success>' ) <EOL> else : <EOL> flash ( u'<STR_LIT>' % name , '<STR_LIT:error>' ) <EOL> except lxc . ContainerAlreadyExists : <EOL> flash ( u'<STR_LIT>' % name , '<STR_LIT:error>' ) <EOL> except subprocess . CalledProcessError : <EOL> flash ( u'<STR_LIT>' % name , '<STR_LIT:error>' ) <EOL> else : <EOL> flash ( u'<STR_LIT>' , '<STR_LIT:error>' ) <EOL> else : <EOL> if name == '<STR_LIT>' : <EOL> flash ( u'<STR_LIT>' , '<STR_LIT:error>' ) <EOL> else : <EOL> flash ( u'<STR_LIT>' % name , '<STR_LIT:error>' ) <EOL> return redirect ( url_for ( '<STR_LIT>' ) ) <EOL> @ mod . route ( '<STR_LIT>' , methods = [ '<STR_LIT:GET>' , '<STR_LIT:POST>' ] ) <EOL> @ if_logged_in ( ) <EOL> def clone_container ( ) : <EOL> """<STR_LIT>""" <EOL> if session [ '<STR_LIT>' ] != '<STR_LIT>' : <EOL> return abort ( <NUM_LIT> ) <EOL> if request . method == '<STR_LIT:POST>' : <EOL> orig = request . form [ '<STR_LIT>' ] <EOL> name = request . form [ '<STR_LIT:name>' ] <EOL> try : <EOL> snapshot = request . form [ '<STR_LIT>' ] <EOL> if snapshot == '<STR_LIT:True>' : <EOL> snapshot = True <EOL> except KeyError : <EOL> snapshot = False <EOL> if re . match ( '<STR_LIT>' , name ) : <EOL> out = None <EOL> try : <EOL> out = lxc . clone ( orig = orig , new = name , snapshot = snapshot ) <EOL> except lxc . ContainerAlreadyExists : <EOL> flash ( u'<STR_LIT>' % name , '<STR_LIT:error>' ) <EOL> except subprocess . CalledProcessError : <EOL> flash ( u'<STR_LIT>' , '<STR_LIT:error>' ) <EOL> if out and out == <NUM_LIT:0> : <EOL> flash ( u'<STR_LIT>' % ( orig , name ) , '<STR_LIT:success>' ) <EOL> elif out and out != <NUM_LIT:0> : <EOL> flash ( u'<STR_LIT>' % ( orig , name ) , '<STR_LIT:error>' ) <EOL> else : <EOL> if name == '<STR_LIT>' : <EOL> flash ( u'<STR_LIT>' , '<STR_LIT:error>' ) <EOL> else : <EOL> flash ( u'<STR_LIT>' % name , '<STR_LIT:error>' ) <EOL> return redirect ( url_for ( '<STR_LIT>' ) ) <EOL> @ mod . route ( '<STR_LIT>' , methods = [ '<STR_LIT:GET>' , '<STR_LIT:POST>' ] ) <EOL> @ if_logged_in ( ) <EOL> def backup_container ( ) : <EOL> """<STR_LIT>""" <EOL> if request . method == '<STR_LIT:POST>' : <EOL> container = request . form [ '<STR_LIT>' ] <EOL> sr_type = request . form [ '<STR_LIT>' ] <EOL> if '<STR_LIT>' in request . form : <EOL> push = request . form [ '<STR_LIT>' ] <EOL> else : <EOL> push = False <EOL> sr_path = None <EOL> for sr in storage_repos : <EOL> if sr_type in sr : <EOL> sr_path = sr [ <NUM_LIT:1> ] <EOL> break <EOL> backup_failed = True <EOL> try : <EOL> backup_file = lxc . backup ( container = container , sr_type = sr_type , destination = sr_path ) <EOL> bucket_token = get_bucket_token ( container ) <EOL> if push and bucket_token and USE_BUCKET : <EOL> os . system ( '<STR_LIT>' . format ( BUCKET_HOST , BUCKET_PORT , bucket_token , backup_file ) ) <EOL> backup_failed = False <EOL> except lxc . ContainerDoesntExists : <EOL> flash ( u'<STR_LIT>' % container , '<STR_LIT:error>' ) <EOL> except lxc . DirectoryDoesntExists : <EOL> flash ( u'<STR_LIT>' % sr_path , '<STR_LIT:error>' ) <EOL> except lxc . NFSDirectoryNotMounted : <EOL> flash ( u'<STR_LIT>' % sr_path , '<STR_LIT:error>' ) <EOL> except subprocess . CalledProcessError : <EOL> flash ( u'<STR_LIT>' , '<STR_LIT:error>' ) <EOL> except : <EOL> flash ( u'<STR_LIT>' , '<STR_LIT:error>' ) <EOL> if backup_failed is not True : <EOL> flash ( u'<STR_LIT>' % container , '<STR_LIT:success>' ) <EOL> else : <EOL> flash ( u'<STR_LIT>' % container , '<STR_LIT:error>' ) <EOL> return redirect ( url_for ( '<STR_LIT>' ) ) <EOL> @ mod . route ( '<STR_LIT>' ) <EOL> @ if_logged_in ( ) <EOL> def refresh_info ( ) : <EOL> return jsonify ( { '<STR_LIT>' : lwp . host_cpu_percent ( ) , <EOL> '<STR_LIT>' : lwp . host_uptime ( ) , <EOL> '<STR_LIT>' : lwp . host_disk_usage ( ) } ) <EOL> @ mod . route ( '<STR_LIT>' ) <EOL> @ if_logged_in ( ) <EOL> def refresh_memory_containers ( name = None ) : <EOL> if name == '<STR_LIT>' : <EOL> containers_running = lxc . running ( ) <EOL> containers = [ ] <EOL> for container in containers_running : <EOL> container = container . replace ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> containers . append ( { '<STR_LIT:name>' : container , '<STR_LIT>' : lwp . memory_usage ( container ) , <EOL> '<STR_LIT>' : lwp . get_container_settings ( container ) } ) <EOL> return jsonify ( data = containers ) <EOL> elif name == '<STR_LIT:host>' : <EOL> return jsonify ( lwp . host_memory_usage ( ) ) <EOL> return jsonify ( { '<STR_LIT>' : lwp . memory_usage ( name ) } ) <EOL> @ mod . route ( '<STR_LIT>' ) <EOL> @ if_logged_in ( ) <EOL> def check_version ( ) : <EOL> return jsonify ( lwp . check_version ( ) ) </s>
