moby/docker.go

118 lines
2.7 KiB
Go
Raw Normal View History

2013-01-19 00:13:39 +00:00
package docker
import (
"container/list"
"fmt"
"io/ioutil"
"log"
2013-01-19 00:13:39 +00:00
"os"
"path"
)
type Docker struct {
root string
repository string
containers *list.List
}
func (docker *Docker) List() []*Container {
containers := []*Container{}
for e := docker.containers.Front(); e != nil; e = e.Next() {
containers = append(containers, e.Value.(*Container))
}
return containers
}
2013-01-22 02:39:52 +00:00
func (docker *Docker) getContainerElement(id string) *list.Element {
2013-01-19 00:13:39 +00:00
for e := docker.containers.Front(); e != nil; e = e.Next() {
container := e.Value.(*Container)
2013-01-22 02:39:52 +00:00
if container.Id == id {
2013-01-19 00:13:39 +00:00
return e
}
}
return nil
}
2013-01-22 02:39:52 +00:00
func (docker *Docker) Get(id string) *Container {
e := docker.getContainerElement(id)
2013-01-19 00:13:39 +00:00
if e == nil {
return nil
}
return e.Value.(*Container)
}
2013-01-22 02:39:52 +00:00
func (docker *Docker) Exists(id string) bool {
return docker.Get(id) != nil
2013-01-19 00:13:39 +00:00
}
2013-01-22 02:39:52 +00:00
func (docker *Docker) Create(id string, command string, args []string, layers []string, config *Config) (*Container, error) {
if docker.Exists(id) {
return nil, fmt.Errorf("Container %v already exists", id)
2013-01-19 00:13:39 +00:00
}
2013-01-22 02:39:52 +00:00
root := path.Join(docker.repository, id)
container, err := createContainer(id, root, command, args, layers, config)
2013-01-19 00:13:39 +00:00
if err != nil {
return nil, err
}
docker.containers.PushBack(container)
return container, nil
}
func (docker *Docker) Destroy(container *Container) error {
2013-01-22 02:39:52 +00:00
element := docker.getContainerElement(container.Id)
2013-01-19 00:13:39 +00:00
if element == nil {
2013-01-22 02:39:52 +00:00
return fmt.Errorf("Container %v not found - maybe it was already destroyed?", container.Id)
2013-01-19 00:13:39 +00:00
}
if err := container.Stop(); err != nil {
return err
}
if container.Filesystem.IsMounted() {
if err := container.Filesystem.Umount(); err != nil {
log.Printf("Unable to umount container %v: %v", container.Id, err)
}
}
2013-01-19 00:13:39 +00:00
if err := os.RemoveAll(container.Root); err != nil {
log.Printf("Unable to remove filesystem for %v: %v", container.Id, err)
2013-01-19 00:13:39 +00:00
}
docker.containers.Remove(element)
return nil
}
func (docker *Docker) restore() error {
dir, err := ioutil.ReadDir(docker.repository)
if err != nil {
return err
}
for _, v := range dir {
container, err := loadContainer(path.Join(docker.repository, v.Name()))
if err != nil {
log.Printf("Failed to load container %v: %v", v.Name(), err)
2013-01-19 00:13:39 +00:00
continue
}
docker.containers.PushBack(container)
}
return nil
}
func New() (*Docker, error) {
return NewFromDirectory("/var/lib/docker")
}
func NewFromDirectory(root string) (*Docker, error) {
docker := &Docker{
root: root,
repository: path.Join(root, "containers"),
containers: list.New(),
}
if err := os.Mkdir(docker.repository, 0700); err != nil && !os.IsExist(err) {
return nil, err
}
if err := docker.restore(); err != nil {
return nil, err
}
return docker, nil
}