Skip to content

Commit 4020817

Browse files
authored
Cortex Patch (#1651)
1 parent 5bf7544 commit 4020817

File tree

27 files changed

+620
-85
lines changed

27 files changed

+620
-85
lines changed

cli/cluster/patch.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
17+
package cluster
18+
19+
import (
20+
"path/filepath"
21+
22+
"github.com/cortexlabs/cortex/pkg/lib/errors"
23+
"github.com/cortexlabs/cortex/pkg/lib/files"
24+
"github.com/cortexlabs/cortex/pkg/lib/json"
25+
s "github.com/cortexlabs/cortex/pkg/lib/strings"
26+
"github.com/cortexlabs/cortex/pkg/operator/schema"
27+
)
28+
29+
func Patch(operatorConfig OperatorConfig, configPath string, force bool) ([]schema.DeployResult, error) {
30+
params := map[string]string{
31+
"force": s.Bool(force),
32+
"configFileName": filepath.Base(configPath),
33+
}
34+
35+
configBytes, err := files.ReadFileBytes(configPath)
36+
if err != nil {
37+
return nil, err
38+
}
39+
40+
response, err := HTTPPostJSON(operatorConfig, "/patch", configBytes, params)
41+
if err != nil {
42+
return nil, err
43+
}
44+
45+
var deployResults []schema.DeployResult
46+
if err := json.Unmarshal(response, &deployResults); err != nil {
47+
return nil, errors.Wrap(err, "/patch", string(response))
48+
}
49+
50+
return deployResults, nil
51+
}

cli/cmd/cluster.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import (
4646
"github.com/cortexlabs/cortex/pkg/types/clusterconfig"
4747
"github.com/cortexlabs/cortex/pkg/types/clusterstate"
4848
"github.com/cortexlabs/cortex/pkg/types/userconfig"
49+
"github.com/cortexlabs/yaml"
4950
"github.com/spf13/cobra"
5051
)
5152

@@ -687,7 +688,12 @@ var _clusterExportCmd = &cobra.Command{
687688
exit.Error(err)
688689
}
689690

690-
err = awsClient.DownloadFileFromS3(info.ClusterConfig.Bucket, apiResponse.Spec.RawAPIKey(info.ClusterConfig.ClusterName), path.Join(baseDir, apiResponse.Spec.FileName))
691+
yamlBytes, err := yaml.Marshal(apiResponse.Spec.API.SubmittedAPISpec)
692+
if err != nil {
693+
exit.Error(err)
694+
}
695+
696+
err = files.WriteFile(yamlBytes, path.Join(baseDir, apiResponse.Spec.FileName))
691697
if err != nil {
692698
exit.Error(err)
693699
}

cli/cmd/deploy.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ func findProjectFiles(provider types.ProviderType, configPath string) ([]string,
195195
return nil, err
196196
}
197197

198+
// Include .env file containing environment variables
198199
dotEnvPath := path.Join(projectRoot, ".env")
199200
if files.IsFile(dotEnvPath) {
200201
projectPaths = append(projectPaths, dotEnvPath)

cli/cmd/patch.go

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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+
17+
package cmd
18+
19+
import (
20+
"fmt"
21+
"strings"
22+
23+
"github.com/cortexlabs/cortex/cli/cluster"
24+
"github.com/cortexlabs/cortex/cli/local"
25+
"github.com/cortexlabs/cortex/cli/types/flags"
26+
"github.com/cortexlabs/cortex/pkg/lib/exit"
27+
libjson "github.com/cortexlabs/cortex/pkg/lib/json"
28+
"github.com/cortexlabs/cortex/pkg/lib/print"
29+
"github.com/cortexlabs/cortex/pkg/lib/telemetry"
30+
"github.com/cortexlabs/cortex/pkg/operator/schema"
31+
"github.com/cortexlabs/cortex/pkg/types"
32+
"github.com/spf13/cobra"
33+
)
34+
35+
var (
36+
_flagPatchEnv string
37+
_flagPatchForce bool
38+
)
39+
40+
func patchInit() {
41+
_patchCmd.Flags().SortFlags = false
42+
_patchCmd.Flags().StringVarP(&_flagPatchEnv, "env", "e", getDefaultEnv(_generalCommandType), "environment to use")
43+
_patchCmd.Flags().BoolVarP(&_flagPatchForce, "force", "f", false, "override the in-progress api update")
44+
_patchCmd.Flags().VarP(&_flagOutput, "output", "o", fmt.Sprintf("output format: one of %s", strings.Join(flags.UserOutputTypeStrings(), "|")))
45+
}
46+
47+
var _patchCmd = &cobra.Command{
48+
Use: "patch [CONFIG_FILE]",
49+
Short: "update API configuration for a deployed API",
50+
Args: cobra.RangeArgs(0, 1),
51+
Run: func(cmd *cobra.Command, args []string) {
52+
env, err := ReadOrConfigureEnv(_flagPatchEnv)
53+
if err != nil {
54+
telemetry.Event("cli.patch")
55+
exit.Error(err)
56+
}
57+
telemetry.Event("cli.patch", map[string]interface{}{"provider": env.Provider.String(), "env_name": env.Name})
58+
59+
err = printEnvIfNotSpecified(_flagPatchEnv, cmd)
60+
if err != nil {
61+
exit.Error(err)
62+
}
63+
64+
configPath := getConfigPath(args)
65+
66+
var deployResults []schema.DeployResult
67+
if env.Provider == types.AWSProviderType {
68+
deployResults, err = cluster.Patch(MustGetOperatorConfig(env.Name), configPath, _flagPatchForce)
69+
if err != nil {
70+
exit.Error(err)
71+
}
72+
} else {
73+
deployResults, err = local.Patch(env, configPath)
74+
if err != nil {
75+
exit.Error(err)
76+
}
77+
}
78+
79+
switch _flagOutput {
80+
case flags.JSONOutputType:
81+
bytes, err := libjson.Marshal(deployResults)
82+
if err != nil {
83+
exit.Error(err)
84+
}
85+
fmt.Println(string(bytes))
86+
case flags.PrettyOutputType:
87+
message := deployMessage(deployResults, env.Name)
88+
if didAnyResultsError(deployResults) {
89+
print.StderrBoldFirstBlock(message)
90+
} else {
91+
print.BoldFirstBlock(message)
92+
}
93+
}
94+
95+
if didAnyResultsError(deployResults) {
96+
exit.Error(nil)
97+
}
98+
},
99+
}

cli/cmd/root.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ func init() {
127127
envInit()
128128
getInit()
129129
logsInit()
130+
patchInit()
130131
predictInit()
131132
refreshInit()
132133
versionInit()
@@ -166,6 +167,7 @@ func Execute() {
166167

167168
_rootCmd.AddCommand(_deployCmd)
168169
_rootCmd.AddCommand(_getCmd)
170+
_rootCmd.AddCommand(_patchCmd)
169171
_rootCmd.AddCommand(_logsCmd)
170172
_rootCmd.AddCommand(_refreshCmd)
171173
_rootCmd.AddCommand(_predictCmd)

cli/local/api.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import (
3737

3838
var _deploymentID = "local"
3939

40-
func UpdateAPI(apiConfig *userconfig.API, models []spec.CuratedModelResource, configPath string, projectID string, deployDisallowPrompt bool, awsClient *aws.Client) (*schema.APIResponse, string, error) {
40+
func UpdateAPI(apiConfig *userconfig.API, models []spec.CuratedModelResource, projectRoot string, projectID string, deployDisallowPrompt bool, awsClient *aws.Client) (*schema.APIResponse, string, error) {
4141
telemetry.Event("operator.deploy", apiConfig.TelemetryEvent(types.LocalProviderType))
4242

4343
var incompatibleVersion string
@@ -83,7 +83,7 @@ func UpdateAPI(apiConfig *userconfig.API, models []spec.CuratedModelResource, co
8383
}
8484
}
8585

86-
newAPISpec.LocalProjectDir = files.Dir(configPath)
86+
newAPISpec.LocalProjectDir = projectRoot
8787

8888
if areAPIsEqual(newAPISpec, prevAPISpec) {
8989
return toAPIResponse(newAPISpec), fmt.Sprintf("%s is up to date", newAPISpec.Resource.UserString()), nil

cli/local/deploy.go

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"github.com/cortexlabs/cortex/pkg/operator/schema"
3131
"github.com/cortexlabs/cortex/pkg/types"
3232
"github.com/cortexlabs/cortex/pkg/types/spec"
33+
"github.com/cortexlabs/cortex/pkg/types/userconfig"
3334
)
3435

3536
func Deploy(env cliconfig.Environment, configPath string, projectFileList []string, deployDisallowPrompt bool) ([]schema.DeployResult, error) {
@@ -45,11 +46,26 @@ func Deploy(env cliconfig.Environment, configPath string, projectFileList []stri
4546
return nil, err
4647
}
4748

48-
projectFiles, err := newProjectFiles(projectFileList, configPath)
49+
if !files.IsAbsOrTildePrefixed(configPath) {
50+
return nil, errors.ErrorUnexpected(fmt.Sprintf("%s is not an absolute path", configPath))
51+
}
52+
projectRoot := files.Dir(configPath)
53+
54+
projectFiles, err := newProjectFiles(projectFileList, projectRoot)
55+
if err != nil {
56+
return nil, err
57+
}
58+
59+
apiConfigs, err := spec.ExtractAPIConfigs(configBytes, types.LocalProviderType, configFileName, nil, nil)
4960
if err != nil {
5061
return nil, err
5162
}
5263

64+
return deploy(env, apiConfigs, projectFiles, deployDisallowPrompt)
65+
}
66+
67+
func deploy(env cliconfig.Environment, apiConfigs []userconfig.API, projectFiles ProjectFiles, deployDisallowPrompt bool) ([]schema.DeployResult, error) {
68+
var err error
5369
var awsClient *aws.Client
5470
var gcpClient *gcp.Client
5571

@@ -72,27 +88,23 @@ func Deploy(env cliconfig.Environment, configPath string, projectFileList []stri
7288
}
7389
}
7490

75-
apiConfigs, err := spec.ExtractAPIConfigs(configBytes, types.LocalProviderType, configFileName, nil, nil)
76-
if err != nil {
77-
return nil, err
78-
}
79-
8091
models := []spec.CuratedModelResource{}
8192
err = ValidateLocalAPIs(apiConfigs, &models, projectFiles, awsClient, gcpClient)
8293
if err != nil {
8394
err = errors.Append(err, fmt.Sprintf("\n\napi configuration schema for Realtime API can be found at https://docs.cortex.dev/v/%s/deployments/realtime-api/api-configuration", consts.CortexVersionMinor))
8495
return nil, err
8596
}
8697

87-
projectID, err := files.HashFile(projectFileList[0], projectFileList[1:]...)
98+
projectRelFilePaths := projectFiles.AllAbsPaths()
99+
projectID, err := files.HashFile(projectRelFilePaths[0], projectRelFilePaths[1:]...)
88100
if err != nil {
89-
return nil, errors.Wrap(err, "failed to hash directory", filepath.Dir(configPath))
101+
return nil, errors.Wrap(err, "failed to hash directory", projectFiles.projectRoot)
90102
}
91103

92104
results := make([]schema.DeployResult, len(apiConfigs))
93105
for i := range apiConfigs {
94106
apiConfig := apiConfigs[i]
95-
api, msg, err := UpdateAPI(&apiConfig, models, configPath, projectID, deployDisallowPrompt, awsClient)
107+
api, msg, err := UpdateAPI(&apiConfig, models, projectFiles.projectRoot, projectID, deployDisallowPrompt, awsClient)
96108
results[i].Message = msg
97109
if err != nil {
98110
results[i].Error = errors.Message(err)

cli/local/patch.go

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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+
17+
package local
18+
19+
import (
20+
"path"
21+
"path/filepath"
22+
23+
"github.com/cortexlabs/cortex/cli/types/cliconfig"
24+
"github.com/cortexlabs/cortex/pkg/lib/files"
25+
"github.com/cortexlabs/cortex/pkg/operator/schema"
26+
"github.com/cortexlabs/cortex/pkg/types"
27+
"github.com/cortexlabs/cortex/pkg/types/spec"
28+
"github.com/cortexlabs/cortex/pkg/types/userconfig"
29+
)
30+
31+
func Patch(env cliconfig.Environment, configPath string) ([]schema.DeployResult, error) {
32+
configFileName := filepath.Base(configPath)
33+
34+
configBytes, err := files.ReadFileBytes(configPath)
35+
if err != nil {
36+
return nil, err
37+
}
38+
39+
apiConfigs, err := spec.ExtractAPIConfigs(configBytes, types.LocalProviderType, configFileName, nil, nil)
40+
if err != nil {
41+
return nil, err
42+
}
43+
44+
deployResults := []schema.DeployResult{}
45+
for i := range apiConfigs {
46+
apiConfig := &apiConfigs[i]
47+
apiResponse, err := GetAPI(apiConfig.Name)
48+
if err != nil {
49+
return nil, err
50+
}
51+
52+
localProjectDir := apiResponse[0].Spec.LocalProjectDir
53+
54+
projectFileList, err := findProjectFiles(localProjectDir)
55+
if err != nil {
56+
return nil, err
57+
}
58+
59+
projectFiles, err := newProjectFiles(projectFileList, localProjectDir)
60+
if err != nil {
61+
return nil, err
62+
}
63+
64+
deployResult, err := deploy(env, []userconfig.API{*apiConfig}, projectFiles, true)
65+
if err != nil {
66+
return nil, err
67+
}
68+
69+
deployResults = append(deployResults, deployResult...)
70+
}
71+
72+
return deployResults, nil
73+
}
74+
75+
func findProjectFiles(projectRoot string) ([]string, error) {
76+
ignoreFns := []files.IgnoreFn{
77+
files.IgnoreCortexDebug,
78+
files.IgnoreHiddenFiles,
79+
files.IgnoreHiddenFolders,
80+
files.IgnorePythonGeneratedFiles,
81+
}
82+
83+
cortexIgnorePath := path.Join(projectRoot, ".cortexignore")
84+
if files.IsFile(cortexIgnorePath) {
85+
cortexIgnore, err := files.GitIgnoreFn(cortexIgnorePath)
86+
if err != nil {
87+
return nil, err
88+
}
89+
ignoreFns = append(ignoreFns, cortexIgnore)
90+
}
91+
92+
projectPaths, err := files.ListDirRecursive(projectRoot, false, ignoreFns...)
93+
if err != nil {
94+
return nil, err
95+
}
96+
97+
// Include .env file containing environment variables
98+
dotEnvPath := path.Join(projectRoot, ".env")
99+
if files.IsFile(dotEnvPath) {
100+
projectPaths = append(projectPaths, dotEnvPath)
101+
}
102+
103+
return projectPaths, nil
104+
}

0 commit comments

Comments
 (0)