check all hosts every tags request

This commit is contained in:
Miki 2024-06-04 15:12:00 +02:00
parent 0f3296c21e
commit 3fedba8a08

62
main.go
View file

@ -25,6 +25,10 @@ type Host struct {
Containers []Container `json:"containers"` Containers []Container `json:"containers"`
} }
type Containers struct {
Containers []Container `json:"containers"`
}
type Container struct { type Container struct {
Name string `json:"names"` Name string `json:"names"`
Port int `json:"port"` Port int `json:"port"`
@ -56,11 +60,20 @@ type Models struct {
Models []Model `json:"models"` Models []Model `json:"models"`
} }
func get_ollama_tags(ctr []Container) (Models, error) { func get_ollama_tags(hosts Hosts) (Models, error) {
var ctr Containers
for _, host := range hosts.Hosts {
fmt.Printf("%s -> %s\n", host.Name, host.IP)
ctr.Adds(get_ollama_containers(host))
// ctr = append(ctr, get_ollama_containers(host)...)
fmt.Println(ctr)
}
retval := []Model{} retval := []Model{}
for _, ollama := range ctr { for _, ollama := range ctr.Containers {
url := fmt.Sprintf("http://%s:%d/api/tags", ollama.IP, ollama.Port) url := fmt.Sprintf("http://%s:%d/api/tags", ollama.IP, ollama.Port)
resp, err := http.Get(url) resp, err := http.Get(url)
if err != nil { if err != nil {
@ -93,9 +106,9 @@ func get_ollama_tags(ctr []Container) (Models, error) {
return Models{Models: retval}, nil return Models{Models: retval}, nil
} }
func get_ollama_containers(host Host) []Container { func get_ollama_containers(host Host) Containers {
ctr_list := []Container{} ctr_list := Containers{}
fmt.Printf("%s -> %s\n", host.Name, host.IP) fmt.Printf("%s -> %s\n", host.Name, host.IP)
cli, err := client.NewClientWithOpts(client.WithHost(fmt.Sprintf("tcp://%s:%s", host.IP, host.Port))) cli, err := client.NewClientWithOpts(client.WithHost(fmt.Sprintf("tcp://%s:%s", host.IP, host.Port)))
@ -115,7 +128,8 @@ func get_ollama_containers(host Host) []Container {
fmt.Println(ctr.Ports[0]) fmt.Println(ctr.Ports[0])
c.IP = host.IP c.IP = host.IP
c.Port = int(ctr.Ports[0].PublicPort) c.Port = int(ctr.Ports[0].PublicPort)
ctr_list = append(ctr_list, c) ctr_list.Add(c)
// ctr_list = append(ctr_list, c)
} }
} }
@ -126,37 +140,37 @@ func (h *Hosts) Add(name string, ip string, port string) {
h.Hosts = append(h.Hosts, Host{Name: name, IP: ip, Port: port}) h.Hosts = append(h.Hosts, Host{Name: name, IP: ip, Port: port})
} }
func (c *Containers) Add(ctr Container) {
c.Containers = append(c.Containers, ctr)
}
func (c *Containers) Adds(ctrs Containers) {
c.Containers = append(c.Containers, ctrs.Containers...)
}
func (h *Hosts) Init() {
h.Add("thor", "192.168.55.13", "2375")
h.Add("ironman", "192.168.55.10", "2375")
}
func main() { func main() {
port := "4000"
hosts := Hosts{} hosts := Hosts{}
hosts.Add("thor", "192.168.55.13", "2375") hosts.Init()
hosts.Add("ironman", "192.168.55.10", "2375")
ctr := []Container{}
// get_ollama_tags()
for _, host := range hosts.Hosts {
fmt.Printf("%s -> %s\n", host.Name, host.IP)
ctr = append(ctr, get_ollama_containers(host)...)
fmt.Println(ctr)
}
models, err := get_ollama_tags(ctr)
if err != nil {
panic(err)
}
fmt.Print(models)
r := gin.Default() r := gin.Default()
r.GET("/api/tags", func(c *gin.Context) { r.GET("/api/tags", func(c *gin.Context) {
models, err := get_ollama_tags(ctr) models, err := get_ollama_tags(hosts)
if err != nil { if err != nil {
panic(err) panic(err)
} }
c.JSON(200, models) c.JSON(200, models)
}) })
r.Run(":4000") fmt.Println("liste")
r.Run(":" + port)
fmt.Print("END") fmt.Print("END")
} }