package docker import ( "path" "github.com/rosti-cz/apps-api/apps" ) // Container extends App struct from App type Container struct { App apps.App `json:"app"` } func (c *Container) getDriver() *Driver { driver := &Driver{} return driver } // volumeHostPath each container has one volume mounted into it, func (c *Container) volumeHostPath() string { return path.Join("/srv", c.App.Name) } // Status returns state of the container // Possible values: running, stopped, data-only, unknown func (c *Container) Status() (string, error) { status := "unknown" // if _, err := os.Stat(path.Join("/srv", c.App.Name)); !os.IsNotExist(err) { // status = "data-only" // } driver := c.getDriver() containerStatus, err := driver.Status(c.App.Name) if err != nil { return status, err } if containerStatus == "no-container" { status = "data-only" } else { status = containerStatus } return status, nil } // DiskUsage returns number of MB and inodes used by the container in it's mounted volume func (c *Container) DiskUsage() (int, int, error) { return du(c.volumeHostPath()) } // ResourceUsage returns amount of memory in MB and CPU in % that the app occupies func (c *Container) ResourceUsage() (float64, int, error) { driver := c.getDriver() cpu, memory, err := driver.Stats(c.App.Name) if err != nil { return 0.0, 0, err } return cpu, memory, nil } // Create creates the container func (c *Container) Create() error { driver := c.getDriver() _, err := driver.Create( c.App.Name, c.App.Image, c.volumeHostPath(), c.App.HTTPPort, c.App.SSHPort, c.App.CPU, c.App.Memory, []string{}, ) return err } // Start starts the container func (c *Container) Start() error { driver := c.getDriver() return driver.Start(c.App.Name) } // Stop stops the container func (c *Container) Stop() error { driver := c.getDriver() return driver.Stop(c.App.Name) } // Restart restarts the container func (c *Container) Restart() error { driver := c.getDriver() err := driver.Stop(c.App.Name) if err != nil { return err } return driver.Start(c.App.Name) } // Destroy removes the container but keeps the data so it can be created again func (c *Container) Destroy() error { driver := c.getDriver() return driver.Remove(c.App.Name) } // Delete removes both data and the container func (c *Container) Delete() error { err := c.Destroy() if err != nil { return err } volumePath := path.Join("/srv", c.App.Name) err = removeDirectory(volumePath) return err }