@@ -415,8 +415,23 @@ func NewCmdUHostCreate() *cobra.Command {
415415 wg := & sync.WaitGroup {}
416416 tokens := make (chan struct {}, concurrent )
417417 wg .Add (count )
418+ batchRename , err := regexp .Match (`[%d,%d]` , []byte (* req .Name ))
419+ if err != nil || ! batchRename {
420+ batchRename = false
421+ }
422+ if batchRename {
423+ var actualRequest uhost.CreateUHostInstanceRequest
424+ actualRequest = * req
425+ if len (bindEipIDs ) > 0 {
426+ if len (bindEipIDs ) != count {
427+ return fmt .Errorf ("bind-eip count should be equal to uhost count" )
428+ }
429+ actualRequest .NetworkInterface = nil
430+ }
431+ wg .Add (1 - count )
432+ createMultipleUhostWrapper (& actualRequest , count , updateEIPReq , bindEipIDs , async , make (chan bool , 1 ), wg , tokens )
418433
419- if count <= 5 {
434+ } else if count <= 5 {
420435 for i := 0 ; i < count ; i ++ {
421436 bindEipID := ""
422437 if len (bindEipIDs ) > i {
@@ -560,6 +575,23 @@ func NewCmdUHostCreate() *cobra.Command {
560575 return cmd
561576}
562577
578+ // createMultipleUhostWrapper 处理UI和并发控制
579+ func createMultipleUhostWrapper (req * uhost.CreateUHostInstanceRequest , count int , updateEIPReq * unet.UpdateEIPAttributeRequest , bindEipIDs []string , async bool , retCh chan <- bool , wg * sync.WaitGroup , tokens chan struct {}) {
580+ //控制并发数量
581+ tokens <- struct {}{}
582+ defer func () {
583+ <- tokens
584+ //设置延时,使报错能渲染出来
585+ time .Sleep (time .Second / 5 )
586+ wg .Done ()
587+ }()
588+
589+ success , logs := createMultipleUhost (req , count , updateEIPReq , bindEipIDs , async )
590+ retCh <- success
591+ logs = append (logs , fmt .Sprintf ("result:%t" , success ))
592+ base .LogInfo (logs ... )
593+ }
594+
563595// createUhostWrapper 处理UI和并发控制
564596func createUhostWrapper (req * uhost.CreateUHostInstanceRequest , updateEIPReq * unet.UpdateEIPAttributeRequest , bindEipID string , async bool , retCh chan <- bool , wg * sync.WaitGroup , tokens chan struct {}, idx int ) {
565597 //控制并发数量
@@ -577,6 +609,90 @@ func createUhostWrapper(req *uhost.CreateUHostInstanceRequest, updateEIPReq *une
577609 base .LogInfo (logs ... )
578610}
579611
612+ func createMultipleUhost (req * uhost.CreateUHostInstanceRequest , count int , updateEIPReq * unet.UpdateEIPAttributeRequest , bindEipIDs []string , async bool ) (bool , []string ) {
613+ resp , err := base .BizClient .CreateUHostInstance (req )
614+ block := ux .NewBlock ()
615+ ux .Doc .Append (block )
616+ logs := []string {"==================================================" }
617+ logs = append (logs , fmt .Sprintf ("api:CreateUHostInstance, request:%v" , base .ToQueryMap (req )))
618+ if err != nil {
619+ logs = append (logs , fmt .Sprintf ("err:%v" , err ))
620+ block .Append (base .ParseError (err ))
621+ return false , logs
622+ }
623+ if len (bindEipIDs ) > 0 && len (bindEipIDs ) != count {
624+ block .Append (fmt .Sprintf ("expect eip count %d, accept %d" , count , len (bindEipIDs )))
625+ return false , logs
626+ }
627+
628+ logs = append (logs , fmt .Sprintf ("resp:%#v" , resp ))
629+ if req .MaxCount == nil {
630+ req .MaxCount = sdk .Int (1 )
631+ }
632+ req .MaxCount = sdk .Int (count )
633+ if len (resp .UHostIds ) != * req .MaxCount {
634+ block .Append (fmt .Sprintf ("expect uhost count %d, accept %d" , count , len (resp .UHostIds )))
635+ return false , logs
636+ }
637+ for i , uhostID := range resp .UHostIds {
638+ text := fmt .Sprintf ("the uhost[%s]" , uhostID )
639+ if len (req .Disks ) > 1 {
640+ text = fmt .Sprintf ("%s which attached a data disk" , text )
641+ if len (req .NetworkInterface ) > 0 {
642+ text = fmt .Sprintf ("%s and binded an eip" , text )
643+ }
644+ } else if len (req .NetworkInterface ) > 0 {
645+ text = fmt .Sprintf ("%s which binded an eip" , text )
646+ }
647+ text = fmt .Sprintf ("%s is initializing" , text )
648+
649+ if async {
650+ block .Append (text )
651+ } else {
652+ uhostSpoller .Sspoll (resp .UHostIds [0 ], text , []string {status .HOST_RUNNING , status .HOST_FAIL }, block , & req .CommonBase )
653+ }
654+ bindEipID := bindEipIDs [i ]
655+ if bindEipID != "" {
656+ eip := base .PickResourceID (bindEipID )
657+ logs = append (logs , fmt .Sprintf ("bind eip: %s" , eip ))
658+ eipLogs , err := sbindEIP (sdk .String (uhostID ), sdk .String ("uhost" ), & eip , req .ProjectId , req .Region )
659+ logs = append (logs , eipLogs ... )
660+ if err != nil {
661+ block .Append (fmt .Sprintf ("bind eip[%s] with uhost[%s] failed: %v" , eip , uhostID , err ))
662+ return false , logs
663+ }
664+ block .Append (fmt .Sprintf ("bind eip[%s] with uhost[%s] successfully" , eip , uhostID ))
665+ } else if len (req .NetworkInterface ) > 0 {
666+ ipSet , err := getEIPByUHostId (uhostID )
667+ if err != nil {
668+ block .Append (err .Error ())
669+ return false , logs
670+ }
671+ block .Append (fmt .Sprintf ("IP:%s Line:%s" , ipSet .IP , ipSet .Type ))
672+ if * updateEIPReq .Name != "" || * updateEIPReq .Remark != "" {
673+ var message string
674+ if * updateEIPReq .Name != "" && * updateEIPReq .Remark != "" {
675+ message = "name and remark"
676+ } else if * updateEIPReq .Name != "" {
677+ message = "name"
678+ } else {
679+ message = "remark"
680+ }
681+
682+ logs = append (logs , fmt .Sprintf ("update attribute %s of eip[%s] binded uhost[%s]" , message , ipSet .IPId , uhostID ))
683+ updateEIPReq .EIPId = sdk .String (ipSet .IPId )
684+ _ , err = base .BizClient .UpdateEIPAttribute (updateEIPReq )
685+ if err != nil {
686+ block .Append (fmt .Sprintf ("update attribute %s of eip[%s] binded uhost[%s] got err, %s" , message , ipSet .IPId , uhostID , err ))
687+ return false , logs
688+ }
689+ block .Append (fmt .Sprintf ("update attribute %s of eip[%s] binded uhost[%s] successfully" , message , ipSet .IPId , uhostID ))
690+ }
691+ }
692+ }
693+ return true , logs
694+ }
695+
580696func createUhost (req * uhost.CreateUHostInstanceRequest , updateEIPReq * unet.UpdateEIPAttributeRequest , bindEipID string , async bool ) (bool , []string ) {
581697 resp , err := base .BizClient .CreateUHostInstance (req )
582698 block := ux .NewBlock ()
0 commit comments