|
| 1 | +package app |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/urfave/cli/v2" |
| 7 | + "google.golang.org/grpc" |
| 8 | + |
| 9 | + "github.com/temporalio/tcld/protogen/api/cloud/cloudservice/v1" |
| 10 | + "github.com/temporalio/tcld/protogen/api/cloud/namespace/v1" |
| 11 | +) |
| 12 | + |
| 13 | +type ( |
| 14 | + MigrationClient struct { |
| 15 | + client cloudservice.CloudServiceClient |
| 16 | + ctx context.Context |
| 17 | + } |
| 18 | + GetMigrationClientFn func(ctx *cli.Context) (*MigrationClient, error) |
| 19 | +) |
| 20 | + |
| 21 | +func NewMigrationClient(ctx context.Context, conn *grpc.ClientConn) *MigrationClient { |
| 22 | + return &MigrationClient{ |
| 23 | + client: cloudservice.NewCloudServiceClient(conn), |
| 24 | + ctx: ctx, |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +func GetMigrationClient(ctx *cli.Context) (*MigrationClient, error) { |
| 29 | + ct, conn, err := GetServerConnection(ctx) |
| 30 | + if err != nil { |
| 31 | + return nil, err |
| 32 | + } |
| 33 | + return NewMigrationClient(ct, conn), nil |
| 34 | +} |
| 35 | + |
| 36 | +func (c *MigrationClient) getMigration(migrationId string) (*namespace.Migration, error) { |
| 37 | + resp, err := c.client.GetMigration(c.ctx, &cloudservice.GetMigrationRequest{ |
| 38 | + MigrationId: migrationId, |
| 39 | + }) |
| 40 | + if err != nil { |
| 41 | + return nil, err |
| 42 | + } |
| 43 | + return resp.Migration, nil |
| 44 | +} |
| 45 | + |
| 46 | +func (c *MigrationClient) listMigrations() error { |
| 47 | + totalRes := &cloudservice.GetMigrationsResponse{} |
| 48 | + pageToken := "" |
| 49 | + for { |
| 50 | + resp, err := c.client.GetMigrations(c.ctx, &cloudservice.GetMigrationsRequest{ |
| 51 | + PageToken: pageToken, |
| 52 | + }) |
| 53 | + if err != nil { |
| 54 | + return err |
| 55 | + } |
| 56 | + totalRes.Migrations = append(totalRes.Migrations, resp.Migrations...) |
| 57 | + pageToken = resp.NextPageToken |
| 58 | + if len(pageToken) == 0 { |
| 59 | + return PrintProto(totalRes) |
| 60 | + } |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +func (c *MigrationClient) startMigration(requestId, migrationEndpointId, sourceNamespace, targetNamespace string) error { |
| 65 | + resp, err := c.client.StartMigration(c.ctx, &cloudservice.StartMigrationRequest{ |
| 66 | + Spec: &namespace.MigrationSpec{ |
| 67 | + MigrationEndpointId: migrationEndpointId, |
| 68 | + Spec: &namespace.MigrationSpec_ToCloudSpec{ |
| 69 | + ToCloudSpec: &namespace.MigrationToCloudSpec{ |
| 70 | + SourceNamespace: sourceNamespace, |
| 71 | + TargetNamespace: targetNamespace, |
| 72 | + }, |
| 73 | + }, |
| 74 | + }, |
| 75 | + AsyncOperationId: requestId, |
| 76 | + }) |
| 77 | + if err != nil { |
| 78 | + return err |
| 79 | + } |
| 80 | + return PrintProto(resp) |
| 81 | +} |
| 82 | + |
| 83 | +func (c *MigrationClient) migrationHandover(requestId, migrationId, replicaId string) error { |
| 84 | + resp, err := c.client.HandoverNamespace(c.ctx, &cloudservice.HandoverNamespaceRequest{ |
| 85 | + MigrationId: migrationId, |
| 86 | + ToReplicaId: replicaId, |
| 87 | + AsyncOperationId: requestId, |
| 88 | + }) |
| 89 | + if err != nil { |
| 90 | + return err |
| 91 | + } |
| 92 | + return PrintProto(resp) |
| 93 | +} |
| 94 | + |
| 95 | +func (c *MigrationClient) confirmMigration(requestId, migrationId string) error { |
| 96 | + resp, err := c.client.ConfirmMigration(c.ctx, &cloudservice.ConfirmMigrationRequest{ |
| 97 | + MigrationId: migrationId, |
| 98 | + AsyncOperationId: requestId, |
| 99 | + }) |
| 100 | + if err != nil { |
| 101 | + return err |
| 102 | + } |
| 103 | + return PrintProto(resp) |
| 104 | +} |
| 105 | + |
| 106 | +func (c *MigrationClient) abortMigration(requestId, migrationId string) error { |
| 107 | + resp, err := c.client.AbortMigration(c.ctx, &cloudservice.AbortMigrationRequest{ |
| 108 | + MigrationId: migrationId, |
| 109 | + AsyncOperationId: requestId, |
| 110 | + }) |
| 111 | + if err != nil { |
| 112 | + return err |
| 113 | + } |
| 114 | + return PrintProto(resp) |
| 115 | +} |
| 116 | + |
| 117 | +func NewMigrationCommand(getMigrationClient GetMigrationClientFn) (CommandOut, error) { |
| 118 | + var c *MigrationClient |
| 119 | + migrationIdFlag := &cli.StringFlag{ |
| 120 | + Name: "id", |
| 121 | + Aliases: []string{"i"}, |
| 122 | + Usage: "Migration id", |
| 123 | + Required: true, |
| 124 | + } |
| 125 | + migrationEndpointIdFlag := &cli.StringFlag{ |
| 126 | + Name: "endpoint-id", |
| 127 | + Aliases: []string{"e"}, |
| 128 | + Usage: "Migration endpoint id", |
| 129 | + Required: true, |
| 130 | + } |
| 131 | + sourceNamespaceFlag := &cli.StringFlag{ |
| 132 | + Name: "source-namespace", |
| 133 | + Aliases: []string{"s"}, |
| 134 | + Usage: "Source namespace name", |
| 135 | + Required: true, |
| 136 | + } |
| 137 | + targetNamespaceFlag := &cli.StringFlag{ |
| 138 | + Name: "target-namespace", |
| 139 | + Aliases: []string{"t"}, |
| 140 | + Usage: "Target namespace name", |
| 141 | + Required: true, |
| 142 | + } |
| 143 | + toReplicaIdFlag := &cli.StringFlag{ |
| 144 | + Name: "to-replica-id", |
| 145 | + Aliases: []string{"rp"}, |
| 146 | + Usage: "The id of the replica to make active", |
| 147 | + Required: true, |
| 148 | + } |
| 149 | + |
| 150 | + return CommandOut{ |
| 151 | + Command: &cli.Command{ |
| 152 | + Name: "migration", |
| 153 | + Aliases: []string{"m"}, |
| 154 | + Before: func(ctx *cli.Context) error { |
| 155 | + var err error |
| 156 | + c, err = getMigrationClient(ctx) |
| 157 | + return err |
| 158 | + }, |
| 159 | + Usage: "(private preview) Manage migrations between self-hosted Temporal and Temporal cloud", |
| 160 | + Subcommands: []*cli.Command{ |
| 161 | + { |
| 162 | + Name: "get", |
| 163 | + Aliases: []string{"g"}, |
| 164 | + Usage: "Get a migration", |
| 165 | + Flags: []cli.Flag{ |
| 166 | + migrationIdFlag, |
| 167 | + }, |
| 168 | + Action: func(ctx *cli.Context) error { |
| 169 | + id := ctx.String(migrationIdFlag.Name) |
| 170 | + m, err := c.getMigration(id) |
| 171 | + if err != nil { |
| 172 | + return err |
| 173 | + } |
| 174 | + return PrintProto(m) |
| 175 | + }, |
| 176 | + }, |
| 177 | + { |
| 178 | + Name: "list", |
| 179 | + Aliases: []string{"l"}, |
| 180 | + Usage: "List migrations", |
| 181 | + Flags: []cli.Flag{}, |
| 182 | + Action: func(ctx *cli.Context) error { |
| 183 | + return c.listMigrations() |
| 184 | + }, |
| 185 | + }, |
| 186 | + { |
| 187 | + Name: "start", |
| 188 | + Aliases: []string{"s"}, |
| 189 | + Usage: "Start a new migration", |
| 190 | + Flags: []cli.Flag{ |
| 191 | + RequestIDFlag, |
| 192 | + migrationEndpointIdFlag, |
| 193 | + sourceNamespaceFlag, |
| 194 | + targetNamespaceFlag, |
| 195 | + }, |
| 196 | + Action: func(ctx *cli.Context) error { |
| 197 | + return c.startMigration( |
| 198 | + ctx.String(RequestIDFlag.Name), |
| 199 | + ctx.String(migrationEndpointIdFlag.Name), |
| 200 | + ctx.String(sourceNamespaceFlag.Name), |
| 201 | + ctx.String(targetNamespaceFlag.Name), |
| 202 | + ) |
| 203 | + }, |
| 204 | + }, |
| 205 | + { |
| 206 | + Name: "handover", |
| 207 | + Aliases: []string{"s"}, |
| 208 | + Usage: "Handover the namespace from on-prem to cloud, or from cloud back to on-prem", |
| 209 | + Flags: []cli.Flag{ |
| 210 | + RequestIDFlag, |
| 211 | + migrationIdFlag, |
| 212 | + toReplicaIdFlag, |
| 213 | + }, |
| 214 | + Action: func(ctx *cli.Context) error { |
| 215 | + return c.migrationHandover( |
| 216 | + ctx.String(RequestIDFlag.Name), |
| 217 | + ctx.String(migrationIdFlag.Name), |
| 218 | + ctx.String(toReplicaIdFlag.Name), |
| 219 | + ) |
| 220 | + }, |
| 221 | + }, |
| 222 | + { |
| 223 | + Name: "confirm", |
| 224 | + Aliases: []string{"c"}, |
| 225 | + Usage: "Confirm the migration", |
| 226 | + Flags: []cli.Flag{ |
| 227 | + RequestIDFlag, |
| 228 | + migrationIdFlag, |
| 229 | + }, |
| 230 | + Action: func(ctx *cli.Context) error { |
| 231 | + return c.confirmMigration( |
| 232 | + ctx.String(RequestIDFlag.Name), |
| 233 | + ctx.String(migrationIdFlag.Name), |
| 234 | + ) |
| 235 | + }, |
| 236 | + }, |
| 237 | + { |
| 238 | + Name: "abort", |
| 239 | + Aliases: []string{"a"}, |
| 240 | + Usage: "Abort the migration", |
| 241 | + Flags: []cli.Flag{ |
| 242 | + RequestIDFlag, |
| 243 | + migrationIdFlag, |
| 244 | + }, |
| 245 | + Action: func(ctx *cli.Context) error { |
| 246 | + return c.abortMigration( |
| 247 | + ctx.String(RequestIDFlag.Name), |
| 248 | + ctx.String(migrationIdFlag.Name), |
| 249 | + ) |
| 250 | + }, |
| 251 | + }, |
| 252 | + }, |
| 253 | + }, |
| 254 | + }, nil |
| 255 | +} |
0 commit comments