<s> import web <EOL> from web . utils import Storage <EOL> import karesansui <EOL> from karesansui . lib . rest import Rest , auth <EOL> from karesansui . lib . virt . virt import KaresansuiVirtConnection <EOL> from karesansui . lib . const import VIRT_COMMAND_START_STORAGE_POOL , VIRT_COMMAND_DESTROY_STORAGE_POOL , ISCSI_COMMAND_START <EOL> from karesansui . db . access . machine import findbyhost1 <EOL> from karesansui . db . access . _2pysilhouette import save_job_collaboration <EOL> from karesansui . db . access . machine2jobgroup import new as m2j_new <EOL> from karesansui . db . model . _2pysilhouette import JobGroup , Job <EOL> from pysilhouette . command import dict2command <EOL> from karesansui . lib . checker import Checker , CHECK_EMPTY , CHECK_VALID <EOL> STORAGE_POOL_START = <NUM_LIT:0> ; <EOL> STORAGE_POOL_STOP = <NUM_LIT:1> ; <EOL> def validates_storage_pool ( obj , uuid = None ) : <EOL> checker = Checker ( ) <EOL> check = True <EOL> _ = obj . _ <EOL> checker . errors = [ ] <EOL> if uuid : <EOL> check = checker . check_unique_key ( _ ( '<STR_LIT>' ) , <EOL> uuid , <EOL> CHECK_EMPTY | CHECK_VALID , <EOL> ) and check <EOL> else : <EOL> check = False <EOL> checker . add_error ( _ ( '<STR_LIT>' ) % _ ( '<STR_LIT>' ) ) <EOL> obj . view . alert = checker . errors <EOL> return check <EOL> class HostBy1StoragePoolBy1Status ( Rest ) : <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> self . view . host_id = host_id <EOL> uuid = param [ <NUM_LIT:1> ] <EOL> if not validates_storage_pool ( self , uuid ) : <EOL> self . logger . debug ( "<STR_LIT>" ) <EOL> return web . badrequest ( self . view . alert ) <EOL> try : <EOL> kvc = KaresansuiVirtConnection ( ) <EOL> inactive_pool = kvc . list_inactive_storage_pool ( ) <EOL> active_pool = kvc . list_active_storage_pool ( ) <EOL> pools = inactive_pool + active_pool <EOL> pools . sort ( ) <EOL> self . view . pools = pools <EOL> pools_obj = kvc . get_storage_pool_UUIDString2kvn_storage_pool ( uuid ) <EOL> if len ( pools_obj ) <= <NUM_LIT:0> : <EOL> self . logger . debug ( "<STR_LIT>" ) <EOL> return web . notfound ( ) <EOL> status = STORAGE_POOL_STOP <EOL> if pools_obj [ <NUM_LIT:0> ] . is_active ( ) is True : <EOL> status = STORAGE_POOL_START <EOL> if self . __template__ [ "<STR_LIT>" ] == '<STR_LIT>' : <EOL> self . view . status = json_dumps ( status ) <EOL> else : <EOL> self . view . status = status <EOL> return True <EOL> finally : <EOL> kvc . close ( ) <EOL> @ auth <EOL> def _PUT ( self , * param , ** params ) : <EOL> host_id = self . chk_hostby1 ( param ) <EOL> if host_id is None : return web . notfound ( ) <EOL> self . view . host_id = host_id <EOL> uuid = param [ <NUM_LIT:1> ] <EOL> if not validates_storage_pool ( self , uuid ) : <EOL> self . logger . debug ( "<STR_LIT>" ) <EOL> return web . badrequest ( self . view . alert ) <EOL> model = findbyhost1 ( self . orm , host_id ) <EOL> try : <EOL> kvc = KaresansuiVirtConnection ( ) <EOL> inactive_pool = kvc . list_inactive_storage_pool ( ) <EOL> active_pool = kvc . list_active_storage_pool ( ) <EOL> pools = inactive_pool + active_pool <EOL> pools . sort ( ) <EOL> self . view . pools = pools <EOL> pools_obj = kvc . get_storage_pool_UUIDString2kvn_storage_pool ( uuid ) <EOL> if len ( pools_obj ) <= <NUM_LIT:0> : <EOL> self . logger . debug ( "<STR_LIT>" ) <EOL> return web . notfound ( ) <EOL> status = int ( self . input . status ) <EOL> if status == STORAGE_POOL_START : <EOL> storagepool_start_stop_job ( self , model , pools_obj [ <NUM_LIT:0> ] , '<STR_LIT:start>' ) <EOL> elif status == STORAGE_POOL_STOP : <EOL> if kvc . is_used_storage_pool ( name = pools_obj [ <NUM_LIT:0> ] . get_storage_name ( ) , <EOL> active_only = True ) is True : <EOL> self . logger . debug ( "<STR_LIT>" ) <EOL> return web . badrequest ( "<STR_LIT>" ) <EOL> else : <EOL> storagepool_start_stop_job ( self , model , pools_obj [ <NUM_LIT:0> ] , '<STR_LIT>' ) <EOL> else : <EOL> self . logger . debug ( "<STR_LIT>" ) <EOL> return web . badrequest ( ) <EOL> return web . accepted ( ) <EOL> finally : <EOL> kvc . close ( ) <EOL> def storagepool_start_stop_job ( obj , host , pool_obj , status ) : <EOL> _iscsi_job = None <EOL> if status == '<STR_LIT:start>' : <EOL> pool_info = pool_obj . get_info ( ) <EOL> if pool_info [ '<STR_LIT:type>' ] . lower ( ) == "<STR_LIT>" : <EOL> _cmd = dict2command ( <EOL> "<STR_LIT>" % ( karesansui . config [ '<STR_LIT>' ] , ISCSI_COMMAND_START ) , <EOL> { "<STR_LIT>" : pool_info [ '<STR_LIT:source>' ] [ '<STR_LIT>' ] } ) <EOL> cmdname = "<STR_LIT>" <EOL> _iscsi_job = Job ( '<STR_LIT>' % cmdname , <NUM_LIT:0> , _cmd ) <EOL> _cmd = dict2command ( <EOL> "<STR_LIT>" % ( karesansui . config [ '<STR_LIT>' ] , VIRT_COMMAND_START_STORAGE_POOL ) , <EOL> { "<STR_LIT:name>" : pool_obj . get_storage_name ( ) } ) <EOL> cmdname = "<STR_LIT>" <EOL> else : <EOL> _cmd = dict2command ( <EOL> "<STR_LIT>" % ( karesansui . config [ '<STR_LIT>' ] , VIRT_COMMAND_DESTROY_STORAGE_POOL ) , <EOL> { "<STR_LIT:name>" : pool_obj . get_storage_name ( ) } ) <EOL> cmdname = "<STR_LIT>" <EOL> _jobgroup = JobGroup ( cmdname , karesansui . sheconf [ '<STR_LIT>' ] ) <EOL> if _iscsi_job : <EOL> _jobgroup . jobs . append ( _iscsi_job ) <EOL> _job = Job ( '<STR_LIT>' % cmdname , <NUM_LIT:1> , _cmd ) <EOL> else : <EOL> _job = Job ( '<STR_LIT>' % cmdname , <NUM_LIT:0> , _cmd ) <EOL> _jobgroup . jobs . append ( _job ) <EOL> _machine2jobgroup = m2j_new ( machine = host , <EOL> jobgroup_id = - <NUM_LIT:1> , <EOL> uniq_key = karesansui . sheconf [ '<STR_LIT>' ] , <EOL> created_user = obj . me , <EOL> modified_user = obj . me , <EOL> ) <EOL> save_job_collaboration ( obj . orm , <EOL> obj . pysilhouette . orm , <EOL> _machine2jobgroup , <EOL> _jobgroup , <EOL> ) <EOL> urls = ( <EOL> '<STR_LIT>' , HostBy1StoragePoolBy1Status , <EOL> ) </s>
