<s> from view_common import * <EOL> from core . models import * <EOL> import functools <EOL> from django . contrib . auth . models import BaseUserManager <EOL> from django . core import serializers <EOL> from django . core . mail import EmailMultiAlternatives <EOL> import json <EOL> BLESSED_DEPLOYMENTS = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> class RequestAccessView ( View ) : <EOL> def post ( self , request , * args , ** kwargs ) : <EOL> email = request . POST . get ( "<STR_LIT:email>" , "<STR_LIT:0>" ) <EOL> firstname = request . POST . get ( "<STR_LIT>" , "<STR_LIT:0>" ) <EOL> lastname = request . POST . get ( "<STR_LIT>" , "<STR_LIT:0>" ) <EOL> site = request . POST . get ( "<STR_LIT>" , "<STR_LIT:0>" ) <EOL> user = User . objects . filter ( email = BaseUserManager . normalize_email ( email ) ) <EOL> if ( user ) : <EOL> user = user [ <NUM_LIT:0> ] <EOL> if user . is_active : <EOL> user . is_registering = True <EOL> user . save ( ) <EOL> return HttpResponse ( json . dumps ( { "<STR_LIT:error>" : "<STR_LIT>" } ) , content_type = '<STR_LIT>' ) <EOL> else : <EOL> return HttpResponse ( json . dumps ( { "<STR_LIT:error>" : "<STR_LIT>" } ) , content_type = '<STR_LIT>' ) <EOL> user = User . deleted_objects . filter ( email = BaseUserManager . normalize_email ( email ) ) <EOL> if ( user ) : <EOL> return HttpResponse ( json . dumps ( { "<STR_LIT:error>" : "<STR_LIT>" } ) , content_type = '<STR_LIT>' ) <EOL> user = User ( <EOL> email = BaseUserManager . normalize_email ( email ) , <EOL> firstname = firstname , <EOL> lastname = lastname , <EOL> is_active = False , <EOL> is_admin = False , <EOL> is_registering = True <EOL> ) <EOL> user . save ( ) <EOL> user . site = Site . objects . get ( name = site ) <EOL> user . save ( update_fields = [ '<STR_LIT>' ] ) <EOL> sitePriv = SitePrivilege . objects . filter ( site = user . site ) <EOL> userId = user . id <EOL> userUrl = "<STR_LIT>" + request . get_host ( ) + "<STR_LIT>" + str ( userId ) <EOL> for sp in sitePriv : <EOL> subject , from_email , to = '<STR_LIT>' , '<STR_LIT>' , str ( sp . user ) <EOL> text_content = '<STR_LIT>' <EOL> html_content = """<STR_LIT>""" + site + """<STR_LIT>""" + firstname + """<STR_LIT:U+0020>""" + lastname + """<STR_LIT>""" + email + """<STR_LIT>""" + userUrl + """<STR_LIT>""" <EOL> msg = EmailMultiAlternatives ( subject , text_content , from_email , [ to ] ) <EOL> msg . attach_alternative ( html_content , "<STR_LIT>" ) <EOL> msg . send ( ) <EOL> return HttpResponse ( serializers . serialize ( "<STR_LIT>" , [ user , ] ) , content_type = '<STR_LIT>' ) <EOL> class TenantCreateSlice ( View ) : <EOL> def post ( self , request , * args , ** kwargs ) : <EOL> if request . user . isReadOnlyUser ( ) : <EOL> return HttpResponseForbidden ( "<STR_LIT>" ) <EOL> sliceName = request . POST . get ( "<STR_LIT>" , "<STR_LIT:0>" ) <EOL> serviceClass = request . POST . get ( "<STR_LIT>" , "<STR_LIT:0>" ) <EOL> imageName = request . POST . get ( "<STR_LIT>" , "<STR_LIT:0>" ) <EOL> actionToDo = request . POST . get ( "<STR_LIT>" , "<STR_LIT:0>" ) <EOL> networkPorts = request . POST . get ( "<STR_LIT>" , "<STR_LIT:0>" ) <EOL> mountDataSets = request . POST . get ( "<STR_LIT>" , "<STR_LIT:0>" ) <EOL> privateVolume = request . POST . get ( "<STR_LIT>" , "<STR_LIT:0>" ) <EOL> userEmail = request . POST . get ( "<STR_LIT>" , "<STR_LIT:0>" ) <EOL> if ( actionToDo == "<STR_LIT>" ) : <EOL> serviceClass = ServiceClass . objects . get ( name = serviceClass ) <EOL> site = request . user . site <EOL> image = Image . objects . get ( name = imageName ) <EOL> newSlice = Slice ( name = sliceName , serviceClass = serviceClass , site = site , image_preference = image , mount_data_sets = mountDataSets ) <EOL> newSlice . save ( ) <EOL> privateTemplate = "<STR_LIT>" <EOL> publicTemplate = "<STR_LIT>" <EOL> privateNetworkName = sliceName + "<STR_LIT:->" + privateTemplate <EOL> publicNetworkName = sliceName + "<STR_LIT:->" + publicTemplate <EOL> slice = Slice . objects . get ( name = sliceName ) <EOL> addNetwork ( privateNetworkName , privateTemplate , slice ) <EOL> addNetwork ( publicNetworkName , publicTemplate , slice ) <EOL> addOrModifyPorts ( networkPorts , sliceName ) <EOL> if privateVolume == "<STR_LIT:true>" : <EOL> privateVolForSlice ( request . user , sliceName ) <EOL> slicePrivs = SlicePrivilege ( user = User . objects . get ( email = userEmail ) , slice = Slice . objects . get ( name = sliceName ) , role = SliceRole . objects . get ( role = "<STR_LIT>" ) ) <EOL> slicePrivs . save ( ) <EOL> return HttpResponse ( json . dumps ( "<STR_LIT>" ) , content_type = '<STR_LIT>' ) <EOL> class TenantAddUser ( View ) : <EOL> def post ( self , request , * args , ** kwargs ) : <EOL> if request . user . isReadOnlyUser ( ) : <EOL> return HttpResponseForbidden ( "<STR_LIT>" ) <EOL> sliceName = request . POST . get ( "<STR_LIT>" , "<STR_LIT:0>" ) <EOL> userEmail = request . POST . get ( "<STR_LIT>" , "<STR_LIT:0>" ) <EOL> slicePrivs = SlicePrivilege ( user = User . objects . get ( email = userEmail ) , slice = Slice . objects . get ( name = sliceName ) , role = SliceRole . objects . get ( role = "<STR_LIT>" ) ) <EOL> slicePrivs . save ( ) <EOL> return HttpResponse ( json . dumps ( "<STR_LIT>" ) , content_type = '<STR_LIT>' ) <EOL> def privateVolForSlice ( user , sliceName ) : <EOL> if not hasPrivateVolume ( sliceName ) : <EOL> volumeName = createPrivateVolume ( user , sliceName ) <EOL> readWrite = "<STR_LIT:true>" <EOL> mountVolume ( sliceName , volumeName , readWrite ) <EOL> class TenantUpdateSlice ( View ) : <EOL> def post ( self , request , * args , ** kwargs ) : <EOL> if request . user . isReadOnlyUser ( ) : <EOL> return HttpResponseForbidden ( "<STR_LIT>" ) <EOL> sliceName = request . POST . get ( "<STR_LIT>" , "<STR_LIT:0>" ) <EOL> serviceClass = request . POST . get ( "<STR_LIT>" , "<STR_LIT:0>" ) <EOL> imageName = request . POST . get ( "<STR_LIT>" , "<STR_LIT:0>" ) <EOL> actionToDo = request . POST . get ( "<STR_LIT>" , "<STR_LIT:0>" ) <EOL> networkPorts = request . POST . get ( "<STR_LIT>" , "<STR_LIT:0>" ) <EOL> dataSet = request . POST . get ( "<STR_LIT>" , "<STR_LIT:0>" ) <EOL> privateVolume = request . POST . get ( "<STR_LIT>" , "<STR_LIT:0>" ) <EOL> slice = Slice . objects . all ( ) <EOL> for entry in slice : <EOL> serviceClass = ServiceClass . objects . get ( name = serviceClass ) <EOL> if ( entry . name == sliceName ) : <EOL> if ( actionToDo == "<STR_LIT>" ) : <EOL> setattr ( entry , '<STR_LIT>' , serviceClass ) <EOL> setattr ( entry , '<STR_LIT>' , imageName ) <EOL> setattr ( entry , '<STR_LIT>' , dataSet ) <EOL> entry . save ( ) <EOL> break <EOL> addOrModifyPorts ( networkPorts , sliceName ) <EOL> if privateVolume == "<STR_LIT:true>" : <EOL> privateVolForSlice ( request . user , sliceName ) <EOL> return HttpResponse ( json . dumps ( "<STR_LIT>" ) , content_type = '<STR_LIT>' ) <EOL> def addNetwork ( name , template , sliceName ) : <EOL> networkTemplate = NetworkTemplate . objects . get ( name = template ) <EOL> newNetwork = Network ( name = name , <EOL> template = networkTemplate , <EOL> owner = sliceName ) <EOL> newNetwork . save ( ) <EOL> addNetworkSlice ( newNetwork , sliceName ) <EOL> def addNetworkSlice ( networkSlice , sliceName ) : <EOL> newNetworkSlice = NetworkSlice ( network = networkSlice , <EOL> slice = sliceName ) <EOL> newNetworkSlice . save ( ) <EOL> def addOrModifyPorts ( networkPorts , sliceName ) : <EOL> networkList = Network . objects . all ( ) <EOL> networkInfo = [ ] <EOL> if networkPorts : <EOL> for networkEntry in networkList : <EOL> networkSlices = networkEntry . slices . all ( ) <EOL> for slice in networkSlices : <EOL> if slice . name == sliceName : <EOL> if networkEntry . template . name == "<STR_LIT>" : <EOL> setattr ( networkEntry , '<STR_LIT>' , networkPorts ) <EOL> networkEntry . save ( ) <EOL> def getTenantSliceInfo ( user , tableFormat = False ) : <EOL> tenantSliceDetails = { } <EOL> tenantSliceData = getTenantInfo ( user ) <EOL> tenantServiceClassData = getServiceClassInfo ( user ) <EOL> if ( tableFormat ) : <EOL> tenantSliceDetails [ '<STR_LIT>' ] = userSliceTableFormatter ( tenantSliceData ) <EOL> tenantSliceDetails [ '<STR_LIT>' ] = userSliceTableFormatter ( tenantServiceClassData ) <EOL> else : <EOL> tenantSliceDetails [ '<STR_LIT>' ] = tenantSliceData <EOL> tenantSliceDetails [ '<STR_LIT>' ] = userSliceTableFormatter ( tenantServiceClassData ) <EOL> tenantSliceDetails [ '<STR_LIT:image>' ] = userSliceTableFormatter ( getImageInfo ( user ) ) <EOL> tenantSliceDetails [ '<STR_LIT>' ] = userSliceTableFormatter ( getDeploymentSites ( ) ) <EOL> tenantSliceDetails [ '<STR_LIT>' ] = userSliceTableFormatter ( getMountDataSets ( ) ) <EOL> tenantSliceDetails [ '<STR_LIT>' ] = getPublicKey ( user ) <EOL> tenantSliceDetails [ '<STR_LIT>' ] = userSliceTableFormatter ( getAvailableSites ( ) ) <EOL> tenantSliceDetails [ '<STR_LIT>' ] = getUserRole ( user ) <EOL> tenantSliceDetails [ '<STR_LIT>' ] = getSiteUsers ( user ) <EOL> return tenantSliceDetails <EOL> def getSiteUsers ( user ) : <EOL> users = User . objects . filter ( site = user . site ) <EOL> siteUsers = [ ] <EOL> for entry in users : <EOL> siteUsers . append ( str ( entry ) ) <EOL> return siteUsers <EOL> def getUserRole ( user ) : <EOL> sp = SitePrivilege . objects . filter ( user = user ) <EOL> for entry in sp : <EOL> return str ( entry . role ) <EOL> def getTenantInfo ( user ) : <EOL> slices = Slice . objects . all ( ) <EOL> userSliceInfo = [ ] <EOL> for entry in slices : <EOL> if ( entry . site == user . site ) : <EOL> sliceName = Slice . objects . get ( id = entry . id ) . name <EOL> slice = Slice . objects . get ( name = Slice . objects . get ( id = entry . id ) . name ) <EOL> sliceServiceClass = entry . serviceClass . name <EOL> preferredImage = entry . image_preference <EOL> sliceNetwork = { } <EOL> numInstance = <NUM_LIT:0> <EOL> sliceImage = "<STR_LIT>" <EOL> sliceSite = { } <EOL> sliceNode = { } <EOL> sliceInstance = { } <EOL> available_sites = getAvailableSites ( ) <EOL> for instance in slice . instances . all ( ) : <EOL> if instance . node . site . name in available_sites : <EOL> sliceSite [ instance . node . site . name ] = sliceSite . get ( instance . node . site . name , <NUM_LIT:0> ) + <NUM_LIT:1> <EOL> sliceImage = instance . image . name <EOL> sliceNode [ str ( instance ) ] = instance . node . name <EOL> numInstance = sum ( sliceSite . values ( ) ) <EOL> numSites = len ( sliceSite ) <EOL> userSliceInfo . append ( { '<STR_LIT>' : sliceName , '<STR_LIT>' : sliceServiceClass , '<STR_LIT>' : preferredImage , '<STR_LIT>' : numSites , '<STR_LIT>' : sliceSite , '<STR_LIT>' : sliceImage , '<STR_LIT>' : numInstance , '<STR_LIT>' : sliceNode } ) <EOL> return userSliceInfo <EOL> def getTenantSitesInfo ( ) : <EOL> availableSites = getAvailableSites ( ) <EOL> tenantSiteInfo = [ ] <EOL> for entry in Site . objects . all ( ) : <EOL> if entry . name in availableSites : <EOL> tenantSiteInfo . append ( { '<STR_LIT>' : entry . name } ) <EOL> return tenantSiteInfo <EOL> def getPublicKey ( user ) : <EOL> users = User . objects . all ( ) <EOL> for key in users : <EOL> if ( str ( key . email ) == str ( user ) ) : <EOL> sshKey = key . public_key <EOL> return sshKey <EOL> def getServiceClassInfo ( user ) : <EOL> serviceClassList = ServiceClass . objects . all ( ) <EOL> sliceInfo = [ ] <EOL> for entry in serviceClassList : <EOL> sliceInfo . append ( { '<STR_LIT>' : entry . name } ) <EOL> return sliceInfo <EOL> def getImageInfo ( user ) : <EOL> imageInfo = [ ] <EOL> tempImageInfo = [ ] <EOL> length = len ( BLESSED_DEPLOYMENTS ) <EOL> for deployment in Deployment . objects . all ( ) : <EOL> if deployment . name in BLESSED_DEPLOYMENTS : <EOL> for x in deployment . imagedeployments . all ( ) : <EOL> tempImageInfo . append ( x . image . name ) <EOL> temp = { } <EOL> for i in set ( tempImageInfo ) : <EOL> temp [ i ] = tempImageInfo . count ( i ) <EOL> for key in temp : <EOL> if temp [ key ] > <NUM_LIT:1> : <EOL> imageInfo . append ( key ) <EOL> return imageInfo <EOL> def createPrivateVolume ( user , sliceName ) : <EOL> caps = Volume . CAP_READ_DATA | Volume . CAP_WRITE_DATA | Volume . CAP_HOST_DATA <EOL> getattr ( Volume . default_gateway_caps , "<STR_LIT>" ) | getattr ( Volume . default_gateway_caps , "<STR_LIT>" ) | getattr ( Volume . default_gateway_caps , "<STR_LIT>" ) <EOL> v = Volume ( name = "<STR_LIT>" + sliceName , owner_id = user , description = "<STR_LIT>" % sliceName , blocksize = <NUM_LIT> , private = True , archive = False , default_gateway_caps = caps ) <EOL> v . save ( ) <EOL> return v <EOL> SYNDICATE_REPLICATE_PORTNUM = <NUM_LIT> <EOL> def get_free_port ( ) : <EOL> inuse = { } <EOL> inuse [ SYNDICATE_REPLICATE_PORTNUM ] = True <EOL> for vs in VolumeSlice . objects . all ( ) : <EOL> inuse [ vs . peer_portnum ] = True <EOL> inuse [ vs . replicate_portnum ] = True <EOL> for network in Network . objects . all ( ) : <EOL> if not network . ports : <EOL> continue <EOL> network_ports = [ x . strip ( ) for x in network . ports . split ( "<STR_LIT:U+002C>" ) ] <EOL> for network_port in network_ports : <EOL> try : <EOL> inuse [ int ( network_port ) ] = True <EOL> except : <EOL> pass <EOL> for i in range ( <NUM_LIT> , <NUM_LIT> ) : <EOL> if not inuse . get ( i , False ) : <EOL> return i <EOL> return False <EOL> def mountVolume ( sliceName , volumeName , readWrite ) : <EOL> slice = Slice . objects . get ( name = sliceName ) <EOL> volume = Volume . objects . get ( name = volumeName ) <EOL> flags = Volume . CAP_READ_DATA <EOL> if readWrite : <EOL> flags = flags | Volume . CAP_WRITE_DATA <EOL> vs = VolumeSlice ( volume_id = volume , slice_id = slice , gateway_caps = flags , peer_portnum = get_free_port ( ) , replicate_portnum = SYNDICATE_REPLICATE_PORTNUM ) <EOL> vs . save ( ) <EOL> def hasPrivateVolume ( sliceName ) : <EOL> slice = Slice . objects . get ( name = sliceName ) <EOL> for vs in VolumeSlice . objects . filter ( slice_id = slice ) : <EOL> if vs . volume_id . private : <EOL> return True <EOL> return False <EOL> def getMountDataSets ( ) : <EOL> dataSetInfo = [ ] <EOL> for volume in Volume . objects . all ( ) : <EOL> if not volume . private : <EOL> dataSetInfo . append ( { '<STR_LIT>' : volume . name } ) <EOL> return dataSetInfo <EOL> def getDeploymentSites ( ) : <EOL> deploymentList = Deployment . objects . all ( ) <EOL> deploymentInfo = [ ] <EOL> for entry in deploymentList : <EOL> deploymentInfo . append ( { '<STR_LIT>' : entry . name } ) <EOL> return deploymentInfo <EOL> def getAvailableSites ( ) : <EOL> available_sites = [ ] <EOL> for deployment in Deployment . objects . all ( ) : <EOL> if deployment . name in BLESSED_DEPLOYMENTS : <EOL> for x in deployment . sitedeployments . all ( ) : <EOL> if x . site . nodes . all ( ) : <EOL> available_sites . append ( x . site . name ) <EOL> return list ( set ( available_sites ) ) <EOL> class TenantDeleteSliceView ( View ) : <EOL> def post ( self , request ) : <EOL> if request . user . isReadOnlyUser ( ) : <EOL> return HttpResponseForbidden ( "<STR_LIT>" ) <EOL> sliceName = request . POST . get ( "<STR_LIT>" , None ) <EOL> slice = Slice . objects . get ( name = sliceName ) <EOL> sliceToDel = Slice ( name = sliceName , id = slice . id ) <EOL> sliceToDel . delete ( ) <EOL> return HttpResponse ( json . dumps ( "<STR_LIT>" ) , content_type = '<STR_LIT>' ) <EOL> class TenantAddOrRemoveInstanceView ( View ) : <EOL> """<STR_LIT>""" <EOL> def post ( self , request , * args , ** kwargs ) : <EOL> siteName = request . POST . get ( "<STR_LIT>" , None ) <EOL> actionToDo = request . POST . get ( "<STR_LIT>" , None ) <EOL> count = int ( request . POST . get ( "<STR_LIT:count>" , "<STR_LIT:0>" ) ) <EOL> sliceName = request . POST . get ( "<STR_LIT>" , None ) <EOL> imageName = request . POST . get ( "<STR_LIT:image>" , None ) <EOL> noAct = request . POST . get ( "<STR_LIT>" , False ) <EOL> if not sliceName : <EOL> return HttpResponseServerError ( "<STR_LIT>" ) <EOL> slice = Slice . objects . get ( name = sliceName ) <EOL> image = Image . objects . get ( name = imageName ) <EOL> if siteName : <EOL> siteList = [ Site . objects . get ( name = siteName ) ] <EOL> else : <EOL> siteList = None <EOL> if ( actionToDo == "<STR_LIT>" ) : <EOL> user_ip = request . GET . get ( "<STR_LIT>" , get_ip ( request ) ) <EOL> if ( siteList is None ) : <EOL> siteList = tenant_pick_sites ( user , user_ip , slice , count ) <EOL> sitesChanged = slice_increase_instances ( request . user , user_ip , siteList , slice , image , count , noAct ) <EOL> elif ( actionToDo == "<STR_LIT>" ) : <EOL> sitesChanged = slice_decrease_instances ( request . user , siteList , slice , count , noAct ) <EOL> else : <EOL> return HttpResponseServerError ( "<STR_LIT>" % actionToDo ) <EOL> return HttpResponse ( json . dumps ( sitesChanged ) , content_type = '<STR_LIT>' ) <EOL> def get ( self , request , * args , ** kwargs ) : <EOL> request . POST = request . GET <EOL> return self . post ( request , * args , ** kwargs ) <EOL> class TenantPickSitesView ( View ) : <EOL> """<STR_LIT>""" <EOL> def get ( self , request , * args , ** kwargs ) : <EOL> count = request . GET . get ( "<STR_LIT:count>" , "<STR_LIT:0>" ) <EOL> slice = request . GET . get ( "<STR_LIT>" , None ) <EOL> if slice : <EOL> slice = Slice . objects . get ( name = slice ) <EOL> ip = request . GET . get ( "<STR_LIT>" , get_ip ( request ) ) <EOL> sites = tenant_pick_sites ( request . user , user_ip = ip , count = <NUM_LIT:0> , slice = slice ) <EOL> sites = [ x . name for x in sites ] <EOL> return HttpResponse ( json . dumps ( sites ) , content_type = '<STR_LIT>' ) <EOL> def siteSortKey ( site , slice = None , count = None , lat = None , lon = None ) : <EOL> has_instances_here = False <EOL> if slice : <EOL> for instance in slice . instances . all ( ) : <EOL> if instance . node . site . name == site . name : <EOL> has_instances_here = True <EOL> d = haversine ( site . location . latitude , site . location . longitude , lat , lon ) <EOL> return ( - has_instances_here , d ) <EOL> def tenant_pick_sites ( user , user_ip = None , slice = None , count = None ) : <EOL> """<STR_LIT>""" <EOL> lat = None <EOL> lon = None <EOL> try : <EOL> client_geo = GeoIP ( ) . city ( user_ip ) <EOL> if client_geo : <EOL> lat = float ( client_geo [ "<STR_LIT>" ] ) <EOL> lon = float ( client_geo [ "<STR_LIT>" ] ) <EOL> except : <EOL> print "<STR_LIT>" <EOL> traceback . print_exc ( ) <EOL> available_sites = getAvailableSites ( ) <EOL> sites = Site . objects . all ( ) <EOL> sites = [ x for x in sites if x . name in available_sites ] <EOL> sites = sorted ( sites , key = functools . partial ( siteSortKey , slice = slice , count = count , lat = lat , lon = lon ) ) <EOL> return sites <EOL> class TenantViewData ( View ) : <EOL> def get ( self , request , ** kwargs ) : <EOL> return HttpResponse ( json . dumps ( getTenantSliceInfo ( request . user , True ) ) , content_type = '<STR_LIT>' ) <EOL> class RequestAccountView ( View ) : <EOL> def get ( self , request , ** kwargs ) : <EOL> return HttpResponse ( ) </s>
