Ejemplo n.º 1
0
func ContainerCgroup(containerID string, cgroups []CgroupData) ([]CgroupResponse, error) {
	var (
		object []CgroupResponse
	)

	for _, pair := range cgroups {
		cgroupResponse := CgroupResponse{
			Group:     pair.Group,
			Subsystem: pair.Subsystem,
		}
		// set
		if len(pair.Value) > 0 {
			if err := fs.Set(containerID, driver, pair.Group, pair.Subsystem, pair.Value); err != nil {
				cgroupResponse.Out = err.Error()
				cgroupResponse.Status = 255
				object = append(object, cgroupResponse)
				continue
			}
		}
		// get
		output, err := fs.Get(containerID, driver, pair.Group, pair.Subsystem)
		if err != nil {
			cgroupResponse.Out = err.Error()
			cgroupResponse.Status = 1
		} else {
			cgroupResponse.Out = output
			cgroupResponse.Status = 0
		}
		object = append(object, cgroupResponse)
	}

	return object, nil
}
Ejemplo n.º 2
0
func (daemon *Daemon) ContainerCgroup(job *engine.Job) engine.Status {
	if len(job.Args) != 1 {
		return job.Errorf("Usage: %s CONTAINER\n", job.Name)
	}

	var (
		name           = job.Args[0]
		saveToFile     = job.GetenvBool("saveToFile")
		readSubsystem  = job.GetenvList("readSubsystem")
		writeSubsystem []struct {
			Key   string
			Value string
		}
		err error
	)

	job.GetenvJson("writeSubsystem", &writeSubsystem)

	log.Infof("name %s, readSubsystem %s, writeSubsystem %s", name, readSubsystem, writeSubsystem)

	if container := daemon.Get(name); container != nil {
		if !container.State.IsRunning() {
			return job.Errorf("Container %s is not running", name)
		}

		var object []interface{}

		// read
		for _, subsystem := range readSubsystem {
			var cgroupResponse struct {
				Subsystem string
				Out       string
				Err       string
				Status    int
			}

			cgroupResponse.Subsystem = subsystem
			output, err := fs.Get(container.ID, daemon.ExecutionDriver().Parent(), subsystem)
			if err != nil {
				cgroupResponse.Err = err.Error()
				cgroupResponse.Status = 255
			} else {
				cgroupResponse.Out = output
				cgroupResponse.Status = 0
			}
			object = append(object, cgroupResponse)
		}

		// write
		for _, pair := range writeSubsystem {
			var cgroupResponse struct {
				Subsystem string
				Out       string
				Err       string
				Status    int
			}

			cgroupResponse.Subsystem = pair.Key
			oldValue, _ := fs.Get(container.ID, daemon.ExecutionDriver().Parent(), pair.Key)

			err = fs.Set(container.ID, daemon.ExecutionDriver().Parent(), pair.Key, pair.Value)
			if err != nil {
				cgroupResponse.Err = err.Error()
				cgroupResponse.Status = 255
			} else {
				newValue, _ := fs.Get(container.ID, daemon.ExecutionDriver().Parent(), pair.Key)
				log.Infof("cgroup: %s old value: %s, new value: %s", pair.Key, oldValue, newValue)

				/* memory.limit_in_bytes 5g != 5368709120
				if newValue != pair.Value {
					return job.Errorf("cgroup %s change value failed, newValue %s is not same as expect value %s", pair.Key, newValue, pair.Value)
				}*/

				if err = updateConfig(container, pair.Key, pair.Value); err != nil {
					cgroupResponse.Out = err.Error()
					cgroupResponse.Status = 1
				} else {
					cgroupResponse.Out = newValue
					cgroupResponse.Status = 0
				}
			}
			object = append(object, cgroupResponse)
		}

		if saveToFile && err == nil {
			if err := container.ToDisk(); err != nil {
				return job.Error(err)
			}
		}

		b, err := json.Marshal(object)
		if err != nil {
			return job.Error(err)
		}

		job.Stdout.Write(b)
		return engine.StatusOK
	}
	return job.Errorf("No such container: %s", name)
}