<s> from __future__ import absolute_import , print_function <EOL> import json <EOL> from flask import Blueprint , request , g , jsonify <EOL> import lwp . lxclite as lxc <EOL> from lwp . utils import api_auth <EOL> mod = Blueprint ( '<STR_LIT>' , __name__ ) <EOL> @ mod . route ( '<STR_LIT>' ) <EOL> @ api_auth ( ) <EOL> def get_containers ( ) : <EOL> """<STR_LIT>""" <EOL> list_container = lxc . list_status ( ) <EOL> return json . dumps ( list_container ) <EOL> @ mod . route ( '<STR_LIT>' ) <EOL> @ api_auth ( ) <EOL> def get_container ( name ) : <EOL> return jsonify ( lxc . info ( name ) ) <EOL> @ mod . route ( '<STR_LIT>' , methods = [ '<STR_LIT:POST>' ] ) <EOL> @ api_auth ( ) <EOL> def post_container ( name ) : <EOL> data = request . get_json ( force = True ) <EOL> if data is None : <EOL> return jsonify ( status = "<STR_LIT:error>" , error = "<STR_LIT>" ) , <NUM_LIT> <EOL> status = data [ '<STR_LIT:action>' ] <EOL> try : <EOL> if status == "<STR_LIT>" : <EOL> lxc . stop ( name ) <EOL> return jsonify ( status = "<STR_LIT>" ) , <NUM_LIT:200> <EOL> elif status == "<STR_LIT:start>" : <EOL> lxc . start ( name ) <EOL> return jsonify ( status = "<STR_LIT>" ) , <NUM_LIT:200> <EOL> elif status == "<STR_LIT>" : <EOL> lxc . freeze ( name ) <EOL> return jsonify ( status = "<STR_LIT>" ) , <NUM_LIT:200> <EOL> return jsonify ( status = "<STR_LIT:error>" , error = "<STR_LIT>" ) , <NUM_LIT> <EOL> except lxc . ContainerDoesntExists : <EOL> return jsonify ( status = "<STR_LIT:error>" , error = "<STR_LIT>" ) , <NUM_LIT> <EOL> @ mod . route ( '<STR_LIT>' , methods = [ '<STR_LIT>' ] ) <EOL> @ api_auth ( ) <EOL> def add_container ( ) : <EOL> data = request . get_json ( force = True ) <EOL> if data is None : <EOL> return jsonify ( status = "<STR_LIT:error>" , error = "<STR_LIT>" ) , <NUM_LIT> <EOL> if ( not ( ( '<STR_LIT>' in data ) or ( '<STR_LIT>' in data ) ) or ( '<STR_LIT:name>' not in data ) ) : <EOL> return jsonify ( status = "<STR_LIT:error>" , error = "<STR_LIT>" ) , <NUM_LIT> <EOL> if '<STR_LIT>' in data : <EOL> if '<STR_LIT:store>' not in data : <EOL> data [ '<STR_LIT:store>' ] = "<STR_LIT>" <EOL> if '<STR_LIT>' not in data : <EOL> data [ '<STR_LIT>' ] = "<STR_LIT>" <EOL> try : <EOL> lxc . create ( data [ '<STR_LIT:name>' ] , data [ '<STR_LIT>' ] , data [ '<STR_LIT:store>' ] , data [ '<STR_LIT>' ] ) <EOL> except lxc . ContainerAlreadyExists : <EOL> return jsonify ( status = "<STR_LIT:error>" , error = "<STR_LIT>" ) , <NUM_LIT> <EOL> else : <EOL> try : <EOL> lxc . clone ( data [ '<STR_LIT>' ] , data [ '<STR_LIT:name>' ] ) <EOL> except lxc . ContainerAlreadyExists : <EOL> return jsonify ( status = "<STR_LIT:error>" , error = "<STR_LIT>" ) , <NUM_LIT> <EOL> return jsonify ( status = "<STR_LIT>" ) , <NUM_LIT:200> <EOL> @ mod . route ( '<STR_LIT>' , methods = [ '<STR_LIT>' ] ) <EOL> @ api_auth ( ) <EOL> def delete_container ( name ) : <EOL> try : <EOL> lxc . destroy ( name ) <EOL> return jsonify ( status = "<STR_LIT>" ) , <NUM_LIT:200> <EOL> except lxc . ContainerDoesntExists : <EOL> return jsonify ( status = "<STR_LIT:error>" , error = "<STR_LIT>" ) , <NUM_LIT> <EOL> @ mod . route ( '<STR_LIT>' , methods = [ '<STR_LIT:POST>' ] ) <EOL> @ api_auth ( ) <EOL> def add_token ( ) : <EOL> data = request . get_json ( force = True ) <EOL> if data is None or '<STR_LIT>' not in data : <EOL> return jsonify ( status = "<STR_LIT:error>" , error = "<STR_LIT>" ) , <NUM_LIT> <EOL> if '<STR_LIT:description>' not in data : <EOL> data . update ( description = "<STR_LIT>" ) <EOL> g . db . execute ( '<STR_LIT>' , [ data [ '<STR_LIT:description>' ] , data [ '<STR_LIT>' ] ] ) <EOL> g . db . commit ( ) <EOL> return jsonify ( status = "<STR_LIT>" ) , <NUM_LIT:200> <EOL> @ mod . route ( '<STR_LIT>' , methods = [ '<STR_LIT>' ] ) <EOL> @ api_auth ( ) <EOL> def delete_token ( token ) : <EOL> g . db . execute ( '<STR_LIT>' , [ token ] ) <EOL> g . db . commit ( ) <EOL> return jsonify ( status = "<STR_LIT>" ) , <NUM_LIT:200> </s>
