Exemplo n.º 1
0
// Setup lo ip address
// options for operation: add or del
func SetupLoopbackAddress(vm *hypervisor.Vm, container, ip, operation string) error {
	execId := fmt.Sprintf("exec-%s", utils.RandStr(10, "alpha"))
	command := "ip addr " + operation + " dev lo " + ip + "/32"
	execcmd, err := json.Marshal(strings.Split(command, " "))
	if err != nil {
		return err
	}

	tty := &hypervisor.TtyIO{
		Callback: make(chan *types.VmResponse, 1),
	}

	result := vm.WaitProcess(false, []string{execId}, 60)
	if result == nil {
		return fmt.Errorf("can not wait %s, id: %s", command, execId)
	}

	if err := vm.Exec(container, execId, string(execcmd), false, tty); err != nil {
		return err
	}

	r, ok := <-result
	if !ok {
		return fmt.Errorf("exec failed %s: %s", command, execId)
	}
	if r.Code != 0 {
		return fmt.Errorf("exec %s on container %s failed with exit code %d", command, container, r.Code)
	}

	return nil
}