func postVmCreate(eng *engine.Engine, version version.Version, w http.ResponseWriter, r *http.Request, vars map[string]string) error { if err := r.ParseForm(); err != nil { return nil } job := eng.Job("vmCreate", r.Form.Get("cpu"), r.Form.Get("mem")) stdoutBuf := bytes.NewBuffer(nil) job.Stdout.Add(stdoutBuf) if err := job.Run(); err != nil { return err } var ( env engine.Env dat map[string]interface{} returnedJSONstr string ) returnedJSONstr = engine.Tail(stdoutBuf, 1) if err := json.Unmarshal([]byte(returnedJSONstr), &dat); err != nil { return err } env.Set("ID", dat["ID"].(string)) env.SetInt("Code", (int)(dat["Code"].(float64))) env.Set("Cause", dat["Cause"].(string)) return writeJSONEnv(w, http.StatusOK, env) }
func postPodRemove(eng *engine.Engine, version version.Version, w http.ResponseWriter, r *http.Request, vars map[string]string) error { if err := r.ParseForm(); err != nil { return nil } glog.V(1).Infof("Pod(%s) is process to be removed", r.Form.Get("podId")) job := eng.Job("podRm", r.Form.Get("podId")) stdoutBuf := bytes.NewBuffer(nil) job.Stdout.Add(stdoutBuf) if err := job.Run(); err != nil { return err } var ( env engine.Env dat map[string]interface{} returnedJSONstr string ) returnedJSONstr = engine.Tail(stdoutBuf, 1) if err := json.Unmarshal([]byte(returnedJSONstr), &dat); err != nil { return err } env.Set("ID", dat["ID"].(string)) env.SetInt("Code", (int)(dat["Code"].(float64))) env.Set("Cause", dat["Cause"].(string)) return writeJSONEnv(w, http.StatusOK, env) }
func postContainerCommit(eng *engine.Engine, version version.Version, w http.ResponseWriter, r *http.Request, vars map[string]string) error { if err := r.ParseForm(); err != nil { return nil } glog.V(1).Infof("container ID is %s", r.Form.Get("container")) job := eng.Job("commit", r.Form.Get("container"), r.Form.Get("repo"), r.Form.Get("author"), r.Form.Get("change"), r.Form.Get("message"), r.Form.Get("pause")) stdoutBuf := bytes.NewBuffer(nil) job.Stdout.Add(stdoutBuf) if err := job.Run(); err != nil { return err } var ( env engine.Env dat map[string]interface{} returnedJSONstr string ) returnedJSONstr = engine.Tail(stdoutBuf, 1) if err := json.Unmarshal([]byte(returnedJSONstr), &dat); err != nil { return err } env.Set("ID", dat["ID"].(string)) env.SetInt("Code", (int)(dat["Code"].(float64))) env.Set("Cause", dat["Cause"].(string)) return writeJSONEnv(w, http.StatusOK, env) }
func postContainerCreate(eng *engine.Engine, version version.Version, w http.ResponseWriter, r *http.Request, vars map[string]string) error { if err := r.ParseForm(); err != nil { return nil } glog.V(1).Infof("Image name is %s", r.Form.Get("imageName")) job := eng.Job("create", r.Form.Get("imageName")) stdoutBuf := bytes.NewBuffer(nil) stderrBuf := bytes.NewBuffer(nil) job.Stdout.Add(stdoutBuf) job.Stderr.Add(stderrBuf) if err := job.Run(); err != nil { return err } var ( env engine.Env dat map[string]interface{} returnedJSONstr string ) returnedJSONstr = engine.Tail(stdoutBuf, 1) if err := json.Unmarshal([]byte(returnedJSONstr), &dat); err != nil { return err } env.Set("ContainerID", dat["ContainerID"].(string)) return writeJSONEnv(w, http.StatusCreated, env) }
func postStop(eng *engine.Engine, version version.Version, w http.ResponseWriter, r *http.Request, vars map[string]string) error { if err := parseForm(r); err != nil { return err } glog.V(1).Infof("Stop the POD name is %s", r.Form.Get("podName")) job := eng.Job("podStop", r.Form.Get("podId"), r.Form.Get("stopVm")) stdoutBuf := bytes.NewBuffer(nil) job.Stdout.Add(stdoutBuf) if err := job.Run(); err != nil { return err } var ( env engine.Env dat map[string]interface{} returnedJSONstr string ) returnedJSONstr = engine.Tail(stdoutBuf, 1) if err := json.Unmarshal([]byte(returnedJSONstr), &dat); err != nil { return err } env.Set("ID", dat["ID"].(string)) env.SetInt("Code", (int)(dat["Code"].(float64))) env.Set("Cause", dat["Cause"].(string)) return writeJSONEnv(w, http.StatusOK, env) }
func postPodLabels(eng *engine.Engine, version version.Version, w http.ResponseWriter, r *http.Request, vars map[string]string) error { if err := parseForm(r); err != nil { return err } podID := r.Form.Get("podId") override := r.Form.Get("override") labels := r.Form.Get("labels") job := eng.Job("podLabels", podID, override, labels) stdoutBuf := bytes.NewBuffer(nil) job.Stdout.Add(stdoutBuf) if err := job.Run(); err != nil { return err } var ( env engine.Env dat map[string]interface{} returnedJSONstr string ) returnedJSONstr = engine.Tail(stdoutBuf, 1) if err := json.Unmarshal([]byte(returnedJSONstr), &dat); err != nil { return err } env.Set("ID", dat["ID"].(string)) env.SetInt("Code", (int)(dat["Code"].(float64))) env.Set("Cause", dat["Cause"].(string)) return writeJSONEnv(w, http.StatusOK, env) }
func getList(eng *engine.Engine, version version.Version, w http.ResponseWriter, r *http.Request, vars map[string]string) error { if err := parseForm(r); err != nil { return err } item := r.Form.Get("item") auxiliary := r.Form.Get("auxiliary") pod := r.Form.Get("pod") vm := r.Form.Get("vm") glog.V(1).Infof("List type is %s, specified pod: [%s], specified vm: [%s], list auxiliary pod: %s", item, pod, vm, auxiliary) job := eng.Job("list", item, pod, vm, auxiliary) stdoutBuf := bytes.NewBuffer(nil) job.Stdout.Add(stdoutBuf) if err := job.Run(); err != nil { return err } str := engine.Tail(stdoutBuf, 1) type listResponse struct { Item string `json:"item"` PodData []string `json:"podData"` VmData []string `json:"vmData"` CData []string `json:"cData"` } var res listResponse if err := json.Unmarshal([]byte(str), &res); err != nil { return err } var env engine.Env env.Set("Item", res.Item) if res.Item == "pod" { env.SetList("podData", res.PodData) } if res.Item == "vm" { env.SetList("vmData", res.VmData) } if res.Item == "container" { env.SetList("cData", res.CData) } return writeJSONEnv(w, http.StatusOK, env) }
func postServiceDelete(eng *engine.Engine, version version.Version, w http.ResponseWriter, r *http.Request, vars map[string]string) error { if err := r.ParseForm(); err != nil { return nil } job := eng.Job("serviceDelete", r.Form.Get("podId"), r.Form.Get("services")) stdoutBuf := bytes.NewBuffer(nil) job.Stdout.Add(stdoutBuf) if err := job.Run(); err != nil { return err } var env engine.Env env.Set("Result", "success") return writeJSONEnv(w, http.StatusOK, env) }
func postPodCreate(eng *engine.Engine, version version.Version, w http.ResponseWriter, r *http.Request, vars map[string]string) error { if err := parseForm(r); err != nil { return err } if r.Body != nil && (r.ContentLength > 0 || r.ContentLength == -1) { if err := checkForJson(r); err != nil { return err } } else { return fmt.Errorf("no provided podfile data") } podArgs, _ := ioutil.ReadAll(r.Body) autoRemove := r.Form.Get("remove") glog.V(1).Infof("Args string is %s, %s", string(podArgs), autoRemove) job := eng.Job("podCreate", string(podArgs), autoRemove) stdoutBuf := bytes.NewBuffer(nil) job.Stdout.Add(stdoutBuf) if err := job.Run(); err != nil { return err } var ( env engine.Env dat map[string]interface{} returnedJSONstr string ) returnedJSONstr = engine.Tail(stdoutBuf, 1) if err := json.Unmarshal([]byte(returnedJSONstr), &dat); err != nil { return err } env.Set("ID", dat["ID"].(string)) env.SetInt("Code", (int)(dat["Code"].(float64))) env.Set("Cause", dat["Cause"].(string)) return writeJSONEnv(w, http.StatusOK, env) }
func postPodStart(eng *engine.Engine, version version.Version, w http.ResponseWriter, r *http.Request, vars map[string]string) error { if err := r.ParseForm(); err != nil { return nil } ttyTag := r.Form.Get("tag") job := eng.Job("podStart", r.Form.Get("podId"), r.Form.Get("vmId"), ttyTag) if ttyTag == "" { stdoutBuf := bytes.NewBuffer(nil) job.Stdout.Add(stdoutBuf) if err := job.Run(); err != nil { return err } var ( env engine.Env dat map[string]interface{} returnedJSONstr string ) returnedJSONstr = engine.Tail(stdoutBuf, 1) if err := json.Unmarshal([]byte(returnedJSONstr), &dat); err != nil { return err } env.Set("ID", dat["ID"].(string)) env.SetInt("Code", (int)(dat["Code"].(float64))) env.Set("Cause", dat["Cause"].(string)) return writeJSONEnv(w, http.StatusOK, env) } else { var ( errOut io.Writer = os.Stderr errStream io.Writer ) // Setting up the streaming http interface. inStream, outStream, err := hijackServer(w) if err != nil { return err } defer closeStreams(inStream, outStream) fmt.Fprintf(outStream, "HTTP/1.1 101 UPGRADED\r\nContent-Type: application/vnd.docker.raw-stream\r\nConnection: Upgrade\r\nUpgrade: tcp\r\n\r\n") errStream = outStream job.Stdin.Add(inStream) job.Stdout.Add(outStream) job.Stderr.Set(errStream) // Now run the user process in container. job.SetCloseIO(false) if err := job.Run(); err != nil { fmt.Fprintf(errOut, "Error starting attach to POD %s: %s\n", r.Form.Get("podname"), err.Error()) return err } w.WriteHeader(http.StatusNoContent) return nil } }