mirror of
https://github.com/ollama/ollama.git
synced 2026-04-18 10:54:10 +02:00
Compare commits
2 Commits
pdevine/sa
...
brucemacd/
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
365a3657ad | ||
|
|
71c1d8d0a9 |
22
auth/auth.go
22
auth/auth.go
@@ -9,6 +9,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -83,3 +84,24 @@ func Sign(ctx context.Context, bts []byte) (string, error) {
|
|||||||
// signature is <pubkey>:<signature>
|
// signature is <pubkey>:<signature>
|
||||||
return fmt.Sprintf("%s:%s", bytes.TrimSpace(parts[1]), base64.StdEncoding.EncodeToString(signedData.Blob)), nil
|
return fmt.Sprintf("%s:%s", bytes.TrimSpace(parts[1]), base64.StdEncoding.EncodeToString(signedData.Blob)), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SignRequest adds a nonce query parameter and an Authorization header with
|
||||||
|
// an Ed25519 signature to req.
|
||||||
|
func SignRequest(ctx context.Context, req *http.Request) error {
|
||||||
|
nonce, err := NewNonce(rand.Reader, 16)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
q := req.URL.Query()
|
||||||
|
q.Set("nonce", nonce)
|
||||||
|
req.URL.RawQuery = q.Encode()
|
||||||
|
|
||||||
|
data := []byte(fmt.Sprintf("%s,%s", req.Method, req.URL.RequestURI()))
|
||||||
|
signature, err := Sign(ctx, data)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
req.Header.Set("Authorization", signature)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|||||||
28
cmd/cmd.go
28
cmd/cmd.go
@@ -1900,6 +1900,21 @@ func runInteractiveTUI(cmd *cobra.Command) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if version.Version != "0.0.0" && version.IsOfficialInstall() && version.IsLocalHost(envconfig.Host()) {
|
||||||
|
if version.HasCachedUpdate() {
|
||||||
|
fmt.Print("A new version of Ollama is available. Run \"ollama update\" to install.\n\n")
|
||||||
|
_ = version.ClearCachedUpdate()
|
||||||
|
}
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
if available, err := version.CheckForUpdate(ctx); err == nil && available {
|
||||||
|
_ = version.CacheAvailableUpdate()
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
// Selector adapters for tui
|
// Selector adapters for tui
|
||||||
singleSelector := func(title string, items []config.ModelItem, current string) (string, error) {
|
singleSelector := func(title string, items []config.ModelItem, current string) (string, error) {
|
||||||
tuiItems := tui.ReorderItems(tui.ConvertItems(items))
|
tuiItems := tui.ReorderItems(tui.ConvertItems(items))
|
||||||
@@ -2317,6 +2332,18 @@ func NewCLI() *cobra.Command {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
updateCmd := &cobra.Command{
|
||||||
|
Use: "update",
|
||||||
|
Short: "Update Ollama to the latest version",
|
||||||
|
Args: cobra.ExactArgs(0),
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
force, _ := cmd.Flags().GetBool("force")
|
||||||
|
_ = version.ClearCachedUpdate()
|
||||||
|
return version.DoUpdate(force)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
updateCmd.Flags().BoolP("force", "f", false, "Force update even if installed via a package manager")
|
||||||
|
|
||||||
rootCmd.AddCommand(
|
rootCmd.AddCommand(
|
||||||
serveCmd,
|
serveCmd,
|
||||||
createCmd,
|
createCmd,
|
||||||
@@ -2334,6 +2361,7 @@ func NewCLI() *cobra.Command {
|
|||||||
copyCmd,
|
copyCmd,
|
||||||
deleteCmd,
|
deleteCmd,
|
||||||
runnerCmd,
|
runnerCmd,
|
||||||
|
updateCmd,
|
||||||
config.LaunchCmd(checkServerHeartbeat, runInteractiveTUI),
|
config.LaunchCmd(checkServerHeartbeat, runInteractiveTUI),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
190
version/update.go
Normal file
190
version/update.go
Normal file
@@ -0,0 +1,190 @@
|
|||||||
|
package version
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net"
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"path/filepath"
|
||||||
|
"runtime"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/ollama/ollama/auth"
|
||||||
|
)
|
||||||
|
|
||||||
|
var updateCheckURLBase = "https://ollama.com"
|
||||||
|
|
||||||
|
// CheckForUpdate calls the ollama.com update API and reports whether a
|
||||||
|
// newer version is available.
|
||||||
|
func CheckForUpdate(ctx context.Context) (bool, error) {
|
||||||
|
requestURL, err := url.Parse(updateCheckURLBase + "/api/update")
|
||||||
|
if err != nil {
|
||||||
|
return false, fmt.Errorf("parse update URL: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
query := requestURL.Query()
|
||||||
|
query.Add("os", runtime.GOOS)
|
||||||
|
query.Add("arch", runtime.GOARCH)
|
||||||
|
query.Add("version", Version)
|
||||||
|
requestURL.RawQuery = query.Encode()
|
||||||
|
|
||||||
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, requestURL.String(), nil)
|
||||||
|
if err != nil {
|
||||||
|
return false, fmt.Errorf("create request: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
_ = auth.SignRequest(ctx, req)
|
||||||
|
|
||||||
|
resp, err := http.DefaultClient.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return false, fmt.Errorf("update check request: %w", err)
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
return resp.StatusCode == http.StatusOK, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func cacheFilePath() (string, error) {
|
||||||
|
home, err := os.UserHomeDir()
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return filepath.Join(home, ".ollama", "update"), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// CacheAvailableUpdate creates the update marker file.
|
||||||
|
func CacheAvailableUpdate() error {
|
||||||
|
path, err := cacheFilePath()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
f, err := os.Create(path)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return f.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
// HasCachedUpdate reports whether a non-stale update marker exists.
|
||||||
|
func HasCachedUpdate() bool {
|
||||||
|
path, err := cacheFilePath()
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
fi, err := os.Stat(path)
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return time.Since(fi.ModTime()) <= 24*time.Hour
|
||||||
|
}
|
||||||
|
|
||||||
|
// ClearCachedUpdate removes the update marker file.
|
||||||
|
func ClearCachedUpdate() error {
|
||||||
|
path, err := cacheFilePath()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = os.Remove(path)
|
||||||
|
if os.IsNotExist(err) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func IsOfficialInstall() bool {
|
||||||
|
exe, err := os.Executable()
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
exe, err = filepath.EvalSymlinks(exe)
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
switch runtime.GOOS {
|
||||||
|
case "windows":
|
||||||
|
localAppData := os.Getenv("LOCALAPPDATA")
|
||||||
|
if localAppData == "" {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return strings.HasPrefix(strings.ToLower(exe), strings.ToLower(filepath.Join(localAppData, "Programs", "Ollama")+string(filepath.Separator)))
|
||||||
|
case "darwin":
|
||||||
|
return strings.HasPrefix(exe, "/Applications/Ollama.app/")
|
||||||
|
default:
|
||||||
|
dir := filepath.Dir(exe)
|
||||||
|
return dir == "/usr/local/bin" || dir == "/usr/bin" || dir == "/bin"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// DoUpdate downloads and runs the platform-appropriate install script.
|
||||||
|
func DoUpdate(force bool) error {
|
||||||
|
if !force && !IsOfficialInstall() {
|
||||||
|
return fmt.Errorf("ollama appears to be installed through a package manager. Please update it using your package manager")
|
||||||
|
}
|
||||||
|
|
||||||
|
var scriptURL, tmpPattern, shell string
|
||||||
|
switch runtime.GOOS {
|
||||||
|
case "windows":
|
||||||
|
scriptURL = "https://ollama.com/install.ps1"
|
||||||
|
tmpPattern = "ollama-install-*.ps1"
|
||||||
|
shell = "powershell"
|
||||||
|
default:
|
||||||
|
scriptURL = "https://ollama.com/install.sh"
|
||||||
|
tmpPattern = "ollama-install-*.sh"
|
||||||
|
shell = "sh"
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, err := http.Get(scriptURL)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("download install script: %w", err)
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
if resp.StatusCode != http.StatusOK {
|
||||||
|
return fmt.Errorf("download install script: status %d", resp.StatusCode)
|
||||||
|
}
|
||||||
|
|
||||||
|
tmpFile, err := os.CreateTemp("", tmpPattern)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("create temp file: %w", err)
|
||||||
|
}
|
||||||
|
defer os.Remove(tmpFile.Name())
|
||||||
|
|
||||||
|
if _, err := io.Copy(tmpFile, resp.Body); err != nil {
|
||||||
|
tmpFile.Close()
|
||||||
|
return fmt.Errorf("write install script: %w", err)
|
||||||
|
}
|
||||||
|
tmpFile.Close()
|
||||||
|
|
||||||
|
cmd := exec.Command(shell, tmpFile.Name())
|
||||||
|
cmd.Stdin = os.Stdin
|
||||||
|
cmd.Stdout = os.Stdout
|
||||||
|
cmd.Stderr = os.Stderr
|
||||||
|
return cmd.Run()
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsLocalHost reports whether the configured Ollama host points to the
|
||||||
|
// local machine.
|
||||||
|
func IsLocalHost(host *url.URL) bool {
|
||||||
|
hostname := host.Hostname()
|
||||||
|
switch hostname {
|
||||||
|
case "", "127.0.0.1", "localhost", "::1", "0.0.0.0":
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
if ip := net.ParseIP(hostname); ip != nil {
|
||||||
|
return ip.IsLoopback()
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
146
version/update_test.go
Normal file
146
version/update_test.go
Normal file
@@ -0,0 +1,146 @@
|
|||||||
|
package version
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"net/url"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"runtime"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func setHome(t *testing.T, dir string) {
|
||||||
|
t.Helper()
|
||||||
|
if runtime.GOOS == "windows" {
|
||||||
|
t.Setenv("USERPROFILE", dir)
|
||||||
|
} else {
|
||||||
|
t.Setenv("HOME", dir)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCheckForUpdate(t *testing.T) {
|
||||||
|
t.Run("update available", func(t *testing.T) {
|
||||||
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.URL.Query().Get("os") == "" || r.URL.Query().Get("arch") == "" || r.URL.Query().Get("version") == "" {
|
||||||
|
t.Error("missing expected query parameters")
|
||||||
|
}
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
}))
|
||||||
|
defer ts.Close()
|
||||||
|
|
||||||
|
old := updateCheckURLBase
|
||||||
|
updateCheckURLBase = ts.URL
|
||||||
|
defer func() { updateCheckURLBase = old }()
|
||||||
|
|
||||||
|
available, err := CheckForUpdate(context.Background())
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("unexpected error: %v", err)
|
||||||
|
}
|
||||||
|
if !available {
|
||||||
|
t.Fatal("expected update to be available")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("up to date", func(t *testing.T) {
|
||||||
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.WriteHeader(http.StatusNoContent)
|
||||||
|
}))
|
||||||
|
defer ts.Close()
|
||||||
|
|
||||||
|
old := updateCheckURLBase
|
||||||
|
updateCheckURLBase = ts.URL
|
||||||
|
defer func() { updateCheckURLBase = old }()
|
||||||
|
|
||||||
|
available, err := CheckForUpdate(context.Background())
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("unexpected error: %v", err)
|
||||||
|
}
|
||||||
|
if available {
|
||||||
|
t.Fatal("expected no update available")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("network error", func(t *testing.T) {
|
||||||
|
old := updateCheckURLBase
|
||||||
|
updateCheckURLBase = "http://localhost:1"
|
||||||
|
defer func() { updateCheckURLBase = old }()
|
||||||
|
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
_, err := CheckForUpdate(ctx)
|
||||||
|
if err == nil {
|
||||||
|
t.Fatal("expected error for unreachable server")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCacheRoundTrip(t *testing.T) {
|
||||||
|
tmp := t.TempDir()
|
||||||
|
setHome(t, tmp)
|
||||||
|
os.MkdirAll(filepath.Join(tmp, ".ollama"), 0o755)
|
||||||
|
|
||||||
|
if err := CacheAvailableUpdate(); err != nil {
|
||||||
|
t.Fatalf("cache write: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !HasCachedUpdate() {
|
||||||
|
t.Fatal("expected cached update to be present")
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := ClearCachedUpdate(); err != nil {
|
||||||
|
t.Fatalf("cache clear: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if HasCachedUpdate() {
|
||||||
|
t.Fatal("expected no cached update after clear")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestHasCachedUpdateStale(t *testing.T) {
|
||||||
|
tmp := t.TempDir()
|
||||||
|
setHome(t, tmp)
|
||||||
|
os.MkdirAll(filepath.Join(tmp, ".ollama"), 0o755)
|
||||||
|
|
||||||
|
if err := CacheAvailableUpdate(); err != nil {
|
||||||
|
t.Fatalf("cache write: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Backdate the file to make it stale
|
||||||
|
path := filepath.Join(tmp, ".ollama", "update")
|
||||||
|
staleTime := time.Now().Add(-25 * time.Hour)
|
||||||
|
os.Chtimes(path, staleTime, staleTime)
|
||||||
|
|
||||||
|
if HasCachedUpdate() {
|
||||||
|
t.Fatal("expected no cached update for stale file")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestIsLocalHost(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
host string
|
||||||
|
local bool
|
||||||
|
}{
|
||||||
|
{"http://127.0.0.1:11434", true},
|
||||||
|
{"http://localhost:11434", true},
|
||||||
|
{"http://[::1]:11434", true},
|
||||||
|
{"http://0.0.0.0:11434", true},
|
||||||
|
{"http://remote.example.com:11434", false},
|
||||||
|
{"http://192.168.1.100:11434", false},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.host, func(t *testing.T) {
|
||||||
|
u, err := url.Parse(tt.host)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("parse URL: %v", err)
|
||||||
|
}
|
||||||
|
if got := IsLocalHost(u); got != tt.local {
|
||||||
|
t.Errorf("IsLocalHost(%s) = %v, want %v", tt.host, got, tt.local)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user