Exemple #1
0
func (cli *Client) startPodWithoutTty(v *url.Values) (string, error) {

	body, _, err := readBody(cli.call("POST", "/pod/start?"+v.Encode(), nil, nil))
	if err != nil {
		return "", err
	}
	out := engine.NewOutput()
	remoteInfo, err := out.AddEnv()
	if err != nil {
		return "", err
	}

	if _, err := out.Write(body); err != nil {
		return "", fmt.Errorf("Error reading remote info: %s", err)
	}
	out.Close()
	errCode := remoteInfo.GetInt("Code")
	if errCode != types.E_OK {
		if errCode != types.E_BAD_REQUEST &&
			errCode != types.E_FAILED {
			return "", fmt.Errorf("Error code is %d", errCode)
		} else {
			return "", fmt.Errorf("Cause is %s", remoteInfo.Get("Cause"))
		}
	}
	return remoteInfo.Get("ID"), nil
}
Exemple #2
0
func (cli *Client) StopPod(podId, stopVm string) (int, string, error) {
	v := url.Values{}
	v.Set("podId", podId)
	v.Set("stopVm", stopVm)
	body, _, err := readBody(cli.call("POST", "/pod/stop?"+v.Encode(), nil, nil))
	if err != nil {
		if strings.Contains(err.Error(), "leveldb: not found") {
			return -1, "", fmt.Errorf("Can not find that POD ID to stop, please check your POD ID!")
		}
		return -1, "", err
	}
	out := engine.NewOutput()
	remoteInfo, err := out.AddEnv()
	if err != nil {
		return -1, "", err
	}

	if _, err := out.Write(body); err != nil {
		return -1, "", err
	}
	out.Close()
	// This 'ID' stands for pod ID
	// This 'Code' should be E_SHUTDOWN
	// THis 'Cause' ..
	if remoteInfo.Exists("ID") {
		// TODO ...
	}
	return remoteInfo.GetInt("Code"), remoteInfo.Get("Cause"), nil
}
Exemple #3
0
func (cli *Client) GetImages(all, quiet bool) (*engine.Env, error) {

	v := url.Values{}
	v.Set("all", "no")
	v.Set("quiet", "no")
	if all == true {
		v.Set("all", "yes")
	}
	if quiet == true {
		v.Set("quiet", "yes")
	}
	body, _, err := readBody(cli.call("GET", "/images/get?"+v.Encode(), nil, nil))
	if err != nil {
		return nil, err
	}
	out := engine.NewOutput()
	remoteInfo, err := out.AddEnv()
	if err != nil {
		return nil, err
	}

	if _, err := out.Write(body); err != nil {
		fmt.Printf("Error reading remote info: %s", err)
		return nil, err
	}
	out.Close()

	return remoteInfo, nil
}
Exemple #4
0
func (cli *Client) List(item, pod, vm string, aux bool) (*engine.Env, error) {
	v := url.Values{}
	v.Set("item", item)
	if aux {
		v.Set("auxiliary", "yes")
	}
	if pod != "" {
		v.Set("pod", pod)
	}
	if vm != "" {
		v.Set("vm", vm)
	}
	body, _, err := readBody(cli.call("GET", "/list?"+v.Encode(), nil, nil))
	if err != nil {
		return nil, err
	}
	out := engine.NewOutput()
	remoteInfo, err := out.AddEnv()
	if err != nil {
		return nil, err
	}

	if _, err := out.Write(body); err != nil {
		fmt.Printf("Error reading remote info: %s", err)
		return nil, err
	}
	out.Close()

	return remoteInfo, nil
}
Exemple #5
0
func (cli *Client) Commit(container, repo, author, message string, changes []string, pause bool) (string, error) {
	v := url.Values{}
	v.Set("author", author)
	changeJson, err := json.Marshal(changes)
	if err != nil {
		return "", err
	}
	v.Set("change", string(changeJson))
	v.Set("message", message)
	if pause == true {
		v.Set("pause", "yes")
	} else {
		v.Set("pause", "no")
	}
	v.Set("container", container)
	v.Set("repo", repo)
	body, _, err := readBody(cli.call("POST", "/container/commit?"+v.Encode(), nil, nil))
	if err != nil {
		return "", err
	}
	out := engine.NewOutput()
	remoteInfo, err := out.AddEnv()
	if err != nil {
		return "", err
	}

	if _, err := out.Write(body); err != nil {
		return "", err
	}
	out.Close()

	return remoteInfo.Get("ID"), nil
}
Exemple #6
0
func (cli *Client) GetContainerByPod(podId string) (string, error) {
	v := url.Values{}
	v.Set("item", "container")
	v.Set("pod", podId)
	body, _, err := readBody(cli.call("GET", "/list?"+v.Encode(), nil, nil))
	if err != nil {
		return "", err
	}
	out := engine.NewOutput()
	remoteInfo, err := out.AddEnv()
	if err != nil {
		return "", err
	}

	if _, err := out.Write(body); err != nil {
		fmt.Printf("Error reading remote info: %s", err)
		return "", err
	}
	out.Close()
	var containerResponse = []string{}
	containerResponse = remoteInfo.GetList("cData")
	for _, c := range containerResponse {
		fields := strings.Split(c, ":")
		if len(fields) < 4 {
			fmt.Printf("can not parse container string: %s", c)
			continue
		}
		containerId := fields[0]
		if podId == fields[2] {
			return containerId, nil
		}
	}

	return "", fmt.Errorf("Container not found")
}
Exemple #7
0
func (cli *Client) 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
}
Exemple #8
0
func (cli *Client) Info() (*engine.Env, error) {

	body, _, err := readBody(cli.call("GET", "/info", nil, nil))
	if err != nil {
		return nil, err
	}

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

	if _, err := out.Write(body); err != nil {
		return nil, err
	}
	out.Close()

	return remoteInfo, nil
}
Exemple #9
0
func (cli *Client) PausePod(podId string) error {
	v := url.Values{}
	v.Set("podId", podId)

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

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

	if _, err := out.Write(body); err != nil {
		return err
	}
	out.Close()

	return nil
}
Exemple #10
0
func (cli *Client) RemoveImage(image string, noprune, force bool) (*engine.Env, error) {
	v := url.Values{}
	v.Set("imageId", image)
	v.Set("noprune", strconv.FormatBool(noprune))
	v.Set("force", strconv.FormatBool(force))
	body, _, err := readBody(cli.call("DELETE", "/image?"+v.Encode(), nil, nil))
	if err != nil {
		return nil, fmt.Errorf("Error remove the image(%s): %s", image, err.Error())
	}
	out := engine.NewOutput()
	remoteInfo, err := out.AddEnv()
	if err != nil {
		return nil, fmt.Errorf("Error remove the image(%s): %s", image, err.Error())
	}

	if _, err := out.Write(body); err != nil {
		return nil, fmt.Errorf("Error remove the image(%s): %s", image, err.Error())
	}
	out.Close()

	return remoteInfo, nil
}
Exemple #11
0
func (cli *Client) RmPod(id string) error {
	v := url.Values{}
	v.Set("podId", id)
	body, _, err := readBody(cli.call("DELETE", "/pod?"+v.Encode(), nil, nil))
	if err != nil {
		return fmt.Errorf("Error to remove pod(%s), %s", id, err.Error())
	}
	out := engine.NewOutput()
	remoteInfo, err := out.AddEnv()
	if err != nil {
		return fmt.Errorf("Error to remove pod(%s), %s", id, err.Error())
	}

	if _, err := out.Write(body); err != nil {
		return fmt.Errorf("Error to remove pod(%s), %s", id, err.Error())
	}
	out.Close()
	errCode := remoteInfo.GetInt("Code")
	if !(errCode == types.E_OK || errCode == types.E_VM_SHUTDOWN) {
		return fmt.Errorf("Error to remove pod(%s), %s", id, remoteInfo.Get("Cause"))
	}
	return nil
}
Exemple #12
0
func (cli *Client) CreateContainer(podID string, spec interface{}) (string, int, error) {
	v := url.Values{}
	v.Set("podID", podID)
	body, statusCode, err := readBody(cli.call("POST", "/container/create?"+v.Encode(), spec, nil))
	if err != nil {
		return "", statusCode, err
	}
	if statusCode != http.StatusCreated && statusCode != http.StatusOK {
		return "", statusCode, err
	}

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

	if _, err := out.Write(body); err != nil {
		return "", statusCode, fmt.Errorf("Error reading remote info: %s", err)
	}
	out.Close()

	return remoteInfo.Get("ID"), statusCode, nil
}
Exemple #13
0
func (cli *Client) CreatePod(spec interface{}) (string, int, error) {
	body, statusCode, err := readBody(cli.call("POST", "/pod/create", spec, nil))
	if err != nil {
		return "", statusCode, err
	}
	if statusCode != http.StatusCreated && statusCode != http.StatusOK {
		return "", statusCode, err
	}

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

	if _, err := out.Write(body); err != nil {
		return "", statusCode, fmt.Errorf("Error reading remote info: %s", err)
	}
	out.Close()
	errCode := remoteInfo.GetInt("Code")
	if errCode == types.E_OK {
		//fmt.Println("VM is successful to start!")
	} else {
		// case types.E_CONTEXT_INIT_FAIL:
		// case types.E_DEVICE_FAIL:
		// case types.E_QMP_INIT_FAIL:
		// case types.E_QMP_COMMAND_FAIL:
		if errCode != types.E_BAD_REQUEST &&
			errCode != types.E_FAILED {
			return "", statusCode, fmt.Errorf("Error code is %d", errCode)
		} else {
			return "", statusCode, fmt.Errorf("Cause is %s", remoteInfo.Get("Cause"))
		}
	}
	return remoteInfo.Get("ID"), statusCode, nil
}
Exemple #14
0
func (cli *Client) 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
}