diff --git a/CHANGELOG.md b/CHANGELOG.md index 6b864af8..56e53e4a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +# 0.0.129 (Nov 12, 2024) +* Added `nullstone iac generate` command to export workspace config to an IaC file. + # 0.0.128 (Nov 05, 2024) * Fixed nil panic when performing `nullstone iac test` without any IaC files. diff --git a/cmd/iac.go b/cmd/iac.go index 146a799e..91a54c6b 100644 --- a/cmd/iac.go +++ b/cmd/iac.go @@ -1,11 +1,13 @@ package cmd import ( + "bytes" "context" "fmt" "github.com/urfave/cli/v2" "gopkg.in/nullstone-io/go-api-client.v0" "gopkg.in/nullstone-io/go-api-client.v0/find" + "gopkg.in/nullstone-io/go-api-client.v0/types" iac2 "gopkg.in/nullstone-io/nullstone.v0/iac" "os" ) @@ -16,6 +18,7 @@ var Iac = &cli.Command{ UsageText: "nullstone iac [subcommand]", Subcommands: []*cli.Command{ IacTest, + IacGenerate, }, } @@ -67,3 +70,32 @@ var IacTest = &cli.Command{ }) }, } + +var IacGenerate = &cli.Command{ + Name: "generate", + Description: "Generate IaC from a Nullstone stack for apps", + Usage: "Generate IaC from an application workspace", + UsageText: "nullstone iac --stack= --env= --app=", + Flags: []cli.Flag{ + StackFlag, + EnvFlag, + &cli.StringFlag{ + Name: "block", + Usage: "Name of the block to use for this operation", + EnvVars: []string{"NULLSTONE_BLOCK"}, + }, + }, + Action: func(c *cli.Context) error { + return BlockWorkspaceAction(c, func(ctx context.Context, cfg api.Config, stack types.Stack, block types.Block, env types.Environment, workspace types.Workspace) error { + apiClient := api.Client{Config: cfg} + buf := bytes.NewBufferString("") + err := apiClient.WorkspaceConfigFiles().GetConfigFile(ctx, stack.Id, block.Id, env.Id, buf) + if err != nil { + return err + } + + fmt.Fprintln(os.Stdout, buf.String()) + return nil + }) + }, +} diff --git a/docs/CLI.md b/docs/CLI.md index 1dc7e44f..d50a6e72 100644 --- a/docs/CLI.md +++ b/docs/CLI.md @@ -134,7 +134,7 @@ $ nullstone envs new --name= --stack= [--provider=] [--pr #### Options | Option | Description | | | --- | --- | --- | -| `--name` | Provide a name for this new environment | required | +| `--name` | Provide a name for this new environment. If creating a preview environment, we recommend ``branch`-`pull_request_id``. | required | | `--stack` | Name of the stack to use for this operation | required | | `--preview` | Use this flag to create a preview environment. If not set, a standard environment will be created. | | | `--provider` | Select the name of the provider to use for this environment. When creating a preview environment, this parameter will not be used. | | @@ -142,8 +142,26 @@ $ nullstone envs new --name= --stack= [--provider=] [--pr | `--zone` | For GCP, select the zone to launch infrastructure for this environment. Defaults to us-east1b | | +## envs delete +Deletes the given environment. Before issuing this command, make sure you have destroyed all infrastructure in the environment. If you are deleting a preview environment, you can use the `--force` flag to skip the confirmation prompt. + +#### Usage +```shell +$ nullstone envs delete --stack= --env= [--force] +``` + +#### Options +| Option | Description | | +| --- | --- | --- | +| `--stack` | Name of the stack to use for this operation | required | +| `--env` | Name of the environment to use for this operation | required | +| `--force` | Use this flag to skip the confirmation prompt when deleting an environment. | | + + ## envs up -Launches an entire environment including all of its apps. This command can be used to stand up an entire preview environment. +Launches an entire environment including all of its apps. +This command can be used to stand up an entire preview environment. +This will only build/deploy apps that have auto-deploy enabled. #### Usage ```shell @@ -158,7 +176,8 @@ $ nullstone envs up --stack= --env= ## envs down -Destroys all the apps in an environment and all their dependent infrastructure. This command is useful for tearing down preview environments once you are finished with them. +Destroys all infrastructure in an environment. +This command is useful for tearing down preview environments once you are finished with them. #### Usage ```shell @@ -186,7 +205,41 @@ $ nullstone exec [--stack=] --app= --env= [optio | `--stack` | Scope this operation to a specific stack. This is only required if there are multiple blocks/apps with the same name. | | | `--app` | Name of the app to use for this operation | | | `--env` | Name of the environment to use for this operation | required | -| `--task` | Select a specific task/replica to execute the command against. This is optional and by default will connect to a random task/replica. If using Kubernetes, this will select which replica of the pod to connect. If using ECS, this will select which task of the service to connect. | | +| `--instance` | Select a specific instance to execute the command against. This allows the user to decide which instance to connect. This is optional and by default will connect to a random instance. This is only used for workspaces that use VMs (e.g. Elastic Beanstalk, EC2 Instances, GCP VMs, Azure VMs, etc.). | | +| `--task` | Select a specific task to execute the command against. This is optional and by default will connect to a random task. This is only used by ECS and determines which task to connect. | | +| `--pod` | Select a pod to execute the command against. When specified, allows you to connect to a specific pod within a replica set. This is optional and will connect to a random pod by default. This is only used by Kubernetes clusters and determines which pod in the replica to connect. | | +| `--container` | Select a specific container within a task or pod. If using sidecars, this allows you to connect to other containers besides the primary application container. | | + + +## iac test +Test the current repository's IaC files against a Nullstone stack. + +#### Usage +```shell +$ nullstone iac test --stack= --env= +``` + +#### Options +| Option | Description | | +| --- | --- | --- | +| `--stack` | Scope this operation to a specific stack. This is only required if there are multiple blocks/apps with the same name. | | +| `--env` | Name of the environment to use for this operation | required | + + +## iac generate +Generate IaC from a Nullstone stack for apps + +#### Usage +```shell +$ nullstone iac --stack= --env= --app= +``` + +#### Options +| Option | Description | | +| --- | --- | --- | +| `--stack` | Scope this operation to a specific stack. This is only required if there are multiple blocks/apps with the same name. | | +| `--env` | Name of the environment to use for this operation | required | +| `--block` | Name of the block to use for this operation | | ## launch @@ -363,7 +416,10 @@ $ nullstone ssh [--stack=] --app= --env= [option | `--stack` | Scope this operation to a specific stack. This is only required if there are multiple blocks/apps with the same name. | | | `--app` | Name of the app to use for this operation | | | `--env` | Name of the environment to use for this operation | required | -| `--task` | Select a specific task/replica to execute the command against. This is optional and by default will connect to a random task/replica. If using Kubernetes, this will select which replica of the pod to connect. If using ECS, this will select which task of the service to connect. | | +| `--instance` | Select a specific instance to execute the command against. This allows the user to decide which instance to connect. This is optional and by default will connect to a random instance. This is only used for workspaces that use VMs (e.g. Elastic Beanstalk, EC2 Instances, GCP VMs, Azure VMs, etc.). | | +| `--task` | Select a specific task to execute the command against. This is optional and by default will connect to a random task. This is only used by ECS and determines which task to connect. | | +| `--pod` | Select a pod to execute the command against. When specified, allows you to connect to a specific pod within a replica set. This is optional and will connect to a random pod by default. This is only used by Kubernetes clusters and determines which pod in the replica to connect. | | +| `--container` | Select a specific container within a task or pod. If using sidecars, this allows you to connect to other containers besides the primary application container. | | | `--forward, -L` | Use this to forward ports from host to local machine. Format: `local-port`:[`remote-host`]:`remote-port` | | @@ -415,7 +471,7 @@ $ nullstone status [--stack=] --app= [--env=] [o ## up -Launches the infrastructure for the given block/environment and it's dependencies. +Launches the infrastructure for the given block/environment and its dependencies. #### Usage ```shell @@ -440,6 +496,28 @@ Prints the version of the CLI. nullstone -v ``` +## wait +Waits for a workspace to reach a specific status. +This is helpful to wait for infrastructure to provision or an app to deploy. +Currently, this supports --for=launched to wait for a workspace to provision. +In the future, we will add --for=destroyed and --for=deployed. + +#### Usage +```shell +$ nullstone wait [--stack=] --block= --env= [options] +``` + +#### Options +| Option | Description | | +| --- | --- | --- | +| `--stack` | Scope this operation to a specific stack. This is only required if there are multiple blocks/apps with the same name. | | +| `--block` | Name of the block to use for this operation | required | +| `--env` | Name of the environment to use for this operation | required | +| `--for` | Configure the wait command to reach a specific status. Currently this supports --for=launched. In the future, we will support --for=destroyed and --for=deployed | | +| `--timeout` | Set --timeout to a golang duration to control how long to wait for a status before cancelling. The default is '1h' (1 hour). | | +| `--approval-timeout` | Set --approval-timeout to a golang duration to control how long to wait for approval before cancelling. If the workspace run never reaches "needs-approval", this has no effect. The default is '15m' (15 minutes). | | + + ## workspaces select Sync a given workspace's state with the current directory. Running this command will allow you to run terraform plans/applies locally against the selected workspace. diff --git a/go.mod b/go.mod index 2c084933..a4af4ad8 100644 --- a/go.mod +++ b/go.mod @@ -21,7 +21,7 @@ require ( github.com/urfave/cli/v2 v2.3.0 golang.org/x/crypto v0.18.0 golang.org/x/sync v0.5.0 - gopkg.in/nullstone-io/go-api-client.v0 v0.0.0-20241104165420-28a5aae528a6 + gopkg.in/nullstone-io/go-api-client.v0 v0.0.0-20241112232234-2148d6bf54f3 k8s.io/api v0.27.2 k8s.io/apimachinery v0.27.2 k8s.io/client-go v0.27.2 diff --git a/go.sum b/go.sum index 7efdca88..c62ec950 100644 --- a/go.sum +++ b/go.sum @@ -1460,8 +1460,8 @@ gopkg.in/gemnasium/logrus-airbrake-hook.v2 v2.1.2/go.mod h1:Xk6kEKp8OKb+X14hQBKW gopkg.in/inf.v0 v0.9.1 h1:73M5CoZyi3ZLMOyDlQh031Cx6N9NDJ2Vvfl76EDAgDc= gopkg.in/inf.v0 v0.9.1/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw= gopkg.in/natefinch/lumberjack.v2 v2.0.0/go.mod h1:l0ndWWf7gzL7RNwBG7wST/UCcT4T24xpD6X8LsfU/+k= -gopkg.in/nullstone-io/go-api-client.v0 v0.0.0-20241104165420-28a5aae528a6 h1:pBqC7B6hjZH+GeFfrKb9cCSabCAlQ8o2Ij0o96GTwUY= -gopkg.in/nullstone-io/go-api-client.v0 v0.0.0-20241104165420-28a5aae528a6/go.mod h1:m/JNiW4XSXjRLAedd7LYOO0l/FfCiKffRFolkKpfpgA= +gopkg.in/nullstone-io/go-api-client.v0 v0.0.0-20241112232234-2148d6bf54f3 h1:oGJth7QLGSx2xIxIiJYNRI7r91ecr6X42+fTlYe7Clg= +gopkg.in/nullstone-io/go-api-client.v0 v0.0.0-20241112232234-2148d6bf54f3/go.mod h1:m/JNiW4XSXjRLAedd7LYOO0l/FfCiKffRFolkKpfpgA= gopkg.in/resty.v1 v1.12.0/go.mod h1:mDo4pnntr5jdWRML875a/NmxYqAlA73dVijT2AXvQQo= gopkg.in/rethinkdb/rethinkdb-go.v6 v6.2.1 h1:d4KQkxAaAiRY2h5Zqis161Pv91A37uZyJOx73duwUwM= gopkg.in/rethinkdb/rethinkdb-go.v6 v6.2.1/go.mod h1:WbjuEoo1oadwzQ4apSDU+JTvmllEHtsNHS6y7vFc7iw=