Exemple #1
0
// Teardown the container. This will kill the docker container but will not free the ports/containers
func Teardown(c types.GenericContainer) error {
	if pretending() {
		log.Printf("[pretend] teardown %s...", c.GetID())
		return nil
	} else {
		log.Printf("teardown %s...", c.GetID())
	}
	defer removeExited()
	dockerLock.Lock()
	err := dockerClient.KillContainer(docker.KillContainerOptions{ID: c.GetDockerID()})
	dockerLock.Unlock()
	if err != nil {
		log.Printf("failed to teardown[kill] %s: %v", c.GetID(), err)
		return err
	}
	// Make sure the container is dead before we return to avoid cmk (or other) race conditions
	dockerLock.Lock()
	_, err = dockerClient.WaitContainer(c.GetDockerID())
	dockerLock.Unlock()
	if err != nil {
		log.Printf("failed to wait on dead container[wait] %s: %v", c.GetID(), err)
		// Continue, since this is non-fatal and we should continue cleaning up.
	}
	// TODO do something with log dir
	return RemoveConfigDir(c)
}
Exemple #2
0
func Deploy(c types.GenericContainer) error {
	dRepo := fmt.Sprintf("%s/%s/%s-%s", RegistryHost, c.GetDockerRepo(), c.GetApp(), c.GetSha())
	// Pull docker container
	if pretending() {
		log.Printf("[%s][pretend] deploy with %s @ %s...", c.GetID(), c.GetApp(), c.GetSha())
		log.Printf("[%s][pretend] docker pull %s", c.GetID(), dRepo)
		log.Printf("[%s][pretend] docker run %s", c.GetID(), dRepo)
		c.SetDockerID(fmt.Sprintf("pretend-docker-id-%s", c.GetID()))
	} else {
		log.Printf("[%s] deploy with %s @ %s...", c.GetID(), c.GetApp(), c.GetSha())
		log.Printf("[%s] docker pull %s", c.GetID(), dRepo)
		dockerLock.Lock()
		err := dockerClient.PullImage(docker.PullImageOptions{Repository: dRepo}, docker.AuthConfiguration{})
		dockerLock.Unlock()
		if err != nil {
			log.Printf("[%s] ERROR: failed to pull %s", c.GetID(), dRepo)
			return err
		}

		// make log dir for volume
		err = os.MkdirAll(helper.HostLogDir(c.GetID()), 0755)
		if err != nil {
			return err
		}
		// make config dir for volume
		err = os.MkdirAll(helper.HostConfigDir(c.GetID()), 0755)
		if err != nil {
			return err
		}
		// put config in config dir
		appCfg, err := AppCfgs(c)
		if err != nil {
			return err
		}
		if err := appCfg.Save(helper.HostConfigFile(c.GetID())); err != nil {
			RemoveConfigDir(c)
			return err
		}

		log.Printf("[%s] docker run %s", c.GetID(), dRepo)
		// create docker container
		dCfg, dHostCfg := DockerCfgs(c)
		dockerLock.Lock()
		dCont, err := dockerClient.CreateContainer(docker.CreateContainerOptions{Name: c.GetID(), Config: dCfg})
		dockerLock.Unlock()
		if err != nil {
			log.Printf("[%s] ERROR: failed to create container: %s", c.GetID(), err.Error())
			return err
		}
		c.SetDockerID(dCont.ID)

		// start docker container
		dockerLock.Lock()
		err = dockerClient.StartContainer(c.GetDockerID(), dHostCfg)
		dockerLock.Unlock()
		if err != nil {
			log.Printf("[%s] ERROR: failed to start container: %s", c.GetID(), err.Error())
			log.Printf("[%s] -- full create response:\n%+v", c.GetID(), dCont)
			log.Printf("[%s] inspecting container for more information...", c.GetID())
			dockerLock.Lock()
			inspCont, ierr := dockerClient.InspectContainer(c.GetDockerID())
			dockerLock.Unlock()
			if ierr != nil {
				log.Printf("[%s] ERROR: failed to inspect container: %s", c.GetID(), ierr.Error())
				return ierr
			}
			log.Printf("[%s] -- inspected container:\n%+v", c.GetID(), inspCont)
			return err
		}

		dockerLock.Lock()
		inspCont, err := dockerClient.InspectContainer(c.GetDockerID())
		dockerLock.Unlock()
		if err != nil {
			log.Printf("[%s] ERROR: failed to inspect container: %s", c.GetID(), err.Error())
			return err
		}
		if inspCont.NetworkSettings == nil {
			log.Printf("[%s] ERROR: failed to get container network settings.")
			return errors.New("Could not get NetworkSettings from docker")
		}
		c.SetIP(inspCont.NetworkSettings.IPAddress)
		c.SetPid(inspCont.State.Pid)
	}
	return nil
}