From 3fedba8a086b03ddb9ec41dc68d9f72309cb6c77 Mon Sep 17 00:00:00 2001 From: Miki Date: Tue, 4 Jun 2024 15:12:00 +0200 Subject: [PATCH] check all hosts every tags request --- main.go | 62 +++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 38 insertions(+), 24 deletions(-) diff --git a/main.go b/main.go index facdb04..c4ecd50 100644 --- a/main.go +++ b/main.go @@ -25,6 +25,10 @@ type Host struct { Containers []Container `json:"containers"` } +type Containers struct { + Containers []Container `json:"containers"` +} + type Container struct { Name string `json:"names"` Port int `json:"port"` @@ -56,11 +60,20 @@ type Models struct { 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{} - for _, ollama := range ctr { + for _, ollama := range ctr.Containers { url := fmt.Sprintf("http://%s:%d/api/tags", ollama.IP, ollama.Port) resp, err := http.Get(url) if err != nil { @@ -93,9 +106,9 @@ func get_ollama_tags(ctr []Container) (Models, error) { 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) 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]) c.IP = host.IP 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}) } +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() { + port := "4000" + hosts := Hosts{} - hosts.Add("thor", "192.168.55.13", "2375") - 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) + hosts.Init() r := gin.Default() r.GET("/api/tags", func(c *gin.Context) { - models, err := get_ollama_tags(ctr) + models, err := get_ollama_tags(hosts) if err != nil { panic(err) } c.JSON(200, models) }) - r.Run(":4000") + fmt.Println("liste") + r.Run(":" + port) fmt.Print("END") }