<commit_before>package main

import (
	"fmt"
	"net"
	"time"

	"github.com/Cloud-Foundations/Dominator/lib/errors"
	"github.com/Cloud-Foundations/Dominator/lib/log"
	"github.com/Cloud-Foundations/Dominator/lib/srpc"
	hyper_proto "github.com/Cloud-Foundations/Dominator/proto/hypervisor"
)

func migrateVmSubcommand(args []string, logger log.DebugLogger) error {
	if err := migrateVm(args[0], logger); err != nil {
		return fmt.Errorf("Error migrating VM: %s", err)
	}
	return nil
}

func discardAccessToken(hypervisor *srpc.Client, ipAddr net.IP) {
	request := hyper_proto.DiscardVmAccessTokenRequest{IpAddress: ipAddr}
	var reply hyper_proto.DiscardVmAccessTokenResponse
	hypervisor.RequestReply("Hypervisor.DiscardVmAccessToken", request, &reply)
}

func getVmAccessTokenClient(hypervisor *srpc.Client,
	ipAddr net.IP) ([]byte, error) {
	request := hyper_proto.GetVmAccessTokenRequest{ipAddr, time.Hour * 24}
	var reply hyper_proto.GetVmAccessTokenResponse
	err := hypervisor.RequestReply("Hypervisor.GetVmAccessToken", request,
		&reply)
	if err != nil {
		return nil, err
	}
	if err := errors.New(reply.Error); err != nil {
		return nil, err
	}
	return reply.Token, nil
}

func migrateVm(vmHostname string, logger log.DebugLogger) error {
	if vmIP, hypervisor, err := searchVmAndHypervisor(vmHostname); err != nil {
		return err
	} else {
		return migrateVmFromHypervisor(hypervisor, vmIP, logger)
	}
}

func migrateVmFromHypervisor(sourceHypervisorAddress string, vmIP net.IP,
	logger log.DebugLogger) error {
	destHypervisorAddress, err := getHypervisorAddress()
	if err != nil {
		return err
	}
	sourceHypervisor, err := dialHypervisor(sourceHypervisorAddress)
	if err != nil {
		return err
	}
	defer sourceHypervisor.Close()
	accessToken, err := getVmAccessTokenClient(sourceHypervisor, vmIP)
	if err != nil {<commit_msg>vm-control migrate-vm: give more clear error message if already migrating.<commit_after>package main

import (
	"fmt"
	"net"
	"time"

	hyperclient "github.com/Cloud-Foundations/Dominator/hypervisor/client"
	"github.com/Cloud-Foundations/Dominator/lib/errors"
	"github.com/Cloud-Foundations/Dominator/lib/log"
	"github.com/Cloud-Foundations/Dominator/lib/srpc"
	hyper_proto "github.com/Cloud-Foundations/Dominator/proto/hypervisor"
)

func migrateVmSubcommand(args []string, logger log.DebugLogger) error {
	if err := migrateVm(args[0], logger); err != nil {
		return fmt.Errorf("Error migrating VM: %s", err)
	}
	return nil
}

func discardAccessToken(hypervisor *srpc.Client, ipAddr net.IP) {
	request := hyper_proto.DiscardVmAccessTokenRequest{IpAddress: ipAddr}
	var reply hyper_proto.DiscardVmAccessTokenResponse
	hypervisor.RequestReply("Hypervisor.DiscardVmAccessToken", request, &reply)
}

func getVmAccessTokenClient(hypervisor *srpc.Client,
	ipAddr net.IP) ([]byte, error) {
	request := hyper_proto.GetVmAccessTokenRequest{ipAddr, time.Hour * 24}
	var reply hyper_proto.GetVmAccessTokenResponse
	err := hypervisor.RequestReply("Hypervisor.GetVmAccessToken", request,
		&reply)
	if err != nil {
		return nil, err
	}
	if err := errors.New(reply.Error); err != nil {
		return nil, err
	}
	return reply.Token, nil
}

func migrateVm(vmHostname string, logger log.DebugLogger) error {
	if vmIP, hypervisor, err := searchVmAndHypervisor(vmHostname); err != nil {
		return err
	} else {
		return migrateVmFromHypervisor(hypervisor, vmIP, logger)
	}
}

func migrateVmFromHypervisor(sourceHypervisorAddress string, vmIP net.IP,
	logger log.DebugLogger) error {
	destHypervisorAddress, err := getHypervisorAddress()
	if err != nil {
		return err
	}
	sourceHypervisor, err := dialHypervisor(sourceHypervisorAddress)
	if err != nil {
		return err
	}
	defer sourceHypervisor.Close()
	vmInfo, err := hyperclient.GetVmInfo(sourceHypervisor, vmIP)
	if err != nil {
		return err
	} else if vmInfo.State == hyper_proto.StateMigrating {
		return errors.New("VM is migrating")
	}
	accessToken, err := getVmAccessTokenClient(sourceHypervisor, vmIP)
	if err != nil {
{'fraction_non_alphanumeric': 0.1125911032922845, 'fraction_numerical': 0.0015079165619502387, 'mean_word_length': 5.461038961038961, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2983007', 'n_tokens_mistral': 1405, 'n_tokens_neox': 1345, 'n_words': 294}