| 43 | } |
| 44 | |
| 45 | func (i *imlDynamicModule) Online(ctx context.Context, module string, id string) error { |
| 46 | _, has := driver.Get(module) |
| 47 | if !has { |
| 48 | return fmt.Errorf("模块【%s】不存在", module) |
| 49 | } |
| 50 | //if len(clusterInput.Clusters) == 0 { |
| 51 | // return fmt.Errorf("上线分区失败,分区为空") |
| 52 | //} |
| 53 | |
| 54 | id = strings.ToLower(fmt.Sprintf("%s_%s", id, module)) |
| 55 | info, err := i.dynamicModuleService.Get(ctx, id) |
| 56 | if err != nil { |
| 57 | return fmt.Errorf("上线失败,配置不存在") |
| 58 | } |
| 59 | clusters, err := i.clusterService.List(ctx) |
| 60 | if err != nil || len(clusters) == 0 { |
| 61 | return fmt.Errorf("上线失败,集群不存在") |
| 62 | } |
| 63 | |
| 64 | return i.transaction.Transaction(ctx, func(ctx context.Context) error { |
| 65 | for _, c := range clusters { |
| 66 | |
| 67 | // 插入发布历史 |
| 68 | err = i.dynamicModulePublishService.Create(ctx, &dynamic_module.CreateDynamicModulePublish{ |
| 69 | ID: uuid.New().String(), |
| 70 | DynamicModule: id, |
| 71 | Module: module, |
| 72 | Cluster: c.Uuid, |
| 73 | Version: info.Version, |
| 74 | }) |
| 75 | if err != nil { |
| 76 | return err |
| 77 | } |
| 78 | err = i.dynamicClient(ctx, c.Uuid, module, func(dynamicClient gateway.IDynamicClient) error { |
| 79 | cfg := &gateway.DynamicRelease{} |
| 80 | err = json.Unmarshal([]byte(info.Config), &cfg) |
| 81 | if err != nil { |
| 82 | return err |
| 83 | } |
| 84 | cfg.ID = id |
| 85 | cfg.Version = info.Version |
| 86 | cfg.MatchLabels = map[string]string{ |
| 87 | "module": module, |
| 88 | } |
| 89 | err = dynamicClient.Online(ctx, cfg) |
| 90 | if err != nil { |
| 91 | return err |
| 92 | } |
| 93 | return nil |
| 94 | }) |
| 95 | if err != nil { |
| 96 | return err |
| 97 | } |
| 98 | |
| 99 | } |
| 100 | |
| 101 | return nil |
| 102 | }) |