Skip to content

Commit 127d473

Browse files
authored
Add prompts for zip file count and total size (#767)
1 parent aba1a99 commit 127d473

File tree

10 files changed

+73
-26
lines changed

10 files changed

+73
-26
lines changed

cli/cmd/cluster.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ var _downCmd = &cobra.Command{
231231
exit.Error(err)
232232
}
233233

234-
prompt.YesOrExit(fmt.Sprintf("your cluster (%s in %s) will be spun down and all apis will be deleted, are you sure you want to continue?", *accessConfig.ClusterName, *accessConfig.Region), "")
234+
prompt.YesOrExit(fmt.Sprintf("your cluster (%s in %s) will be spun down and all apis will be deleted, are you sure you want to continue?", *accessConfig.ClusterName, *accessConfig.Region), "", "")
235235

236236
out, exitCode, err := runManagerAccessCommand("/root/uninstall.sh", *accessConfig, awsCreds)
237237
if err != nil {

cli/cmd/delete.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func delete(apiName string, keepCache bool) {
5454
if !_flagDeleteForce {
5555
readyReplicas := getReadyReplicasOrNil(apiName)
5656
if readyReplicas != nil && *readyReplicas > 2 {
57-
prompt.YesOrExit(fmt.Sprintf("are you sure you want to delete the %s api (which has %d running replicas)?", apiName, *readyReplicas), "")
57+
prompt.YesOrExit(fmt.Sprintf("are you sure you want to delete the %s api (which has %d running replicas)?", apiName, *readyReplicas), "", "")
5858
}
5959
}
6060

cli/cmd/deploy.go

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,24 @@ import (
2727
"github.com/cortexlabs/cortex/pkg/lib/exit"
2828
"github.com/cortexlabs/cortex/pkg/lib/files"
2929
"github.com/cortexlabs/cortex/pkg/lib/json"
30+
"github.com/cortexlabs/cortex/pkg/lib/prompt"
3031
s "github.com/cortexlabs/cortex/pkg/lib/strings"
3132
"github.com/cortexlabs/cortex/pkg/lib/telemetry"
3233
"github.com/cortexlabs/cortex/pkg/lib/zip"
3334
"github.com/cortexlabs/cortex/pkg/operator/schema"
3435
"github.com/spf13/cobra"
3536
)
3637

37-
var _warningFileSize = 1024 * 1024 * 10
38+
var _warningFileBytes = 1024 * 1024 * 10
39+
var _warningProjectBytes = 1024 * 1024 * 10
40+
var _warningFileCount = 100
41+
3842
var _flagDeployForce bool
43+
var _flagDeployYes bool
3944

4045
func init() {
4146
_deployCmd.PersistentFlags().BoolVarP(&_flagDeployForce, "force", "f", false, "override the in-progress api update")
47+
_deployCmd.PersistentFlags().BoolVarP(&_flagDeployYes, "yes", "y", false, "skip prompts")
4248
addEnvFlag(_deployCmd)
4349
}
4450

@@ -89,18 +95,35 @@ func deploy(configPath string, force bool) {
8995

9096
projectRoot := filepath.Dir(files.UserRelToAbsPath(configPath))
9197

92-
projectPaths, err := files.ListDirRecursive(projectRoot, false,
98+
ignoreFns := []files.IgnoreFn{
9399
files.IgnoreSpecificFiles(files.UserRelToAbsPath(configPath)),
94100
files.IgnoreCortexDebug,
95101
files.IgnoreHiddenFiles,
96102
files.IgnoreHiddenFolders,
97103
files.IgnorePythonGeneratedFiles,
98-
files.PromptForFilesAboveSize(_warningFileSize),
99-
)
104+
}
105+
if !_flagDeployYes {
106+
ignoreFns = append(ignoreFns, files.PromptForFilesAboveSize(_warningFileBytes, "do you want to upload %s (%s)?"))
107+
}
108+
109+
projectPaths, err := files.ListDirRecursive(projectRoot, false, ignoreFns...)
100110
if err != nil {
101111
exit.Error(err)
102112
}
103113

114+
canSkipPromptMsg := "you can skip this prompt next time with `cortex deploy --yes`\n"
115+
rootDirMsg := "this directory"
116+
if s.EnsureSuffix(projectRoot, "/") != _cwd {
117+
rootDirMsg = fmt.Sprintf("./%s", files.DirPathRelativeToCWD(projectRoot))
118+
}
119+
120+
didPromptFileCount := false
121+
if !_flagDeployYes && len(projectPaths) >= _warningFileCount {
122+
msg := fmt.Sprintf("cortex will zip %d files in %s and upload them to the cluster, though we recommend you upload large files (e.g. models) to s3 and download them in your api's __init__ function. Would you like to continue?", len(projectPaths), rootDirMsg)
123+
prompt.YesOrExit(msg, canSkipPromptMsg, "")
124+
didPromptFileCount = true
125+
}
126+
104127
projectZipBytes, err := zip.ToMem(&zip.Input{
105128
FileLists: []zip.FileListInput{
106129
{
@@ -109,11 +132,15 @@ func deploy(configPath string, force bool) {
109132
},
110133
},
111134
})
112-
113135
if err != nil {
114136
exit.Error(errors.Wrap(err, "failed to zip project folder"))
115137
}
116138

139+
if !_flagDeployYes && !didPromptFileCount && len(projectZipBytes) >= _warningProjectBytes {
140+
msg := fmt.Sprintf("cortex will zip %d files in %s (%s) and upload them to the cluster, though we recommend you upload large files (e.g. models) to s3 and download them in your api's __init__ function. Would you like to continue?", len(projectPaths), rootDirMsg, s.IntToBase2Byte(len(projectZipBytes)))
141+
prompt.YesOrExit(msg, canSkipPromptMsg, "")
142+
}
143+
117144
uploadBytes["project.zip"] = projectZipBytes
118145

119146
uploadInput := &HTTPUploadInput{

cli/cmd/lib_cluster_config.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -335,14 +335,14 @@ func confirmInstallClusterConfig(clusterConfig *clusterconfig.Config, awsCreds A
335335
}
336336

337337
exitMessage := fmt.Sprintf("cluster configuration can be modified via the cluster config file; see https://cortex.dev/v/%s/cluster-management/config for more information", consts.CortexVersionMinor)
338-
prompt.YesOrExit("would you like to continue?", exitMessage)
338+
prompt.YesOrExit("would you like to continue?", "", exitMessage)
339339
}
340340

341341
func confirmUpdateClusterConfig(clusterConfig clusterconfig.Config, awsCreds AWSCredentials, awsClient *aws.Client) {
342342
fmt.Println(clusterConfigConfirmaionStr(clusterConfig, awsCreds, awsClient))
343343

344344
exitMessage := fmt.Sprintf("cluster configuration can be modified via the cluster config file; see https://cortex.dev/v/%s/cluster-management/config for more information", consts.CortexVersionMinor)
345-
prompt.YesOrExit(fmt.Sprintf("your cluster (%s in %s) will be updated according to the configuration above, are you sure you want to continue?", clusterConfig.ClusterName, *clusterConfig.Region), exitMessage)
345+
prompt.YesOrExit(fmt.Sprintf("your cluster (%s in %s) will be updated according to the configuration above, are you sure you want to continue?", clusterConfig.ClusterName, *clusterConfig.Region), "", exitMessage)
346346
}
347347

348348
func clusterConfigConfirmaionStr(clusterConfig clusterconfig.Config, awsCreds AWSCredentials, awsClient *aws.Client) string {

cli/cmd/lib_manager.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ func runManager(containerConfig *container.Config) (string, *int, error) {
182182

183183
output := outputBuffer.String()
184184

185-
// Let the ctrl+C handler run its course
185+
// Let the ctrl+c handler run its course
186186
if caughtCtrlC {
187187
time.Sleep(5 * time.Second)
188188
}

cli/cmd/root.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"strings"
2424

2525
"github.com/cortexlabs/cortex/pkg/lib/exit"
26+
s "github.com/cortexlabs/cortex/pkg/lib/strings"
2627
"github.com/cortexlabs/cortex/pkg/lib/telemetry"
2728
homedir "github.com/mitchellh/go-homedir"
2829
"github.com/spf13/cobra"
@@ -37,10 +38,17 @@ var _cliConfigPath string
3738
var _clientIDPath string
3839
var _emailPath string
3940
var _debugPath string
41+
var _cwd string
4042

4143
var _flagEnv string
4244

4345
func init() {
46+
cwd, err := os.Getwd()
47+
if err != nil {
48+
exit.Error(err)
49+
}
50+
_cwd = s.EnsureSuffix(cwd, "/")
51+
4452
homeDir, err := homedir.Dir()
4553
if err != nil {
4654
exit.Error(err)

docs/cluster-management/cli.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Flags:
1414
-e, --env string environment (default "default")
1515
-f, --force override the in-progress api update
1616
-h, --help help for deploy
17+
-y, --yes skip prompts
1718
```
1819

1920
## get

docs/contributing/development.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ If you're making changes in the operator and want faster iterations, you can run
150150

151151
If you want to switch back to the in-cluster operator:
152152

153-
1. `<ctrl+C>` to stop your off-cluster operator
153+
1. `<ctrl+c>` to stop your off-cluster operator
154154
2. `make operator-start` to install the operator in your cluster
155155

156156
## Dev workflow

pkg/lib/files/files.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,10 @@ func PathRelativeToCWD(absPath string) string {
127127
return strings.TrimPrefix(absPath, cwd)
128128
}
129129

130+
func DirPathRelativeToCWD(absPath string) string {
131+
return s.EnsureSuffix(PathRelativeToCWD(absPath), "/")
132+
}
133+
130134
func IsFileOrDir(path string) bool {
131135
_, err := os.Stat(path)
132136
if err == nil {
@@ -343,11 +347,16 @@ func IgnoreSpecificFiles(absPaths ...string) IgnoreFn {
343347
}
344348
}
345349

346-
func PromptForFilesAboveSize(size int) IgnoreFn {
350+
// promptMsgTemplate should have two placeholders: the first is for the file path and the second is for the file size
351+
func PromptForFilesAboveSize(size int, promptMsgTemplate string) IgnoreFn {
352+
if promptMsgTemplate == "" {
353+
promptMsgTemplate = "do you want to zip %s (%s)?"
354+
}
355+
347356
return func(path string, fi os.FileInfo) (bool, error) {
348357
if !fi.IsDir() && fi.Size() > int64(size) {
349-
promptMsg := fmt.Sprintf("do you want to zip %s (%s)?", PathRelativeToCWD(path), s.IntToBase2Byte(int(fi.Size())))
350-
return !prompt.YesOrNo(promptMsg, true), nil
358+
promptMsg := fmt.Sprintf(promptMsgTemplate, PathRelativeToCWD(path), s.IntToBase2Byte(int(fi.Size())))
359+
return !prompt.YesOrNo(promptMsg, "", ""), nil
351360
}
352361
return false, nil
353362
}

pkg/lib/prompt/prompt.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -74,20 +74,23 @@ func Prompt(opts *Options) string {
7474
return val
7575
}
7676

77-
func YesOrExit(prompt string, exitMessage string) {
77+
func YesOrExit(prompt string, yesMessage string, noMessage string) {
7878
for true {
7979
str := Prompt(&Options{
8080
Prompt: prompt + " (y/n)",
8181
HideDefault: true,
8282
})
8383

8484
if strings.ToLower(str) == "y" {
85+
if yesMessage != "" {
86+
fmt.Println(yesMessage)
87+
}
8588
return
8689
}
8790

8891
if strings.ToLower(str) == "n" {
89-
if exitMessage != "" {
90-
fmt.Println(exitMessage)
92+
if noMessage != "" {
93+
fmt.Println(noMessage)
9194
}
9295
exit.ErrorNoPrintNoTelemetry()
9396
}
@@ -97,25 +100,24 @@ func YesOrExit(prompt string, exitMessage string) {
97100
}
98101
}
99102

100-
func YesOrNo(prompt string, showCancelHint bool) bool {
103+
func YesOrNo(prompt string, yesMessage string, noMessage string) bool {
101104
for true {
102-
var fullPrompt string
103-
if showCancelHint {
104-
fullPrompt = prompt + " (y/n, ctrl+C to cancel)"
105-
} else {
106-
fullPrompt = prompt + " (y/n)"
107-
}
108-
109105
str := Prompt(&Options{
110-
Prompt: fullPrompt,
106+
Prompt: prompt + " (y/n)",
111107
HideDefault: true,
112108
})
113109

114110
if strings.ToLower(str) == "y" {
111+
if yesMessage != "" {
112+
fmt.Println(yesMessage)
113+
}
115114
return true
116115
}
117116

118117
if strings.ToLower(str) == "n" {
118+
if noMessage != "" {
119+
fmt.Println(noMessage)
120+
}
119121
return false
120122
}
121123

0 commit comments

Comments
 (0)