Skip to content

Commit 6e77ab8

Browse files
authored
Add cortex env rename command (#2165)
1 parent 28019b9 commit 6e77ab8

File tree

7 files changed

+83
-6
lines changed

7 files changed

+83
-6
lines changed

cli/cmd/env.go

+21
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ func envInit() {
4545
_envDefaultCmd.Flags().SortFlags = false
4646
_envCmd.AddCommand(_envDefaultCmd)
4747

48+
_envRenameCmd.Flags().SortFlags = false
49+
_envCmd.AddCommand(_envRenameCmd)
50+
4851
_envDeleteCmd.Flags().SortFlags = false
4952
_envCmd.AddCommand(_envDeleteCmd)
5053
}
@@ -160,6 +163,24 @@ var _envDefaultCmd = &cobra.Command{
160163
},
161164
}
162165

166+
var _envRenameCmd = &cobra.Command{
167+
Use: "rename EXISTING_NAME NEW_NAME",
168+
Short: "rename an environment",
169+
Args: cobra.ExactArgs(2),
170+
Run: func(cmd *cobra.Command, args []string) {
171+
telemetry.Event("cli.env.rename")
172+
173+
oldEnvName := args[0]
174+
newEnvName := args[1]
175+
176+
if err := renameEnv(oldEnvName, newEnvName); err != nil {
177+
exit.Error(err)
178+
}
179+
180+
print.BoldFirstLine(fmt.Sprintf("renamed the %s environment to %s", oldEnvName, newEnvName))
181+
},
182+
}
183+
163184
var _envDeleteCmd = &cobra.Command{
164185
Use: "delete [ENVIRONMENT_NAME]",
165186
Short: "delete an environment configuration",

cli/cmd/lib_cli_config.go

+34
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,40 @@ func setDefaultEnv(envName string) error {
299299
return nil
300300
}
301301

302+
func renameEnv(oldEnvName string, newEnvName string) error {
303+
cliConfig, err := readCLIConfig()
304+
if err != nil {
305+
return err
306+
}
307+
308+
renamedEnv := false
309+
310+
for _, env := range cliConfig.Environments {
311+
if env.Name == newEnvName {
312+
return cliconfig.ErrorEnvironmentAlreadyConfigured(newEnvName)
313+
}
314+
315+
if env.Name == oldEnvName {
316+
env.Name = newEnvName
317+
renamedEnv = true
318+
}
319+
}
320+
321+
if !renamedEnv {
322+
return cliconfig.ErrorEnvironmentNotConfigured(oldEnvName)
323+
}
324+
325+
if cliConfig.DefaultEnvironment != nil && *cliConfig.DefaultEnvironment == oldEnvName {
326+
cliConfig.DefaultEnvironment = &newEnvName
327+
}
328+
329+
if err := writeCLIConfig(cliConfig); err != nil {
330+
return err
331+
}
332+
333+
return nil
334+
}
335+
302336
func readTelemetryConfig() (bool, error) {
303337
cliConfig, err := readCLIConfig()
304338
if err != nil {

cli/types/cliconfig/errors.go

+11-3
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,22 @@ import (
2424
)
2525

2626
const (
27-
ErrEnvironmentNotConfigured = "cliconfig.environment_not_configured"
28-
ErrDuplicateEnvironmentNames = "cliconfig.duplicate_environment_names"
27+
ErrEnvironmentNotConfigured = "cliconfig.environment_not_configured"
28+
ErrEnvironmentAlreadyConfigured = "cliconfig.environment_already_configured"
29+
ErrDuplicateEnvironmentNames = "cliconfig.duplicate_environment_names"
2930
)
3031

3132
func ErrorEnvironmentNotConfigured(envName string) error {
3233
return errors.WithStack(&errors.Error{
3334
Kind: ErrEnvironmentNotConfigured,
34-
Message: fmt.Sprintf("%s environment is not configured", envName),
35+
Message: fmt.Sprintf("there is no environment named %s", envName),
36+
})
37+
}
38+
39+
func ErrorEnvironmentAlreadyConfigured(envName string) error {
40+
return errors.WithStack(&errors.Error{
41+
Kind: ErrEnvironmentAlreadyConfigured,
42+
Message: fmt.Sprintf("there is already an environment named %s", envName),
3543
})
3644
}
3745

dev/generate_cli_md.sh

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ commands=(
4646
"env configure"
4747
"env list"
4848
"env default"
49+
"env rename"
4950
"env delete"
5051
"version"
5152
"completion"

docs/clients/cli.md

+12
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,18 @@ Flags:
225225
-h, --help help for default
226226
```
227227

228+
## env rename
229+
230+
```text
231+
rename an environment
232+
233+
Usage:
234+
cortex env rename EXISTING_NAME NEW_NAME [flags]
235+
236+
Flags:
237+
-h, --help help for rename
238+
```
239+
228240
## env delete
229241

230242
```text

docs/clusters/management/environments.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
When you create a cluster with `cortex cluster up`, an environment with the same name as your cluster is automatically created to point to your cluster and is configured to be the default environment. You can name the environment something else via the `--configure-env` flag, e.g. `cortex cluster up --configure-env prod`. You can also use the `--configure-env` flag with `cortex cluster info` to create / update the specified environment.
44

5-
You can list your environments with `cortex env list`, change the default environment with `cortex env default`, delete an environment with `cortex env delete`, and create/update an environment with `cortex env configure`.
5+
You can list your environments with `cortex env list`, change the default environment with `cortex env default`, rename an environment with `cortex env rename`, delete an environment with `cortex env delete`, and create/update an environment with `cortex env configure`.
66

77
## Multiple clusters
88

docs/clusters/management/update.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,11 @@ cortex cluster up cluster.yaml
2626

2727
In production environments, you can upgrade your cluster without downtime if you have a backend service or DNS in front of your Cortex cluster:
2828

29-
1. Spin up a new cluster. For example: `cortex cluster up new-cluster.yaml --configure-env new` (this will create a CLI environment named `new` for accessing the new cluster).
30-
1. Re-deploy your APIs in your new cluster. For example, if the name of your CLI environment for your existing cluster is `previous`, you can use `cortex get --env previous` to list all running APIs in your cluster, and re-deploy them in the new cluster by changing directories to each API's project folder and running `cortex deploy --env new`. Alternatively, you can run `cortex cluster export --name <previous_cluster_name> --region <region>` to export all of your APIs (including configuration and application code), change directories into each API/ID subfolder that was exported, and run `cortex deploy --env new`.
29+
1. Spin up a new cluster. For example: `cortex cluster up new-cluster.yaml --configure-env cortex2` (this will create a CLI environment named `cortex2` for accessing the new cluster).
30+
1. Re-deploy your APIs in your new cluster. For example, if the name of your CLI environment for your existing cluster is `cortex`, you can use `cortex get --env cortex` to list all running APIs in your cluster, and re-deploy them in the new cluster by changing directories to each API's project folder and running `cortex deploy --env cortex2`. Alternatively, you can run `cortex cluster export --name <previous_cluster_name> --region <region>` to export all of your APIs (including configuration and application code), change directories into each API/ID subfolder that was exported, and run `cortex deploy --env cortex2`.
3131
1. Route requests to your new cluster.
3232
* If you are using a custom domain: update the A record in your Route 53 hosted zone to point to your new cluster's API load balancer.
3333
* If you have a backend service which makes requests to Cortex: update your backend service to make requests to the new cluster's endpoints.
3434
* If you have a self-managed API Gateway in front of your Cortex cluster: update the routes to use new cluster's endpoints.
3535
1. Spin down your previous cluster. If you updated DNS settings, wait 24-48 hours before spinning down your previous cluster to allow the DNS cache to be flushed.
36+
1. You may now rename your new CLI environment name if you'd like (e.g. to rename it back to "cortex": `cortex env rename cortex2 cortex`)

0 commit comments

Comments
 (0)