Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
163 changes: 152 additions & 11 deletions cmd/model.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package cmd

import (
"encoding/json"
"fmt"
"net/http"
"strings"
"time"

"github.com/abiosoft/colima/cli"
"github.com/abiosoft/colima/cmd/root"
Expand All @@ -13,6 +17,9 @@ import (
"github.com/abiosoft/colima/environment/vm/lima/limaconfig"
"github.com/abiosoft/colima/store"
"github.com/abiosoft/colima/util"
"github.com/abiosoft/colima/util/terminal"
"github.com/coreos/go-semver/semver"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

Expand All @@ -32,8 +39,8 @@ var modelCmd = &cobra.Command{
Long: `Manage AI models inside the VM.
This requires docker runtime and krunkit VM type for GPU access.

All arguments are passed to AI model runner (ramalama).
You can specify '--' to interact directly with the underlying ramalama command.
All arguments are passed to the AI model runner.
You can specify '--' to pass arguments directly to the underlying tool.

Examples:
colima model list
Expand Down Expand Up @@ -71,7 +78,7 @@ var modelSetupCmd = &cobra.Command{
return validateModelPrerequisites()
},
RunE: func(cmd *cobra.Command, args []string) error {
return provisionRamalama()
return setupOrUpdateRamalama()
},
}

Expand Down Expand Up @@ -133,28 +140,162 @@ func ensureRamalamaProvisioned() error {
return provisionRamalama()
}

const ramalamaReleasesURL = "https://api.github.com/repos/containers/ramalama/releases/latest"

// setupOrUpdateRamalama handles both fresh installs and updates with version checking.
func setupOrUpdateRamalama() error {
s, _ := store.Load()

// Fresh install - no version check needed
if !s.RamalamaProvisioned {
if err := provisionRamalama(); err != nil {
return err
}
// Print installed version
if version := getRamalamaVersion(); version != "" {
fmt.Println("AI model runner")
fmt.Printf("version: %s", version)
fmt.Println()
}
return nil
}

// Update - check versions first
currentVersion := getRamalamaVersion()
if currentVersion == "" {
// Can't determine current version, proceed with update
log.Debug("could not determine current ramalama version, proceeding with update")
return provisionRamalama()
}

latestVersion, err := getLatestRamalamaVersion()
if err != nil {
log.Debugf("could not fetch latest ramalama version: %v", err)
return fmt.Errorf("could not check for updates: %w", err)
}

// Compare versions
current, err := semver.NewVersion(currentVersion)
if err != nil {
log.Debugf("could not parse current version %q: %v", currentVersion, err)
return provisionRamalama()
}

latest, err := semver.NewVersion(latestVersion)
if err != nil {
log.Debugf("could not parse latest version %q: %v", latestVersion, err)
return provisionRamalama()
}

// Show version info
fmt.Println("AI model runner")
fmt.Printf("current: %s", currentVersion)
fmt.Println()
fmt.Printf("latest: %s", latestVersion)
fmt.Println()

if current.Compare(*latest) >= 0 {
fmt.Println()
fmt.Println("Already up to date")
return nil
}

if err := provisionRamalama(); err != nil {
return err
}

// Print new version
if newVersion := getRamalamaVersion(); newVersion != "" {
fmt.Printf("updated: %s", newVersion)
fmt.Println()
}
return nil
}

// getRamalamaVersion returns the currently installed ramalama version in the VM.
// Returns empty string if ramalama is not installed or version cannot be determined.
func getRamalamaVersion() string {
guest := lima.New(host.New())
output, err := guest.RunOutput("sh", "-c", `export PATH="$HOME/.local/bin:$PATH"; ramalama version 2>/dev/null`)
if err != nil {
return ""
}
// Output format: "ramalama version 0.17.1"
output = strings.TrimSpace(output)
if version, ok := strings.CutPrefix(output, "ramalama version "); ok {
return version
}
return ""
}

// getLatestRamalamaVersion fetches the latest release version from GitHub.
func getLatestRamalamaVersion() (string, error) {
client := &http.Client{Timeout: 10 * time.Second}
resp, err := client.Get(ramalamaReleasesURL)
if err != nil {
return "", fmt.Errorf("failed to fetch releases: %w", err)
}
defer func() { _ = resp.Body.Close() }()

if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("unexpected status code: %d", resp.StatusCode)
}

var release struct {
TagName string `json:"tag_name"`
}
if err := json.NewDecoder(resp.Body).Decode(&release); err != nil {
return "", fmt.Errorf("failed to decode response: %w", err)
}

// Tag might be "v0.17.1" or "0.17.1"
version := strings.TrimPrefix(release.TagName, "v")
return version, nil
}

// provisionRamalama installs ramalama and its dependencies in the VM.
func provisionRamalama() error {
guest := lima.New(host.New())

script := `set -e
export PATH="$HOME/.local/bin:$PATH"
log.Println("Installing AI model runner...")

# install ramalama
// step 1: Install ramalama binary (uses normal scrolling output)
installScript := `set -e
export PATH="$HOME/.local/bin:$PATH"
curl -fsSL https://ramalama.ai/install.sh | bash
`
if err := guest.Run("sh", "-c", installScript); err != nil {
return fmt.Errorf("error installing AI model runner: %w", err)
}

# pull ramalama container images
// step 2: Pull container images (uses alternate screen for progress bars)
pullScript := `set -e
docker pull quay.io/ramalama/ramalama
docker pull quay.io/ramalama/ramalama-rag
`
if err := terminal.WithAltScreen(func() error {
log.Println()
log.Println(" Colima - AI Model Runner Setup")
log.Println(" ===============================")
log.Println()
log.Println(" Pulling container images...")
log.Println(" This may take a few minutes depending on your internet connection.")
log.Println()
return guest.RunInteractive("sh", "-c", pullScript)
}); err != nil {
return fmt.Errorf("error pulling container images: %w", err)
}

log.Println("Configuring AI model runner...")

# fix ownership of persistent data dir and symlink to expected location
// step 3: Post-install setup (uses normal scrolling output)
setupScript := `set -e
sudo chown -R $(id -u):$(id -g) /var/lib/ramalama
mkdir -p "$HOME/.local/share"
ln -sfn /var/lib/ramalama "$HOME/.local/share/ramalama"
`

if err := guest.Run("sh", "-c", script); err != nil {
return fmt.Errorf("error provisioning ramalama: %w", err)
if err := guest.Run("sh", "-c", setupScript); err != nil {
return fmt.Errorf("error configuring AI model runner: %w", err)
}

// mark as provisioned
Expand Down
11 changes: 11 additions & 0 deletions cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/abiosoft/colima/app"
"github.com/abiosoft/colima/cmd/root"
"github.com/abiosoft/colima/config"
"github.com/abiosoft/colima/store"
"github.com/spf13/cobra"
)

Expand All @@ -22,6 +23,16 @@ var versionCmd = &cobra.Command{

if colimaApp, err := app.New(); err == nil {
_ = colimaApp.Version()

// Show AI model runner version if provisioned
s, _ := store.Load()
if s.RamalamaProvisioned {
if modelVersion := getRamalamaVersion(); modelVersion != "" {
fmt.Println()
fmt.Println("AI model runner")
fmt.Println("version:", modelVersion)
}
}
}
},
}
Expand Down
55 changes: 55 additions & 0 deletions util/terminal/terminal.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package terminal
import (
"fmt"
"os"
"os/signal"

"golang.org/x/term"
)
Expand All @@ -17,3 +18,57 @@ func ClearLine() {

fmt.Print("\033[1A \033[2K \r")
}

// EnterAltScreen switches to the alternate screen buffer.
// This preserves the main terminal content which can be restored
// by calling ExitAltScreen.
func EnterAltScreen() {
if !isTerminal {
return
}
// Switch to alternate screen buffer and move cursor to top-left
fmt.Print("\033[?1049h\033[H")
}

// ExitAltScreen switches back to the main screen buffer,
// restoring the previous terminal content.
func ExitAltScreen() {
if !isTerminal {
return
}
fmt.Print("\033[?1049l")
}

// WithAltScreen runs the provided function in the alternate screen buffer.
// The main terminal content is preserved and restored after the function completes.
// Handles Ctrl-C to ensure the terminal is restored even on interrupt.
func WithAltScreen(fn func() error) error {
if !isTerminal {
return fn()
}

EnterAltScreen()

// Handle Ctrl-C to ensure terminal is restored even on interrupt
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, os.Interrupt)
defer signal.Stop(sigCh)

done := make(chan struct{})
go func() {
select {
case <-sigCh:
ExitAltScreen()
os.Exit(1)
case <-done:
return
}
}()

err := fn()

close(done)
ExitAltScreen()

return err
}
Loading