@@ -15,14 +15,18 @@ import (
1515 commonapi "yunion.io/x/onecloud/pkg/apis"
1616 computeapi "yunion.io/x/onecloud/pkg/apis/compute"
1717 api "yunion.io/x/onecloud/pkg/apis/llm"
18+ "yunion.io/x/onecloud/pkg/apis/notify"
19+ notifyapi "yunion.io/x/onecloud/pkg/apis/notify"
1820 "yunion.io/x/onecloud/pkg/cloudcommon/db"
1921 "yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
22+ "yunion.io/x/onecloud/pkg/cloudcommon/notifyclient"
2023 "yunion.io/x/onecloud/pkg/httperrors"
2124 "yunion.io/x/onecloud/pkg/llm/options"
2225 llmutils "yunion.io/x/onecloud/pkg/llm/utils"
2326 "yunion.io/x/onecloud/pkg/mcclient"
2427 "yunion.io/x/onecloud/pkg/mcclient/auth"
2528 "yunion.io/x/onecloud/pkg/mcclient/modules/compute"
29+ baseoptions "yunion.io/x/onecloud/pkg/mcclient/options"
2630 computeoptions "yunion.io/x/onecloud/pkg/mcclient/options/compute"
2731 "yunion.io/x/onecloud/pkg/util/stringutils2"
2832)
@@ -478,10 +482,31 @@ func (llm *SLLM) PerformStart(ctx context.Context, userCred mcclient.TokenCreden
478482 return nil , errors .Wrapf (errors .ErrInvalidStatus , "llm id: %s status: %s" , llm .Id , llm .Status )
479483 }
480484
481- if err := llm .StartStartTask (ctx , userCred , "" ); err != nil {
482- return nil , errors .Wrap (err , "StartStartTask" )
485+ _ , err := llm .GetVolume ()
486+ if err != nil {
487+ if errors .Cause (err ) == sql .ErrNoRows {
488+ return nil , errors .Wrapf (errors .ErrNotSupported , "llm id: %s missing volume" , llm .Id )
489+ }
490+ return nil , errors .Wrap (err , "GetVolume" )
491+ }
492+ taskinput := & api.LLMRestartTaskInput {
493+ LLMId : llm .Id ,
494+ LLMStatus : api .LLM_STATUS_READY ,
495+ }
496+ _ , err = llm .StartRestartTask (ctx , userCred , taskinput , "" )
497+ if err != nil {
498+ return nil , errors .Wrap (err , "StartRestartTask" )
499+ }
500+ return nil , nil
501+ }
502+
503+ func (d * SLLM ) StartStartTaskInternal (ctx context.Context , userCred mcclient.TokenCredential , parentTaskId string ) error {
504+ d .SetStatus (ctx , userCred , computeapi .VM_STARTING , "" )
505+ task , err := taskman .TaskManager .NewTask (ctx , "LLMStartTask" , d , userCred , nil , parentTaskId , "" , nil )
506+ if err != nil {
507+ return errors .Wrapf (err , "NewTask" )
483508 }
484- return jsonutils . Marshal (nil ), nil
509+ return task . ScheduleRun (nil )
485510}
486511
487512func (llm * SLLM ) StartStartTask (ctx context.Context , userCred mcclient.TokenCredential , parentTaskId string ) error {
@@ -504,6 +529,96 @@ func (llm *SLLM) PerformStop(ctx context.Context, userCred mcclient.TokenCredent
504529 return nil , nil
505530}
506531
532+ func (llm * SLLM ) ValidateRestartInput (ctx context.Context , userCred mcclient.TokenCredential , input * api.LLMRestartInput ) (* api.LLMRestartTaskInput , error ) {
533+ if len (llm .CmpId ) == 0 {
534+ return nil , errors .Wrap (errors .ErrInvalidStatus , "empty cmp_id" )
535+ }
536+
537+ srv , err := llm .GetServer (ctx )
538+ if err != nil {
539+ return nil , errors .Wrap (err , "GetServer" )
540+ }
541+
542+ if (llm .Status != api .LLM_STATUS_READY && llm .Status != api .LLM_STATUS_RUNNING ) || (srv .Status != computeapi .VM_READY && ! utils .IsInArray (srv .Status , computeapi .VM_RUNNING_STATUS )) {
543+ return nil , errors .Wrapf (errors .ErrInvalidStatus , "invalid llm status %s" , llm .Status )
544+ }
545+
546+ return & api.LLMRestartTaskInput {}, nil
547+ }
548+
549+ func (llm * SLLM ) PerformRestart (ctx context.Context , userCred mcclient.TokenCredential , query jsonutils.JSONObject , input * api.LLMRestartInput ) (jsonutils.JSONObject , error ) {
550+ taskInput , err := llm .ValidateRestartInput (ctx , userCred , input )
551+ if err != nil {
552+ return nil , errors .Wrap (err , "ValidateRestartInput" )
553+ }
554+ _ , err = llm .StartRestartTask (ctx , userCred , taskInput , "" )
555+ if err != nil {
556+ return nil , errors .Wrap (err , "StartRestartTask" )
557+ }
558+ return nil , nil
559+ }
560+
561+ func (llm * SLLM ) StartRestartTask (ctx context.Context , userCred mcclient.TokenCredential , params * api.LLMRestartTaskInput , parentTaskId string ) (* taskman.STask , error ) {
562+ key := "perform_restart"
563+ if params .ResetDataDisk {
564+ key = "perform_reset"
565+ }
566+ llm .SetStatus (ctx , userCred , api .LLM_STATUS_START_RESTART , key )
567+ taskName := "LLMRestartTask"
568+ if params .ResetDataDisk {
569+ taskName = "LLMResetTask"
570+ }
571+ params .LLMId = llm .Id
572+ task , err := taskman .TaskManager .NewTask (ctx , taskName , llm , userCred , jsonutils .Marshal (params ).(* jsonutils.JSONDict ), parentTaskId , "" , nil )
573+ if err != nil {
574+ return nil , errors .Wrap (err , "NewTask" )
575+ }
576+ if err := task .ScheduleRun (nil ); err != nil {
577+ return nil , errors .Wrap (err , "ScheduleRun" )
578+ }
579+ return task , nil
580+ }
581+
582+ func (llm * SLLM ) PerformReset (ctx context.Context , userCred mcclient.TokenCredential , query jsonutils.JSONObject , input * api.LLMRestartInput ) (jsonutils.JSONObject , error ) {
583+ taskInput , err := llm .ValidateRestartInput (ctx , userCred , input )
584+ if err != nil {
585+ return nil , errors .Wrap (err , "ValidateRestartInput" )
586+ }
587+ _ , err = llm .StartResetTask (ctx , userCred , taskInput , "" )
588+ if err != nil {
589+ return nil , errors .Wrap (err , "StartRestartTask" )
590+ }
591+ return nil , nil
592+ }
593+
594+ func (llm * SLLM ) StartResetTask (ctx context.Context , userCred mcclient.TokenCredential , params * api.LLMRestartTaskInput , parentTaskId string ) (* taskman.STask , error ) {
595+ llm .SetStatus (ctx , userCred , api .LLM_STATUS_START_RESTART , "perform_reset" )
596+ task , err := taskman .TaskManager .NewTask (ctx , "LLMResetTask" , llm , userCred , jsonutils .Marshal (params ).(* jsonutils.JSONDict ), parentTaskId , "" , nil )
597+ if err != nil {
598+ return nil , errors .Wrapf (err , "NewTask" )
599+ }
600+ if err := task .ScheduleRun (nil ); err != nil {
601+ return nil , errors .Wrap (err , "ScheduleRun" )
602+ }
603+ return task , nil
604+ }
605+
606+ func (llm * SLLM ) NotifyRequest (ctx context.Context , userCred mcclient.TokenCredential , action notify.SAction , model jsonutils.JSONObject , success bool ) {
607+ obj := func (ctx context.Context , details * jsonutils.JSONDict ) {}
608+ if model != nil {
609+ obj = func (ctx context.Context , details * jsonutils.JSONDict ) {
610+ details .Set ("customize_details" , model )
611+ }
612+ }
613+ notifyclient .EventNotify (ctx , userCred , notifyclient.SEventNotifyParam {
614+ Obj : llm ,
615+ Action : action ,
616+ ObjDetailsDecorator : obj ,
617+ IsFail : ! success ,
618+ ResourceType : notifyapi .TOPIC_RESOURCE_LLM ,
619+ })
620+ }
621+
507622func (llm * SLLM ) StartLLMStopTask (ctx context.Context , userCred mcclient.TokenCredential , parentTaskId string ) error {
508623 task , err := taskman .TaskManager .NewTask (ctx , "LLMStopTask" , llm , userCred , nil , parentTaskId , "" , nil )
509624 if err != nil {
@@ -628,3 +743,106 @@ func (man *SLLMManager) GetAvailableNetwork(ctx context.Context, userCred mcclie
628743
629744 return ret , nil
630745}
746+
747+ func (llm * SLLM ) StartBindVolumeTask (ctx context.Context , userCred mcclient.TokenCredential , volumeId string , autoStart bool , parenentTaskId string ) (* taskman.STask , error ) {
748+ llm .SetStatus (ctx , userCred , api .LLM_STATUS_START_BIND , "perform bind volume" )
749+ params := api.LLMVolumeInput {
750+ LLMId : llm .Id ,
751+ VolumeId : volumeId ,
752+ AutoStart : autoStart ,
753+ }
754+ task , err := taskman .TaskManager .NewTask (ctx , "LLMAttachTask" , llm , userCred , jsonutils .Marshal (params ).(* jsonutils.JSONDict ), parenentTaskId , "" , nil )
755+ if err != nil {
756+ return nil , errors .Wrap (err , "NewTask" )
757+ }
758+ err = task .ScheduleRun (nil )
759+ if err != nil {
760+ return nil , errors .Wrap (err , "ScheduleRun" )
761+ }
762+ return task , nil
763+ }
764+
765+ func (llm * SLLM ) ChangeServerNetworkConfig (ctx context.Context , bandwidth int , whitePrefixes []string , noSync bool ) error {
766+ s := auth .GetAdminSession (ctx , options .Options .Region )
767+ params := baseoptions.BaseListOptions {}
768+ params .Scope = "max"
769+ limit := 0
770+ params .Limit = & limit
771+ serverNicObjs , err := compute .Servernetworks .ListDescendent (s , llm .CmpId , jsonutils .Marshal (params ))
772+ if err != nil {
773+ return errors .Wrap (err , "compute.Servernetworks.ListDescendent" )
774+ } else if len (serverNicObjs .Data ) == 0 {
775+ return errors .Wrap (httperrors .ErrEmptyRequest , "compute.Servernetworks.ListDescendent" )
776+ }
777+ gns := computeapi.GuestnetworkDetails {}
778+ err = serverNicObjs .Data [0 ].Unmarshal (& gns )
779+ if err != nil {
780+ return errors .Wrap (err , "Unmarshal GuestnetworkDetails" )
781+ }
782+ if gns .BwLimit != bandwidth {
783+ // need to change bandwidth
784+ params := computeapi.ServerChangeBandwidthInput {}
785+ params .Mac = gns .MacAddr
786+ params .Index = 0
787+ params .Bandwidth = bandwidth
788+ params .NoSync = & noSync
789+ _ , err := compute .Servers .PerformAction (s , llm .CmpId , "change-bandwidth" , jsonutils .Marshal (params ))
790+ if err != nil {
791+ return errors .Wrap (err , "compute.Servers.PerformAction change-bandwidth" )
792+ }
793+ }
794+ /*if len(adbWhitePrefixes) > 0 {
795+ for _, pm := range gns.PortMappings {
796+ if pm.Port == apis.PHONE_ADB_PORT {
797+ // verify adb port remote ips
798+ remoteIps := stringutils2.NewSortedStrings(pm.RemoteIps)
799+ remoteIps2 := stringutils2.NewSortedStrings(adbWhitePrefixes)
800+ if !stringutils2.Equals(remoteIps, remoteIps2) {
801+ // need to update remote Ips
802+ params := computeapi.GuestnetworkUpdateInput{}
803+ for i := range gns.PortMappings {
804+ npm := gns.PortMappings[i]
805+ if gns.PortMappings[i].Port == apis.PHONE_ADB_PORT {
806+ npm.RemoteIps = adbWhitePrefixes
807+ }
808+ params.PortMappings = append(params.PortMappings, npm)
809+ }
810+ _, err := compute.Servernetworks.Update(s, gns.GuestId, gns.NetworkId, nil, jsonutils.Marshal(params))
811+ if err != nil {
812+ return errors.Wrap(err, "Servernetworks.Update")
813+ }
814+ }
815+ break
816+ }
817+ }
818+ }*/
819+ return nil
820+ }
821+
822+ func (llm * SLLM ) PerformNetConfig (
823+ ctx context.Context ,
824+ userCred mcclient.TokenCredential ,
825+ query jsonutils.JSONObject ,
826+ input api.LLMChangeNetworkInput ,
827+ ) (jsonutils.JSONObject , error ) {
828+ err := llm .ChangeServerNetworkConfig (ctx , input .BandwidthMb , input .WhitePrefxies , false )
829+ if err != nil {
830+ return nil , errors .Wrap (err , "changeServerNetworkConfig" )
831+ }
832+
833+ if llm .BandwidthMb != input .BandwidthMb {
834+ _ , err := db .Update (llm , func () error {
835+ llm .BandwidthMb = input .BandwidthMb
836+ return nil
837+ })
838+ if err != nil {
839+ return nil , errors .Wrap (err , "update" )
840+ }
841+ }
842+
843+ return nil , nil
844+ }
845+
846+ func (llm * SLLM ) purgeModelList () error {
847+ return GetLLMInstantModelManager ().purgeModelList (llm .Id )
848+ }
0 commit comments