func (s *GardenServer) handleCreate(w http.ResponseWriter, r *http.Request) {
	var spec garden.ContainerSpec
	if !s.readRequest(&spec, w, r) {
		return
	}

	hLog := s.logger.Session("create", lager.Data{
		"request": containerDebugInfo{
			Handle:     spec.Handle,
			GraceTime:  spec.GraceTime,
			RootFSPath: spec.RootFSPath,
			BindMounts: spec.BindMounts,
			Network:    spec.Network,
			Privileged: spec.Privileged,
			Limits:     spec.Limits,
		},
	})

	if spec.GraceTime == 0 {
		spec.GraceTime = s.containerGraceTime
	}

	hLog.Debug("creating")

	container, err := s.backend.Create(spec)
	if err != nil {
		s.writeError(w, err, hLog)
		return
	}

	hLog.Info("created")

	s.bomberman.Strap(container)

	s.writeResponse(w, &struct{ Handle string }{
		Handle: container.Handle(),
	})
}
Exemple #2
0
// Create creates a container by combining the results of networker.Network,
// volumizer.Create and containzer.Create.
func (g *Gardener) Create(spec garden.ContainerSpec) (ctr garden.Container, err error) {
	if err := g.checkDuplicateHandle(spec.Handle); err != nil {
		return nil, err
	}

	if spec.Handle == "" {
		spec.Handle = g.UidGenerator.Generate()
	}

	log := g.Logger.Session("create", lager.Data{"handle": spec.Handle})

	log.Info("start")
	defer func() {
		if err != nil {
			log := log.Session("create-failed-cleaningup", lager.Data{
				"cause": err.Error(),
			})

			log.Info("start")

			err := g.destroy(log, spec.Handle)
			if err != nil {
				log.Error("destroy-failed", err)
			}

			log.Info("cleanedup")
		} else {
			log.Info("created")
		}
	}()

	rootFSURL, err := url.Parse(spec.RootFSPath)
	if err != nil {
		return nil, err
	}

	if err := g.VolumeCreator.GC(log); err != nil {
		log.Error("graph-cleanup-failed", err)
	}

	var rootFSPath string
	var env []string

	if rootFSURL.Scheme == RawRootFSScheme {
		rootFSPath = rootFSURL.Path
	} else {
		var err error
		rootFSPath, env, err = g.VolumeCreator.Create(log, spec.Handle, rootfs_provider.Spec{
			RootFS:     rootFSURL,
			QuotaSize:  int64(spec.Limits.Disk.ByteHard),
			QuotaScope: spec.Limits.Disk.Scope,
			Namespaced: !spec.Privileged,
		})
		if err != nil {
			return nil, err
		}
	}

	if err := g.Containerizer.Create(log, DesiredContainerSpec{
		Handle:     spec.Handle,
		RootFSPath: rootFSPath,
		Hostname:   spec.Handle,
		Privileged: spec.Privileged,
		BindMounts: spec.BindMounts,
		Limits:     spec.Limits,
		Env:        append(env, spec.Env...),
	}); err != nil {
		return nil, err
	}

	actualSpec, err := g.Containerizer.Info(log, spec.Handle)
	if err != nil {
		return nil, err
	}

	if err = g.Networker.Network(log, spec, actualSpec.Pid); err != nil {
		return nil, err
	}

	container, err := g.Lookup(spec.Handle)
	if err != nil {
		return nil, err
	}

	if spec.GraceTime != 0 {
		if err := container.SetGraceTime(spec.GraceTime); err != nil {
			return nil, err
		}
	}

	for name, value := range spec.Properties {
		if err := container.SetProperty(name, value); err != nil {
			return nil, err
		}
	}

	if err := container.SetProperty("garden.state", "created"); err != nil {
		return nil, err
	}

	return container, nil
}