示例#1
0
func ApplyServices(vm *hypervisor.Vm, container string, services []pod.UserService) error {
	// Update lo ip addresses
	var command []string
	oldServices, err := GetServices(vm, container)
	if err != nil {
		return err
	}
	err = UpdateLoopbackAddress(vm, container, oldServices, services)
	if err != nil {
		return err
	}

	// Update haproxy config
	config := path.Join(ServiceVolume, ServiceConfig)
	vm.WriteFile(container, config, GenerateServiceConfig(services))

	command = append(command, "sh")
	command = append(command, "-c")
	command = append(command, "haproxy -f /usr/local/etc/haproxy/haproxy.cfg -p /var/run/haproxy.pid -sf `cat /var/run/haproxy.pid`")
	execcmd, err := json.Marshal(command)
	if err != nil {
		return err
	}

	return vm.Exec(nil, nil, string(execcmd), "", container)
}
示例#2
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),
	}

	vm.Pod.AddExec(container, execId, command, false)
	defer vm.Pod.DeleteExec(execId)

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

	es := vm.Pod.GetExec(execId)
	if es == nil {
		return fmt.Errorf("cannot find exec status for %s: %s", command, execId)
	}
	if es.ExitCode != 0 {
		return fmt.Errorf("exec %s on container %s failed with exit code %d", command, container, es.ExitCode)
	}

	return nil
}
示例#3
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
}
示例#4
0
// Setup lo ip address
// options for operation: add or del
func SetupLoopbackAddress(vm *hypervisor.Vm, container, ip, operation string) error {
	command := "ip addr " + operation + " dev lo " + ip + "/32"
	execcmd, err := json.Marshal(strings.Split(command, " "))
	if err != nil {
		return err
	}

	return vm.Exec(nil, nil, string(execcmd), "", container)
}
示例#5
0
func ApplyServices(vm *hypervisor.Vm, container string, services []pod.UserService) error {
	// Update lo ip addresses
	var command []string
	oldServices, err := GetServices(vm, container)
	if err != nil {
		return err
	}
	err = UpdateLoopbackAddress(vm, container, oldServices, services)
	if err != nil {
		return err
	}

	// Update haproxy config
	config := path.Join(ServiceVolume, ServiceConfig)
	vm.WriteFile(container, config, GenerateServiceConfig(services))

	command = append(command, "sh")
	command = append(command, "-c")
	command = append(command, "haproxy -f /usr/local/etc/haproxy/haproxy.cfg -p /var/run/haproxy.pid -sf `cat /var/run/haproxy.pid`")
	execcmd, err := json.Marshal(command)
	if err != nil {
		return err
	}

	tty := &hypervisor.TtyIO{
		Callback:  make(chan *types.VmResponse, 1),
		ClientTag: pod.RandStr(8, "alphanum"),
	}

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

	if tty.ExitCode != 0 {
		return fmt.Errorf("exec %s on container %s failed with exit code %d", command, container, tty.ExitCode)
	}

	return nil
}
示例#6
0
// Setup lo ip address
// options for operation: add or del
func SetupLoopbackAddress(vm *hypervisor.Vm, container, ip, operation string) error {
	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),
		ClientTag: pod.RandStr(8, "alphanum"),
	}

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

	if tty.ExitCode != 0 {
		return fmt.Errorf("exec %s on container %s failed with exit code %d", command, container, tty.ExitCode)
	}

	return nil
}