Пример #1
1
func (cli *HyperClient) GetPodInfo(podName string) (string, error) {
	// get the pod or container info before we start the exec
	v := url.Values{}
	v.Set("podName", podName)
	body, _, err := readBody(cli.call("GET", "/pod/info?"+v.Encode(), nil, nil))
	if err != nil {
		fmt.Printf("Error: %s\n", err)
		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()
	if remoteInfo.Exists("hostname") {
		hostname := remoteInfo.Get("hostname")
		if hostname == "" {
			return "", nil
		} else {
			return hostname, nil
		}
	}

	return "", nil
}
Пример #2
0
Файл: pod.go Проект: m1911/hyper
func (cli *HyperClient) 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
}
Пример #3
0
func (cli *HyperClient) HyperCmdUnpause(args ...string) error {
	var parser = gflag.NewParser(nil, gflag.Default|gflag.IgnoreUnknown)
	parser.Usage = "unpause Pod\n\nUnpause the pod"
	args, err := parser.ParseArgs(args)
	if err != nil {
		if !strings.Contains(err.Error(), "Usage") {
			return err
		} else {
			return nil
		}
	}
	if len(args) == 0 {
		return fmt.Errorf("Can not accept the 'unpause' command without Pod ID!")
	}

	v := url.Values{}
	v.Set("podId", args[0])

	body, _, err := readBody(cli.call("POST", "/pod/unpause?"+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
}
Пример #4
0
func (daemon *Daemon) CmdCreate(job *engine.Job) error {
	imgName := job.Args[0]
	cli := daemon.DockerCli
	body, _, err := cli.SendCmdCreate("", imgName, []string{}, 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 while reading remote info!\n")
	}
	out.Close()

	v := &engine.Env{}
	v.SetJson("ID", daemon.ID)
	containerId := remoteInfo.Get("Id")
	if containerId != "" {
		v.Set("ContainerID", containerId)
		glog.V(3).Infof("The ContainerID is %s\n", containerId)
	} else {
		return fmt.Errorf("Hyper ERROR: AN error encountered during creating container!\n")
	}

	if _, err := v.WriteTo(job.Stdout); err != nil {
		return err
	}

	return nil
}
Пример #5
0
func (cli *HyperClient) CreatePod(jsonbody string) (string, error) {
	v := url.Values{}
	v.Set("podArgs", jsonbody)
	body, _, err := readBody(cli.call("POST", "/pod/create?"+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 {
		//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 "", fmt.Errorf("Error code is %d", errCode)
		} else {
			return "", fmt.Errorf("Cause is %s", remoteInfo.Get("Cause"))
		}
	}
	return remoteInfo.Get("ID"), nil
}
Пример #6
0
func (cli *HyperClient) 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
}
Пример #7
0
func (cli *HyperClient) 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, ":")
		containerId := fields[0]
		if podId == fields[2] {
			return containerId, nil
		}
	}

	return "", fmt.Errorf("Container not found")
}
Пример #8
0
// we need this *info* function to get the whole status from the hyper daemon
func (cli *HyperClient) HyperCmdInfo(args ...string) error {
	var parser = gflag.NewParser(nil, gflag.Default)
	parser.Usage = "info\n\nDisplay system-wide information"
	args, err := parser.Parse()
	if err != nil {
		if !strings.Contains(err.Error(), "Usage") {
			return err
		} else {
			return nil
		}
	}
	body, _, err := readBody(cli.call("GET", "/info", 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()
	fmt.Fprintf(cli.out, "Images: %d\n", remoteInfo.GetInt("Images"))
	if remoteInfo.Exists("Containers") {
		fmt.Fprintf(cli.out, "Containers: %d\n", remoteInfo.GetInt("Containers"))
	}
	fmt.Fprintf(cli.out, "PODs: %d\n", remoteInfo.GetInt("Pods"))
	fmt.Fprintf(cli.out, "Storage Driver: %s\n", remoteInfo.Get("Driver"))
	var status [][]string
	err = remoteInfo.GetJson("DriverStatus", &status)
	if err == nil {
		for _, pair := range status {
			fmt.Fprintf(cli.out, "  %s: %s\n", pair[0], pair[1])
		}
	}

	fmt.Fprintf(cli.out, "Hyper Root Dir: %s\n", remoteInfo.Get("DockerRootDir"))
	fmt.Fprintf(cli.out, "Index Server Address: %s\n", remoteInfo.Get("IndexServerAddress"))
	fmt.Fprintf(cli.out, "Execution Driver: %s\n", remoteInfo.Get("ExecutionDriver"))

	memTotal := getMemSizeString(remoteInfo.GetInt("MemTotal"))
	fmt.Fprintf(cli.out, "Total Memory: %s\n", memTotal)
	fmt.Fprintf(cli.out, "Operating System: %s\n", remoteInfo.Get("Operating System"))

	return nil
}
Пример #9
0
Файл: pod.go Проект: m1911/hyper
func (cli *HyperClient) CreatePod(jsonbody string, remove bool) (string, error) {
	v := url.Values{}
	if remove {
		v.Set("remove", "yes")
	}
	var tmpPod pod.UserPod
	if err := json.Unmarshal([]byte(jsonbody), &tmpPod); err != nil {
		return "", err
	}
	body, statusCode, err := readBody(cli.call("POST", "/pod/create?"+v.Encode(), tmpPod, nil))
	if statusCode == 404 {
		if err := cli.PullImages(jsonbody); err != nil {
			return "", fmt.Errorf("failed to pull images: %s", err.Error())
		}
		if body, _, err = readBody(cli.call("POST", "/pod/create?"+v.Encode(), tmpPod, nil)); err != nil {
			return "", err
		}
	} else 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 {
		//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 "", fmt.Errorf("Error code is %d", errCode)
		} else {
			return "", fmt.Errorf("Cause is %s", remoteInfo.Get("Cause"))
		}
	}
	return remoteInfo.Get("ID"), nil
}
Пример #10
0
Файл: vm.go Проект: 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
}
Пример #11
0
func (cli *HyperClient) RunPod(podstring string, autoremove bool) (string, error) {
	v := url.Values{}
	v.Set("podArgs", podstring)
	if autoremove == true {
		v.Set("remove", "yes")
	} else {
		v.Set("remove", "no")
	}
	body, statusCode, err := readBody(cli.call("POST", "/pod/run?"+v.Encode(), nil, nil))
	if statusCode == 404 {
		if err := cli.PullImages(podstring); err != nil {
			return "", fmt.Errorf("failed to pull images: %s", err.Error())
		}
		if body, _, err = readBody(cli.call("POST", "/pod/run?"+v.Encode(), nil, nil)); err != nil {
			return "", err
		}
	} else 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 {
		//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 "", fmt.Errorf("Error code is %d", errCode)
		} else {
			return "", fmt.Errorf("Cause is %s", remoteInfo.Get("Cause"))
		}
	}
	return remoteInfo.Get("ID"), nil
}
Пример #12
0
func (cli *HyperClient) HyperCmdRm(args ...string) error {
	var parser = gflag.NewParser(nil, gflag.Default)
	parser.Usage = "rm POD [POD...]\n\nRemove one or more pods"
	args, err := parser.Parse()
	if err != nil {
		if !strings.Contains(err.Error(), "Usage") {
			return err
		} else {
			return nil
		}
	}
	if len(args) < 2 {
		return fmt.Errorf("\"rm\" requires a minimum of 1 argument, please provide POD ID.\n")
	}
	pods := args[1:]
	for _, id := range pods {
		v := url.Values{}
		v.Set("podId", id)
		body, _, err := readBody(cli.call("POST", "/pod/remove?"+v.Encode(), nil, nil))
		if err != nil {
			fmt.Fprintf(cli.out, "Error to remove pod(%s), %s", id, err.Error())
			continue
		}
		out := engine.NewOutput()
		remoteInfo, err := out.AddEnv()
		if err != nil {
			fmt.Fprintf(cli.out, "Error to remove pod(%s), %s", id, err.Error())
			continue
		}

		if _, err := out.Write(body); err != nil {
			fmt.Fprintf(cli.out, "Error to remove pod(%s), %s", id, err.Error())
			continue
		}
		out.Close()
		errCode := remoteInfo.GetInt("Code")
		if errCode == types.E_OK || errCode == types.E_VM_SHUTDOWN {
			//fmt.Println("VM is successful to start!")
			fmt.Fprintf(cli.out, "Pod(%s) is successful to be deleted!\n", id)
		} else {
			fmt.Fprintf(cli.out, "Error to remove pod(%s), %s", id, remoteInfo.Get("Cause"))
		}
	}
	return nil
}
Пример #13
0
func (cli *HyperClient) HyperCmdVm(args ...string) error {
	var parser = gflag.NewParser(nil, gflag.Default)
	parser.Usage = "vm\n\nRun a VM, without any Pod running on it"
	args, err := parser.Parse()
	if err != nil {
		if !strings.Contains(err.Error(), "Usage") {
			return err
		} else {
			return nil
		}
	}

	// Only run a new VM
	body, _, err := readBody(cli.call("POST", "/vm/create", 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 {
		//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 fmt.Errorf("Error code is %d", errCode)
		} else {
			return fmt.Errorf("Cause is %s", remoteInfo.Get("Cause"))
		}
	}
	fmt.Printf("New VM id is %s\n", remoteInfo.Get("ID"))
	return nil
}
Пример #14
0
Файл: pod.go Проект: m1911/hyper
func (cli *HyperClient) RunPod(podstring string, autoremove bool) (string, error) {
	v := url.Values{}
	if autoremove == true {
		v.Set("remove", "yes")
	} else {
		v.Set("remove", "no")
	}
	var tmpPod pod.UserPod
	if err := json.Unmarshal([]byte(podstring), &tmpPod); err != nil {
		return "", err
	}
	body, statusCode, err := readBody(cli.call("POST", "/pod/run?"+v.Encode(), tmpPod, nil))
	if statusCode == 404 {
		if err := cli.PullImages(podstring); err != nil {
			return "", fmt.Errorf("failed to pull images: %s", err.Error())
		}
		if body, _, err = readBody(cli.call("POST", "/pod/run?"+v.Encode(), tmpPod, nil)); err != nil {
			return "", err
		}
	} else 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
}
Пример #15
0
func (cli *HyperClient) HyperCmdKill(args ...string) error {
	var parser = gflag.NewParser(nil, gflag.Default)
	parser.Usage = "kill VM_ID\n\nTerminate a VM instance"
	args, err := parser.ParseArgs(args)
	if err != nil {
		if !strings.Contains(err.Error(), "Usage") {
			return err
		} else {
			return nil
		}
	}
	if len(args) == 0 {
		return fmt.Errorf("\"kill\" requires a minimum of 1 argument, please provide VM ID.\n")
	}

	vmId := args[0]
	v := url.Values{}
	v.Set("vm", vmId)
	body, _, err := readBody(cli.call("DELETE", "/vm?"+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()
	if remoteInfo.GetInt("Code") != types.E_VM_SHUTDOWN {
		fmt.Fprintf(cli.err, "VM(%s) is failed to shutdown, %s\n", remoteInfo.Get("ID"), remoteInfo.Get("Cause"))
	}

	return nil
}
Пример #16
0
func (cli *HyperClient) 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
}
Пример #17
0
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
}
Пример #18
0
func (cli *HyperClient) HyperCmdList(args ...string) error {
	var parser = gflag.NewParser(nil, gflag.Default)
	parser.Usage = "list [pod|container]\n\nlist all pods or container information"
	args, err := parser.Parse()
	if err != nil {
		if !strings.Contains(err.Error(), "Usage") {
			return err
		} else {
			return nil
		}
	}
	var item string
	if len(args) == 1 {
		item = "pod"
	} else {
		item = args[1]
	}

	if item != "pod" && item != "vm" && item != "container" {
		return fmt.Errorf("Error, the %s can not support %s list!", os.Args[0], item)
	}

	v := url.Values{}
	v.Set("item", item)
	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 (
		vmResponse        = []string{}
		podResponse       = []string{}
		containerResponse = []string{}
	)
	if remoteInfo.Exists("item") {
		item = remoteInfo.Get("item")
	}
	if remoteInfo.Exists("Error") {
		return fmt.Errorf("Found an error while getting %s list: %s", item, remoteInfo.Get("Error"))
	}

	if item == "vm" {
		vmResponse = remoteInfo.GetList("vmData")
	}
	if item == "pod" {
		podResponse = remoteInfo.GetList("podData")
	}
	if item == "container" {
		containerResponse = remoteInfo.GetList("cData")
	}

	//fmt.Printf("Item is %s\n", item)
	if item == "vm" {
		fmt.Printf("%15s%20s\n", "VM name", "Status")
		for _, vm := range vmResponse {
			fields := strings.Split(vm, ":")
			fmt.Printf("%15s%20s\n", fields[0], fields[2])
		}
	}

	if item == "pod" {
		fmt.Printf("%15s%30s%20s%10s\n", "POD ID", "POD Name", "VM name", "Status")
		for _, p := range podResponse {
			fields := strings.Split(p, ":")
			var podName = fields[1]
			if len(fields[1]) > 27 {
				podName = fields[1][:27]
			}
			fmt.Printf("%15s%30s%20s%10s\n", fields[0], podName, fields[2], fields[3])
		}
	}

	if item == "container" {
		fmt.Printf("%-66s%-20s%15s%10s\n", "Container ID", "Name", "POD ID", "Status")
		for _, c := range containerResponse {
			fields := strings.Split(c, ":")
			name := fields[1]
			if len(name) > 0 {
				if name[0] == '/' {
					name = name[1:]
				}
			}
			fmt.Printf("%-66s%-20s%15s%10s\n", fields[0], name, fields[2], fields[3])
		}
	}
	return nil
}
Пример #19
0
/*
	-a, --author=       Author (e.g., "Hello World <*****@*****.**>")
	-c, --change=[]     Apply Dockerfile instruction to the created image
	--help=false        Print usage
	-m, --message=      Commit message
	-p, --pause=true    Pause container during Commit
*/
func (cli *HyperClient) HyperCmdCommit(args ...string) error {
	var opts struct {
		Author  string   `short:"a" long:"author" default:"" value-name:"\"\"" description:"Author (e.g., \"Hello World <*****@*****.**>\")"`
		Change  []string `short:"c" long:"change" default:"" value-name:"[]" description:"Apply Dockerfile instruction to the created image"`
		Message string   `short:"m" long:"message" default:"" value-name:"\"\"" description:"Commit message"`
		Pause   bool     `short:"p" long:"pause" default:"false" value-name:"true" description:"Pause container during Commit"`
	}
	var parser = gflag.NewParser(&opts, gflag.Default)

	parser.Usage = "commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]\n\nCreate a new image from a container's changes"
	args, err := parser.Parse()
	if err != nil {
		if !strings.Contains(err.Error(), "Usage") {
			return err
		} else {
			return nil
		}
	}
	if len(args) == 0 {
		return fmt.Errorf("%s: \"commit\" requires a minimum of 1 argument, See 'hyper build --help'.", os.Args[0])
	}
	var (
		containerId string = ""
		repo        string = ""
	)
	if len(args) > 1 {
		containerId = args[1]
	}
	if len(args) > 2 {
		repo = args[2]
	}
	v := url.Values{}
	v.Set("author", opts.Author)
	changeJson, err := json.Marshal(opts.Change)
	if err != nil {
		return err
	}
	v.Set("change", string(changeJson))
	v.Set("message", opts.Message)
	if opts.Pause == true {
		v.Set("pause", "yes")
	} else {
		v.Set("pause", "no")
	}
	v.Set("container", containerId)
	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()
	fmt.Fprintf(cli.out, "%s\n", remoteInfo.Get("ID"))
	return nil
}
Пример #20
0
Файл: pod.go Проект: m1911/hyper
func (cli *HyperClient) HyperCmdStart(args ...string) error {
	var opts struct {
		// OnlyVm    bool     `long:"onlyvm" default:"false" value-name:"false" description:"Only start a new VM"`
		Cpu int `short:"c" long:"cpu" default:"1" value-name:"1" description:"CPU number for the VM"`
		Mem int `short:"m" long:"memory" default:"128" value-name:"128" description:"Memory size (MB) for the VM"`
	}
	var parser = gflag.NewParser(&opts, gflag.Default)
	parser.Usage = "start [-c 1 -m 128]| POD_ID \n\nLaunch a 'pending' pod"
	args, err := parser.Parse()
	if err != nil {
		if !strings.Contains(err.Error(), "Usage") {
			return err
		} else {
			return nil
		}
	}
	if false {
		// Only run a new VM
		v := url.Values{}
		v.Set("cpu", fmt.Sprintf("%d", opts.Cpu))
		v.Set("mem", fmt.Sprintf("%d", opts.Mem))
		body, _, err := readBody(cli.call("POST", "/vm/create?"+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 {
			//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 fmt.Errorf("Error code is %d", errCode)
			} else {
				return fmt.Errorf("Cause is %s", remoteInfo.Get("Cause"))
			}
		}
		fmt.Printf("New VM id is %s\n", remoteInfo.Get("ID"))
		return nil
	}
	if len(args) < 2 {
		return fmt.Errorf("\"start\" requires a minimum of 1 argument, please provide POD ID.\n")
	}
	var (
		podId string
		vmId  string
	)
	if len(args) == 2 {
		podId = args[1]
	}
	if len(args) == 3 {
		podId = args[1]
		vmId = args[2]
	}
	// fmt.Printf("Pod ID is %s, VM ID is %s\n", podId, vmId)
	_, err = cli.StartPod(podId, vmId, false)
	if err != nil {
		return err
	}
	fmt.Fprintf(cli.out, "Successfully started the Pod(%s)\n", podId)
	return nil
}
Пример #21
0
func (cli *HyperClient) HyperCmdRmi(args ...string) error {
	var opts struct {
		Noprune bool `long:"no-prune" default:"false" default-mask:"-" description:"Do not delete untagged parents"`
		Force   bool `short:"f" long:"force" default:"true" default-mask:"-" description:"Force removal of the image"`
	}
	var parser = gflag.NewParser(&opts, gflag.Default)
	parser.Usage = "rmi [OPTIONS] IMAGE [IMAGE...]\n\nRemove one or more images"
	args, err := parser.ParseArgs(args)
	if err != nil {
		if !strings.Contains(err.Error(), "Usage") {
			return err
		} else {
			return nil
		}
	}
	var (
		noprune string = "no"
		force   string = "yes"
	)
	if len(args) == 0 {
		return fmt.Errorf("\"rmi\" requires a minimum of 1 argument, please provide IMAGE ID.\n")
	}
	if opts.Noprune == true {
		noprune = "yes"
	}
	if opts.Force == false {
		force = "no"
	}
	images := args
	for _, imageId := range images {
		v := url.Values{}
		v.Set("imageId", imageId)
		v.Set("noprune", noprune)
		v.Set("force", force)
		body, _, err := readBody(cli.call("DELETE", "/image?"+v.Encode(), nil, nil))
		if err != nil {
			fmt.Fprintf(cli.err, "Error remove the image(%s): %s\n", imageId, err.Error())
			continue
		}
		out := engine.NewOutput()
		remoteInfo, err := out.AddEnv()
		if err != nil {
			fmt.Fprintf(cli.err, "Error remove the image(%s): %s\n", imageId, err.Error())
			continue
		}

		if _, err := out.Write(body); err != nil {
			fmt.Fprintf(cli.err, "Error remove the image(%s): %s\n", imageId, err.Error())
			continue
		}
		out.Close()
		errCode := remoteInfo.GetInt("Code")
		if errCode == types.E_OK || errCode == types.E_VM_SHUTDOWN {
			//fmt.Println("VM is successful to start!")
			fmt.Fprintf(cli.out, "Image(%s) is successful to be deleted!\n", imageId)
		} else {
			fmt.Fprintf(cli.err, "Error remove the image(%s): %s\n", imageId, remoteInfo.Get("Cause"))
		}
	}
	return nil
}
Пример #22
0
func (cli *HyperClient) HyperCmdImages(args ...string) error {
	var opts struct {
		All bool `short:"a" long:"all" default:"false" description:"Show all images (by default filter out the intermediate image layers)"`
		Num bool `short:"q" long:"quiet" default:"false" description:"Only show numeric IDs"`
	}
	var parser = gflag.NewParser(&opts, gflag.Default)

	parser.Usage = "images [OPTIONS] [REPOSITORY]\n\nList images"
	args, err := parser.ParseArgs(args)
	if err != nil {
		if !strings.Contains(err.Error(), "Usage") {
			return err
		} else {
			return nil
		}
	}
	v := url.Values{}
	v.Set("all", "no")
	v.Set("quiet", "no")
	if opts.All == true {
		v.Set("all", "yes")
	}
	if opts.Num == true {
		v.Set("quiet", "yes")
	}
	body, _, err := readBody(cli.call("GET", "/images/get?"+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 (
		imagesList = []string{}
	)
	imagesList = remoteInfo.GetList("imagesList")

	w := tabwriter.NewWriter(cli.out, 20, 1, 3, ' ', 0)
	if opts.Num == false {
		fmt.Fprintln(w, "REPOSITORY\tTAG\tIMAGE ID\tCREATED\tVIRTUAL SIZE")
		for _, item := range imagesList {
			fields := strings.Split(item, ":")
			date, _ := strconv.ParseUint(fields[3], 0, 64)
			fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\n", fields[0], fields[1], fields[2][:12], time.Unix(int64(date), 0).Format("2006-01-02 15:04:05"), getImageSizeString(fields[4]))
		}
	} else {
		for _, item := range imagesList {
			fields := strings.Split(item, ":")
			fmt.Fprintf(w, "%s\n", fields[2][:12])
		}
	}
	w.Flush()

	return nil
}
Пример #23
0
func (cli *HyperClient) HyperCmdList(args ...string) error {
	var opts struct {
		Aux bool   `short:"x" long:"aux" default:"false" value-name:"false" description:"show the auxiliary containers"`
		Pod string `short:"p" long:"pod" value-name:"\"\"" description:"only list the specified pod"`
	}

	var parser = gflag.NewParser(&opts, gflag.Default|gflag.IgnoreUnknown)
	parser.Usage = "list [OPTIONS] [pod|container]\n\nlist all pods or container information"
	args, err := parser.Parse()
	if err != nil {
		if !strings.Contains(err.Error(), "Usage") {
			return err
		} else {
			return nil
		}
	}
	var item string
	if len(args) == 1 {
		item = "pod"
	} else {
		item = args[1]
	}

	if item != "pod" && item != "vm" && item != "container" {
		return fmt.Errorf("Error, the %s can not support %s list!", os.Args[0], item)
	}

	v := url.Values{}
	v.Set("item", item)
	if opts.Aux {
		v.Set("auxiliary", "yes")
	}
	if opts.Pod != "" {
		v.Set("pod", opts.Pod)
	}
	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 (
		vmResponse        = []string{}
		podResponse       = []string{}
		containerResponse = []string{}
	)
	if remoteInfo.Exists("item") {
		item = remoteInfo.Get("item")
	}
	if remoteInfo.Exists("Error") {
		return fmt.Errorf("Found an error while getting %s list: %s", item, remoteInfo.Get("Error"))
	}

	w := tabwriter.NewWriter(cli.out, 20, 1, 3, ' ', 0)
	if item == "vm" {
		vmResponse = remoteInfo.GetList("vmData")
	}
	if item == "pod" {
		podResponse = remoteInfo.GetList("podData")
	}
	if item == "container" {
		containerResponse = remoteInfo.GetList("cData")
	}

	//fmt.Printf("Item is %s\n", item)
	if item == "vm" {
		fmt.Fprintln(w, "VM name\tStatus")
		for _, vm := range vmResponse {
			fields := strings.Split(vm, ":")
			fmt.Fprintf(w, "%s\t%s\n", fields[0], fields[2])
		}
	}

	if item == "pod" {
		fmt.Fprintln(w, "POD ID\tPOD Name\tVM name\tStatus")
		for _, p := range podResponse {
			fields := strings.Split(p, ":")
			fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", fields[0], fields[1], fields[2], fields[3])
		}
	}

	if item == "container" {
		fmt.Fprintln(w, "Container ID\tName\tPOD ID\tStatus")
		for _, c := range containerResponse {
			fields := strings.Split(c, ":")
			name := fields[1]
			if len(name) > 0 {
				if name[0] == '/' {
					name = name[1:]
				}
			}
			fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", fields[0], name, fields[2], fields[3])
		}
	}
	w.Flush()
	return nil
}