2017-06-08 14:51:02 +00:00
|
|
|
package container
|
2016-12-22 16:15:22 +00:00
|
|
|
|
|
|
|
import (
|
2017-06-27 16:21:16 +00:00
|
|
|
"github.com/bcicen/ctop/connector/collector"
|
2017-11-22 14:27:38 +00:00
|
|
|
"github.com/bcicen/ctop/connector/manager"
|
2017-03-06 08:25:59 +00:00
|
|
|
"github.com/bcicen/ctop/cwidgets"
|
2017-02-26 22:04:24 +00:00
|
|
|
"github.com/bcicen/ctop/cwidgets/compact"
|
2017-06-08 14:51:02 +00:00
|
|
|
"github.com/bcicen/ctop/logging"
|
2017-06-27 15:46:03 +00:00
|
|
|
"github.com/bcicen/ctop/models"
|
2016-12-22 16:15:22 +00:00
|
|
|
)
|
|
|
|
|
2017-06-08 14:51:02 +00:00
|
|
|
var (
|
|
|
|
log = logging.Init()
|
|
|
|
)
|
|
|
|
|
2018-10-25 20:23:44 +00:00
|
|
|
const (
|
|
|
|
running = "running"
|
|
|
|
)
|
|
|
|
|
2017-03-03 07:57:26 +00:00
|
|
|
// Metrics and metadata representing a container
|
2016-12-22 22:04:21 +00:00
|
|
|
type Container struct {
|
2017-06-27 15:46:03 +00:00
|
|
|
models.Metrics
|
2017-03-03 07:57:26 +00:00
|
|
|
Id string
|
2019-06-08 21:34:43 +00:00
|
|
|
Meta models.Meta
|
2017-03-03 07:57:26 +00:00
|
|
|
Widgets *compact.Compact
|
2017-06-08 14:51:02 +00:00
|
|
|
Display bool // display this container in compact view
|
2017-03-06 08:25:59 +00:00
|
|
|
updater cwidgets.WidgetUpdater
|
2017-06-27 16:21:16 +00:00
|
|
|
collector collector.Collector
|
2017-11-20 11:09:36 +00:00
|
|
|
manager manager.Manager
|
2017-01-06 11:51:11 +00:00
|
|
|
}
|
|
|
|
|
2017-11-20 11:09:36 +00:00
|
|
|
func New(id string, collector collector.Collector, manager manager.Manager) *Container {
|
2017-03-06 08:25:59 +00:00
|
|
|
widgets := compact.NewCompact(id)
|
2017-03-03 07:57:26 +00:00
|
|
|
return &Container{
|
2017-06-27 15:46:03 +00:00
|
|
|
Metrics: models.NewMetrics(),
|
2017-03-03 07:57:26 +00:00
|
|
|
Id: id,
|
2019-06-08 21:34:43 +00:00
|
|
|
Meta: models.NewMeta(),
|
2017-03-06 08:25:59 +00:00
|
|
|
Widgets: widgets,
|
|
|
|
updater: widgets,
|
2017-03-03 07:57:26 +00:00
|
|
|
collector: collector,
|
2017-11-20 11:09:36 +00:00
|
|
|
manager: manager,
|
2017-02-24 01:18:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-06 08:25:59 +00:00
|
|
|
func (c *Container) SetUpdater(u cwidgets.WidgetUpdater) {
|
|
|
|
c.updater = u
|
2019-06-08 21:34:43 +00:00
|
|
|
c.updater.SetMeta(c.Meta)
|
2017-02-24 01:18:59 +00:00
|
|
|
}
|
|
|
|
|
2017-03-03 07:57:26 +00:00
|
|
|
func (c *Container) SetMeta(k, v string) {
|
|
|
|
c.Meta[k] = v
|
2019-06-08 21:34:43 +00:00
|
|
|
c.updater.SetMeta(c.Meta)
|
2017-02-24 01:18:59 +00:00
|
|
|
}
|
|
|
|
|
2017-03-06 08:25:59 +00:00
|
|
|
func (c *Container) GetMeta(k string) string {
|
2019-06-08 21:34:43 +00:00
|
|
|
return c.Meta.Get(k)
|
2017-03-05 06:46:41 +00:00
|
|
|
}
|
|
|
|
|
2017-02-03 23:33:13 +00:00
|
|
|
func (c *Container) SetState(s string) {
|
2017-03-06 08:25:59 +00:00
|
|
|
c.SetMeta("state", s)
|
2017-03-03 07:57:26 +00:00
|
|
|
// start collector, if needed
|
2018-10-25 20:23:44 +00:00
|
|
|
if s == running && !c.collector.Running() {
|
2017-03-03 07:57:26 +00:00
|
|
|
c.collector.Start()
|
|
|
|
c.Read(c.collector.Stream())
|
|
|
|
}
|
|
|
|
// stop collector, if needed
|
2018-10-25 20:23:44 +00:00
|
|
|
if s != running && c.collector.Running() {
|
2017-03-03 07:57:26 +00:00
|
|
|
c.collector.Stop()
|
|
|
|
}
|
2017-02-24 09:10:14 +00:00
|
|
|
}
|
|
|
|
|
2019-03-07 02:33:29 +00:00
|
|
|
// Logs returns container log collector
|
2017-06-27 17:18:17 +00:00
|
|
|
func (c *Container) Logs() collector.LogCollector {
|
|
|
|
return c.collector.Logs()
|
|
|
|
}
|
|
|
|
|
2017-02-23 02:01:56 +00:00
|
|
|
// Read metric stream, updating widgets
|
2017-06-27 15:46:03 +00:00
|
|
|
func (c *Container) Read(stream chan models.Metrics) {
|
2016-12-22 22:04:21 +00:00
|
|
|
go func() {
|
2017-02-23 02:01:56 +00:00
|
|
|
for metrics := range stream {
|
2017-03-03 07:57:26 +00:00
|
|
|
c.Metrics = metrics
|
2017-03-06 08:25:59 +00:00
|
|
|
c.updater.SetMetrics(metrics)
|
2016-12-22 22:04:21 +00:00
|
|
|
}
|
2017-03-03 07:57:26 +00:00
|
|
|
log.Infof("reader stopped for container: %s", c.Id)
|
2017-06-27 15:46:03 +00:00
|
|
|
c.Metrics = models.NewMetrics()
|
2017-03-03 07:57:26 +00:00
|
|
|
c.Widgets.Reset()
|
2016-12-22 22:04:21 +00:00
|
|
|
}()
|
2017-03-03 07:57:26 +00:00
|
|
|
log.Infof("reader started for container: %s", c.Id)
|
2016-12-22 22:04:21 +00:00
|
|
|
}
|
2017-11-20 11:09:36 +00:00
|
|
|
|
|
|
|
func (c *Container) Start() {
|
2018-10-25 20:23:44 +00:00
|
|
|
if c.Meta["state"] != running {
|
2017-11-20 11:09:36 +00:00
|
|
|
if err := c.manager.Start(); err != nil {
|
|
|
|
log.Warningf("container %s: %v", c.Id, err)
|
2018-01-11 18:27:30 +00:00
|
|
|
log.StatusErr(err)
|
2017-11-20 11:09:36 +00:00
|
|
|
return
|
|
|
|
}
|
2018-10-25 20:23:44 +00:00
|
|
|
c.SetState(running)
|
2017-11-20 11:09:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Container) Stop() {
|
2018-10-25 20:23:44 +00:00
|
|
|
if c.Meta["state"] == running {
|
2017-11-20 11:09:36 +00:00
|
|
|
if err := c.manager.Stop(); err != nil {
|
|
|
|
log.Warningf("container %s: %v", c.Id, err)
|
2018-01-11 18:27:30 +00:00
|
|
|
log.StatusErr(err)
|
2017-11-20 11:09:36 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
c.SetState("exited")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Container) Remove() {
|
2018-01-11 14:40:21 +00:00
|
|
|
if err := c.manager.Remove(); err != nil {
|
|
|
|
log.Warningf("container %s: %v", c.Id, err)
|
2018-01-11 18:27:30 +00:00
|
|
|
log.StatusErr(err)
|
2017-11-20 11:09:36 +00:00
|
|
|
}
|
|
|
|
}
|
2018-06-22 07:41:16 +00:00
|
|
|
|
|
|
|
func (c *Container) Pause() {
|
2018-10-25 20:23:44 +00:00
|
|
|
if c.Meta["state"] == running {
|
2018-06-22 07:41:16 +00:00
|
|
|
if err := c.manager.Pause(); err != nil {
|
|
|
|
log.Warningf("container %s: %v", c.Id, err)
|
|
|
|
log.StatusErr(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.SetState("paused")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Container) Unpause() {
|
|
|
|
if c.Meta["state"] == "paused" {
|
|
|
|
if err := c.manager.Unpause(); err != nil {
|
|
|
|
log.Warningf("container %s: %v", c.Id, err)
|
|
|
|
log.StatusErr(err)
|
|
|
|
return
|
|
|
|
}
|
2018-10-25 20:23:44 +00:00
|
|
|
c.SetState(running)
|
2018-06-22 07:41:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Container) Restart() {
|
2018-10-25 20:23:44 +00:00
|
|
|
if c.Meta["state"] == running {
|
2018-06-22 07:41:16 +00:00
|
|
|
if err := c.manager.Restart(); err != nil {
|
|
|
|
log.Warningf("container %s: %v", c.Id, err)
|
|
|
|
log.StatusErr(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-10-13 05:33:53 +00:00
|
|
|
|
|
|
|
func (c *Container) Exec(cmd []string) error {
|
|
|
|
return c.manager.Exec(cmd)
|
|
|
|
}
|