<commit_before>package vsphere

import (
	"github.com/Sirupsen/logrus"
	"github.com/emc-advanced-dev/unik/pkg/providers/common"
	"github.com/emc-advanced-dev/unik/pkg/types"
	unikutil "github.com/emc-advanced-dev/unik/pkg/util"
	"github.com/layer-x/layerx-commons/lxerrors"
	"github.com/layer-x/layerx-commons/lxhttpclient"
	"os"
	"time"
)

func (p *VsphereProvider) RunInstance(name, imageId string, mntPointsToVolumeIds map[string]string, env map[string]string) (_ *types.Instance, err error) {
	logrus.WithFields(logrus.Fields{
		"image-id": imageId,
		"mounts":   mntPointsToVolumeIds,
		"env":      env,
	}).Infof("running instance %s", name)

	if _, err := p.GetInstance(name); err == nil {
		return nil, lxerrors.New("instance with name "+name+" already exists. virtualbox provider requires unique names for instances", nil)
	}

	image, err := p.GetImage(imageId)
	if err != nil {
		return nil, lxerrors.New("getting image", err)
	}

	if err := common.VerifyMntsInput(p, image, mntPointsToVolumeIds); err != nil {
		return nil, lxerrors.New("invalid mapping for volume", err)
	}

	instanceDir := getInstanceDatastoreDir(name)

	portsUsed := []int{}

	c := p.getClient()

	defer func() {
		if err != nil {
			logrus.WithError(err).Errorf("error encountered, ensuring vm and disks are destroyed")
			c.PowerOffVm(name)
			for _, portUsed := range portsUsed {
				c.DetachDisk(name, portUsed)
			}
			c.DestroyVm(name)
			os.RemoveAll(instanceDir)
		}
	}()

	logrus.Debugf("creating vsphere vm")

	if err := c.CreateVm(name, p.config.DefaultInstanceMemory); err != nil {
		return nil, lxerrors.New("creating vm", err)
	}

	logrus.Debugf("powering on vm to assign mac addr")
	if err := c.PowerOnVm(name); err != nil {
		return nil, lxerrors.New("failed to power on vm to assign mac addr", err)
	}

	vm, err := c.GetVm(name)
	if err != nil {
		return nil, lxerrors.New("failed to retrieve vm info after create", err)
	}

	macAddr := ""
	if vm.Config.Hardware.Device != nil {
		for _, device := range vm.Config.Hardware.Device {
			if len(device.MacAddress) > 0 {
				macAddr = device.MacAddress
				break
			}
		}
	}
	if macAddr == "" {
		logrus.WithFields(logrus.Fields{"vm": vm}).Warnf("vm found, cannot identify mac addr")
		return nil, lxerrors.New("could not find mac addr on vm", nil)
	}

	logrus.Debugf("copying base boot vmdk to instance dir")
	instanceBootImagePath := instanceDir + "/boot.vmdk"
	if err := c.CopyVmdk(getImageDatastorePath(image.Name), instanceBootImagePath); err != nil {
		return nil, lxerrors.New("copying base boot image", err)
	}
	if err := c.AttachDisk(name, instanceBootImagePath, 0); err != nil {
		return nil, lxerrors.New("attaching boot vol to instance", err)
	}

	for mntPoint, volumeId := range mntPointsToVolumeIds {
		volume, err := p.GetVolume(volumeId)
		if err != nil {
			return nil, lxerrors.New("getting volume", err)
		}
		controllerPort, err := common.GetControllerPortForMnt(image, mntPoint)
		if err != nil {
			return nil, lxerrors.New("getting controller port for mnt point", err)
		}
		if err := c.AttachDisk(name, getVolumeDatastorePath(volume.Name), controllerPort); err != nil {<commit_msg>poweron/off vm to get mac addr<commit_after>package vsphere

import (
	"github.com/Sirupsen/logrus"
	"github.com/emc-advanced-dev/unik/pkg/providers/common"
	"github.com/emc-advanced-dev/unik/pkg/types"
	unikutil "github.com/emc-advanced-dev/unik/pkg/util"
	"github.com/layer-x/layerx-commons/lxerrors"
	"github.com/layer-x/layerx-commons/lxhttpclient"
	"os"
	"time"
)

func (p *VsphereProvider) RunInstance(name, imageId string, mntPointsToVolumeIds map[string]string, env map[string]string) (_ *types.Instance, err error) {
	logrus.WithFields(logrus.Fields{
		"image-id": imageId,
		"mounts":   mntPointsToVolumeIds,
		"env":      env,
	}).Infof("running instance %s", name)

	if _, err := p.GetInstance(name); err == nil {
		return nil, lxerrors.New("instance with name "+name+" already exists. virtualbox provider requires unique names for instances", nil)
	}

	image, err := p.GetImage(imageId)
	if err != nil {
		return nil, lxerrors.New("getting image", err)
	}

	if err := common.VerifyMntsInput(p, image, mntPointsToVolumeIds); err != nil {
		return nil, lxerrors.New("invalid mapping for volume", err)
	}

	instanceDir := getInstanceDatastoreDir(name)

	portsUsed := []int{}

	c := p.getClient()

	defer func() {
		if err != nil {
			logrus.WithError(err).Errorf("error encountered, ensuring vm and disks are destroyed")
			c.PowerOffVm(name)
			for _, portUsed := range portsUsed {
				c.DetachDisk(name, portUsed)
			}
			c.DestroyVm(name)
			os.RemoveAll(instanceDir)
		}
	}()

	logrus.Debugf("creating vsphere vm")

	if err := c.CreateVm(name, p.config.DefaultInstanceMemory); err != nil {
		return nil, lxerrors.New("creating vm", err)
	}

	logrus.Debugf("powering on vm to assign mac addr")
	if err := c.PowerOnVm(name); err != nil {
		return nil, lxerrors.New("failed to power on vm to assign mac addr", err)
	}

	vm, err := c.GetVm(name)
	if err != nil {
		return nil, lxerrors.New("failed to retrieve vm info after create", err)
	}

	macAddr := ""
	if vm.Config.Hardware.Device != nil {
		for _, device := range vm.Config.Hardware.Device {
			if len(device.MacAddress) > 0 {
				macAddr = device.MacAddress
				break
			}
		}
	}
	if macAddr == "" {
		logrus.WithFields(logrus.Fields{"vm": vm}).Warnf("vm found, cannot identify mac addr")
		return nil, lxerrors.New("could not find mac addr on vm", nil)
	}
	if err := c.PowerOffVm(name); err != nil {
		return nil, lxerrors.New("failed to power off vm after retrieving mac addr", err)
	}

	logrus.Debugf("copying base boot vmdk to instance dir")
	instanceBootImagePath := instanceDir + "/boot.vmdk"
	if err := c.CopyVmdk(getImageDatastorePath(image.Name), instanceBootImagePath); err != nil {
		return nil, lxerrors.New("copying base boot image", err)
	}
	if err := c.AttachDisk(name, instanceBootImagePath, 0); err != nil {
		return nil, lxerrors.New("attaching boot vol to instance", err)
	}

	for mntPoint, volumeId := range mntPointsToVolumeIds {
		volume, err := p.GetVolume(volumeId)
		if err != nil {
			return nil, lxerrors.New("getting volume", err)
		}
		controllerPort, err := common.GetControllerPortForMnt(image, mntPoint)
		if err != nil {
			return nil, lxerrors.New("getting controller port for mnt point", err)
		}
		if err := c.AttachDisk(name, getVolumeDatastorePath(volume.Name), controllerPort); err != nil {
{'fraction_non_alphanumeric': 0.1383803909401179, 'fraction_numerical': 0.0006205398696866273, 'mean_word_length': 4.207592891760905, 'pattern_counts': {'":': 8, '<': 3, '<?xml version=': 0, '>': 5, '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': '5379694', 'n_tokens_mistral': 2413, 'n_tokens_neox': 2254, 'n_words': 572}