Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.

Add cvm flag to CLI and fields to coder-sdk #201

Merged
merged 4 commits into from
Dec 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions coder-sdk/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@ type Client struct {

// newHTTPClient creates a default underlying http client and sets the auth cookie.
//
// NOTE: As we do not specify a custom transport, the default one from the stdlib will be used,
// resulting in a persistent connection pool.
// We do not set a timeout here as it could cause issue with the websocket.
// The caller is expected to set it when needed.
// NOTE:
// As we do not specify a custom transport, the default one from the stdlib will be used,
// resulting in a persistent connection pool.
// We do not set a timeout here as it could cause issue with the websocket.
// The caller is expected to set it when needed.
//
// WARNING: If the caller sets a custom transport to set TLS settings or a custom CA, the default
// pool will not be used and it might result in a new dns lookup/tls session/socket begin
// established each time.
// WARNING:
// If the caller sets a custom transport to set TLS settings or a custom CA the default
// pool will not be used and it might result in a new dns lookup/tls session/socket begin
// established each time.
func (c Client) newHTTPClient() (*http.Client, error) {
jar, err := cookiejar.New(nil)
if err != nil {
Expand Down
19 changes: 11 additions & 8 deletions coder-sdk/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type Environment struct {
LastConnectionAt time.Time `json:"last_connection_at" table:"-"`
AutoOffThreshold Duration `json:"auto_off_threshold" table:"-"`
SSHAvailable bool `json:"ssh_available" table:"-"`
UseContainerVM bool `json:"use_container_vm" table:"CVM"`
}

// RebuildMessage defines the message shown when an Environment requires a rebuild for it can be accessed.
Expand Down Expand Up @@ -71,14 +72,15 @@ const (

// CreateEnvironmentRequest is used to configure a new environment.
type CreateEnvironmentRequest struct {
Name string `json:"name"`
ImageID string `json:"image_id"`
ImageTag string `json:"image_tag"`
CPUCores float32 `json:"cpu_cores"`
MemoryGB float32 `json:"memory_gb"`
DiskGB int `json:"disk_gb"`
GPUs int `json:"gpus"`
Services []string `json:"services"`
Name string `json:"name"`
ImageID string `json:"image_id"`
ImageTag string `json:"image_tag"`
CPUCores float32 `json:"cpu_cores"`
MemoryGB float32 `json:"memory_gb"`
DiskGB int `json:"disk_gb"`
GPUs int `json:"gpus"`
Services []string `json:"services"`
UseContainerVM bool `json:"use_container_vm"`
}

// CreateEnvironment sends a request to create an environment.
Expand Down Expand Up @@ -130,6 +132,7 @@ type UpdateEnvironmentReq struct {
GPUs *int `json:"gpus"`
Services *[]string `json:"services"`
CodeServerReleaseURL *string `json:"code_server_release_url"`
UseContainerVM *bool `json:"use_container_vm"`
}

// RebuildEnvironment requests that the given envID is rebuilt with no changes to its specification.
Expand Down
1 change: 1 addition & 0 deletions docs/coder_envs_create.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ coder envs create my-new-powerful-env --cpu 12 --disk 100 --memory 16 --image ub
### Options

```
--container-vm deploy the environment as a Container-based VM
-c, --cpu float32 number of cpu cores the environment should be provisioned with.
-d, --disk int GB of disk storage an environment should be provisioned with.
--follow follow buildlog after initiating rebuild
Expand Down
20 changes: 11 additions & 9 deletions docs/coder_envs_edit.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,17 @@ coder envs edit back-end-env --disk 20
### Options

```
-c, --cpu float32 The number of cpu cores the environment should be provisioned with.
-d, --disk int The amount of disk storage an environment should be provisioned with.
--follow follow buildlog after initiating rebuild
-g, --gpu int The amount of disk storage to provision the environment with.
-h, --help help for edit
-i, --image string name of the image you want the environment to be based off of.
-m, --memory float32 The amount of RAM an environment should be provisioned with.
-o, --org string name of the organization the environment should be created under.
-t, --tag string image tag of the image you want to base the environment off of. (default "latest")
--container-vm deploy the environment as a Container-based VM
-c, --cpu float32 The number of cpu cores the environment should be provisioned with.
-d, --disk int The amount of disk storage an environment should be provisioned with.
--follow follow buildlog after initiating rebuild
-g, --gpu int The amount of disk storage to provision the environment with.
-h, --help help for edit
-i, --image string name of the image you want the environment to be based off of.
-m, --memory float32 The amount of RAM an environment should be provisioned with.
--not-container-vm do not deploy the environment as a Container-based VM
-o, --org string name of the organization the environment should be created under.
-t, --tag string image tag of the image you want to base the environment off of. (default "latest")
```

### Options inherited from parent commands
Expand Down
37 changes: 30 additions & 7 deletions internal/cmd/envs.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ func createEnvCmd(user *string) *cobra.Command {
img string
tag string
follow bool
useCVM bool
)

cmd := &cobra.Command{
Expand Down Expand Up @@ -189,13 +190,14 @@ coder envs create my-new-powerful-env --cpu 12 --disk 100 --memory 16 --image ub

// ExactArgs(1) ensures our name value can't panic on an out of bounds.
createReq := &coder.CreateEnvironmentRequest{
Name: args[0],
ImageID: importedImg.ID,
ImageTag: tag,
CPUCores: cpu,
MemoryGB: memory,
DiskGB: disk,
GPUs: gpus,
Name: args[0],
ImageID: importedImg.ID,
ImageTag: tag,
CPUCores: cpu,
MemoryGB: memory,
DiskGB: disk,
GPUs: gpus,
UseContainerVM: useCVM,
}

// if any of these defaulted to their zero value we provision
Expand Down Expand Up @@ -238,6 +240,7 @@ coder envs create my-new-powerful-env --cpu 12 --disk 100 --memory 16 --image ub
cmd.Flags().IntVarP(&gpus, "gpus", "g", 0, "number GPUs an environment should be provisioned with.")
cmd.Flags().StringVarP(&img, "image", "i", "", "name of the image to base the environment off of.")
cmd.Flags().BoolVar(&follow, "follow", false, "follow buildlog after initiating rebuild")
cmd.Flags().BoolVar(&useCVM, "container-vm", false, "deploy the environment as a Container-based VM")
_ = cmd.MarkFlagRequired("image")
return cmd
}
Expand All @@ -252,6 +255,8 @@ func editEnvCmd(user *string) *cobra.Command {
disk int
gpus int
follow bool
useCVM bool
notCVM bool
)

cmd := &cobra.Command{
Expand Down Expand Up @@ -296,6 +301,8 @@ coder envs edit back-end-env --disk 20`,
image: img,
imageTag: tag,
orgName: org,
useCVM: useCVM,
notCVM: notCVM,
})
if err != nil {
return err
Expand Down Expand Up @@ -328,6 +335,8 @@ coder envs edit back-end-env --disk 20`,
cmd.Flags().IntVarP(&disk, "disk", "d", 0, "The amount of disk storage an environment should be provisioned with.")
cmd.Flags().IntVarP(&gpus, "gpu", "g", 0, "The amount of disk storage to provision the environment with.")
cmd.Flags().BoolVar(&follow, "follow", false, "follow buildlog after initiating rebuild")
cmd.Flags().BoolVar(&useCVM, "container-vm", false, "deploy the environment as a Container-based VM")
cmd.Flags().BoolVar(&notCVM, "not-container-vm", false, "do not deploy the environment as a Container-based VM")
return cmd
}

Expand Down Expand Up @@ -391,8 +400,12 @@ type updateConf struct {
image string
imageTag string
orgName string
useCVM bool
notCVM bool
}

func boolP(a bool) *bool { return &a }

func buildUpdateReq(ctx context.Context, client *coder.Client, conf updateConf) (*coder.UpdateEnvironmentReq, error) {
var (
updateReq coder.UpdateEnvironmentReq
Expand All @@ -401,6 +414,16 @@ func buildUpdateReq(ctx context.Context, client *coder.Client, conf updateConf)
defaultDiskGB int
)

if conf.useCVM && conf.notCVM {
return nil, xerrors.New("--container-vm and --not-container-vm flags conflict")
}
if conf.useCVM {
updateReq.UseContainerVM = boolP(true)
}
if conf.notCVM {
updateReq.UseContainerVM = boolP(false)
}

// If this is not empty it means the user is requesting to change the environment image.
if conf.image != "" {
importedImg, err := findImg(ctx, client, findImgConf{
Expand Down