removed client isLocal()
This commit is contained in:
parent
b5ff0ed4ff
commit
8d5739b833
@ -390,26 +390,6 @@ func (c *Client) Version(ctx context.Context) (string, error) {
|
|||||||
return version.Version, nil
|
return version.Version, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsLocal checks whether the client is connecting to a local server.
|
|
||||||
func (c *Client) IsLocal() bool {
|
|
||||||
// Resolve the host to an IP address and check if the IP is local
|
|
||||||
// Currently, only checks if it is localhost or loopback
|
|
||||||
host, _, err := net.SplitHostPort(c.base.Host)
|
|
||||||
if err != nil {
|
|
||||||
host = c.base.Host
|
|
||||||
}
|
|
||||||
|
|
||||||
if host == "" || host == "localhost" {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
if ip := net.ParseIP(host); ip != nil {
|
|
||||||
return ip.IsLoopback()
|
|
||||||
}
|
|
||||||
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
func Authorization(ctx context.Context, request *http.Request) (string, error) {
|
func Authorization(ctx context.Context, request *http.Request) (string, error) {
|
||||||
data := []byte(fmt.Sprintf("%s,%s,%d", request.Method, request.URL.RequestURI(), time.Now().Unix()))
|
data := []byte(fmt.Sprintf("%s,%s,%d", request.Method, request.URL.RequestURI(), time.Now().Unix()))
|
||||||
|
|
||||||
|
@ -1,8 +1,6 @@
|
|||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
|
||||||
"net/url"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/ollama/ollama/envconfig"
|
"github.com/ollama/ollama/envconfig"
|
||||||
@ -48,80 +46,3 @@ func TestClientFromEnvironment(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test function
|
|
||||||
func TestIsLocal(t *testing.T) {
|
|
||||||
type test struct {
|
|
||||||
client *Client
|
|
||||||
want bool
|
|
||||||
err error
|
|
||||||
}
|
|
||||||
|
|
||||||
tests := map[string]test{
|
|
||||||
"localhost": {
|
|
||||||
client: func() *Client {
|
|
||||||
baseURL, _ := url.Parse("http://localhost:1234")
|
|
||||||
return &Client{base: baseURL, http: &http.Client{}}
|
|
||||||
}(),
|
|
||||||
want: true,
|
|
||||||
err: nil,
|
|
||||||
},
|
|
||||||
"127.0.0.1": {
|
|
||||||
client: func() *Client {
|
|
||||||
baseURL, _ := url.Parse("http://127.0.0.1:1234")
|
|
||||||
return &Client{base: baseURL, http: &http.Client{}}
|
|
||||||
}(),
|
|
||||||
want: true,
|
|
||||||
err: nil,
|
|
||||||
},
|
|
||||||
"example.com": {
|
|
||||||
client: func() *Client {
|
|
||||||
baseURL, _ := url.Parse("http://example.com:1111")
|
|
||||||
return &Client{base: baseURL, http: &http.Client{}}
|
|
||||||
}(),
|
|
||||||
want: false,
|
|
||||||
err: nil,
|
|
||||||
},
|
|
||||||
"8.8.8.8": {
|
|
||||||
client: func() *Client {
|
|
||||||
baseURL, _ := url.Parse("http://8.8.8.8:1234")
|
|
||||||
return &Client{base: baseURL, http: &http.Client{}}
|
|
||||||
}(),
|
|
||||||
want: false,
|
|
||||||
err: nil,
|
|
||||||
},
|
|
||||||
"empty host with port": {
|
|
||||||
client: func() *Client {
|
|
||||||
baseURL, _ := url.Parse("http://:1234")
|
|
||||||
return &Client{base: baseURL, http: &http.Client{}}
|
|
||||||
}(),
|
|
||||||
want: true,
|
|
||||||
err: nil,
|
|
||||||
},
|
|
||||||
"empty host without port": {
|
|
||||||
client: func() *Client {
|
|
||||||
baseURL, _ := url.Parse("http://")
|
|
||||||
return &Client{base: baseURL, http: &http.Client{}}
|
|
||||||
}(),
|
|
||||||
want: true,
|
|
||||||
err: nil,
|
|
||||||
},
|
|
||||||
"remote host without port": {
|
|
||||||
client: func() *Client {
|
|
||||||
baseURL, _ := url.Parse("http://example.com")
|
|
||||||
return &Client{base: baseURL, http: &http.Client{}}
|
|
||||||
}(),
|
|
||||||
want: false,
|
|
||||||
err: nil,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for name, tc := range tests {
|
|
||||||
t.Run(name, func(t *testing.T) {
|
|
||||||
got := tc.client.IsLocal()
|
|
||||||
if got != tc.want {
|
|
||||||
t.Errorf("test %s failed: got %v, want %v", name, got, tc.want)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
37
cmd/cmd.go
37
cmd/cmd.go
@ -282,52 +282,33 @@ func createBlob(cmd *cobra.Command, client *api.Client, path string) (string, er
|
|||||||
|
|
||||||
digest := fmt.Sprintf("sha256:%x", hash.Sum(nil))
|
digest := fmt.Sprintf("sha256:%x", hash.Sum(nil))
|
||||||
|
|
||||||
// Here, we want to check if the server is local
|
// We check if we can find the models directory locally
|
||||||
// If true, call, createBlobLocal
|
// If we can, we return the path to the directory
|
||||||
// This should find the model directory, copy blob over, and return the digest
|
// If we can't, we return an error
|
||||||
// If this fails, just upload it
|
// If the blob exists already, we return the digest
|
||||||
// If this is successful, return the digest
|
|
||||||
|
|
||||||
// Resolve server to IP
|
|
||||||
// Check if server is local
|
|
||||||
/* if client.IsLocal() {
|
|
||||||
digest = strings.ReplaceAll(digest, ":", "-")
|
|
||||||
config, err := client.HeadBlob(cmd.Context(), digest)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
modelDir := config.ModelDir
|
|
||||||
|
|
||||||
// Get blob destination
|
|
||||||
|
|
||||||
dest := filepath.Join(modelDir, "blobs", digest)
|
|
||||||
|
|
||||||
err = createBlobLocal(path, dest)
|
|
||||||
if err == nil {
|
|
||||||
return digest, nil
|
|
||||||
}
|
|
||||||
} */
|
|
||||||
if client.IsLocal() {
|
|
||||||
dest, err := getLocalPath(cmd.Context(), digest)
|
dest, err := getLocalPath(cmd.Context(), digest)
|
||||||
|
|
||||||
if errors.Is(err, ErrBlobExists) {
|
if errors.Is(err, ErrBlobExists) {
|
||||||
return digest, nil
|
return digest, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Successfuly found the model directory
|
||||||
if err == nil {
|
if err == nil {
|
||||||
|
// Copy blob in via OS specific copy
|
||||||
|
// Linux errors out to use io.copy
|
||||||
err = localCopy(path, dest)
|
err = localCopy(path, dest)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
return digest, nil
|
return digest, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Default copy using io.copy
|
||||||
err = defaultCopy(path, dest)
|
err = defaultCopy(path, dest)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
return digest, nil
|
return digest, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
// If at any point copying the blob over locally fails, we default to the copy through the server
|
||||||
if err = client.CreateBlob(cmd.Context(), digest, bin); err != nil {
|
if err = client.CreateBlob(cmd.Context(), digest, bin); err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user