<s> """<STR_LIT>""" <EOL> import base64 <EOL> import netaddr <EOL> import urllib <EOL> from nova import compute <EOL> from nova import db <EOL> from nova import exception <EOL> from nova import flags <EOL> from nova import log as logging <EOL> from nova import utils <EOL> from nova . api . ec2 import ec2utils <EOL> from nova . auth import manager <EOL> from nova . compute import vm_states <EOL> FLAGS = flags . FLAGS <EOL> LOG = logging . getLogger ( '<STR_LIT>' ) <EOL> def user_dict ( user , base64_file = None ) : <EOL> """<STR_LIT>""" <EOL> if user : <EOL> return { <EOL> '<STR_LIT:username>' : user . id , <EOL> '<STR_LIT>' : user . access , <EOL> '<STR_LIT>' : user . secret , <EOL> '<STR_LIT:file>' : base64_file } <EOL> else : <EOL> return { } <EOL> def project_dict ( project ) : <EOL> """<STR_LIT>""" <EOL> if project : <EOL> return { <EOL> '<STR_LIT>' : project . id , <EOL> '<STR_LIT>' : project . project_manager_id , <EOL> '<STR_LIT:description>' : project . description } <EOL> else : <EOL> return { } <EOL> def host_dict ( host , compute_service , instances , volume_service , volumes , now ) : <EOL> """<STR_LIT>""" <EOL> rv = { '<STR_LIT>' : host , '<STR_LIT>' : len ( instances ) , <EOL> '<STR_LIT>' : len ( volumes ) } <EOL> if compute_service : <EOL> latest = compute_service [ '<STR_LIT>' ] or compute_service [ '<STR_LIT>' ] <EOL> delta = now - latest <EOL> if delta . seconds <= FLAGS . service_down_time : <EOL> rv [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> else : <EOL> rv [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> if volume_service : <EOL> latest = volume_service [ '<STR_LIT>' ] or volume_service [ '<STR_LIT>' ] <EOL> delta = now - latest <EOL> if delta . seconds <= FLAGS . service_down_time : <EOL> rv [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> else : <EOL> rv [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> return rv <EOL> def instance_dict ( inst ) : <EOL> return { '<STR_LIT:name>' : inst [ '<STR_LIT:name>' ] , <EOL> '<STR_LIT>' : inst [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : inst [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : inst [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : inst [ '<STR_LIT>' ] } <EOL> def vpn_dict ( project , vpn_instance ) : <EOL> rv = { '<STR_LIT>' : project . id , <EOL> '<STR_LIT>' : project . vpn_ip , <EOL> '<STR_LIT>' : project . vpn_port } <EOL> if vpn_instance : <EOL> rv [ '<STR_LIT>' ] = ec2utils . id_to_ec2_id ( vpn_instance [ '<STR_LIT:id>' ] ) <EOL> rv [ '<STR_LIT>' ] = utils . isotime ( vpn_instance [ '<STR_LIT>' ] ) <EOL> address = vpn_instance . get ( '<STR_LIT>' , None ) <EOL> if address : <EOL> rv [ '<STR_LIT>' ] = address [ '<STR_LIT:address>' ] <EOL> if project . vpn_ip and project . vpn_port : <EOL> if utils . vpn_ping ( project . vpn_ip , project . vpn_port ) : <EOL> rv [ '<STR_LIT:state>' ] = '<STR_LIT>' <EOL> else : <EOL> rv [ '<STR_LIT:state>' ] = '<STR_LIT>' <EOL> else : <EOL> rv [ '<STR_LIT:state>' ] = '<STR_LIT>' <EOL> else : <EOL> rv [ '<STR_LIT:state>' ] = '<STR_LIT>' <EOL> return rv <EOL> class AdminController ( object ) : <EOL> """<STR_LIT>""" <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT>' <EOL> def __init__ ( self ) : <EOL> self . compute_api = compute . API ( ) <EOL> def describe_instance_types ( self , context , ** _kwargs ) : <EOL> """<STR_LIT>""" <EOL> return { '<STR_LIT>' : [ instance_dict ( v ) for v in <EOL> db . instance_type_get_all ( context ) . values ( ) ] } <EOL> def describe_user ( self , _context , name , ** _kwargs ) : <EOL> """<STR_LIT>""" <EOL> return user_dict ( manager . AuthManager ( ) . get_user ( name ) ) <EOL> def describe_users ( self , _context , ** _kwargs ) : <EOL> """<STR_LIT>""" <EOL> return { '<STR_LIT>' : <EOL> [ user_dict ( u ) for u in manager . AuthManager ( ) . get_users ( ) ] } <EOL> def register_user ( self , context , name , ** _kwargs ) : <EOL> """<STR_LIT>""" <EOL> LOG . audit ( _ ( "<STR_LIT>" ) , name , context = context ) <EOL> return user_dict ( manager . AuthManager ( ) . create_user ( name ) ) <EOL> def deregister_user ( self , context , name , ** _kwargs ) : <EOL> """<STR_LIT>""" <EOL> LOG . audit ( _ ( "<STR_LIT>" ) , name , context = context ) <EOL> manager . AuthManager ( ) . delete_user ( name ) <EOL> return True <EOL> def describe_roles ( self , context , project_roles = True , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> roles = manager . AuthManager ( ) . get_roles ( project_roles ) <EOL> return { '<STR_LIT>' : [ { '<STR_LIT>' : r } for r in roles ] } <EOL> def describe_user_roles ( self , context , user , project = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> roles = manager . AuthManager ( ) . get_user_roles ( user , project = project ) <EOL> return { '<STR_LIT>' : [ { '<STR_LIT>' : r } for r in roles ] } <EOL> def modify_user_role ( self , context , user , role , project = None , <EOL> operation = '<STR_LIT>' , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if operation == '<STR_LIT>' : <EOL> if project : <EOL> msg = _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) % locals ( ) <EOL> LOG . audit ( msg , context = context ) <EOL> else : <EOL> msg = _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) % locals ( ) <EOL> LOG . audit ( msg , context = context ) <EOL> manager . AuthManager ( ) . add_role ( user , role , project ) <EOL> elif operation == '<STR_LIT>' : <EOL> if project : <EOL> msg = _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) % locals ( ) <EOL> LOG . audit ( msg , context = context ) <EOL> else : <EOL> msg = _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) % locals ( ) <EOL> LOG . audit ( msg , context = context ) <EOL> manager . AuthManager ( ) . remove_role ( user , role , project ) <EOL> else : <EOL> raise exception . ApiError ( _ ( '<STR_LIT>' ) ) <EOL> return True <EOL> def generate_x509_for_user ( self , context , name , project = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if project is None : <EOL> project = name <EOL> project = manager . AuthManager ( ) . get_project ( project ) <EOL> user = manager . AuthManager ( ) . get_user ( name ) <EOL> msg = _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) % locals ( ) <EOL> LOG . audit ( msg , context = context ) <EOL> return user_dict ( user , base64 . b64encode ( project . get_credentials ( user ) ) ) <EOL> def describe_project ( self , context , name , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> return project_dict ( manager . AuthManager ( ) . get_project ( name ) ) <EOL> def describe_projects ( self , context , user = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> return { '<STR_LIT>' : <EOL> [ project_dict ( u ) for u in <EOL> manager . AuthManager ( ) . get_projects ( user = user ) ] } <EOL> def register_project ( self , context , name , manager_user , description = None , <EOL> member_users = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> msg = _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) % locals ( ) <EOL> LOG . audit ( msg , context = context ) <EOL> return project_dict ( <EOL> manager . AuthManager ( ) . create_project ( <EOL> name , <EOL> manager_user , <EOL> description = None , <EOL> member_users = None ) ) <EOL> def modify_project ( self , context , name , manager_user , description = None , <EOL> ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> msg = _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) % locals ( ) <EOL> LOG . audit ( msg , context = context ) <EOL> manager . AuthManager ( ) . modify_project ( name , <EOL> manager_user = manager_user , <EOL> description = description ) <EOL> return True <EOL> def deregister_project ( self , context , name ) : <EOL> """<STR_LIT>""" <EOL> LOG . audit ( _ ( "<STR_LIT>" ) , name , context = context ) <EOL> manager . AuthManager ( ) . delete_project ( name ) <EOL> return True <EOL> def describe_project_members ( self , context , name , ** kwargs ) : <EOL> project = manager . AuthManager ( ) . get_project ( name ) <EOL> result = { <EOL> '<STR_LIT>' : [ { '<STR_LIT>' : m } for m in project . member_ids ] } <EOL> return result <EOL> def modify_project_member ( self , context , user , project , operation , <EOL> ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if operation == '<STR_LIT>' : <EOL> msg = _ ( "<STR_LIT>" ) % locals ( ) <EOL> LOG . audit ( msg , context = context ) <EOL> manager . AuthManager ( ) . add_to_project ( user , project ) <EOL> elif operation == '<STR_LIT>' : <EOL> msg = _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) % locals ( ) <EOL> LOG . audit ( msg , context = context ) <EOL> manager . AuthManager ( ) . remove_from_project ( user , project ) <EOL> else : <EOL> raise exception . ApiError ( _ ( '<STR_LIT>' ) ) <EOL> return True <EOL> def _vpn_for ( self , context , project_id ) : <EOL> """<STR_LIT>""" <EOL> for instance in db . instance_get_all_by_project ( context , project_id ) : <EOL> if ( instance [ '<STR_LIT>' ] == str ( FLAGS . vpn_image_id ) <EOL> and not instance [ '<STR_LIT>' ] in [ vm_states . DELETED ] ) : <EOL> return instance <EOL> def start_vpn ( self , context , project ) : <EOL> instance = self . _vpn_for ( context , project ) <EOL> if not instance : <EOL> from nova . cloudpipe import pipelib <EOL> pipe = pipelib . CloudPipe ( ) <EOL> proj = manager . AuthManager ( ) . get_project ( project ) <EOL> user_id = proj . project_manager_id <EOL> try : <EOL> pipe . launch_vpn_instance ( project , user_id ) <EOL> except db . NoMoreNetworks : <EOL> raise exception . ApiError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> instance = self . _vpn_for ( context , project ) <EOL> return { '<STR_LIT>' : ec2utils . id_to_ec2_id ( instance [ '<STR_LIT:id>' ] ) } <EOL> def describe_vpns ( self , context ) : <EOL> vpns = [ ] <EOL> for project in manager . AuthManager ( ) . get_projects ( ) : <EOL> instance = self . _vpn_for ( context , project . id ) <EOL> vpns . append ( vpn_dict ( project , instance ) ) <EOL> return { '<STR_LIT>' : vpns } <EOL> def describe_hosts ( self , context , ** _kwargs ) : <EOL> """<STR_LIT>""" <EOL> services = db . service_get_all ( context , False ) <EOL> now = utils . utcnow ( ) <EOL> hosts = [ ] <EOL> rv = [ ] <EOL> for host in [ service [ '<STR_LIT:host>' ] for service in services ] : <EOL> if not host in hosts : <EOL> hosts . append ( host ) <EOL> for host in hosts : <EOL> compute = [ s for s in services if s [ '<STR_LIT:host>' ] == host and s [ '<STR_LIT>' ] == '<STR_LIT>' ] <EOL> if compute : <EOL> compute = compute [ <NUM_LIT:0> ] <EOL> instances = db . instance_get_all_by_host ( context , host ) <EOL> volume = [ s for s in services if s [ '<STR_LIT:host>' ] == host and s [ '<STR_LIT>' ] == '<STR_LIT>' ] <EOL> if volume : <EOL> volume = volume [ <NUM_LIT:0> ] <EOL> volumes = db . volume_get_all_by_host ( context , host ) <EOL> rv . append ( host_dict ( host , compute , instances , volume , volumes , <EOL> now ) ) <EOL> return { '<STR_LIT>' : rv } <EOL> def _provider_fw_rule_exists ( self , context , rule ) : <EOL> for old_rule in db . provider_fw_rule_get_all ( context ) : <EOL> if all ( [ rule [ k ] == old_rule [ k ] for k in ( '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ) ] ) : <EOL> return True <EOL> return False <EOL> def block_external_addresses ( self , context , cidr ) : <EOL> """<STR_LIT>""" <EOL> LOG . audit ( _ ( '<STR_LIT>' ) , <EOL> cidr , context = context ) <EOL> cidr = urllib . unquote ( cidr ) . decode ( ) <EOL> netaddr . IPNetwork ( cidr ) <EOL> rule = { '<STR_LIT>' : cidr } <EOL> tcp_rule = rule . copy ( ) <EOL> tcp_rule . update ( { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT>' : <NUM_LIT> } ) <EOL> udp_rule = rule . copy ( ) <EOL> udp_rule . update ( { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT>' : <NUM_LIT> } ) <EOL> icmp_rule = rule . copy ( ) <EOL> icmp_rule . update ( { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : - <NUM_LIT:1> , <EOL> '<STR_LIT>' : None } ) <EOL> rules_added = <NUM_LIT:0> <EOL> if not self . _provider_fw_rule_exists ( context , tcp_rule ) : <EOL> db . provider_fw_rule_create ( context , tcp_rule ) <EOL> rules_added += <NUM_LIT:1> <EOL> if not self . _provider_fw_rule_exists ( context , udp_rule ) : <EOL> db . provider_fw_rule_create ( context , udp_rule ) <EOL> rules_added += <NUM_LIT:1> <EOL> if not self . _provider_fw_rule_exists ( context , icmp_rule ) : <EOL> db . provider_fw_rule_create ( context , icmp_rule ) <EOL> rules_added += <NUM_LIT:1> <EOL> if not rules_added : <EOL> raise exception . ApiError ( _ ( '<STR_LIT>' ) ) <EOL> self . compute_api . trigger_provider_fw_rules_refresh ( context ) <EOL> return { '<STR_LIT:status>' : '<STR_LIT:OK>' , '<STR_LIT:message>' : '<STR_LIT>' % rules_added } <EOL> def describe_external_address_blocks ( self , context ) : <EOL> blocks = db . provider_fw_rule_get_all ( context ) <EOL> blocks = set ( [ b . cidr for b in blocks ] ) <EOL> blocks = [ { '<STR_LIT>' : b } for b in blocks ] <EOL> return { '<STR_LIT>' : <EOL> list ( sorted ( blocks , key = lambda k : k [ '<STR_LIT>' ] ) ) } <EOL> def remove_external_address_block ( self , context , cidr ) : <EOL> LOG . audit ( _ ( '<STR_LIT>' ) , cidr , context = context ) <EOL> cidr = urllib . unquote ( cidr ) . decode ( ) <EOL> netaddr . IPNetwork ( cidr ) <EOL> rules = db . provider_fw_rule_get_all_by_cidr ( context , cidr ) <EOL> for rule in rules : <EOL> db . provider_fw_rule_destroy ( context , rule [ '<STR_LIT:id>' ] ) <EOL> if rules : <EOL> self . compute_api . trigger_provider_fw_rules_refresh ( context ) <EOL> return { '<STR_LIT:status>' : '<STR_LIT:OK>' , '<STR_LIT:message>' : '<STR_LIT>' % len ( rules ) } </s>
