|
| 1 | +package commands |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "github.com/docker/model-runner/cmd/cli/pkg/types" |
| 6 | + |
| 7 | + "github.com/docker/model-runner/cmd/cli/commands/completion" |
| 8 | + "github.com/docker/model-runner/cmd/cli/desktop" |
| 9 | + gpupkg "github.com/docker/model-runner/cmd/cli/pkg/gpu" |
| 10 | + "github.com/docker/model-runner/cmd/cli/pkg/standalone" |
| 11 | + "github.com/spf13/cobra" |
| 12 | +) |
| 13 | + |
| 14 | +func newReinstallRunner() *cobra.Command { |
| 15 | + var port uint16 |
| 16 | + var host string |
| 17 | + var gpuMode string |
| 18 | + var doNotTrack bool |
| 19 | + c := &cobra.Command{ |
| 20 | + Use: "reinstall-runner", |
| 21 | + Short: "Reinstall Docker Model Runner (Docker Engine only)", |
| 22 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 23 | + // Ensure that we're running in a supported model runner context. |
| 24 | + engineKind := modelRunner.EngineKind() |
| 25 | + if engineKind == types.ModelRunnerEngineKindDesktop { |
| 26 | + cmd.Println("Standalone reinstallation not supported with Docker Desktop") |
| 27 | + cmd.Println("Use `docker desktop disable model-runner` and `docker desktop enable model-runner` instead") |
| 28 | + return nil |
| 29 | + } else if engineKind == types.ModelRunnerEngineKindMobyManual { |
| 30 | + cmd.Println("Standalone reinstallation not supported with MODEL_RUNNER_HOST set") |
| 31 | + return nil |
| 32 | + } |
| 33 | + |
| 34 | + if port == 0 { |
| 35 | + // Use "0" as a sentinel default flag value so it's not displayed automatically. |
| 36 | + // The default values are written in the usage string. |
| 37 | + // Hence, the user currently won't be able to set the port to 0 in order to get a random available port. |
| 38 | + port = standalone.DefaultControllerPortMoby |
| 39 | + } |
| 40 | + // HACK: If we're in a Cloud context, then we need to use a |
| 41 | + // different default port because it conflicts with Docker Desktop's |
| 42 | + // default model runner host-side port. Unfortunately we can't make |
| 43 | + // the port flag default dynamic (at least not easily) because of |
| 44 | + // when context detection happens. So assume that a default value |
| 45 | + // indicates that we want the Cloud default port. This is less |
| 46 | + // problematic in Cloud since the UX there is mostly invisible. |
| 47 | + if engineKind == types.ModelRunnerEngineKindCloud && |
| 48 | + port == standalone.DefaultControllerPortMoby { |
| 49 | + port = standalone.DefaultControllerPortCloud |
| 50 | + } |
| 51 | + |
| 52 | + // Set the appropriate environment. |
| 53 | + environment := "moby" |
| 54 | + if engineKind == types.ModelRunnerEngineKindCloud { |
| 55 | + environment = "cloud" |
| 56 | + } |
| 57 | + |
| 58 | + // Create a Docker client for the active context. |
| 59 | + dockerClient, err := desktop.DockerClientForContext(dockerCLI, dockerCLI.CurrentContext()) |
| 60 | + if err != nil { |
| 61 | + return fmt.Errorf("failed to create Docker client: %w", err) |
| 62 | + } |
| 63 | + |
| 64 | + // Remove any model runner containers (but keep models and images). |
| 65 | + if err := standalone.PruneControllerContainers(cmd.Context(), dockerClient, false, cmd); err != nil { |
| 66 | + return fmt.Errorf("unable to remove model runner container(s): %w", err) |
| 67 | + } |
| 68 | + |
| 69 | + // Determine GPU support. |
| 70 | + var gpu gpupkg.GPUSupport |
| 71 | + if gpuMode == "auto" { |
| 72 | + gpu, err = gpupkg.ProbeGPUSupport(cmd.Context(), dockerClient) |
| 73 | + if err != nil { |
| 74 | + return fmt.Errorf("unable to probe GPU support: %w", err) |
| 75 | + } |
| 76 | + } else if gpuMode == "cuda" { |
| 77 | + gpu = gpupkg.GPUSupportCUDA |
| 78 | + } else if gpuMode != "none" { |
| 79 | + return fmt.Errorf("unknown GPU specification: %q", gpuMode) |
| 80 | + } |
| 81 | + |
| 82 | + // Ensure that we have an up-to-date copy of the image. |
| 83 | + if err := standalone.EnsureControllerImage(cmd.Context(), dockerClient, gpu, cmd); err != nil { |
| 84 | + return fmt.Errorf("unable to pull latest standalone model runner image: %w", err) |
| 85 | + } |
| 86 | + |
| 87 | + // Ensure that we have a model storage volume. |
| 88 | + modelStorageVolume, err := standalone.EnsureModelStorageVolume(cmd.Context(), dockerClient, cmd) |
| 89 | + if err != nil { |
| 90 | + return fmt.Errorf("unable to initialize standalone model storage: %w", err) |
| 91 | + } |
| 92 | + |
| 93 | + // Create the model runner container. |
| 94 | + if err := standalone.CreateControllerContainer(cmd.Context(), dockerClient, port, host, environment, doNotTrack, gpu, modelStorageVolume, cmd, engineKind); err != nil { |
| 95 | + return fmt.Errorf("unable to initialize standalone model runner container: %w", err) |
| 96 | + } |
| 97 | + |
| 98 | + // Poll until we get a response from the model runner. |
| 99 | + return waitForStandaloneRunnerAfterInstall(cmd.Context()) |
| 100 | + }, |
| 101 | + ValidArgsFunction: completion.NoComplete, |
| 102 | + } |
| 103 | + c.Flags().Uint16Var(&port, "port", 0, |
| 104 | + "Docker container port for Docker Model Runner (default: 12434 for Docker CE, 12435 for Cloud mode)") |
| 105 | + c.Flags().StringVar(&host, "host", "127.0.0.1", "Host address to bind Docker Model Runner") |
| 106 | + c.Flags().StringVar(&gpuMode, "gpu", "auto", "Specify GPU support (none|auto|cuda)") |
| 107 | + c.Flags().BoolVar(&doNotTrack, "do-not-track", false, "Do not track models usage in Docker Model Runner") |
| 108 | + return c |
| 109 | +} |
0 commit comments