ctop/containermap.go
2017-01-12 14:24:12 -05:00

78 lines
1.5 KiB
Go

package main
import (
"sort"
"github.com/fsouza/go-dockerclient"
)
var filters = map[string][]string{
"status": []string{"running"},
}
func NewContainerMap() *ContainerMap {
// init docker client
client, err := docker.NewClient(GlobalConfig["dockerHost"])
if err != nil {
panic(err)
}
cm := &ContainerMap{
client: client,
containers: make(map[string]*Container),
}
cm.Refresh()
return cm
}
type ContainerMap struct {
client *docker.Client
containers map[string]*Container
}
func (cm *ContainerMap) Refresh() {
var id string
opts := docker.ListContainersOptions{
Filters: filters,
}
containers, err := cm.client.ListContainers(opts)
if err != nil {
panic(err)
}
for _, c := range containers {
id = c.ID[:12]
if _, ok := cm.containers[id]; ok == false {
cm.containers[id] = NewContainer(c)
cm.containers[id].Collect(cm.client)
}
}
}
// Kill a container by ID
func (cm *ContainerMap) Kill(id string, sig docker.Signal) error {
opts := docker.KillContainerOptions{
ID: id,
Signal: sig,
}
return cm.client.KillContainer(opts)
}
// Return number of containers/rows
func (cm *ContainerMap) Len() uint {
return uint(len(cm.containers))
}
// Get a single container, by ID
func (cm *ContainerMap) Get(id string) *Container {
return cm.containers[id]
}
// Return array of all containers, sorted by field
func (cm *ContainerMap) All() []*Container {
var containers Containers
for _, c := range cm.containers {
containers = append(containers, c)
}
sort.Sort(containers)
return containers
}