<s> import os . path <EOL> import re <EOL> import glob <EOL> import time <EOL> import web <EOL> import simplejson as json <EOL> import karesansui <EOL> from karesansui . gadget . guest import regist_guest <EOL> from karesansui . lib . rest import Rest , auth <EOL> from karesansui . lib . utils import chk_create_disk , json_dumps , is_param , base64_encode , base64_decode , get_dom_list , get_dom_type , string_from_uuid , generate_uuid , comma_split , uniq_sort , get_partition_info <EOL> from karesansui . lib . const import VIRT_COMMAND_IMPORT_GUEST <EOL> from karesansui . lib . virt . virt import KaresansuiVirtConnection <EOL> from karesansui . lib . virt . config import ConfigParam <EOL> from karesansui . lib . virt . config_export import ExportConfigParam <EOL> from karesansui . db . access . machine import findbyhost1 <EOL> from karesansui . db . access . machine2jobgroup import new as m2j_new <EOL> from karesansui . db . access . _2pysilhouette import save_job_collaboration <EOL> from pysilhouette . command import dict2command <EOL> from karesansui . db . model . _2pysilhouette import Job , JobGroup <EOL> from karesansui . db . access . machine import new as m_new , findby1uniquekey as m_findby1uniquekey <EOL> from karesansui . db . access . notebook import new as n_new <EOL> from karesansui . db . access . tag import new as t_new <EOL> from karesansui . db . access . machine import findby1 as m_findby1 <EOL> from karesansui . db . access . notebook import findby1 as n_findby1 <EOL> from karesansui . db . access . tag import findby1 as t_findby1 <EOL> from karesansui . db . access . tag import samecount as t_count <EOL> from karesansui . db . access . tag import findby1name as t_name <EOL> from karesansui . lib . checker import Checker , CHECK_EMPTY , CHECK_VALID , CHECK_ISDIR , CHECK_STARTROOT , CHECK_EXIST <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> def validates_guest_import ( obj ) : <EOL> checker = Checker ( ) <EOL> check = True <EOL> _ = obj . _ <EOL> checker . errors = [ ] <EOL> obj . view . alert = checker . errors <EOL> return check <EOL> class GuestImport ( Rest ) : <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 not validates_guest_import ( self ) : <EOL> return web . badrequest ( self . view . alert ) <EOL> uuid = self . input . uuid <EOL> kvc = KaresansuiVirtConnection ( ) <EOL> try : <EOL> inactive_pool = [ ] <EOL> active_pool = kvc . list_active_storage_pool ( ) <EOL> pools = inactive_pool + active_pool <EOL> pools . sort ( ) <EOL> export = [ ] <EOL> for pool_name in pools : <EOL> pool = 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> e_param = ExportConfigParam ( ) <EOL> e_param . load_xml_config ( _afile ) <EOL> if e_param . get_uuid ( ) != uuid : <EOL> continue <EOL> e_name = e_param . get_domain ( ) <EOL> _dir = os . path . dirname ( _afile ) <EOL> param = ConfigParam ( e_name ) <EOL> path = "<STR_LIT>" % ( _dir , e_name ) <EOL> if os . path . isfile ( path ) is False : <EOL> self . logger . error ( '<STR_LIT>' % path ) <EOL> return web . internalerror ( ) <EOL> param . load_xml_config ( path ) <EOL> if e_name != param . get_domain_name ( ) : <EOL> self . logger . error ( '<STR_LIT>' % ( e_name , param . get_name ( ) ) ) <EOL> return web . internalerror ( ) <EOL> _dir = os . path . dirname ( _afile ) <EOL> title = e_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> created = e_param . get_created ( ) <EOL> if created != "<STR_LIT>" : <EOL> created_str = time . strftime ( "<STR_LIT>" , time . localtime ( float ( created ) ) ) <EOL> else : <EOL> created_str = _ ( "<STR_LIT>" ) <EOL> export . append ( { "<STR_LIT:info>" : { "<STR_LIT>" : _dir , <EOL> "<STR_LIT>" : pool_name , <EOL> "<STR_LIT>" : e_param . get_uuid ( ) , <EOL> "<STR_LIT:name>" : e_param . get_domain ( ) , <EOL> "<STR_LIT>" : int ( created ) , <EOL> "<STR_LIT>" : created_str , <EOL> "<STR_LIT:title>" : title , <EOL> "<STR_LIT>" : { "<STR_LIT:name>" : e_param . database [ "<STR_LIT:name>" ] , <EOL> "<STR_LIT>" : e_param . database [ "<STR_LIT>" ] , <EOL> "<STR_LIT>" : e_param . database [ "<STR_LIT>" ] , <EOL> "<STR_LIT>" : { "<STR_LIT:title>" : e_param . database [ "<STR_LIT>" ] [ "<STR_LIT:title>" ] , <EOL> "<STR_LIT:value>" : e_param . database [ "<STR_LIT>" ] [ "<STR_LIT:value>" ] , <EOL> } , <EOL> "<STR_LIT>" : e_param . database [ "<STR_LIT>" ] , <EOL> "<STR_LIT>" : e_param . database [ "<STR_LIT>" ] , <EOL> "<STR_LIT>" : e_param . database [ "<STR_LIT>" ] , <EOL> } , <EOL> } , <EOL> "<STR_LIT>" : { "<STR_LIT>" : param . get_behavior ( '<STR_LIT>' ) , <EOL> "<STR_LIT>" : param . get_behavior ( '<STR_LIT>' ) , <EOL> "<STR_LIT>" : param . get_behavior ( '<STR_LIT>' ) , <EOL> "<STR_LIT>" : param . get_boot_dev ( ) , <EOL> '<STR_LIT>' : param . get_disk ( ) , <EOL> "<STR_LIT>" : param . get_domain_name ( ) , <EOL> "<STR_LIT>" : param . get_domain_type ( ) , <EOL> "<STR_LIT>" : param . get_features_acpi ( ) , <EOL> "<STR_LIT>" : param . get_features_apic ( ) , <EOL> "<STR_LIT>" : param . get_features_pae ( ) , <EOL> "<STR_LIT>" : param . get_interface ( ) , <EOL> "<STR_LIT>" : param . get_max_memory ( ) , <EOL> '<STR_LIT>' : param . get_max_vcpus ( ) , <EOL> "<STR_LIT>" : param . get_max_vcpus_limit ( ) , <EOL> "<STR_LIT>" : param . get_memory ( ) , <EOL> "<STR_LIT>" : param . get_uuid ( ) , <EOL> "<STR_LIT>" : param . get_vcpus ( ) , <EOL> "<STR_LIT>" : param . get_vcpus_limit ( ) , <EOL> "<STR_LIT>" : param . get_graphics_autoport ( ) , <EOL> "<STR_LIT>" : param . get_graphics_keymap ( ) , <EOL> "<STR_LIT>" : param . get_graphics_listen ( ) , <EOL> "<STR_LIT>" : param . get_graphics_passwd ( ) , <EOL> "<STR_LIT>" : param . get_graphics_port ( ) , <EOL> } , <EOL> "<STR_LIT>" : pool [ <NUM_LIT:0> ] . get_info ( ) , <EOL> } ) <EOL> if len ( export ) != <NUM_LIT:1> : <EOL> self . logger . info ( "<STR_LIT>" % self . view . uuid ) <EOL> return web . badrequest ( ) <EOL> else : <EOL> export = export [ <NUM_LIT:0> ] <EOL> finally : <EOL> kvc . close ( ) <EOL> if export [ '<STR_LIT>' ] [ '<STR_LIT>' ] is False : <EOL> return web . badrequest ( "<STR_LIT>" ) <EOL> dest_domname = export [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> dest_uniqkey = export [ '<STR_LIT:info>' ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> if m_findby1uniquekey ( self . orm , dest_uniqkey ) is not None : <EOL> self . logger . info ( _ ( "<STR_LIT>" ) % ( dest_domname , dest_uniqkey ) ) <EOL> return web . badrequest ( _ ( "<STR_LIT>" ) % dest_domname ) <EOL> dest_dir = "<STR_LIT>" % ( export [ '<STR_LIT>' ] [ '<STR_LIT:target>' ] [ '<STR_LIT:path>' ] , export [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) <EOL> if os . path . exists ( dest_dir ) is True : <EOL> self . logger . info ( _ ( "<STR_LIT>" ) % ( dest_domname , dest_dir ) ) <EOL> return web . badrequest ( _ ( "<STR_LIT>" ) % dest_domname ) <EOL> try : <EOL> src_disk = "<STR_LIT>" % ( export [ "<STR_LIT:info>" ] [ "<STR_LIT>" ] , export [ "<STR_LIT:info>" ] [ "<STR_LIT:name>" ] , export [ "<STR_LIT:info>" ] [ "<STR_LIT:name>" ] ) <EOL> if os . path . exists ( src_disk ) : <EOL> s_size = os . path . getsize ( src_disk ) / ( <NUM_LIT> * <NUM_LIT> ) <EOL> if chk_create_disk ( export [ "<STR_LIT:info>" ] [ "<STR_LIT>" ] , s_size ) is False : <EOL> partition = get_partition_info ( export [ "<STR_LIT:info>" ] [ "<STR_LIT>" ] , header = False ) <EOL> return web . badrequest ( <EOL> _ ( "<STR_LIT>" ) % partition [ <NUM_LIT:5> ] [ <NUM_LIT:0> ] ) <EOL> except : <EOL> pass <EOL> extra_uniq_key = string_from_uuid ( generate_uuid ( ) ) <EOL> options = { } <EOL> options [ "<STR_LIT>" ] = export [ "<STR_LIT:info>" ] [ "<STR_LIT>" ] <EOL> options [ "<STR_LIT>" ] = extra_uniq_key <EOL> options [ "<STR_LIT>" ] = None <EOL> try : <EOL> _notebook = n_new ( <EOL> export [ "<STR_LIT:info>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT:title>" ] , <EOL> export [ "<STR_LIT:info>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT:value>" ] , <EOL> ) <EOL> except : <EOL> _notebook = None <EOL> _tags = [ ] <EOL> try : <EOL> tag_array = comma_split ( export [ "<STR_LIT:info>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] ) <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> except : <EOL> _tags . append ( t_new ( "<STR_LIT>" ) ) <EOL> parent = m_findby1 ( self . orm , host_id ) <EOL> dest_guest = m_new ( created_user = self . me , <EOL> modified_user = self . me , <EOL> uniq_key = extra_uniq_key , <EOL> name = export [ "<STR_LIT:info>" ] [ "<STR_LIT>" ] [ "<STR_LIT:name>" ] , <EOL> attribute = int ( export [ "<STR_LIT:info>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] ) , <EOL> hypervisor = int ( export [ "<STR_LIT:info>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] ) , <EOL> notebook = _notebook , <EOL> tags = _tags , <EOL> icon = export [ "<STR_LIT:info>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] , <EOL> is_deleted = False , <EOL> parent = parent , <EOL> ) <EOL> ret = regist_guest ( self , <EOL> _guest = dest_guest , <EOL> icon_filename = export [ "<STR_LIT:info>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] , <EOL> cmd = VIRT_COMMAND_IMPORT_GUEST , <EOL> options = options , <EOL> cmdname = [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> rollback_options = { "<STR_LIT:name>" : export [ "<STR_LIT>" ] [ "<STR_LIT>" ] } , <EOL> is_create = False <EOL> ) <EOL> if ret is True : <EOL> return web . accepted ( ) <EOL> else : <EOL> return False <EOL> urls = ( <EOL> '<STR_LIT>' , GuestImport , <EOL> ) </s>
