Example #1
0
func (c *containerRouter) postContainerCreate(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
	if err := httputils.ParseForm(r); err != nil {
		return err
	}

	if err := httputils.CheckForJSON(r); err != nil {
		return err
	}

	podId := r.Form.Get("podId")
	if podId == "" {
		return fmt.Errorf("podId is required to create a new container")
	}

	containerArgs, err := ioutil.ReadAll(r.Body)
	if err != nil {
		return err
	}

	glog.V(1).Infof("Create container %s in pod %s", string(containerArgs), podId)

	containterID, err := c.backend.CmdCreateContainer(podId, containerArgs)
	if err != nil {
		return err
	}

	v := &engine.Env{}
	v.SetJson("ID", containterID)
	return v.WriteJSON(w, http.StatusCreated)
}
Example #2
0
func (p *podRouter) postPodCreate(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
	if err := httputils.ParseForm(r); err != nil {
		return err
	}

	if err := httputils.CheckForJSON(r); err != nil {
		return err
	}

	podArgs, _ := ioutil.ReadAll(r.Body)
	glog.V(1).Infof("Args string is %s", string(podArgs))

	env, err := p.backend.CmdCreatePod(string(podArgs))
	if err != nil {
		return err
	}

	return env.WriteJSON(w, http.StatusCreated)
}
Example #3
0
// debugRequestMiddleware dumps the request to logger
func debugRequestMiddleware(handler httputils.APIFunc) httputils.APIFunc {
	return func(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
		glog.V(3).Infof("%s %s", r.Method, r.RequestURI)

		if r.Method != "POST" {
			return handler(ctx, w, r, vars)
		}
		if err := httputils.CheckForJSON(r); err != nil {
			return handler(ctx, w, r, vars)
		}
		maxBodySize := 4096 // 4KB
		if r.ContentLength > int64(maxBodySize) {
			return handler(ctx, w, r, vars)
		}

		body := r.Body
		bufReader := bufio.NewReaderSize(body, maxBodySize)
		r.Body = ioutils.NewReadCloserWrapper(bufReader, func() error { return body.Close() })

		b, err := bufReader.Peek(maxBodySize)
		if err != io.EOF {
			// either there was an error reading, or the buffer is full (in which case the request is too large)
			return handler(ctx, w, r, vars)
		}

		var postForm map[string]interface{}
		if err := json.Unmarshal(b, &postForm); err == nil {
			if _, exists := postForm["password"]; exists {
				postForm["password"] = "******"
			}
			formStr, errMarshal := json.Marshal(postForm)
			if errMarshal == nil {
				glog.V(3).Infof("form data: %s", string(formStr))
			} else {
				glog.V(3).Infof("form data: %q", postForm)
			}
		}

		return handler(ctx, w, r, vars)
	}
}
Example #4
0
func (p *podRouter) postPodCreate(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
	if err := httputils.ParseForm(r); err != nil {
		return err
	}

	if err := httputils.CheckForJSON(r); err != nil {
		return err
	}

	podArgs, _ := ioutil.ReadAll(r.Body)
	autoRemove := false
	if r.Form.Get("remove") == "yes" || r.Form.Get("remove") == "true" {
		autoRemove = true
	}
	glog.V(1).Infof("Args string is %s, autoremove %v", string(podArgs), autoRemove)

	env, err := p.backend.CmdCreatePod(string(podArgs), autoRemove)
	if err != nil {
		return err
	}

	return env.WriteJSON(w, http.StatusCreated)
}