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 1 commit
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
Next Next commit
Add cvm flag to CLI and fields to coder-sdk
  • Loading branch information
cmoog committed Dec 5, 2020
commit 24c3f70ad7f0694fc68709f6e28424a562eef4d0
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
35 changes: 28 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 @@ -328,6 +333,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 +398,12 @@ type updateConf struct {
image string
imageTag string
orgName string
useCVM bool
noCVM 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 +412,16 @@ func buildUpdateReq(ctx context.Context, client *coder.Client, conf updateConf)
defaultDiskGB int
)

if conf.useCVM && conf.noCVM {
return nil, xerrors.New("--container-vm and --not-container-vm flags conflict")
}
if conf.useCVM {
updateReq.UseContainerVM = boolP(true)
}
if conf.noCVM {
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