78 lines
1.9 KiB
Go
78 lines
1.9 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log"
|
|
"strings"
|
|
|
|
"github.com/docker/docker/api/types/container"
|
|
"github.com/docker/docker/client"
|
|
)
|
|
|
|
type Containers struct {
|
|
Containers []Container `json:"containers"`
|
|
}
|
|
|
|
type Container struct {
|
|
Name string `json:"names"`
|
|
Port int `json:"port"`
|
|
IP string `json:"ip"`
|
|
Image string `json:"image"`
|
|
ID string `json:"id"`
|
|
Engine string `json:"engine"`
|
|
Status string `json:"status"`
|
|
State string `json:"state"`
|
|
}
|
|
|
|
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 get_containers_list(host Host, all bool) Containers {
|
|
|
|
ctr_list := Containers{}
|
|
|
|
cli, err := client.NewClientWithOpts(client.WithHost(fmt.Sprintf("tcp://%s:%s", host.IP, host.Port)))
|
|
if err != nil {
|
|
log.Println(err)
|
|
return ctr_list
|
|
}
|
|
defer cli.Close()
|
|
|
|
containers, err := cli.ContainerList(context.Background(), container.ListOptions{All: all})
|
|
if err != nil {
|
|
log.Println(err)
|
|
return ctr_list
|
|
}
|
|
|
|
for _, ctr := range containers {
|
|
if strings.Contains(ctr.Names[0], "ollama") {
|
|
c := Container{ID: ctr.ID, Name: ctr.Names[0], Image: ctr.Image, State: ctr.State, Status: ctr.Status, Engine: "ollama"}
|
|
if ctr.State == "running" {
|
|
// fmt.Println(ctr.Ports[0])
|
|
c.Port = int(ctr.Ports[0].PublicPort)
|
|
}
|
|
c.IP = host.IP
|
|
ctr_list.Add(c)
|
|
// ctr_list = append(ctr_list, c)
|
|
} else if strings.Contains(ctr.Names[0], "vllm") {
|
|
c := Container{ID: ctr.ID, Name: ctr.Names[0], Image: ctr.Image, State: ctr.State, Status: ctr.Status, Engine: "vllm"}
|
|
if ctr.State == "running" {
|
|
// fmt.Println(ctr.Ports[0])
|
|
c.Port = int(ctr.Ports[0].PublicPort)
|
|
}
|
|
c.IP = host.IP
|
|
ctr_list.Add(c)
|
|
// ctr_list = append(ctr_list, c)
|
|
}
|
|
}
|
|
|
|
// fmt.Println(ctr_list)
|
|
|
|
return ctr_list
|
|
}
|