This repository was archived by the owner on Aug 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
Add envs create command #149
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package integration | ||
|
||
import ( | ||
"context" | ||
"regexp" | ||
"testing" | ||
|
||
"cdr.dev/coder-cli/ci/tcli" | ||
) | ||
|
||
// From Coder organization images | ||
const ubuntuImgID = "5f443b16-30652892427b955601330fa5" | ||
|
||
func TestEnvsCLI(t *testing.T) { | ||
t.Parallel() | ||
|
||
run(t, "coder-cli-env-tests", func(t *testing.T, ctx context.Context, c *tcli.ContainerRunner) { | ||
headlessLogin(ctx, t, c) | ||
|
||
// Ensure binary is present. | ||
c.Run(ctx, "which coder").Assert(t, | ||
tcli.Success(), | ||
tcli.StdoutMatches("/usr/sbin/coder"), | ||
tcli.StderrEmpty(), | ||
) | ||
|
||
// Minimum args not received. | ||
c.Run(ctx, "coder envs create").Assert(t, | ||
tcli.StderrMatches(regexp.QuoteMeta("accepts 1 arg(s), received 0")), | ||
tcli.Error(), | ||
) | ||
|
||
// Successfully output help. | ||
c.Run(ctx, "coder envs create --help").Assert(t, | ||
tcli.Success(), | ||
tcli.StdoutMatches(regexp.QuoteMeta("Create a new environment under the active user.")), | ||
tcli.StderrEmpty(), | ||
) | ||
|
||
// TODO(Faris) : uncomment this when we can safely purge the environments | ||
// the integrations tests would create in the sidecar | ||
// Successfully create environment. | ||
// c.Run(ctx, "coder envs create --image "+ubuntuImgID+" test-ubuntu").Assert(t, | ||
// tcli.Success(), | ||
// // why does flog.Success write to stderr? | ||
// tcli.StderrMatches(regexp.QuoteMeta("Successfully created environment \"test-ubuntu\"")), | ||
// ) | ||
|
||
// Invalid environment name should fail. | ||
c.Run(ctx, "coder envs create --image "+ubuntuImgID+" this-IS-an-invalid-EnvironmentName").Assert(t, | ||
tcli.Error(), | ||
tcli.StderrMatches(regexp.QuoteMeta("environment name must conform to regex ^[a-z0-9]([a-z0-9-]+)?")), | ||
) | ||
|
||
// TODO(Faris) : uncomment this when we can safely purge the environments | ||
// the integrations tests would create in the sidecar | ||
// Successfully provision environment with fractional resource amounts | ||
// c.Run(ctx, fmt.Sprintf(`coder envs create -i %s -c 1.2 -m 1.4 non-whole-resource-amounts`, ubuntuImgID)).Assert(t, | ||
// tcli.Success(), | ||
// tcli.StderrMatches(regexp.QuoteMeta("Successfully created environment \"non-whole-resource-amounts\"")), | ||
// ) | ||
|
||
// Image does not exist should fail. | ||
c.Run(ctx, "coder envs create --image does-not-exist env-will-not-be-created").Assert(t, | ||
tcli.Error(), | ||
tcli.StderrMatches(regexp.QuoteMeta("does not exist")), | ||
) | ||
}) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,3 +27,4 @@ coder envs ls [flags] | |
### SEE ALSO | ||
|
||
* [coder envs](coder_envs.md) - Interact with Coder environments | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,3 +41,4 @@ coder envs --user [email protected] ls -o json \ | |
### SEE ALSO | ||
|
||
* [coder envs](coder_envs.md) - Interact with Coder environments | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,3 +26,4 @@ coder secrets ls [flags] | |
### SEE ALSO | ||
|
||
* [coder secrets](coder_secrets.md) - Interact with Coder Secrets | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,15 @@ import ( | |
"golang.org/x/xerrors" | ||
) | ||
|
||
const ( | ||
defaultOrg = "default" | ||
defaultImgTag = "latest" | ||
defaultCPUCores float32 = 1 | ||
defaultMemGB float32 = 1 | ||
defaultDiskGB = 10 | ||
defaultGPUs = 0 | ||
) | ||
|
||
func envsCommand() *cobra.Command { | ||
var outputFmt string | ||
var user string | ||
|
@@ -64,9 +73,9 @@ func envsCommand() *cobra.Command { | |
lsCmd.Flags().StringVarP(&outputFmt, "output", "o", "human", "human | json") | ||
cmd.AddCommand(lsCmd) | ||
cmd.AddCommand(stopEnvCommand(&user)) | ||
|
||
cmd.AddCommand(watchBuildLogCommand()) | ||
cmd.AddCommand(rebuildEnvCommand()) | ||
cmd.AddCommand(createEnvCommand()) | ||
return cmd | ||
} | ||
|
||
|
@@ -125,3 +134,76 @@ coder envs --user [email protected] ls -o json \ | |
}, | ||
} | ||
} | ||
|
||
func createEnvCommand() *cobra.Command { | ||
var ( | ||
org string | ||
img string | ||
tag string | ||
follow bool | ||
) | ||
|
||
cmd := &cobra.Command{ | ||
Use: "create [environment_name]", | ||
Short: "create a new environment.", | ||
Args: cobra.ExactArgs(1), | ||
// Don't unhide this command until we can pass image names instead of image id's. | ||
Hidden: true, | ||
Long: "Create a new environment under the active user.", | ||
Example: `# create a new environment using default resource amounts | ||
coder envs create --image 5f443b16-30652892427b955601330fa5 my-env-name | ||
|
||
# create a new environment using custom resource amounts | ||
coder envs create --cpu 4 --disk 100 --memory 8 --image 5f443b16-30652892427b955601330fa5 my-env-name`, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
if img == "" { | ||
return xerrors.New("image id unset") | ||
} | ||
// ExactArgs(1) ensures our name value can't panic on an out of bounds. | ||
createReq := &coder.CreateEnvironmentRequest{ | ||
Name: args[0], | ||
ImageID: img, | ||
ImageTag: tag, | ||
} | ||
// We're explicitly ignoring errors for these because all of these flags | ||
// have a non-zero-value default value set already. | ||
createReq.CPUCores, _ = cmd.Flags().GetFloat32("cpu") | ||
createReq.MemoryGB, _ = cmd.Flags().GetFloat32("memory") | ||
createReq.DiskGB, _ = cmd.Flags().GetInt("disk") | ||
createReq.GPUs, _ = cmd.Flags().GetInt("gpus") | ||
|
||
client, err := newClient() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
env, err := client.CreateEnvironment(cmd.Context(), org, *createReq) | ||
if err != nil { | ||
return xerrors.Errorf("create environment: %w", err) | ||
} | ||
|
||
clog.LogSuccess( | ||
"creating environment...", | ||
clog.BlankLine, | ||
clog.Tip(`run "coder envs watch-build %q" to trail the build logs`, args[0]), | ||
) | ||
|
||
if follow { | ||
if err := trailBuildLogs(cmd.Context(), client, env.ID); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
}, | ||
} | ||
cmd.Flags().StringVarP(&org, "org", "o", defaultOrg, "ID of the organization the environment should be created under.") | ||
cmd.Flags().StringVarP(&tag, "tag", "t", defaultImgTag, "tag of the image the environment will be based off of.") | ||
cmd.Flags().Float32P("cpu", "c", defaultCPUCores, "number of cpu cores the environment should be provisioned with.") | ||
cmd.Flags().Float32P("memory", "m", defaultMemGB, "GB of RAM an environment should be provisioned with.") | ||
cmd.Flags().IntP("disk", "d", defaultDiskGB, "GB of disk storage an environment should be provisioned with.") | ||
cmd.Flags().IntP("gpus", "g", defaultGPUs, "number GPUs an environment should be provisioned with.") | ||
cmd.Flags().StringVarP(&img, "image", "i", "", "ID of the image to base the environment off of.") | ||
cmd.Flags().BoolVar(&follow, "follow", false, "follow buildlog after initiating rebuild") | ||
cmd.MarkFlagRequired("image") | ||
return cmd | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.