<s> import web <EOL> import karesansui <EOL> from karesansui . lib . rest import Rest , auth <EOL> from karesansui . lib . const import VIRT_COMMAND_SET_VCPUS , VCPUS_MIN_SIZE <EOL> from karesansui . lib . virt . virt import KaresansuiVirtException , KaresansuiVirtConnection <EOL> from karesansui . lib . utils import is_int , is_param , is_empty <EOL> from karesansui . lib . checker import Checker , CHECK_EMPTY , CHECK_VALID , CHECK_MIN <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 . access . machine import findbyguest1 <EOL> from karesansui . db . model . _2pysilhouette import Job , JobGroup <EOL> from pysilhouette . command import dict2command <EOL> def validates_cpu ( obj ) : <EOL> checker = Checker ( ) <EOL> check = True <EOL> _ = obj . _ <EOL> checker . errors = [ ] <EOL> if is_param ( obj . input , '<STR_LIT>' ) : <EOL> check = checker . check_number ( <EOL> _ ( '<STR_LIT>' ) , <EOL> obj . input . max_vcpus , <EOL> CHECK_EMPTY | CHECK_VALID | CHECK_MIN , <EOL> min = VCPUS_MIN_SIZE , <EOL> ) and check <EOL> else : <EOL> check = False <EOL> checker . add_error ( _ ( '<STR_LIT>' ) % _ ( '<STR_LIT>' ) ) <EOL> if is_param ( obj . input , '<STR_LIT>' ) : <EOL> check = checker . check_number ( <EOL> _ ( "<STR_LIT>" ) , <EOL> obj . input . vcpus , <EOL> CHECK_EMPTY | CHECK_VALID | CHECK_MIN , <EOL> min = VCPUS_MIN_SIZE , <EOL> ) and check ; <EOL> obj . view . alert = checker . errors <EOL> return check <EOL> class GuestBy1Cpu ( Rest ) : <EOL> @ auth <EOL> def _GET ( self , * param , ** params ) : <EOL> ( host_id , guest_id ) = self . chk_guestby1 ( param ) <EOL> if guest_id is None : return web . notfound ( ) <EOL> model = findbyguest1 ( self . orm , guest_id ) <EOL> kvc = KaresansuiVirtConnection ( ) <EOL> try : <EOL> domname = kvc . uuid_to_domname ( model . uniq_key ) <EOL> if not domname : return web . notfound ( ) <EOL> virt = kvc . search_kvg_guests ( domname ) [ <NUM_LIT:0> ] <EOL> vcpus_info = virt . get_vcpus_info ( ) <EOL> self . view . max_vcpus_limit = kvc . get_max_vcpus ( ) <EOL> self . view . max_vcpus = vcpus_info [ '<STR_LIT>' ] <EOL> self . view . vcpus_limit = vcpus_info [ '<STR_LIT>' ] <EOL> self . view . vcpus = vcpus_info [ '<STR_LIT>' ] <EOL> self . view . cpuTime = virt . get_info ( ) [ '<STR_LIT>' ] <EOL> self . view . hypervisor = virt . get_info ( ) [ '<STR_LIT>' ] <EOL> self . view . guest = model <EOL> finally : <EOL> kvc . close ( ) <EOL> return True <EOL> @ auth <EOL> def _PUT ( self , * param , ** params ) : <EOL> """<STR_LIT>""" <EOL> ( host_id , guest_id ) = self . chk_guestby1 ( param ) <EOL> if guest_id is None : return web . notfound ( ) <EOL> if not validates_cpu ( self ) : <EOL> return web . badrequest ( self . view . alert ) <EOL> if is_param ( self . input , '<STR_LIT>' ) : <EOL> vcpus = int ( self . input . vcpus ) <EOL> else : <EOL> vcpus = None <EOL> max_vcpus = int ( self . input . max_vcpus ) <EOL> model = findbyguest1 ( self . orm , guest_id ) <EOL> kvc = KaresansuiVirtConnection ( ) <EOL> try : <EOL> domname = kvc . uuid_to_domname ( model . uniq_key ) <EOL> if not domname : return web . conflict ( web . ctx . path ) <EOL> virt = kvc . search_kvg_guests ( domname ) [ <NUM_LIT:0> ] <EOL> vcpus_info = virt . get_vcpus_info ( ) <EOL> max_vcpus_limit = kvc . get_max_vcpus ( ) <EOL> vcpus_limit = vcpus_info [ '<STR_LIT>' ] <EOL> finally : <EOL> kvc . close ( ) <EOL> if max_vcpus_limit < max_vcpus : <EOL> return web . badrequest ( "<STR_LIT>" % ( _ ( '<STR_LIT>' ) , self . input . max_vcpus_limit , self . input . max_vcpus , ) ) <EOL> if vcpus is not None : <EOL> if vcpus_limit < vcpus : <EOL> return web . badrequest ( "<STR_LIT>" % ( _ ( '<STR_LIT>' ) , self . input . vcpus_limit , self . input . vcpus , ) ) <EOL> options = { } <EOL> options [ "<STR_LIT:name>" ] = domname <EOL> if vcpus is not None : <EOL> options [ "<STR_LIT>" ] = vcpus <EOL> options [ "<STR_LIT>" ] = max_vcpus <EOL> _cmd = dict2command ( "<STR_LIT>" % ( karesansui . config [ '<STR_LIT>' ] , <EOL> VIRT_COMMAND_SET_VCPUS ) , <EOL> options ) <EOL> cmdname = "<STR_LIT>" <EOL> _jobgroup = JobGroup ( cmdname , karesansui . sheconf [ '<STR_LIT>' ] ) <EOL> _jobgroup . jobs . append ( Job ( '<STR_LIT>' % cmdname , <NUM_LIT:0> , _cmd ) ) <EOL> _machine2jobgroup = m2j_new ( machine = model , <EOL> jobgroup_id = - <NUM_LIT:1> , <EOL> uniq_key = karesansui . sheconf [ '<STR_LIT>' ] , <EOL> created_user = self . me , <EOL> modified_user = self . me , <EOL> ) <EOL> save_job_collaboration ( self . orm , <EOL> self . pysilhouette . orm , <EOL> _machine2jobgroup , <EOL> _jobgroup , <EOL> ) <EOL> return web . accepted ( ) <EOL> urls = ( '<STR_LIT>' , GuestBy1Cpu , ) </s>
