virtualbox

package
v0.0.0-...-deba36d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 7, 2023 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Manager = schema.ResourceManager{
	Name:              "virtualbox",
	ResourcePrototype: &Virtualbox{},
	GroupPrototype:    &VirtualboxGroup{},
	Provision: func(group interface{}, resource interface{}, l schema.Logger) error {
		g := group.(*VirtualboxGroup)
		s := resource.(*Virtualbox)

		label := g.DecommissionTag
		if len(label) > 0 {
			label += "_"
		}
		label += s.Name

		exists := true
		vminfoArr, err := vmBoxManageMatch(showVMInfoRegexp, "showvminfo", label)
		vminfo := make(map[string]string)
		if err != nil {
			if strings.Contains(err.stderr, "Could not find a registered machine named") {
				exists = false
			} else {
				return err
			}
		} else {
			for _, m := range vminfoArr {
				vminfo[m["name"]] = m["value"]
			}
		}

		if !exists {
			ovaPath := s.Image
			if _, err := url.Parse(s.Image); err == nil {

				h := sha256.New()
				io.WriteString(h, s.Image)
				filename := filepath.Base(s.Image)
				ovaPath = fmt.Sprintf(".dogocache/virtualbox/ova/%x-%v", h.Sum(nil), filename)

				if _, err = os.Stat(ovaPath); os.IsNotExist(err) {
					l.Logf("Downloading image: %v", s.Image)

					response, err := http.Get(s.Image)
					if err != nil {
						return err
					}
					defer response.Body.Close()

					downloadpath := ovaPath + ".dl"
					err = os.MkdirAll(filepath.Dir(downloadpath), 0777)
					if err != nil {
						return err
					}
					f, err := os.Create(downloadpath)
					if err != nil {
						return err
					}
					defer f.Close()

					reader := &progressReader{reader: response.Body, length: response.ContentLength}
					go func() {
						for reader.progress < 1 {
							l.SetProgress(reader.progress)
							time.Sleep(time.Second)
						}
						l.SetProgress(0)
					}()
					_, err = io.Copy(f, reader)
					if err != nil {
						return err
					}

					fmt.Println("moving from ", downloadpath, "to", ovaPath)
					err = os.Rename(downloadpath, ovaPath)
					if err != nil {
						return err
					}
				}
			} else {
				if _, err := os.Stat(ovaPath); os.IsNotExist(err) {
					return fmt.Errorf("could not find .ova image at %v", ovaPath)
				}
			}

			l.Logf("importing virtual machine image")
			if _, _, err := VMBoxManage("import", ovaPath,
				"--vsys", "0",
				"--vmname", label,
			); err != nil {
				return err
			}
		}

		for i := 1; i != 9; i++ {
			current := vminfo[fmt.Sprintf("NIC %v", i)]
			if i == 1 || i <= s.Networks {
				if !strings.Contains(current, "Attachment: NAT") {
					l.Logf(" - configuring NIC #%v", i)
					if _, _, err := changeConfig(l, vminfo, "modifyvm", label, fmt.Sprintf("--nic%v", i), "nat"); err != nil {
						return err
					}
				}
			} else if current != "disabled" && current != "" {
				l.Logf(" - removing NIC #%v", i)
				if _, _, err := changeConfig(l, vminfo, "modifyvm", label, fmt.Sprintf("--nic%v", i), "none"); err != nil {
					return err
				}
			}
		}

		for k, v := range vminfo {
			fmt.Println(k+":", v)
		}

		if vminfo["Memory size"] != fmt.Sprintf("%vMB", s.Memory) {
			l.Logf(" - setting memory to %v", s.Memory)
			if _, _, err := changeConfig(l, vminfo, "modifyvm", label, "--memory", fmt.Sprintf("%v", s.Memory)); err != nil {
				return err
			}
		}

		if s.CPUs < 1 {
			s.CPUs = 1
		}
		if vminfo["Number of CPUs"] != fmt.Sprintf("%v", s.CPUs) {
			l.Logf(" - setting cpus to %v", s.CPUs)
			if _, _, err := changeConfig(l, vminfo, "modifyvm", label, "--cpus", fmt.Sprintf("%v", s.CPUs)); err != nil {
				return err
			}
		}

		if !strings.HasPrefix(vminfo["State"], "running") {
			l.Logf("starting virtual machine")
			if _, _, err := VMBoxManage("startvm", label, "--type", "headless"); err != nil {
				return err
			}
		}
		return nil
	},
	FindUnused: func(shouldExist map[interface{}][]string, decommisionRoot *commandtree.Command, l schema.Logger) ([]string, error) {
		return []string{}, errors.New("Not implemented yet")
	},
}

Manager is the main entry point for this resource type

Functions

This section is empty.

Types

type VMBoxManageError

type VMBoxManageError struct {
	// contains filtered or unexported fields
}

func VMBoxManage

func VMBoxManage(args ...string) (string, string, *VMBoxManageError)

func (*VMBoxManageError) Error

func (e *VMBoxManageError) Error() string

type Virtualbox

type Virtualbox struct {
	Name string
	//Image           schema.Template   `required:"true" default:"ubuntu/trusty64" description:"The vagrant box to use"`
	Image      string            `` /* 185-byte string literal not displayed */
	PrivateIPs []schema.Template `required:"false" description:"The private ips to assign to the machine (using NAT between guest and host)"`
	//SharedFolders []schema.Template `required:"false" description:"Any shared folders between host and guest. Each entry should be in the form hostdir:guestdir"`
	Networks int `required:"false" description:"The number of netwok interfaces"`
	Memory   int `required:"false" description:"The amount of ram to dedicate to the virtual machine"`
	CPUs     int `required:"false" description:"The number of CPUs in the virtual machine"`
	Storage  int `required:"false" description:"The size in mb of the storage in the virtual machine"`
}

func (*Virtualbox) OpenConnection

func (s *Virtualbox) OpenConnection() (schema.ServerConnection, error)

type VirtualboxGroup

type VirtualboxGroup struct {
	DecommissionTag string `` /* 166-byte string literal not displayed */
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL