<s> import base64 <EOL> from webob import exc <EOL> from xml . dom import minidom <EOL> from nova import exception <EOL> from nova import flags <EOL> from nova import log as logging <EOL> import nova . image <EOL> from nova import quota <EOL> from nova import utils <EOL> from nova . compute import instance_types <EOL> from nova . api . openstack import common <EOL> from nova . api . openstack import wsgi <EOL> from nova . rpc . common import RemoteError <EOL> LOG = logging . getLogger ( '<STR_LIT>' ) <EOL> FLAGS = flags . FLAGS <EOL> class CreateFault ( exception . NovaException ) : <EOL> message = _ ( "<STR_LIT>" ) <EOL> def __init__ ( self , fault ) : <EOL> self . fault = fault <EOL> super ( CreateFault , self ) . __init__ ( ) <EOL> class CreateInstanceHelper ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , controller ) : <EOL> """<STR_LIT>""" <EOL> self . controller = controller <EOL> self . _image_service = utils . import_object ( FLAGS . image_service ) <EOL> super ( CreateInstanceHelper , self ) . __init__ ( ) <EOL> def create_instance ( self , req , body , create_method ) : <EOL> """<STR_LIT>""" <EOL> if not body : <EOL> raise exc . HTTPUnprocessableEntity ( ) <EOL> if not '<STR_LIT>' in body : <EOL> raise exc . HTTPUnprocessableEntity ( ) <EOL> context = req . environ [ '<STR_LIT>' ] <EOL> server_dict = body [ '<STR_LIT>' ] <EOL> password = self . controller . _get_server_admin_password ( server_dict ) <EOL> if not '<STR_LIT:name>' in server_dict : <EOL> msg = _ ( "<STR_LIT>" ) <EOL> raise exc . HTTPBadRequest ( explanation = msg ) <EOL> name = server_dict [ '<STR_LIT:name>' ] <EOL> self . _validate_server_name ( name ) <EOL> name = name . strip ( ) <EOL> image_href = self . controller . _image_ref_from_req_data ( body ) <EOL> if str ( image_href ) . startswith ( req . application_url ) : <EOL> image_href = image_href . split ( '<STR_LIT:/>' ) . pop ( ) <EOL> try : <EOL> image_service , image_id = nova . image . get_image_service ( context , <EOL> image_href ) <EOL> kernel_id , ramdisk_id = self . _get_kernel_ramdisk_from_image ( <EOL> req , image_service , image_id ) <EOL> images = set ( [ str ( x [ '<STR_LIT:id>' ] ) for x in image_service . index ( context ) ] ) <EOL> assert str ( image_id ) in images <EOL> except Exception , e : <EOL> msg = _ ( "<STR_LIT>" % <EOL> locals ( ) ) <EOL> raise exc . HTTPBadRequest ( explanation = msg ) <EOL> personality = server_dict . get ( '<STR_LIT>' ) <EOL> config_drive = server_dict . get ( '<STR_LIT>' ) <EOL> injected_files = [ ] <EOL> if personality : <EOL> injected_files = self . _get_injected_files ( personality ) <EOL> sg_names = [ ] <EOL> security_groups = server_dict . get ( '<STR_LIT>' ) <EOL> if security_groups is not None : <EOL> sg_names = [ sg [ '<STR_LIT:name>' ] for sg in security_groups if sg . get ( '<STR_LIT:name>' ) ] <EOL> if not sg_names : <EOL> sg_names . append ( '<STR_LIT:default>' ) <EOL> sg_names = list ( set ( sg_names ) ) <EOL> requested_networks = server_dict . get ( '<STR_LIT>' ) <EOL> if requested_networks is not None : <EOL> requested_networks = self . _get_requested_networks ( <EOL> requested_networks ) <EOL> try : <EOL> flavor_id = self . controller . _flavor_id_from_req_data ( body ) <EOL> except ValueError as error : <EOL> msg = _ ( "<STR_LIT>" ) <EOL> raise exc . HTTPBadRequest ( explanation = msg ) <EOL> zone_blob = server_dict . get ( '<STR_LIT>' ) <EOL> key_name = server_dict . get ( '<STR_LIT>' ) <EOL> user_data = server_dict . get ( '<STR_LIT>' ) <EOL> self . _validate_user_data ( user_data ) <EOL> availability_zone = server_dict . get ( '<STR_LIT>' ) <EOL> name = server_dict [ '<STR_LIT:name>' ] <EOL> self . _validate_server_name ( name ) <EOL> name = name . strip ( ) <EOL> reservation_id = server_dict . get ( '<STR_LIT>' ) <EOL> min_count = server_dict . get ( '<STR_LIT>' ) <EOL> max_count = server_dict . get ( '<STR_LIT>' ) <EOL> min_count = int ( min_count ) if min_count else <NUM_LIT:1> <EOL> max_count = int ( max_count ) if max_count else min_count <EOL> if min_count > max_count : <EOL> min_count = max_count <EOL> try : <EOL> inst_type = instance_types . get_instance_type_by_flavor_id ( flavor_id ) <EOL> extra_values = { <EOL> '<STR_LIT>' : inst_type , <EOL> '<STR_LIT>' : image_href , <EOL> '<STR_LIT>' : config_drive , <EOL> '<STR_LIT:password>' : password } <EOL> return ( extra_values , <EOL> create_method ( context , <EOL> inst_type , <EOL> image_id , <EOL> kernel_id = kernel_id , <EOL> ramdisk_id = ramdisk_id , <EOL> display_name = name , <EOL> display_description = name , <EOL> key_name = key_name , <EOL> metadata = server_dict . get ( '<STR_LIT>' , { } ) , <EOL> access_ip_v4 = server_dict . get ( '<STR_LIT>' ) , <EOL> access_ip_v6 = server_dict . get ( '<STR_LIT>' ) , <EOL> injected_files = injected_files , <EOL> admin_password = password , <EOL> zone_blob = zone_blob , <EOL> reservation_id = reservation_id , <EOL> min_count = min_count , <EOL> max_count = max_count , <EOL> requested_networks = requested_networks , <EOL> security_group = sg_names , <EOL> user_data = user_data , <EOL> availability_zone = availability_zone , <EOL> config_drive = config_drive , ) ) <EOL> except quota . QuotaError as error : <EOL> self . _handle_quota_error ( error ) <EOL> except exception . ImageNotFound as error : <EOL> msg = _ ( "<STR_LIT>" ) <EOL> raise exc . HTTPBadRequest ( explanation = msg ) <EOL> except exception . FlavorNotFound as error : <EOL> msg = _ ( "<STR_LIT>" ) <EOL> raise exc . HTTPBadRequest ( explanation = msg ) <EOL> except exception . KeypairNotFound as error : <EOL> msg = _ ( "<STR_LIT>" ) <EOL> raise exc . HTTPBadRequest ( explanation = msg ) <EOL> except exception . SecurityGroupNotFound as error : <EOL> raise exc . HTTPBadRequest ( explanation = unicode ( error ) ) <EOL> except RemoteError as err : <EOL> msg = "<STR_LIT>" % { '<STR_LIT>' : err . exc_type , '<STR_LIT>' : err . value } <EOL> raise exc . HTTPBadRequest ( explanation = msg ) <EOL> def _handle_quota_error ( self , error ) : <EOL> """<STR_LIT>""" <EOL> if error . code == "<STR_LIT>" : <EOL> expl = _ ( "<STR_LIT>" ) <EOL> raise exc . HTTPRequestEntityTooLarge ( explanation = error . message , <EOL> headers = { '<STR_LIT>' : <NUM_LIT:0> } ) <EOL> if error . code == "<STR_LIT>" : <EOL> expl = _ ( "<STR_LIT>" ) <EOL> raise exc . HTTPRequestEntityTooLarge ( explanation = error . message , <EOL> headers = { '<STR_LIT>' : <NUM_LIT:0> } ) <EOL> if error . code == "<STR_LIT>" : <EOL> expl = _ ( "<STR_LIT>" ) <EOL> raise exc . HTTPRequestEntityTooLarge ( explanation = error . message , <EOL> headers = { '<STR_LIT>' : <NUM_LIT:0> } ) <EOL> if error . code == "<STR_LIT>" : <EOL> expl = _ ( "<STR_LIT>" ) <EOL> raise exc . HTTPRequestEntityTooLarge ( explanation = error . message , <EOL> headers = { '<STR_LIT>' : <NUM_LIT:0> } ) <EOL> raise error <EOL> def _deserialize_create ( self , request ) : <EOL> """<STR_LIT>""" <EOL> if request . content_type == "<STR_LIT>" : <EOL> deserializer = ServerXMLDeserializer ( ) <EOL> return deserializer . deserialize ( request . body ) <EOL> else : <EOL> return self . _deserialize ( request . body , request . get_content_type ( ) ) <EOL> def _validate_server_name ( self , value ) : <EOL> if not isinstance ( value , basestring ) : <EOL> msg = _ ( "<STR_LIT>" ) <EOL> raise exc . HTTPBadRequest ( explanation = msg ) <EOL> if value . strip ( ) == '<STR_LIT>' : <EOL> msg = _ ( "<STR_LIT>" ) <EOL> raise exc . HTTPBadRequest ( explanation = msg ) <EOL> def _get_kernel_ramdisk_from_image ( self , req , image_service , image_id ) : <EOL> """<STR_LIT>""" <EOL> context = req . environ [ '<STR_LIT>' ] <EOL> image_meta = image_service . show ( context , image_id ) <EOL> kernel_id , ramdisk_id = self . _do_get_kernel_ramdisk_from_image ( <EOL> image_meta ) <EOL> return kernel_id , ramdisk_id <EOL> @ staticmethod <EOL> def _do_get_kernel_ramdisk_from_image ( image_meta ) : <EOL> """<STR_LIT>""" <EOL> image_id = image_meta [ '<STR_LIT:id>' ] <EOL> if image_meta [ '<STR_LIT:status>' ] != '<STR_LIT>' : <EOL> raise exception . ImageUnacceptable ( image_id = image_id , <EOL> reason = _ ( "<STR_LIT>" ) ) <EOL> if image_meta . get ( '<STR_LIT>' ) != '<STR_LIT>' : <EOL> return None , None <EOL> try : <EOL> kernel_id = image_meta [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> except KeyError : <EOL> raise exception . KernelNotFoundForImage ( image_id = image_id ) <EOL> try : <EOL> ramdisk_id = image_meta [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> except KeyError : <EOL> ramdisk_id = None <EOL> return kernel_id , ramdisk_id <EOL> def _get_injected_files ( self , personality ) : <EOL> """<STR_LIT>""" <EOL> injected_files = [ ] <EOL> for item in personality : <EOL> try : <EOL> path = item [ '<STR_LIT:path>' ] <EOL> contents = item [ '<STR_LIT>' ] <EOL> except KeyError as key : <EOL> expl = _ ( '<STR_LIT>' ) % key <EOL> raise exc . HTTPBadRequest ( explanation = expl ) <EOL> except TypeError : <EOL> expl = _ ( '<STR_LIT>' ) <EOL> raise exc . HTTPBadRequest ( explanation = expl ) <EOL> try : <EOL> contents = base64 . b64decode ( contents ) <EOL> except TypeError : <EOL> expl = _ ( '<STR_LIT>' ) % path <EOL> raise exc . HTTPBadRequest ( explanation = expl ) <EOL> injected_files . append ( ( path , contents ) ) <EOL> return injected_files <EOL> def _get_server_admin_password_old_style ( self , server ) : <EOL> """<STR_LIT>""" <EOL> return utils . generate_password ( <NUM_LIT:16> ) <EOL> def _get_server_admin_password_new_style ( self , server ) : <EOL> """<STR_LIT>""" <EOL> password = server . get ( '<STR_LIT>' ) <EOL> if password is None : <EOL> return utils . generate_password ( <NUM_LIT:16> ) <EOL> if not isinstance ( password , basestring ) or password == '<STR_LIT>' : <EOL> msg = _ ( "<STR_LIT>" ) <EOL> raise exc . HTTPBadRequest ( explanation = msg ) <EOL> return password <EOL> def _get_requested_networks ( self , requested_networks ) : <EOL> """<STR_LIT>""" <EOL> networks = [ ] <EOL> for network in requested_networks : <EOL> try : <EOL> network_uuid = network [ '<STR_LIT>' ] <EOL> if not utils . is_uuid_like ( network_uuid ) : <EOL> msg = _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) % network_uuid <EOL> raise exc . HTTPBadRequest ( explanation = msg ) <EOL> address = network . get ( '<STR_LIT>' , None ) <EOL> if address is not None and not utils . is_valid_ipv4 ( address ) : <EOL> msg = _ ( "<STR_LIT>" ) % address <EOL> raise exc . HTTPBadRequest ( explanation = msg ) <EOL> for id , ip in networks : <EOL> if id == network_uuid : <EOL> expl = _ ( "<STR_LIT>" ) % network_uuid <EOL> raise exc . HTTPBadRequest ( explanation = expl ) <EOL> networks . append ( ( network_uuid , address ) ) <EOL> except KeyError as key : <EOL> expl = _ ( '<STR_LIT>' ) % key <EOL> raise exc . HTTPBadRequest ( explanation = expl ) <EOL> except TypeError : <EOL> expl = _ ( '<STR_LIT>' ) <EOL> raise exc . HTTPBadRequest ( explanation = expl ) <EOL> return networks <EOL> def _validate_user_data ( self , user_data ) : <EOL> """<STR_LIT>""" <EOL> if not user_data : <EOL> return <EOL> try : <EOL> user_data = base64 . b64decode ( user_data ) <EOL> except TypeError : <EOL> expl = _ ( '<STR_LIT>' ) <EOL> raise exc . HTTPBadRequest ( explanation = expl ) <EOL> class ServerXMLDeserializer ( wsgi . XMLDeserializer ) : <EOL> """<STR_LIT>""" <EOL> metadata_deserializer = common . MetadataXMLDeserializer ( ) <EOL> def create ( self , string ) : <EOL> """<STR_LIT>""" <EOL> dom = minidom . parseString ( string ) <EOL> server = self . _extract_server ( dom ) <EOL> return { '<STR_LIT:body>' : { '<STR_LIT>' : server } } <EOL> def _extract_server ( self , node ) : <EOL> """<STR_LIT>""" <EOL> server = { } <EOL> server_node = self . find_first_child_named ( node , '<STR_LIT>' ) <EOL> attributes = [ "<STR_LIT:name>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> for attr in attributes : <EOL> if server_node . getAttribute ( attr ) : <EOL> server [ attr ] = server_node . getAttribute ( attr ) <EOL> metadata_node = self . find_first_child_named ( server_node , "<STR_LIT>" ) <EOL> server [ "<STR_LIT>" ] = self . metadata_deserializer . extract_metadata ( <EOL> metadata_node ) <EOL> server [ "<STR_LIT>" ] = self . _extract_personality ( server_node ) <EOL> return server <EOL> def _extract_personality ( self , server_node ) : <EOL> """<STR_LIT>""" <EOL> node = self . find_first_child_named ( server_node , "<STR_LIT>" ) <EOL> personality = [ ] <EOL> if node is not None : <EOL> for file_node in self . find_children_named ( node , "<STR_LIT:file>" ) : <EOL> item = { } <EOL> if file_node . hasAttribute ( "<STR_LIT:path>" ) : <EOL> item [ "<STR_LIT:path>" ] = file_node . getAttribute ( "<STR_LIT:path>" ) <EOL> item [ "<STR_LIT>" ] = self . extract_text ( file_node ) <EOL> personality . append ( item ) <EOL> return personality <EOL> class ServerXMLDeserializerV11 ( wsgi . MetadataXMLDeserializer ) : <EOL> """<STR_LIT>""" <EOL> metadata_deserializer = common . MetadataXMLDeserializer ( ) <EOL> def action ( self , string ) : <EOL> dom = minidom . parseString ( string ) <EOL> action_node = dom . childNodes [ <NUM_LIT:0> ] <EOL> action_name = action_node . tagName <EOL> action_deserializer = { <EOL> '<STR_LIT>' : self . _action_create_image , <EOL> '<STR_LIT>' : self . _action_create_backup , <EOL> '<STR_LIT>' : self . _action_change_password , <EOL> '<STR_LIT>' : self . _action_reboot , <EOL> '<STR_LIT>' : self . _action_rebuild , <EOL> '<STR_LIT>' : self . _action_resize , <EOL> '<STR_LIT>' : self . _action_confirm_resize , <EOL> '<STR_LIT>' : self . _action_revert_resize , <EOL> } . get ( action_name , self . default ) <EOL> action_data = action_deserializer ( action_node ) <EOL> return { '<STR_LIT:body>' : { action_name : action_data } } <EOL> def _action_create_image ( self , node ) : <EOL> return self . _deserialize_image_action ( node , ( '<STR_LIT:name>' , ) ) <EOL> def _action_create_backup ( self , node ) : <EOL> attributes = ( '<STR_LIT:name>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> return self . _deserialize_image_action ( node , attributes ) <EOL> def _action_change_password ( self , node ) : <EOL> if not node . hasAttribute ( "<STR_LIT>" ) : <EOL> raise AttributeError ( "<STR_LIT>" ) <EOL> return { "<STR_LIT>" : node . getAttribute ( "<STR_LIT>" ) } <EOL> def _action_reboot ( self , node ) : <EOL> if not node . hasAttribute ( "<STR_LIT:type>" ) : <EOL> raise AttributeError ( "<STR_LIT>" ) <EOL> return { "<STR_LIT:type>" : node . getAttribute ( "<STR_LIT:type>" ) } <EOL> def _action_rebuild ( self , node ) : <EOL> rebuild = { } <EOL> if node . hasAttribute ( "<STR_LIT:name>" ) : <EOL> rebuild [ '<STR_LIT:name>' ] = node . getAttribute ( "<STR_LIT:name>" ) <EOL> metadata_node = self . find_first_child_named ( node , "<STR_LIT>" ) <EOL> if metadata_node is not None : <EOL> rebuild [ "<STR_LIT>" ] = self . extract_metadata ( metadata_node ) <EOL> personality = self . _extract_personality ( node ) <EOL> if personality is not None : <EOL> rebuild [ "<STR_LIT>" ] = personality <EOL> if not node . hasAttribute ( "<STR_LIT>" ) : <EOL> raise AttributeError ( "<STR_LIT>" ) <EOL> rebuild [ "<STR_LIT>" ] = node . getAttribute ( "<STR_LIT>" ) <EOL> return rebuild <EOL> def _action_resize ( self , node ) : <EOL> if not node . hasAttribute ( "<STR_LIT>" ) : <EOL> raise AttributeError ( "<STR_LIT>" ) <EOL> return { "<STR_LIT>" : node . getAttribute ( "<STR_LIT>" ) } <EOL> def _action_confirm_resize ( self , node ) : <EOL> return None <EOL> def _action_revert_resize ( self , node ) : <EOL> return None <EOL> def _deserialize_image_action ( self , node , allowed_attributes ) : <EOL> data = { } <EOL> for attribute in allowed_attributes : <EOL> value = node . getAttribute ( attribute ) <EOL> if value : <EOL> data [ attribute ] = value <EOL> metadata_node = self . find_first_child_named ( node , '<STR_LIT>' ) <EOL> if metadata_node is not None : <EOL> metadata = self . metadata_deserializer . extract_metadata ( <EOL> metadata_node ) <EOL> data [ '<STR_LIT>' ] = metadata <EOL> return data <EOL> def create ( self , string ) : <EOL> """<STR_LIT>""" <EOL> dom = minidom . parseString ( string ) <EOL> server = self . _extract_server ( dom ) <EOL> return { '<STR_LIT:body>' : { '<STR_LIT>' : server } } <EOL> def _extract_server ( self , node ) : <EOL> """<STR_LIT>""" <EOL> server = { } <EOL> server_node = self . find_first_child_named ( node , '<STR_LIT>' ) <EOL> attributes = [ "<STR_LIT:name>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" ] <EOL> for attr in attributes : <EOL> if server_node . getAttribute ( attr ) : <EOL> server [ attr ] = server_node . getAttribute ( attr ) <EOL> metadata_node = self . find_first_child_named ( server_node , "<STR_LIT>" ) <EOL> if metadata_node is not None : <EOL> server [ "<STR_LIT>" ] = self . extract_metadata ( metadata_node ) <EOL> personality = self . _extract_personality ( server_node ) <EOL> if personality is not None : <EOL> server [ "<STR_LIT>" ] = personality <EOL> networks = self . _extract_networks ( server_node ) <EOL> if networks is not None : <EOL> server [ "<STR_LIT>" ] = networks <EOL> security_groups = self . _extract_security_groups ( server_node ) <EOL> if security_groups is not None : <EOL> server [ "<STR_LIT>" ] = security_groups <EOL> return server <EOL> def _extract_personality ( self , server_node ) : <EOL> """<STR_LIT>""" <EOL> node = self . find_first_child_named ( server_node , "<STR_LIT>" ) <EOL> if node is not None : <EOL> personality = [ ] <EOL> for file_node in self . find_children_named ( node , "<STR_LIT:file>" ) : <EOL> item = { } <EOL> if file_node . hasAttribute ( "<STR_LIT:path>" ) : <EOL> item [ "<STR_LIT:path>" ] = file_node . getAttribute ( "<STR_LIT:path>" ) <EOL> item [ "<STR_LIT>" ] = self . extract_text ( file_node ) <EOL> personality . append ( item ) <EOL> return personality <EOL> else : <EOL> return None <EOL> def _extract_networks ( self , server_node ) : <EOL> """<STR_LIT>""" <EOL> node = self . find_first_child_named ( server_node , "<STR_LIT>" ) <EOL> if node is not None : <EOL> networks = [ ] <EOL> for network_node in self . find_children_named ( node , <EOL> "<STR_LIT>" ) : <EOL> item = { } <EOL> if network_node . hasAttribute ( "<STR_LIT>" ) : <EOL> item [ "<STR_LIT>" ] = network_node . getAttribute ( "<STR_LIT>" ) <EOL> if network_node . hasAttribute ( "<STR_LIT>" ) : <EOL> item [ "<STR_LIT>" ] = network_node . getAttribute ( "<STR_LIT>" ) <EOL> networks . append ( item ) <EOL> return networks <EOL> else : <EOL> return None <EOL> def _extract_security_groups ( self , server_node ) : <EOL> """<STR_LIT>""" <EOL> node = self . find_first_child_named ( server_node , "<STR_LIT>" ) <EOL> if node is not None : <EOL> security_groups = [ ] <EOL> for sg_node in self . find_children_named ( node , "<STR_LIT>" ) : <EOL> item = { } <EOL> name_node = self . find_first_child_named ( sg_node , "<STR_LIT:name>" ) <EOL> if name_node : <EOL> item [ "<STR_LIT:name>" ] = self . extract_text ( name_node ) <EOL> security_groups . append ( item ) <EOL> return security_groups <EOL> else : <EOL> return None </s>
