<s> import re <EOL> import pwd <EOL> import os . path <EOL> import glob <EOL> import time <EOL> import web <EOL> import karesansui <EOL> from karesansui . lib . rest import Rest , auth <EOL> from karesansui . lib . const import ICON_DIR_TPL , VIRT_COMMAND_CREATE_GUEST , VIRT_COMMAND_REPLICATE_GUEST , VIRT_COMMAND_DELETE_GUEST , GRAPHICS_PORT_MIN_NUMBER , PORT_MAX_NUMBER , MEMORY_MIN_SIZE , DISK_MIN_SIZE , VIRT_DOMAINS_DIR , DEFAULT_KEYMAP <EOL> from karesansui . lib . const import XEN_KEYMAP_DIR , KVM_KEYMAP_DIR , KVM_BRIDGE_PREFIX <EOL> from karesansui . lib . utils import comma_split , generate_mac_address , is_param , next_number , generate_uuid , string_from_uuid , uniq_sort , uni_force , get_partition_info , chk_create_disk , json_dumps , get_ifconfig_info , get_keymaps , available_virt_mechs , available_virt_uris , is_iso9660_filesystem_format , get_dom_list , get_dom_type , base64_encode , uri_split , uri_join , dict_search <EOL> from karesansui . lib . utils import get_xml_parse as XMLParse <EOL> from karesansui . lib . utils import get_xml_xpath as XMLXpath <EOL> from karesansui . lib . utils import get_nums_xml_xpath as XMLXpathNum <EOL> from karesansui . lib . file . configfile import ConfigFile <EOL> from karesansui . lib . checker import Checker , CHECK_EMPTY , CHECK_VALID , CHECK_LENGTH , CHECK_ONLYSPACE , CHECK_MIN , CHECK_MAX , CHECK_EXIST <EOL> from karesansui . lib . const import NOTE_TITLE_MIN_LENGTH , NOTE_TITLE_MAX_LENGTH , MACHINE_NAME_MIN_LENGTH , MACHINE_NAME_MAX_LENGTH , TAG_MIN_LENGTH , TAG_MAX_LENGTH , GRAPHICS_PORT_MIN_NUMBER , GRAPHICS_PORT_MAX_NUMBER , HYPERVISOR_MIN_SIZE , HYPERVISOR_MAX_SIZE , MEMORY_MIN_SIZE , DISK_MIN_SIZE , DOMAIN_NAME_MIN_LENGTH , DOMAIN_NAME_MAX_LENGTH , MACHINE_HYPERVISOR , MACHINE_ATTRIBUTE , DISK_QEMU_FORMAT , DISK_NON_QEMU_FORMAT , VIRT_COMMAND_CREATE_STORAGE_VOLUME , VIRT_COMMAND_DELETE_STORAGE_VOLUME , STORAGE_VOLUME_PWD , DISK_USES <EOL> from karesansui . lib . virt . virt import KaresansuiVirtConnection , KaresansuiVirtConnectionAuth <EOL> from karesansui . lib . virt . config_export import ExportConfigParam <EOL> from karesansui . lib . merge import MergeGuest , MergeHost <EOL> from karesansui . db . access . machine import findbyhost1guestall , findbyhost1 , findbyguest1 , new as m_new , save as m_save , delete as m_delete <EOL> from karesansui . db . access . machine2jobgroup import new as m2j_new , save as m2j_save <EOL> from karesansui . db . access . notebook import new as n_new <EOL> from karesansui . db . access . tag import new as t_new , samecount as t_count , findby1name as t_name <EOL> from karesansui . db . access . _2pysilhouette import jg_save , jg_delete <EOL> from karesansui . db . model . _2pysilhouette import Job , JobGroup <EOL> from pysilhouette . command import dict2command <EOL> def validates_guest_add ( obj ) : <EOL> checker = Checker ( ) <EOL> check = True <EOL> _ = obj . _ <EOL> checker . errors = [ ] <EOL> if not is_param ( obj . input , '<STR_LIT>' ) : <EOL> check = False <EOL> checker . add_error ( _ ( '<STR_LIT>' ) ) <EOL> else : <EOL> check = checker . check_string ( <EOL> _ ( '<STR_LIT>' ) , <EOL> obj . input . m_name , <EOL> CHECK_EMPTY | CHECK_LENGTH | CHECK_ONLYSPACE , <EOL> None , <EOL> min = MACHINE_NAME_MIN_LENGTH , <EOL> max = MACHINE_NAME_MAX_LENGTH , <EOL> ) and check <EOL> if is_param ( obj . input , '<STR_LIT>' ) : <EOL> check = checker . check_string ( <EOL> _ ( '<STR_LIT>' ) , <EOL> obj . input . note_title , <EOL> CHECK_LENGTH | CHECK_ONLYSPACE , <EOL> None , <EOL> min = NOTE_TITLE_MIN_LENGTH , <EOL> max = NOTE_TITLE_MAX_LENGTH , <EOL> ) and check <EOL> if is_param ( obj . input , '<STR_LIT>' ) : <EOL> check = checker . check_string ( <EOL> _ ( '<STR_LIT>' ) , <EOL> obj . input . note_value , <EOL> CHECK_ONLYSPACE , <EOL> None , <EOL> None , <EOL> None , <EOL> ) and check <EOL> if is_param ( obj . input , '<STR_LIT>' ) : <EOL> for tag in comma_split ( obj . input . tags ) : <EOL> check = checker . check_string ( <EOL> _ ( '<STR_LIT>' ) , <EOL> tag , <EOL> CHECK_LENGTH | CHECK_ONLYSPACE , <EOL> None , <EOL> min = TAG_MIN_LENGTH , <EOL> max = TAG_MAX_LENGTH , <EOL> ) and check <EOL> if not is_param ( obj . input , '<STR_LIT>' ) : <EOL> check = False <EOL> checker . add_error ( _ ( '<STR_LIT>' ) ) <EOL> else : <EOL> check = checker . check_hypervisor ( <EOL> _ ( '<STR_LIT>' ) , <EOL> obj . input . m_hypervisor , <EOL> CHECK_EMPTY | CHECK_VALID | CHECK_MIN | CHECK_MAX , <EOL> HYPERVISOR_MIN_SIZE , <EOL> HYPERVISOR_MAX_SIZE , <EOL> ) and check <EOL> if not is_param ( obj . input , '<STR_LIT>' ) : <EOL> check = False <EOL> checker . add_error ( _ ( '<STR_LIT>' ) ) <EOL> else : <EOL> check = checker . check_string ( <EOL> _ ( '<STR_LIT>' ) , <EOL> obj . input . domain_name , <EOL> CHECK_EMPTY | CHECK_VALID | CHECK_LENGTH | CHECK_ONLYSPACE , <EOL> '<STR_LIT>' , <EOL> DOMAIN_NAME_MIN_LENGTH , <EOL> DOMAIN_NAME_MAX_LENGTH , <EOL> ) and check <EOL> if obj . input . domain_name in get_dom_list ( ) : <EOL> dom_type = get_dom_type ( obj . input . domain_name ) <EOL> checker . add_error ( _ ( "<STR_LIT>" ) % dom_type . upper ( ) ) <EOL> check = False <EOL> if is_param ( obj . input , '<STR_LIT>' ) : <EOL> check = checker . check_number ( <EOL> _ ( '<STR_LIT>' ) , <EOL> obj . input . vm_mem_size , <EOL> CHECK_VALID | CHECK_MIN | CHECK_EMPTY , <EOL> MEMORY_MIN_SIZE , <EOL> None , <EOL> ) and check <EOL> if is_param ( obj . input , '<STR_LIT>' ) : <EOL> if obj . input . pool_type != "<STR_LIT>" : <EOL> if is_param ( obj . input , '<STR_LIT>' ) : <EOL> check = checker . check_number ( <EOL> _ ( '<STR_LIT>' ) , <EOL> obj . input . vm_disk_size , <EOL> CHECK_VALID | CHECK_MIN | CHECK_EMPTY , <EOL> DISK_MIN_SIZE , <EOL> None , <EOL> ) and check <EOL> if not is_param ( obj . input , '<STR_LIT>' ) : <EOL> check = False <EOL> checker . add_error ( _ ( '<STR_LIT>' ) ) <EOL> else : <EOL> if obj . input . boot_image == "<STR_LIT>" : <EOL> if not is_param ( obj . input , '<STR_LIT>' ) : <EOL> check = False <EOL> checker . add_error ( _ ( '<STR_LIT>' ) ) <EOL> else : <EOL> check = checker . check_startfile ( <EOL> _ ( '<STR_LIT>' ) , <EOL> obj . input . vm_kernel , <EOL> CHECK_EMPTY | CHECK_VALID | CHECK_EXIST , <EOL> ) and check <EOL> if not is_param ( obj . input , '<STR_LIT>' ) : <EOL> check = False <EOL> checker . add_error ( _ ( '<STR_LIT>' ) ) <EOL> else : <EOL> check = checker . check_startfile ( <EOL> _ ( '<STR_LIT>' ) , <EOL> obj . input . vm_initrd , <EOL> CHECK_EMPTY | CHECK_VALID | CHECK_EXIST , <EOL> ) and check <EOL> if obj . input . boot_image == "<STR_LIT>" : <EOL> if not is_param ( obj . input , '<STR_LIT>' ) : <EOL> check = False <EOL> checker . add_error ( _ ( '<STR_LIT>' ) ) <EOL> else : <EOL> check = checker . check_startfile ( <EOL> _ ( '<STR_LIT>' ) , <EOL> obj . input . vm_iso , <EOL> CHECK_EMPTY | CHECK_VALID | CHECK_EXIST , <EOL> ) and check <EOL> if check : <EOL> check = is_iso9660_filesystem_format ( obj . input . vm_iso ) <EOL> checker . add_error ( _ ( '<STR_LIT>' ) % obj . input . vm_iso ) <EOL> if not is_param ( obj . input , '<STR_LIT>' ) : <EOL> check = False <EOL> checker . add_error ( _ ( '<STR_LIT>' ) % _ ( '<STR_LIT>' ) ) <EOL> else : <EOL> hypervisor = "<STR_LIT>" <EOL> if int ( obj . input . m_hypervisor ) == MACHINE_HYPERVISOR [ '<STR_LIT>' ] : <EOL> hypervisor = "<STR_LIT>" <EOL> elif int ( obj . input . m_hypervisor ) == MACHINE_HYPERVISOR [ '<STR_LIT>' ] : <EOL> hypervisor = "<STR_LIT>" <EOL> check = checker . check_keymap ( <EOL> _ ( '<STR_LIT>' ) , <EOL> obj . input . keymap , <EOL> CHECK_EMPTY | CHECK_EXIST , <EOL> hypervisor <EOL> ) and check <EOL> if not is_param ( obj . input , '<STR_LIT>' ) : <EOL> check = False <EOL> checker . add_error ( _ ( '<STR_LIT>' ) ) <EOL> else : <EOL> check = checker . check_number ( <EOL> _ ( '<STR_LIT>' ) , <EOL> obj . input . vm_graphics_port , <EOL> CHECK_EMPTY | CHECK_VALID | CHECK_MIN | CHECK_MAX , <EOL> GRAPHICS_PORT_MIN_NUMBER , <EOL> GRAPHICS_PORT_MAX_NUMBER , <EOL> ) and check <EOL> if not is_param ( obj . input , '<STR_LIT>' ) : <EOL> check = False <EOL> checker . add_error ( _ ( '<STR_LIT>' ) ) <EOL> else : <EOL> check = checker . check_macaddr ( <EOL> _ ( '<STR_LIT>' ) , <EOL> obj . input . vm_mac , <EOL> CHECK_EMPTY | CHECK_VALID , <EOL> ) and check <EOL> obj . view . alert = checker . errors <EOL> return check <EOL> def make_storage_volume_job ( uuid , name , pool_name , format , <EOL> capacity , allocation , unit , order ) : <EOL> cmdname = u"<STR_LIT>" <EOL> cmd = VIRT_COMMAND_CREATE_STORAGE_VOLUME <EOL> options = { } <EOL> options [ '<STR_LIT>' ] = uuid <EOL> options [ '<STR_LIT:name>' ] = name <EOL> options [ '<STR_LIT>' ] = pool_name <EOL> options [ '<STR_LIT>' ] = format <EOL> options [ '<STR_LIT>' ] = capacity <EOL> options [ '<STR_LIT>' ] = allocation <EOL> options [ '<STR_LIT>' ] = unit <EOL> options [ '<STR_LIT>' ] = pwd . getpwnam ( STORAGE_VOLUME_PWD [ "<STR_LIT>" ] ) [ <NUM_LIT:2> ] <EOL> options [ '<STR_LIT>' ] = pwd . getpwnam ( STORAGE_VOLUME_PWD [ "<STR_LIT>" ] ) [ <NUM_LIT:2> ] <EOL> options [ '<STR_LIT>' ] = STORAGE_VOLUME_PWD [ "<STR_LIT>" ] <EOL> options [ '<STR_LIT>' ] = DISK_USES [ "<STR_LIT>" ] <EOL> _cmd = dict2command ( <EOL> "<STR_LIT>" % ( karesansui . config [ '<STR_LIT>' ] , cmd ) , options ) <EOL> rollback_options = { } <EOL> rollback_options [ "<STR_LIT:name>" ] = uuid <EOL> rollback_options [ "<STR_LIT>" ] = pool_name <EOL> rollback_options [ "<STR_LIT>" ] = DISK_USES [ "<STR_LIT>" ] <EOL> rollback_cmd = dict2command ( <EOL> "<STR_LIT>" % ( karesansui . config [ '<STR_LIT>' ] , VIRT_COMMAND_DELETE_STORAGE_VOLUME ) , <EOL> rollback_options ) <EOL> _job = Job ( '<STR_LIT>' % cmdname , order , _cmd ) <EOL> return _job <EOL> def regist_guest ( obj , _guest , icon_filename , <EOL> cmd , options , cmdname , rollback_options , is_create = False ) : <EOL> if icon_filename : <EOL> _guest . icon = icon_filename <EOL> if ( karesansui . sheconf . has_key ( '<STR_LIT>' ) is False ) or ( karesansui . sheconf [ '<STR_LIT>' ] . strip ( '<STR_LIT>' ) == '<STR_LIT>' ) : <EOL> raise <EOL> action_cmd = dict2command ( <EOL> "<STR_LIT>" % ( karesansui . config [ '<STR_LIT>' ] , cmd ) , <EOL> options ) <EOL> rollback_cmd = dict2command ( <EOL> "<STR_LIT>" % ( karesansui . config [ '<STR_LIT>' ] , VIRT_COMMAND_DELETE_GUEST ) , <EOL> rollback_options ) <EOL> _jobgroup = JobGroup ( cmdname [ <NUM_LIT:0> ] , karesansui . sheconf [ '<STR_LIT>' ] ) <EOL> order = <NUM_LIT:0> <EOL> if is_create is True : <EOL> _volume_job = make_storage_volume_job ( options [ "<STR_LIT>" ] , <EOL> options [ "<STR_LIT>" ] , <EOL> options [ "<STR_LIT>" ] , <EOL> options [ "<STR_LIT>" ] , <EOL> options [ "<STR_LIT>" ] , <EOL> options [ "<STR_LIT>" ] , <EOL> '<STR_LIT:M>' , <EOL> order <EOL> ) <EOL> order += <NUM_LIT:1> <EOL> _jobgroup . jobs . append ( _volume_job ) <EOL> _job = Job ( '<STR_LIT>' % cmdname [ <NUM_LIT:1> ] , order , action_cmd ) <EOL> _job . rollback_command = rollback_cmd <EOL> _jobgroup . jobs . append ( _job ) <EOL> try : <EOL> m_save ( obj . orm , _guest ) <EOL> obj . orm . commit ( ) <EOL> except : <EOL> obj . logger . error ( '<STR_LIT>' % _guest . name ) <EOL> raise <EOL> try : <EOL> jg_save ( obj . pysilhouette . orm , _jobgroup ) <EOL> obj . pysilhouette . orm . commit ( ) <EOL> except : <EOL> obj . logger . error ( '<STR_LIT>' % _jobgroup . name ) <EOL> try : <EOL> m_delete ( obj . orm , _guest ) <EOL> obj . orm . commit ( ) <EOL> obj . logger . error ( '<STR_LIT>' % _guest . id ) <EOL> except : <EOL> obj . logger . critical ( '<STR_LIT>' % _guest . id ) <EOL> raise <EOL> raise <EOL> try : <EOL> _m2j = m2j_new ( machine = _guest , <EOL> jobgroup_id = _jobgroup . id , <EOL> uniq_key = karesansui . sheconf [ '<STR_LIT>' ] , <EOL> created_user = obj . me , <EOL> modified_user = obj . me , <EOL> ) <EOL> m2j_save ( obj . orm , _m2j ) <EOL> obj . orm . commit ( ) <EOL> except : <EOL> try : <EOL> m_delete ( obj . orm , _guest ) <EOL> obj . orm . commit ( ) <EOL> except : <EOL> obj . logger . critical ( '<STR_LIT>' % _guest . id ) <EOL> try : <EOL> jg_delete ( obj . pysilhouette . orm , _jobgroup ) <EOL> obj . pysilhouette . orm . commit ( ) <EOL> except : <EOL> obj . logger . critical ( '<STR_LIT>' % _jobgroup . id ) <EOL> raise <EOL> return True <EOL> class Guest ( Rest ) : <EOL> def _post ( self , f ) : <EOL> ret = Rest . _post ( self , f ) <EOL> if hasattr ( self , "<STR_LIT>" ) is True : <EOL> self . kvc . close ( ) <EOL> return ret <EOL> @ auth <EOL> def _GET ( self , * param , ** params ) : <EOL> host_id = self . chk_hostby1 ( param ) <EOL> if host_id is None : return web . notfound ( ) <EOL> model = findbyhost1 ( self . orm , host_id ) <EOL> uris = available_virt_uris ( ) <EOL> if model . attribute == MACHINE_ATTRIBUTE [ "<STR_LIT>" ] : <EOL> uri_guests = [ ] <EOL> uri_guests_status = { } <EOL> uri_guests_kvg = { } <EOL> uri_guests_info = { } <EOL> uri_guests_name = { } <EOL> segs = uri_split ( model . hostname ) <EOL> uri = uri_join ( segs , without_auth = True ) <EOL> creds = '<STR_LIT>' <EOL> if segs [ "<STR_LIT:user>" ] is not None : <EOL> creds += segs [ "<STR_LIT:user>" ] <EOL> if segs [ "<STR_LIT>" ] is not None : <EOL> creds += '<STR_LIT::>' + segs [ "<STR_LIT>" ] <EOL> if self . is_mode_input ( ) is not True : <EOL> try : <EOL> self . kvc = KaresansuiVirtConnectionAuth ( uri , creds ) <EOL> host = MergeHost ( self . kvc , model ) <EOL> for guest in host . guests : <EOL> _virt = self . kvc . search_kvg_guests ( guest . info [ "<STR_LIT>" ] . name ) <EOL> if <NUM_LIT:0> < len ( _virt ) : <EOL> for _v in _virt : <EOL> uuid = _v . get_info ( ) [ "<STR_LIT>" ] <EOL> uri_guests_info [ uuid ] = guest . info <EOL> uri_guests_kvg [ uuid ] = _v <EOL> uri_guests_name [ uuid ] = guest . info [ "<STR_LIT>" ] . name . encode ( "<STR_LIT:utf8>" ) <EOL> for name in sorted ( uri_guests_name . values ( ) , key = str . lower ) : <EOL> for uuid in dict_search ( name , uri_guests_name ) : <EOL> uri_guests . append ( MergeGuest ( uri_guests_info [ uuid ] [ "<STR_LIT>" ] , uri_guests_kvg [ uuid ] ) ) <EOL> uri_guests_status [ uuid ] = uri_guests_info [ uuid ] [ '<STR_LIT>' ] . status ( ) <EOL> finally : <EOL> self . kvc . close ( ) <EOL> if self . is_json ( ) is True : <EOL> guests_json = [ ] <EOL> for x in uri_guests : <EOL> guests_json . append ( x . get_json ( self . me . languages ) ) <EOL> self . view . uri_guests = json_dumps ( guests_json ) <EOL> else : <EOL> self . view . uri_guests = uri_guests <EOL> self . view . uri_guests_status = uri_guests_status <EOL> self . kvc = KaresansuiVirtConnection ( ) <EOL> try : <EOL> inactive_pool = [ ] <EOL> active_pool = self . kvc . list_active_storage_pool ( ) <EOL> pools = inactive_pool + active_pool <EOL> pools . sort ( ) <EOL> if not pools : <EOL> return web . badrequest ( '<STR_LIT>' ) <EOL> if self . is_mode_input ( ) is True : <EOL> self . view . pools = pools <EOL> pools_info = { } <EOL> pools_vols_info = { } <EOL> pools_iscsi_blocks = { } <EOL> already_vols = [ ] <EOL> guests = [ ] <EOL> guests += self . kvc . list_inactive_guest ( ) <EOL> guests += self . kvc . list_active_guest ( ) <EOL> for guest in guests : <EOL> already_vol = self . kvc . get_storage_volume_bydomain ( domain = guest , <EOL> image_type = None , <EOL> attr = '<STR_LIT:path>' ) <EOL> if already_vol : <EOL> already_vols += already_vol . keys ( ) <EOL> for pool in pools : <EOL> pool_obj = self . kvc . search_kvn_storage_pools ( pool ) [ <NUM_LIT:0> ] <EOL> if pool_obj . is_active ( ) is True : <EOL> pools_info [ pool ] = pool_obj . get_info ( ) <EOL> blocks = None <EOL> if pools_info [ pool ] [ '<STR_LIT:type>' ] == '<STR_LIT>' : <EOL> blocks = self . kvc . get_storage_volume_iscsi_block_bypool ( pool ) <EOL> if blocks : <EOL> pools_iscsi_blocks [ pool ] = [ ] <EOL> vols_obj = pool_obj . search_kvn_storage_volumes ( self . kvc ) <EOL> vols_info = { } <EOL> for vol_obj in vols_obj : <EOL> vol_name = vol_obj . get_storage_volume_name ( ) <EOL> vols_info [ vol_name ] = vol_obj . get_info ( ) <EOL> if blocks : <EOL> if vol_name in blocks and vol_name not in already_vols : <EOL> pools_iscsi_blocks [ pool ] . append ( vol_obj . get_info ( ) ) <EOL> pools_vols_info [ pool ] = vols_info <EOL> self . view . pools_info = pools_info <EOL> self . view . pools_vols_info = pools_vols_info <EOL> self . view . pools_iscsi_blocks = pools_iscsi_blocks <EOL> bridge_prefix = { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : KVM_BRIDGE_PREFIX , <EOL> } <EOL> self . view . host_id = host_id <EOL> self . view . DEFAULT_KEYMAP = DEFAULT_KEYMAP <EOL> self . view . DISK_NON_QEMU_FORMAT = DISK_NON_QEMU_FORMAT <EOL> self . view . DISK_QEMU_FORMAT = DISK_QEMU_FORMAT <EOL> self . view . hypervisors = { } <EOL> self . view . mac_address = { } <EOL> self . view . keymaps = { } <EOL> self . view . phydev = { } <EOL> self . view . virnet = { } <EOL> used_ports = { } <EOL> for k , v in MACHINE_HYPERVISOR . iteritems ( ) : <EOL> if k in available_virt_mechs ( ) : <EOL> self . view . hypervisors [ k ] = v <EOL> uri = uris [ k ] <EOL> mem_info = self . kvc . get_mem_info ( ) <EOL> active_networks = self . kvc . list_active_network ( ) <EOL> used_graphics_ports = self . kvc . list_used_graphics_port ( ) <EOL> bus_types = self . kvc . bus_types <EOL> self . view . bus_types = bus_types <EOL> self . view . max_mem = mem_info [ '<STR_LIT>' ] <EOL> self . view . free_mem = mem_info [ '<STR_LIT>' ] <EOL> self . view . alloc_mem = mem_info [ '<STR_LIT>' ] <EOL> self . view . mac_address [ k ] = generate_mac_address ( k ) <EOL> self . view . keymaps [ k ] = eval ( "<STR_LIT>" % k ) <EOL> phydev = [ ] <EOL> phydev_regex = re . compile ( r"<STR_LIT:%s>" % bridge_prefix [ k ] ) <EOL> for dev , dev_info in get_ifconfig_info ( ) . iteritems ( ) : <EOL> try : <EOL> if phydev_regex . match ( dev ) : <EOL> phydev . append ( dev ) <EOL> except : <EOL> pass <EOL> if len ( phydev ) == <NUM_LIT:0> : <EOL> phydev . append ( "<STR_LIT>" % bridge_prefix [ k ] ) <EOL> phydev . sort ( ) <EOL> self . view . phydev [ k ] = phydev <EOL> self . view . virnet [ k ] = sorted ( active_networks ) <EOL> used_ports [ k ] = used_graphics_ports <EOL> exclude_ports = [ ] <EOL> for k , _used_port in used_ports . iteritems ( ) : <EOL> exclude_ports = exclude_ports + _used_port <EOL> exclude_ports = sorted ( exclude_ports ) <EOL> exclude_ports = [ p for p , q in zip ( exclude_ports , exclude_ports [ <NUM_LIT:1> : ] + [ None ] ) if p != q ] <EOL> self . view . graphics_port = next_number ( GRAPHICS_PORT_MIN_NUMBER , <EOL> PORT_MAX_NUMBER , <EOL> exclude_ports ) <EOL> else : <EOL> models = findbyhost1guestall ( self . orm , host_id ) <EOL> guests = [ ] <EOL> if models : <EOL> self . view . hypervisors = { } <EOL> for model in models : <EOL> for k , v in MACHINE_HYPERVISOR . iteritems ( ) : <EOL> if k in available_virt_mechs ( ) : <EOL> self . view . hypervisors [ k ] = v <EOL> uri = uris [ k ] <EOL> if hasattr ( self , "<STR_LIT>" ) is not True : <EOL> self . kvc = KaresansuiVirtConnection ( uri ) <EOL> domname = self . kvc . uuid_to_domname ( model . uniq_key ) <EOL> _virt = self . kvc . search_kvg_guests ( domname ) <EOL> if <NUM_LIT:0> < len ( _virt ) : <EOL> guests . append ( MergeGuest ( model , _virt [ <NUM_LIT:0> ] ) ) <EOL> else : <EOL> guests . append ( MergeGuest ( model , None ) ) <EOL> exports = { } <EOL> for pool_name in pools : <EOL> files = [ ] <EOL> pool = self . kvc . search_kvn_storage_pools ( pool_name ) <EOL> path = pool [ <NUM_LIT:0> ] . get_info ( ) [ "<STR_LIT:target>" ] [ "<STR_LIT:path>" ] <EOL> if os . path . exists ( path ) : <EOL> for _afile in glob . glob ( "<STR_LIT>" % ( path , ) ) : <EOL> param = ExportConfigParam ( ) <EOL> param . load_xml_config ( _afile ) <EOL> _dir = os . path . dirname ( _afile ) <EOL> uuid = param . get_uuid ( ) <EOL> name = param . get_domain ( ) <EOL> created = param . get_created ( ) <EOL> title = param . get_title ( ) <EOL> if title != "<STR_LIT>" : <EOL> title = re . sub ( "<STR_LIT>" , "<STR_LIT>" , title ) <EOL> if title == "<STR_LIT>" : <EOL> title = _ ( '<STR_LIT>' ) <EOL> if created != "<STR_LIT>" : <EOL> created_str = time . strftime ( "<STR_LIT>" , time . localtime ( float ( created ) ) ) <EOL> else : <EOL> created_str = _ ( "<STR_LIT>" ) <EOL> files . append ( { "<STR_LIT>" : _dir , <EOL> "<STR_LIT>" : pool_name , <EOL> "<STR_LIT>" : uuid , <EOL> "<STR_LIT:name>" : name , <EOL> "<STR_LIT>" : int ( created ) , <EOL> "<STR_LIT>" : created_str , <EOL> "<STR_LIT:title>" : title , <EOL> "<STR_LIT>" : param . get_database ( ) [ "<STR_LIT>" ] , <EOL> } ) <EOL> exports [ pool_name ] = files <EOL> if self . is_json ( ) is True : <EOL> guests_json = [ ] <EOL> for x in guests : <EOL> guests_json . append ( x . get_json ( self . me . languages ) ) <EOL> self . view . guests = json_dumps ( guests_json ) <EOL> else : <EOL> self . view . exports = exports <EOL> self . view . guests = guests <EOL> return True <EOL> except : <EOL> pass <EOL> finally : <EOL> pass <EOL> @ auth <EOL> def _POST ( self , * param , ** params ) : <EOL> host_id = self . chk_hostby1 ( param ) <EOL> if host_id is None : return web . notfound ( ) <EOL> model = findbyhost1 ( self . orm , host_id ) <EOL> if model . attribute == <NUM_LIT:0> and model . hypervisor == MACHINE_HYPERVISOR [ "<STR_LIT>" ] : <EOL> uri = uris [ "<STR_LIT>" ] <EOL> elif model . attribute == <NUM_LIT:0> and model . hypervisor == MACHINE_HYPERVISOR [ "<STR_LIT>" ] : <EOL> uri = uris [ "<STR_LIT>" ] <EOL> else : <EOL> uri = None <EOL> if not validates_guest_add ( self ) : <EOL> return web . badrequest ( self . view . alert ) <EOL> try : <EOL> try : <EOL> self . kvc = KaresansuiVirtConnection ( uri ) <EOL> active_guests = self . kvc . list_active_guest ( ) <EOL> inactive_guests = self . kvc . list_inactive_guest ( ) <EOL> used_graphics_ports = self . kvc . list_used_graphics_port ( ) <EOL> used_mac_addrs = self . kvc . list_used_mac_addr ( ) <EOL> mem_info = self . kvc . get_mem_info ( ) <EOL> if is_param ( self . input , "<STR_LIT>" ) : <EOL> if mem_info [ '<STR_LIT>' ] < int ( self . input . vm_mem_size ) : <EOL> return web . badrequest ( _ ( "<STR_LIT>" ) ) <EOL> if is_param ( self . input , "<STR_LIT>" ) and self . input . pool_type != "<STR_LIT>" and is_param ( self . input , "<STR_LIT>" ) : <EOL> target_path = self . kvc . get_storage_pool_targetpath ( self . input . pool_dir ) <EOL> if target_path : <EOL> if not chk_create_disk ( target_path , self . input . vm_disk_size ) : <EOL> partition = get_partition_info ( target_path , header = False ) <EOL> return web . badrequest ( _ ( "<STR_LIT>" ) % partition [ <NUM_LIT:5> ] [ <NUM_LIT:0> ] ) <EOL> except : <EOL> raise <EOL> finally : <EOL> del self . kvc <EOL> if ( self . input . domain_name in active_guests ) or ( self . input . domain_name in inactive_guests ) : <EOL> return web . conflict ( web . ctx . path , "<STR_LIT>" ) <EOL> if ( int ( self . input . vm_graphics_port ) in used_graphics_ports ) : <EOL> return web . conflict ( web . ctx . path , "<STR_LIT>" ) <EOL> if ( self . input . vm_mac in used_mac_addrs ) : <EOL> return web . conflict ( web . ctx . path , "<STR_LIT>" ) <EOL> uuid = string_from_uuid ( generate_uuid ( ) ) <EOL> options = { } <EOL> options [ '<STR_LIT>' ] = uuid <EOL> if is_param ( self . input , "<STR_LIT>" ) : <EOL> options [ '<STR_LIT:name>' ] = self . input . domain_name <EOL> if is_param ( self . input , "<STR_LIT>" ) : <EOL> options [ '<STR_LIT>' ] = self . input . vm_mem_size <EOL> if is_param ( self . input , "<STR_LIT>" ) : <EOL> options [ '<STR_LIT>' ] = self . input . vm_kernel <EOL> if is_param ( self . input , "<STR_LIT>" ) : <EOL> options [ '<STR_LIT>' ] = self . input . vm_initrd <EOL> if is_param ( self . input , "<STR_LIT>" ) : <EOL> options [ '<STR_LIT>' ] = self . input . vm_iso <EOL> if is_param ( self . input , "<STR_LIT>" ) : <EOL> options [ '<STR_LIT>' ] = self . input . keymap <EOL> is_create = False <EOL> if is_param ( self . input , "<STR_LIT>" ) : <EOL> if is_param ( self . input , "<STR_LIT>" ) : <EOL> options [ '<STR_LIT>' ] = self . input . bus_type <EOL> if self . input . pool_type == "<STR_LIT>" or self . input . pool_type == "<STR_LIT>" : <EOL> is_create = True <EOL> options [ '<STR_LIT>' ] = self . input . disk_format <EOL> options [ "<STR_LIT>" ] = self . input . pool_dir <EOL> options [ "<STR_LIT>" ] = options [ '<STR_LIT:name>' ] <EOL> options [ '<STR_LIT>' ] = self . input . vm_disk_size <EOL> elif self . input . pool_type == "<STR_LIT>" : <EOL> ( iscsi_pool , iscsi_volume ) = self . input . pool_dir . split ( "<STR_LIT:/>" , <NUM_LIT:2> ) <EOL> options [ "<STR_LIT>" ] = iscsi_pool <EOL> options [ "<STR_LIT>" ] = iscsi_volume <EOL> else : <EOL> return web . badrequest ( ) <EOL> else : <EOL> return web . badrequest ( ) <EOL> if is_param ( self . input , "<STR_LIT>" ) : <EOL> options [ '<STR_LIT>' ] = self . input . vm_graphics_port <EOL> if is_param ( self . input , "<STR_LIT>" ) : <EOL> options [ '<STR_LIT>' ] = self . input . vm_mac <EOL> if is_param ( self . input , "<STR_LIT>" ) : <EOL> options [ '<STR_LIT>' ] = self . input . vm_extra <EOL> if is_param ( self . input , "<STR_LIT>" ) : <EOL> if self . input . nic_type == "<STR_LIT>" : <EOL> options [ '<STR_LIT>' ] = "<STR_LIT>" + self . input . phydev <EOL> elif self . input . nic_type == "<STR_LIT>" : <EOL> options [ '<STR_LIT>' ] = "<STR_LIT>" + self . input . virnet <EOL> if int ( self . input . m_hypervisor ) == MACHINE_HYPERVISOR [ '<STR_LIT>' ] : <EOL> i_hypervisor = MACHINE_HYPERVISOR [ '<STR_LIT>' ] <EOL> options [ '<STR_LIT:type>' ] = u"<STR_LIT>" <EOL> elif int ( self . input . m_hypervisor ) == MACHINE_HYPERVISOR [ '<STR_LIT>' ] : <EOL> i_hypervisor = MACHINE_HYPERVISOR [ '<STR_LIT>' ] <EOL> options [ '<STR_LIT:type>' ] = u"<STR_LIT>" <EOL> else : <EOL> return web . badrequest ( "<STR_LIT>" ) <EOL> host = findbyhost1 ( self . orm , host_id ) <EOL> note_title = None <EOL> if is_param ( self . input , "<STR_LIT>" ) : <EOL> note_title = self . input . note_title <EOL> note_value = None <EOL> if is_param ( self . input , "<STR_LIT>" ) : <EOL> note_value = self . input . note_value <EOL> _notebook = n_new ( note_title , note_value ) <EOL> _tags = None <EOL> if is_param ( self . input , "<STR_LIT>" ) : <EOL> _tags = [ ] <EOL> tag_array = comma_split ( self . input . tags ) <EOL> tag_array = uniq_sort ( tag_array ) <EOL> for x in tag_array : <EOL> if t_count ( self . orm , x ) == <NUM_LIT:0> : <EOL> _tags . append ( t_new ( x ) ) <EOL> else : <EOL> _tags . append ( t_name ( self . orm , x ) ) <EOL> icon_filename = None <EOL> if is_param ( self . input , "<STR_LIT>" , empty = True ) : <EOL> icon_filename = self . input . icon_filename <EOL> _guest = m_new ( created_user = self . me , <EOL> modified_user = self . me , <EOL> uniq_key = uni_force ( uuid ) , <EOL> name = self . input . m_name , <EOL> attribute = MACHINE_ATTRIBUTE [ '<STR_LIT>' ] , <EOL> hypervisor = i_hypervisor , <EOL> notebook = _notebook , <EOL> tags = _tags , <EOL> icon = icon_filename , <EOL> is_deleted = False , <EOL> parent = host , <EOL> ) <EOL> ret = regist_guest ( self , <EOL> _guest , <EOL> icon_filename , <EOL> VIRT_COMMAND_CREATE_GUEST , <EOL> options , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> { "<STR_LIT:name>" : options [ '<STR_LIT:name>' ] , <EOL> "<STR_LIT>" : options [ "<STR_LIT>" ] , <EOL> "<STR_LIT>" : options [ "<STR_LIT>" ] , <EOL> } , <EOL> is_create , <EOL> ) <EOL> if ret is True : <EOL> return web . accepted ( ) <EOL> else : <EOL> return False <EOL> urls = ( <EOL> '<STR_LIT>' , Guest , <EOL> ) </s>
