Skip to content

Commit 293bcdb

Browse files
authored
Python client (#1449)
1 parent e7e323a commit 293bcdb

File tree

22 files changed

+952
-30
lines changed

22 files changed

+952
-30
lines changed

cli/cmd/delete.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func deleteInit() {
4545
// only applies to aws provider because local doesn't support multiple replicas
4646
_deleteCmd.Flags().BoolVarP(&_flagDeleteForce, "force", "f", false, "delete the api without confirmation")
4747
_deleteCmd.Flags().BoolVarP(&_flagDeleteKeepCache, "keep-cache", "c", false, "keep cached data for the api")
48-
_deleteCmd.Flags().VarP(&_flagOutput, "output", "o", fmt.Sprintf("output format: one of %s", strings.Join(flags.OutputTypeStrings(), "|")))
48+
_deleteCmd.Flags().VarP(&_flagOutput, "output", "o", fmt.Sprintf("output format: one of %s", strings.Join(flags.UserOutputTypeStrings(), "|")))
4949
}
5050

5151
var _deleteCmd = &cobra.Command{

cli/cmd/deploy.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func deployInit() {
5959
_deployCmd.Flags().StringVarP(&_flagDeployEnv, "env", "e", getDefaultEnv(_generalCommandType), "environment to use")
6060
_deployCmd.Flags().BoolVarP(&_flagDeployForce, "force", "f", false, "override the in-progress api update")
6161
_deployCmd.Flags().BoolVarP(&_flagDeployDisallowPrompt, "yes", "y", false, "skip prompts")
62-
_deployCmd.Flags().VarP(&_flagOutput, "output", "o", fmt.Sprintf("output format: one of %s", strings.Join(flags.OutputTypeStrings(), "|")))
62+
_deployCmd.Flags().VarP(&_flagOutput, "output", "o", fmt.Sprintf("output format: one of %s", strings.Join(flags.UserOutputTypeStrings(), "|")))
6363
}
6464

6565
var _deployCmd = &cobra.Command{
@@ -106,23 +106,31 @@ var _deployCmd = &cobra.Command{
106106
exit.Error(err)
107107
}
108108

109+
local.OutputType = _flagOutput // Set output type for the Local package
109110
deployResults, err = local.Deploy(env, configPath, projectFiles, _flagDeployDisallowPrompt)
110111
if err != nil {
111112
exit.Error(err)
112113
}
113114
}
114115

115-
if _flagOutput == flags.JSONOutputType {
116+
switch _flagOutput {
117+
case flags.JSONOutputType:
116118
bytes, err := libjson.Marshal(deployResults)
117119
if err != nil {
118120
exit.Error(err)
119121
}
120122
fmt.Println(string(bytes))
121123
return
124+
case flags.MixedOutputType:
125+
err := mixedPrint(deployResults)
126+
if err != nil {
127+
exit.Error(err)
128+
}
129+
return
130+
case flags.PrettyOutputType:
131+
message := deployMessage(deployResults, env.Name)
132+
print.BoldFirstBlock(message)
122133
}
123-
124-
message := deployMessage(deployResults, env.Name)
125-
print.BoldFirstBlock(message)
126134
},
127135
}
128136

cli/cmd/env.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func envInit() {
4848
_envCmd.AddCommand(_envConfigureCmd)
4949

5050
_envListCmd.Flags().SortFlags = false
51-
_envListCmd.Flags().VarP(&_flagOutput, "output", "o", fmt.Sprintf("output format: one of %s", strings.Join(flags.OutputTypeStrings(), "|")))
51+
_envListCmd.Flags().VarP(&_flagOutput, "output", "o", fmt.Sprintf("output format: one of %s", strings.Join(flags.UserOutputTypeStrings(), "|")))
5252
_envCmd.AddCommand(_envListCmd)
5353

5454
_envDefaultCmd.Flags().SortFlags = false

cli/cmd/get.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func getInit() {
6262
_getCmd.Flags().SortFlags = false
6363
_getCmd.Flags().StringVarP(&_flagGetEnv, "env", "e", getDefaultEnv(_generalCommandType), "environment to use")
6464
_getCmd.Flags().BoolVarP(&_flagWatch, "watch", "w", false, "re-run the command every 2 seconds")
65-
_getCmd.Flags().VarP(&_flagOutput, "output", "o", fmt.Sprintf("output format: one of %s", strings.Join(flags.OutputTypeStrings(), "|")))
65+
_getCmd.Flags().VarP(&_flagOutput, "output", "o", fmt.Sprintf("output format: one of %s", strings.Join(flags.UserOutputTypeStrings(), "|")))
6666
}
6767

6868
var _getCmd = &cobra.Command{

cli/cmd/refresh.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func refreshInit() {
4040
_refreshCmd.Flags().SortFlags = false
4141
_refreshCmd.Flags().StringVarP(&_flagRefreshEnv, "env", "e", getDefaultEnv(_generalCommandType), "environment to use")
4242
_refreshCmd.Flags().BoolVarP(&_flagRefreshForce, "force", "f", false, "override the in-progress api update")
43-
_refreshCmd.Flags().VarP(&_flagOutput, "output", "o", fmt.Sprintf("output format: one of %s", strings.Join(flags.OutputTypeStrings(), "|")))
43+
_refreshCmd.Flags().VarP(&_flagOutput, "output", "o", fmt.Sprintf("output format: one of %s", strings.Join(flags.UserOutputTypeStrings(), "|")))
4444
}
4545

4646
var _refreshCmd = &cobra.Command{

cli/cmd/root.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package cmd
1818

1919
import (
20+
"encoding/base64"
2021
"fmt"
2122
"os"
2223
"path/filepath"
@@ -25,6 +26,7 @@ import (
2526
"github.com/cortexlabs/cortex/cli/types/flags"
2627
"github.com/cortexlabs/cortex/pkg/lib/errors"
2728
"github.com/cortexlabs/cortex/pkg/lib/exit"
29+
libjson "github.com/cortexlabs/cortex/pkg/lib/json"
2830
s "github.com/cortexlabs/cortex/pkg/lib/strings"
2931
"github.com/cortexlabs/cortex/pkg/lib/telemetry"
3032
homedir "github.com/mitchellh/go-homedir"
@@ -122,11 +124,18 @@ func init() {
122124

123125
func initTelemetry() {
124126
cID := clientID()
127+
128+
invoker := os.Getenv("CORTEX_CLI_INVOKER")
129+
if invoker == "" {
130+
invoker = "direct"
131+
}
132+
125133
telemetry.Init(telemetry.Config{
126134
Enabled: true,
127135
UserID: cID,
128136
Properties: map[string]string{
129137
"client_id": cID,
138+
"invoker": invoker,
130139
},
131140
Environment: "cli",
132141
LogErrors: false,
@@ -229,3 +238,12 @@ func printLeadingNewLine() {
229238
}
230239
fmt.Println("")
231240
}
241+
242+
func mixedPrint(a interface{}) error {
243+
jsonBytes, err := libjson.Marshal(a)
244+
if err != nil {
245+
return err
246+
}
247+
fmt.Println(fmt.Sprintf("~~cortex~~%s~~cortex~~", base64.StdEncoding.EncodeToString(jsonBytes)))
248+
return nil
249+
}

cli/local/docker_spec.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,7 @@ func retryWithNvidiaRuntime(err error, containerConfig *container.Config, hostCo
463463
}
464464

465465
if _, ok := docker.MustDockerClient().Info.Runtimes["nvidia"]; ok {
466-
fmt.Println("retrying API deployment using nvidia runtime because device driver for GPU was not found")
466+
localPrintln("retrying API deployment using nvidia runtime because device driver for GPU was not found")
467467
hostConfig.Runtime = "nvidia"
468468
hostConfig.Resources.DeviceRequests = nil
469469
containerCreateRequest, err := docker.MustDockerClient().ContainerCreate(context.Background(), containerConfig, hostConfig, nil, "")

cli/local/model_cache.go

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

26+
"github.com/cortexlabs/cortex/cli/types/flags"
2627
"github.com/cortexlabs/cortex/pkg/lib/archive"
2728
"github.com/cortexlabs/cortex/pkg/lib/aws"
2829
"github.com/cortexlabs/cortex/pkg/lib/cron"
@@ -75,7 +76,7 @@ func CacheModels(apiSpec *spec.API, awsClient *aws.Client) ([]*spec.LocalModelCa
7576
}
7677

7778
if uncachedModelCount > 0 {
78-
fmt.Println("") // Newline to group all of the model information
79+
localPrintln("") // Newline to group all of the model information
7980
}
8081

8182
return localModelCaches, nil
@@ -134,13 +135,13 @@ func cacheModel(modelPath string, localModelCache spec.LocalModelCache, awsClien
134135
return err
135136
}
136137
} else if strings.HasSuffix(modelPath, ".onnx") {
137-
fmt.Println(fmt.Sprintf("○ caching model %s ...", modelPath))
138+
localPrintln(fmt.Sprintf("○ caching model %s ...", modelPath))
138139
err := files.CopyFileOverwrite(modelPath, filepath.Join(modelDir, filepath.Base(modelPath)))
139140
if err != nil {
140141
return err
141142
}
142143
} else {
143-
fmt.Println(fmt.Sprintf("○ caching model %s ...", modelPath))
144+
localPrintln(fmt.Sprintf("○ caching model %s ...", modelPath))
144145
tfModelVersion := filepath.Base(modelPath)
145146
err := files.CopyDirOverwrite(strings.TrimSuffix(modelPath, "/"), s.EnsureSuffix(filepath.Join(modelDir, tfModelVersion), "/"))
146147
if err != nil {
@@ -196,10 +197,12 @@ func DeleteCachedModelsByID(modelIDs []string) error {
196197
}
197198

198199
func downloadModel(modelPath string, modelDir string, awsClientForBucket *aws.Client) error {
199-
fmt.Printf("○ downloading model %s ", modelPath)
200-
defer fmt.Print(" ✓\n")
201-
dotCron := cron.Run(print.Dot, nil, 2*time.Second)
202-
defer dotCron.Cancel()
200+
localPrintf("○ downloading model %s ", modelPath)
201+
defer localPrint(" ✓\n")
202+
if OutputType != flags.JSONOutputType {
203+
dotCron := cron.Run(print.Dot, nil, 2*time.Second)
204+
defer dotCron.Cancel()
205+
}
203206

204207
bucket, prefix, err := aws.SplitS3Path(modelPath)
205208
if err != nil {
@@ -234,7 +237,7 @@ func downloadModel(modelPath string, modelDir string, awsClientForBucket *aws.Cl
234237
}
235238

236239
func unzipAndValidate(originalModelPath string, zipFile string, destPath string) error {
237-
fmt.Println(fmt.Sprintf("○ unzipping model %s ...", originalModelPath))
240+
localPrintln(fmt.Sprintf("○ unzipping model %s ...", originalModelPath))
238241
tmpDir := filepath.Join(filepath.Dir(destPath), filepath.Base(destPath)+"-tmp")
239242
err := files.CreateDir(tmpDir)
240243
if err != nil {

cli/local/print.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
Copyright 2020 Cortex Labs, Inc.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
package local
17+
18+
import (
19+
"fmt"
20+
21+
"github.com/cortexlabs/cortex/cli/types/flags"
22+
)
23+
24+
// Can be overwritten by CLI commands
25+
var OutputType flags.OutputType = flags.PrettyOutputType
26+
27+
func localPrintln(a ...interface{}) {
28+
if OutputType != flags.JSONOutputType {
29+
fmt.Println(a...)
30+
}
31+
}
32+
33+
func localPrint(a ...interface{}) {
34+
if OutputType != flags.JSONOutputType {
35+
fmt.Print(a...)
36+
}
37+
}
38+
39+
func localPrintf(format string, a ...interface{}) {
40+
if OutputType != flags.JSONOutputType {
41+
fmt.Printf(format, a...)
42+
}
43+
}

cli/local/validations.go

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

26+
"github.com/cortexlabs/cortex/cli/types/flags"
2627
"github.com/cortexlabs/cortex/pkg/lib/aws"
2728
"github.com/cortexlabs/cortex/pkg/lib/docker"
2829
"github.com/cortexlabs/cortex/pkg/lib/errors"
@@ -151,7 +152,12 @@ func ValidateLocalAPIs(apis []userconfig.API, projectFiles ProjectFiles, awsClie
151152
}
152153
}
153154

154-
pulledThisImage, err := docker.PullImage(image, dockerAuth, docker.PrintDots)
155+
pullVerbosity := docker.PrintDots
156+
if OutputType == flags.JSONOutputType {
157+
pullVerbosity = docker.NoPrint
158+
}
159+
160+
pulledThisImage, err := docker.PullImage(image, dockerAuth, pullVerbosity)
155161
if err != nil {
156162
return errors.Wrap(err, "failed to pull image", image)
157163
}
@@ -162,7 +168,7 @@ func ValidateLocalAPIs(apis []userconfig.API, projectFiles ProjectFiles, awsClie
162168
}
163169

164170
if pulledImage {
165-
fmt.Println()
171+
localPrintln()
166172
}
167173

168174
portToRunningAPIsMap, err := getPortToRunningAPIsMap()

0 commit comments

Comments
 (0)