Skip to content

Commit

Permalink
Added iac generate command (#201)
Browse files Browse the repository at this point in the history
  • Loading branch information
BSick7 authored Nov 12, 2024
1 parent 382c213 commit 3b88db4
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 9 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.

Expand Down
32 changes: 32 additions & 0 deletions cmd/iac.go
Original file line number Diff line number Diff line change
@@ -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"
)
Expand All @@ -16,6 +18,7 @@ var Iac = &cli.Command{
UsageText: "nullstone iac [subcommand]",
Subcommands: []*cli.Command{
IacTest,
IacGenerate,
},
}

Expand Down Expand Up @@ -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=<stack> --env=<env> --app=<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
})
},
}
90 changes: 84 additions & 6 deletions docs/CLI.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,16 +134,34 @@ $ nullstone envs new --name=<name> --stack=<stack> [--provider=<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. | |
| `--region` | Select which region to launch infrastructure for this environment. Defaults to us-east-1 for AWS and us-east1 for GCP. | |
| `--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=<stack> --env=<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
Expand All @@ -158,7 +176,8 @@ $ nullstone envs up --stack=<stack> --env=<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
Expand Down Expand Up @@ -186,7 +205,41 @@ $ nullstone exec [--stack=<stack-name>] --app=<app-name> --env=<env-name> [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=<stack> --env=<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=<stack> --env=<env> --app=<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
Expand Down Expand Up @@ -363,7 +416,10 @@ $ nullstone ssh [--stack=<stack-name>] --app=<app-name> --env=<env-name> [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` | |


Expand Down Expand Up @@ -415,7 +471,7 @@ $ nullstone status [--stack=<stack-name>] --app=<app-name> [--env=<env-name>] [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
Expand All @@ -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=<stack-name>] --block=<block-name> --env=<env-name> [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.

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down

0 comments on commit 3b88db4

Please sign in to comment.