Skip to content

Commit d530094

Browse files
authored
Add cog migrate command (#2290)
* Add cog migrate command * Attempts to rewrite python packages to python requirements * Removes run commands that are known to be safe * Add fast: true to config * Rewrites python code to deal with Optionals properly * Fix accept flag * Fix wrong variable assigned to accept * Fix integration test
1 parent 28411cf commit d530094

File tree

27 files changed

+755
-59
lines changed

27 files changed

+755
-59
lines changed

pkg/cli/build.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func buildCommand(cmd *cobra.Command, args []string) error {
6868
logClient := coglog.NewClient(client)
6969
logCtx := logClient.StartBuild(buildFast, buildLocalImage)
7070

71-
cfg, projectDir, err := config.GetConfig(projectDirFlag)
71+
cfg, projectDir, err := config.GetConfig()
7272
if err != nil {
7373
logClient.EndBuild(ctx, err, logCtx)
7474
return err

pkg/cli/debug.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func newDebugCommand() *cobra.Command {
3737
func cmdDockerfile(cmd *cobra.Command, args []string) error {
3838
ctx := cmd.Context()
3939

40-
cfg, projectDir, err := config.GetConfig(projectDirFlag)
40+
cfg, projectDir, err := config.GetConfig()
4141
if err != nil {
4242
return err
4343
}

pkg/cli/migrate.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package cli
2+
3+
import (
4+
"github.com/spf13/cobra"
5+
6+
"github.com/replicate/cog/pkg/migrate"
7+
)
8+
9+
var migrateAccept bool
10+
11+
func newMigrateCommand() *cobra.Command {
12+
cmd := &cobra.Command{
13+
Use: "migrate",
14+
Short: "Run a migration",
15+
Long: `Run a migration.
16+
17+
This will attempt to migrate your cog project to be compatible with fast boots.`,
18+
RunE: cmdMigrate,
19+
Args: cobra.MaximumNArgs(0),
20+
Hidden: true,
21+
}
22+
23+
addYesFlag(cmd)
24+
25+
return cmd
26+
}
27+
28+
func cmdMigrate(cmd *cobra.Command, args []string) error {
29+
ctx := cmd.Context()
30+
migrator, err := migrate.NewMigrator(migrate.MigrationV1, migrate.MigrationV1Fast, !migrateAccept)
31+
if err != nil {
32+
return err
33+
}
34+
err = migrator.Migrate(ctx)
35+
if err != nil {
36+
return err
37+
}
38+
39+
return nil
40+
}
41+
42+
func addYesFlag(cmd *cobra.Command) {
43+
const acceptFlag = "y"
44+
cmd.Flags().BoolVar(&migrateAccept, acceptFlag, false, "Whether to disable interaction and automatically accept the changes.")
45+
}

pkg/cli/predict.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func cmdPredict(cmd *cobra.Command, args []string) error {
7878
if len(args) == 0 {
7979
// Build image
8080

81-
cfg, projectDir, err := config.GetConfig(projectDirFlag)
81+
cfg, projectDir, err := config.GetConfig()
8282
if err != nil {
8383
return err
8484
}

pkg/cli/push.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func push(cmd *cobra.Command, args []string) error {
5454
logClient := coglog.NewClient(client)
5555
logCtx := logClient.StartPush(buildFast, buildLocalImage)
5656

57-
cfg, projectDir, err := config.GetConfig(projectDirFlag)
57+
cfg, projectDir, err := config.GetConfig()
5858
if err != nil {
5959
logClient.EndPush(ctx, err, logCtx)
6060
return err

pkg/cli/root.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ import (
1010
"github.com/replicate/cog/pkg/util/console"
1111
)
1212

13-
var projectDirFlag string
14-
1513
func NewRootCommand() (*cobra.Command, error) {
1614
rootCmd := cobra.Command{
1715
Use: "cog",
@@ -47,6 +45,7 @@ https://github.com/replicate/cog`,
4745
newRunCommand(),
4846
newServeCommand(),
4947
newTrainCommand(),
48+
newMigrateCommand(),
5049
)
5150

5251
return &rootCmd, nil

pkg/cli/run.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func newRunCommand() *cobra.Command {
5454
func run(cmd *cobra.Command, args []string) error {
5555
ctx := cmd.Context()
5656

57-
cfg, projectDir, err := config.GetConfig(projectDirFlag)
57+
cfg, projectDir, err := config.GetConfig()
5858
if err != nil {
5959
return err
6060
}

pkg/cli/serve.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ Generate and run an HTTP server based on the declared model inputs and outputs.`
4343
func cmdServe(cmd *cobra.Command, arg []string) error {
4444
ctx := cmd.Context()
4545

46-
cfg, projectDir, err := config.GetConfig(projectDirFlag)
46+
cfg, projectDir, err := config.GetConfig()
4747
if err != nil {
4848
return err
4949
}

pkg/cli/train.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func cmdTrain(cmd *cobra.Command, args []string) error {
6161
volumes := []docker.Volume{}
6262
gpus := gpusFlag
6363

64-
cfg, projectDir, err := config.GetConfig(projectDirFlag)
64+
cfg, projectDir, err := config.GetConfig()
6565
if err != nil {
6666
return err
6767
}

pkg/config/config.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,21 +41,21 @@ type RunItem struct {
4141
Type string `json:"type,omitempty" yaml:"type"`
4242
ID string `json:"id,omitempty" yaml:"id"`
4343
Target string `json:"target,omitempty" yaml:"target"`
44-
} `json:"mounts,omitempty" yaml:"mounts"`
44+
} `json:"mounts,omitempty" yaml:"mounts,omitempty"`
4545
}
4646

4747
type Build struct {
48-
GPU bool `json:"gpu,omitempty" yaml:"gpu"`
48+
GPU bool `json:"gpu,omitempty" yaml:"gpu,omitempty"`
4949
PythonVersion string `json:"python_version,omitempty" yaml:"python_version"`
5050
PythonRequirements string `json:"python_requirements,omitempty" yaml:"python_requirements"`
51-
PythonPackages []string `json:"python_packages,omitempty" yaml:"python_packages"` // Deprecated, but included for backwards compatibility
52-
Run []RunItem `json:"run,omitempty" yaml:"run"`
53-
SystemPackages []string `json:"system_packages,omitempty" yaml:"system_packages"`
54-
PreInstall []string `json:"pre_install,omitempty" yaml:"pre_install"` // Deprecated, but included for backwards compatibility
55-
CUDA string `json:"cuda,omitempty" yaml:"cuda"`
56-
CuDNN string `json:"cudnn,omitempty" yaml:"cudnn"`
51+
PythonPackages []string `json:"python_packages,omitempty" yaml:"python_packages,omitempty"` // Deprecated, but included for backwards compatibility
52+
Run []RunItem `json:"run,omitempty" yaml:"run,omitempty"`
53+
SystemPackages []string `json:"system_packages,omitempty" yaml:"system_packages,omitempty"`
54+
PreInstall []string `json:"pre_install,omitempty" yaml:"pre_install,omitempty"` // Deprecated, but included for backwards compatibility
55+
CUDA string `json:"cuda,omitempty" yaml:"cuda,omitempty"`
56+
CuDNN string `json:"cudnn,omitempty" yaml:"cudnn,omitempty"`
5757
Fast bool `json:"fast,omitempty" yaml:"fast"`
58-
PythonOverrides string `json:"python_overrides,omitempty" yaml:"python_overrides"`
58+
PythonOverrides string `json:"python_overrides,omitempty" yaml:"python_overrides,omitempty"`
5959

6060
pythonRequirementsContent []string
6161
}
@@ -71,10 +71,10 @@ type Example struct {
7171

7272
type Config struct {
7373
Build *Build `json:"build" yaml:"build"`
74-
Image string `json:"image,omitempty" yaml:"image"`
74+
Image string `json:"image,omitempty" yaml:"image,omitempty"`
7575
Predict string `json:"predict,omitempty" yaml:"predict"`
76-
Train string `json:"train,omitempty" yaml:"train"`
77-
Concurrency *Concurrency `json:"concurrency,omitempty" yaml:"concurrency"`
76+
Train string `json:"train,omitempty" yaml:"train,omitempty"`
77+
Concurrency *Concurrency `json:"concurrency,omitempty" yaml:"concurrency,omitempty"`
7878
}
7979

8080
func DefaultConfig() *Config {

0 commit comments

Comments
 (0)