(ch *cmdutil.Helper)
| 11 | ) |
| 12 | |
| 13 | func DeleteInstanceCmd(ch *cmdutil.Helper) *cobra.Command { |
| 14 | var audience string |
| 15 | |
| 16 | cmd := &cobra.Command{ |
| 17 | Use: "delete-instance <host> <instance_id>", |
| 18 | Args: cobra.ExactArgs(2), |
| 19 | Short: "Forcefully deletes an instance on a runtime", |
| 20 | Long: "Forcefully deletes an instance on a runtime. Should only be used to clean up orphaned instances. Use `sudo project hibernate` or `project delete` to tear down healthy deployments.", |
| 21 | RunE: func(cmd *cobra.Command, args []string) error { |
| 22 | // Parse args |
| 23 | host := args[0] |
| 24 | instanceID := args[1] |
| 25 | if audience == "" { |
| 26 | audience = host |
| 27 | } |
| 28 | |
| 29 | // Obtain a manager token for the host |
| 30 | client, err := ch.Client() |
| 31 | if err != nil { |
| 32 | return err |
| 33 | } |
| 34 | tokenRes, err := client.SudoIssueRuntimeManagerToken(cmd.Context(), &adminv1.SudoIssueRuntimeManagerTokenRequest{ |
| 35 | Host: audience, |
| 36 | }) |
| 37 | if err != nil { |
| 38 | return err |
| 39 | } |
| 40 | token := tokenRes.Token |
| 41 | |
| 42 | // Open a connection to the runtime |
| 43 | rt, err := runtimeclient.New(host, token) |
| 44 | if err != nil { |
| 45 | return fmt.Errorf("failed to connect to runtime: %w", err) |
| 46 | } |
| 47 | |
| 48 | // Delete the instance |
| 49 | _, err = rt.DeleteInstance(cmd.Context(), &runtimev1.DeleteInstanceRequest{InstanceId: instanceID}) |
| 50 | if err != nil { |
| 51 | return fmt.Errorf("failed to delete instance: %w", err) |
| 52 | } |
| 53 | |
| 54 | return nil |
| 55 | }, |
| 56 | } |
| 57 | |
| 58 | cmd.Flags().StringVar(&audience, "audience", "", "Override JWT audience if it differs from the host") |
| 59 | |
| 60 | return cmd |
| 61 | } |
no test coverage detected