mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Move "start" to daemon/start.go
This is part of an effort to break apart the deprecated server/ package Docker-DCO-1.1-Signed-off-by: Solomon Hykes <solomon@docker.com> (github: shykes)
This commit is contained in:
parent
03c07617cc
commit
4180b87514
4 changed files with 71 additions and 60 deletions
|
@ -129,6 +129,9 @@ func (daemon *Daemon) Install(eng *engine.Engine) error {
|
||||||
if err := eng.Register("stop", daemon.ContainerStop); err != nil {
|
if err := eng.Register("stop", daemon.ContainerStop); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
if err := eng.Register("start", daemon.ContainerStart); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
68
daemon/start.go
Normal file
68
daemon/start.go
Normal file
|
@ -0,0 +1,68 @@
|
||||||
|
package daemon
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/docker/docker/engine"
|
||||||
|
"github.com/docker/docker/runconfig"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (daemon *Daemon) ContainerStart(job *engine.Job) engine.Status {
|
||||||
|
if len(job.Args) < 1 {
|
||||||
|
return job.Errorf("Usage: %s container_id", job.Name)
|
||||||
|
}
|
||||||
|
var (
|
||||||
|
name = job.Args[0]
|
||||||
|
container = daemon.Get(name)
|
||||||
|
)
|
||||||
|
|
||||||
|
if container == nil {
|
||||||
|
return job.Errorf("No such container: %s", name)
|
||||||
|
}
|
||||||
|
|
||||||
|
if container.State.IsRunning() {
|
||||||
|
return job.Errorf("Container already started")
|
||||||
|
}
|
||||||
|
|
||||||
|
// If no environment was set, then no hostconfig was passed.
|
||||||
|
if len(job.Environ()) > 0 {
|
||||||
|
hostConfig := runconfig.ContainerHostConfigFromJob(job)
|
||||||
|
if err := daemon.setHostConfig(container, hostConfig); err != nil {
|
||||||
|
return job.Error(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if err := container.Start(); err != nil {
|
||||||
|
return job.Errorf("Cannot start container %s: %s", name, err)
|
||||||
|
}
|
||||||
|
job.Eng.Job("log", "start", container.ID, daemon.Repositories().ImageName(container.Image)).Run()
|
||||||
|
|
||||||
|
return engine.StatusOK
|
||||||
|
}
|
||||||
|
|
||||||
|
func (daemon *Daemon) setHostConfig(container *Container, hostConfig *runconfig.HostConfig) error {
|
||||||
|
// Validate the HostConfig binds. Make sure that:
|
||||||
|
// the source exists
|
||||||
|
for _, bind := range hostConfig.Binds {
|
||||||
|
splitBind := strings.Split(bind, ":")
|
||||||
|
source := splitBind[0]
|
||||||
|
|
||||||
|
// ensure the source exists on the host
|
||||||
|
_, err := os.Stat(source)
|
||||||
|
if err != nil && os.IsNotExist(err) {
|
||||||
|
err = os.MkdirAll(source, 0755)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("Could not create local directory '%s' for bind mount: %s!", source, err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Register any links from the host config before starting the container
|
||||||
|
if err := daemon.RegisterLinks(container, hostConfig); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
container.SetHostConfig(hostConfig)
|
||||||
|
container.ToDisk()
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
|
@ -408,65 +408,6 @@ func (srv *Server) ContainerDestroy(job *engine.Job) engine.Status {
|
||||||
return engine.StatusOK
|
return engine.StatusOK
|
||||||
}
|
}
|
||||||
|
|
||||||
func (srv *Server) setHostConfig(container *daemon.Container, hostConfig *runconfig.HostConfig) error {
|
|
||||||
// Validate the HostConfig binds. Make sure that:
|
|
||||||
// the source exists
|
|
||||||
for _, bind := range hostConfig.Binds {
|
|
||||||
splitBind := strings.Split(bind, ":")
|
|
||||||
source := splitBind[0]
|
|
||||||
|
|
||||||
// ensure the source exists on the host
|
|
||||||
_, err := os.Stat(source)
|
|
||||||
if err != nil && os.IsNotExist(err) {
|
|
||||||
err = os.MkdirAll(source, 0755)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("Could not create local directory '%s' for bind mount: %s!", source, err.Error())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Register any links from the host config before starting the container
|
|
||||||
if err := srv.daemon.RegisterLinks(container, hostConfig); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
container.SetHostConfig(hostConfig)
|
|
||||||
container.ToDisk()
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (srv *Server) ContainerStart(job *engine.Job) engine.Status {
|
|
||||||
if len(job.Args) < 1 {
|
|
||||||
return job.Errorf("Usage: %s container_id", job.Name)
|
|
||||||
}
|
|
||||||
var (
|
|
||||||
name = job.Args[0]
|
|
||||||
daemon = srv.daemon
|
|
||||||
container = daemon.Get(name)
|
|
||||||
)
|
|
||||||
|
|
||||||
if container == nil {
|
|
||||||
return job.Errorf("No such container: %s", name)
|
|
||||||
}
|
|
||||||
|
|
||||||
if container.State.IsRunning() {
|
|
||||||
return job.Errorf("Container already started")
|
|
||||||
}
|
|
||||||
|
|
||||||
// If no environment was set, then no hostconfig was passed.
|
|
||||||
if len(job.Environ()) > 0 {
|
|
||||||
hostConfig := runconfig.ContainerHostConfigFromJob(job)
|
|
||||||
if err := srv.setHostConfig(container, hostConfig); err != nil {
|
|
||||||
return job.Error(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if err := container.Start(); err != nil {
|
|
||||||
return job.Errorf("Cannot start container %s: %s", name, err)
|
|
||||||
}
|
|
||||||
srv.LogEvent("start", container.ID, daemon.Repositories().ImageName(container.Image))
|
|
||||||
|
|
||||||
return engine.StatusOK
|
|
||||||
}
|
|
||||||
|
|
||||||
func (srv *Server) ContainerWait(job *engine.Job) engine.Status {
|
func (srv *Server) ContainerWait(job *engine.Job) engine.Status {
|
||||||
if len(job.Args) != 1 {
|
if len(job.Args) != 1 {
|
||||||
return job.Errorf("Usage: %s", job.Name)
|
return job.Errorf("Usage: %s", job.Name)
|
||||||
|
|
|
@ -87,7 +87,6 @@ func InitServer(job *engine.Job) engine.Status {
|
||||||
|
|
||||||
for name, handler := range map[string]engine.Handler{
|
for name, handler := range map[string]engine.Handler{
|
||||||
"restart": srv.ContainerRestart,
|
"restart": srv.ContainerRestart,
|
||||||
"start": srv.ContainerStart,
|
|
||||||
"wait": srv.ContainerWait,
|
"wait": srv.ContainerWait,
|
||||||
"tag": srv.ImageTag, // FIXME merge with "image_tag"
|
"tag": srv.ImageTag, // FIXME merge with "image_tag"
|
||||||
"resize": srv.ContainerResize,
|
"resize": srv.ContainerResize,
|
||||||
|
|
Loading…
Reference in a new issue