Example #1
0
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)
}
Example #2
0
func getImages(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("images", r.Form.Get("all"))
	stdoutBuf := bytes.NewBuffer(nil)

	job.Stdout.Add(stdoutBuf)

	if err := job.Run(); err != nil {
		return err
	}

	str := engine.Tail(stdoutBuf, 1)
	type response struct {
		Images []string `json:"imagesList"`
	}
	var res response
	if err := json.Unmarshal([]byte(str), &res); err != nil {
		return err
	}
	var env engine.Env
	env.SetList("imagesList", res.Images)
	return writeJSONEnv(w, http.StatusOK, env)
}
Example #3
0
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)
}
Example #4
0
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)
}
Example #5
0
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)
}
Example #6
0
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)
}
Example #7
0
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)
}
Example #8
0
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)
}
Example #9
0
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)
}
Example #10
0
func postImagesRemove(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(%s) is process to be removed", r.Form.Get("imageId"))
	var (
		force   string = "yes"
		noprune string = "yes"
	)
	if r.Form.Get("force") != "" {
		force = r.Form.Get("force")
	}
	if r.Form.Get("noprune") != "" {
		noprune = r.Form.Get("noprune")
	}
	job := eng.Job("imagesremove", r.Form.Get("imageId"), force, noprune)
	stdoutBuf := bytes.NewBuffer(nil)

	job.Stdout.Add(stdoutBuf)

	if err := job.Run(); err != nil {
		return err
	}

	str := engine.Tail(stdoutBuf, 1)
	type response struct {
		Images []string `json:"imagesList"`
	}
	var res response
	if err := json.Unmarshal([]byte(str), &res); err != nil {
		return err
	}
	var env engine.Env
	env.SetList("imagesList", res.Images)
	return writeJSONEnv(w, http.StatusOK, env)
}
Example #11
0
File: vm.go Project: ZJU-SEL/hyper
func (cli *HyperClient) RmVm(vm string) (err error) {
	var (
		body       []byte
		remoteInfo *engine.Env
	)

	v := url.Values{}
	v.Set("vm", vm)
	body, _, err = readBody(cli.call("DELETE", "/vm?"+v.Encode(), nil, nil))
	if err != nil {
		return fmt.Errorf("Error to remove vm(%s), %s", vm, err.Error())
	}

	out := engine.NewOutput()
	remoteInfo, err = out.AddEnv()
	if err != nil {
		return err
	}

	if _, err = out.Write(body); err != nil {
		err = fmt.Errorf("Error reading remote info: %v", err)
		return err
	}
	out.Close()
	errCode := remoteInfo.GetInt("Code")
	if errCode != types.E_OK {
		if errCode != types.E_BAD_REQUEST &&
			errCode != types.E_FAILED {
			err = fmt.Errorf("Error code is %d", errCode)
		} else {
			err = fmt.Errorf("Cause is %s", remoteInfo.Get("Cause"))
		}
	}

	return err
}
Example #12
0
File: vm.go Project: m1911/hyper
func (cli *HyperClient) CreateVm(cpu, mem int, async bool) (id string, err error) {
	var (
		body       []byte
		remoteInfo *engine.Env
	)

	v := url.Values{}
	if cpu > 0 {
		v.Set("cpu", strconv.Itoa(cpu))
	}
	if mem > 0 {
		v.Set("mem", strconv.Itoa(mem))
	}
	if async {
		v.Set("async", "yes")
	}

	body, _, err = readBody(cli.call("POST", "/vm/create?"+v.Encode(), nil, nil))
	if err != nil {
		return id, err
	}

	out := engine.NewOutput()
	remoteInfo, err = out.AddEnv()
	if err != nil {
		return id, err
	}

	if _, err = out.Write(body); err != nil {
		err = fmt.Errorf("Error reading remote info: %v", err)
		return id, err
	}
	out.Close()
	errCode := remoteInfo.GetInt("Code")
	if errCode != types.E_OK {
		if errCode != types.E_BAD_REQUEST &&
			errCode != types.E_FAILED {
			err = fmt.Errorf("Error code is %d", errCode)
		} else {
			err = fmt.Errorf("Cause is %s", remoteInfo.Get("Cause"))
		}
	} else {
		id = remoteInfo.Get("ID")
	}

	return id, err
}
Example #13
0
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)
}
Example #14
0
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
	}
}
Example #15
0
// writeJSONEnv writes the engine.Env values to the http response stream as a
// json encoded body.
func writeJSONEnv(w http.ResponseWriter, code int, v engine.Env) error {
	w.Header().Set("Content-Type", "application/json")
	w.WriteHeader(code)
	return v.Encode(w)
}