func (hardDisks HardDiskMap) AddHardDisks(xmlHardDisk *xmlHardDisk, parent *uuid.UUID, dir string) (disk *HardDisk, err error) { diskUUID, err := uuid.ParseHex(xmlHardDisk.UUID) if err != nil { return nil, err } disk = &HardDisk{ UUID: *diskUUID, Location: xmlHardDisk.Location, Format: xmlHardDisk.Format, Type: xmlHardDisk.Type, AutoReset: xmlHardDisk.AutoReset, Parent: parent, } if !path.IsAbs(disk.Location) { disk.Location = path.Join(dir, disk.Location) } lenChildDisks := len(xmlHardDisk.Children) if lenChildDisks != 0 { disk.Children = make([]*uuid.UUID, lenChildDisks) for index, childXmlDisk := range xmlHardDisk.Children { childDisk, err := hardDisks.AddHardDisks(&childXmlDisk, &disk.UUID, dir) if err != nil { return nil, err } disk.Children[index] = &childDisk.UUID } } hardDisks[*diskUUID] = disk return disk, nil }
func (p *Proxy) machineInstanceHandler(w http.ResponseWriter, r *http.Request) { params := r.URL.Query() uuidString := params.Get(":uuid") if uuidString == "" { sendJsonError(w, errors.New("Was expecting uuid.")) return } uuid, err := uuid.ParseHex(uuidString) if err != nil { err := fmt.Errorf("Could not parse uuid: %s error: %s", uuidString, err) sendJsonError(w, err) return } machine, ok := p.VirtualBox.Machines[*uuid] if !ok { err := fmt.Errorf("Could not find machine with uuid: %s.", uuidString) sendJsonError(w, err) return } op := params.Get(":op") switch op { case "stop": p.stopVmHandler(w, r, machine) case "start": p.startVmHandler(w, r, machine) } }
func extractUUIDs(text string) (uuids []*uuid.UUID) { re, err := regexp.Compile("([[:xdigit:]]{8}-[[:xdigit:]]{4}-[[:xdigit:]]{4}-[[:xdigit:]]{4}-[[:xdigit:]]{12})") if err != nil { log.Fatal("Failed to compile UUID regexp.") } uuidStrings := re.FindAllString(text, -1) uuids = make([]*uuid.UUID, len(uuidStrings)) for index, uuidString := range uuidStrings { uuid, err := uuid.ParseHex(uuidString) if err != nil { log.Fatal("Failed to compile parse UUID despite validating regexp.") } uuids[index] = uuid } return uuids }
// Load the given configuration file func Decode(configPath string) (vbox *VirtualBox, err error) { runningMachineUUIDs, err := runningMachineUUIDs() if err != nil { return } // top level xml file file, err := os.Open(configPath) if err != nil { return } machineList := new(xmlMachineList) err = xml.NewDecoder(file).Decode(machineList) if err != nil { return } // per machine xml file vbox = new(VirtualBox) vbox.Machines = make(MachineMap, len(machineList.Machines)) vbox.HardDisks = make(HardDiskMap) for _, machineListEntry := range machineList.Machines { file, err := os.Open(machineListEntry.Source) if err != nil { return nil, err } xmlMachineRoot := new(xmlMachineRoot) err = xml.NewDecoder(file).Decode(xmlMachineRoot) if err != nil { return nil, err } if len(xmlMachineRoot.Machines) != 1 { return nil, errors.New("Was expecting exactly 1 machine.") } xmlMachine := xmlMachineRoot.Machines[0] machineUUID, err := uuid.ParseHex(machineListEntry.UUID) if err != nil { return nil, err } status := Off if runningMachineUUIDs[*machineUUID] { status = Running } vrdePort := 0 if xmlMachine.RemoteDisplay.Enabled { vrdePortString := findProperty(&xmlMachine.RemoteDisplay.Properties, "TCP/Ports") if vrdePortString != "" { vrdePort, err = strconv.Atoi(vrdePortString) if err != nil { return nil, err } } } seleniumPort := 0 for _, forwarding := range xmlMachine.Forwarding { if forwarding.Name == "selenium" { seleniumPort = forwarding.HostPort } } machine := &Machine{ UUID: *machineUUID, Source: machineListEntry.Source, Name: xmlMachine.Name, OSType: OSType(xmlMachine.OSType), Status: status, VRDEPort: vrdePort, SeleniumPort: seleniumPort, } for _, xmlHardDisk := range xmlMachine.RegisteredHardDisks { _, err := vbox.HardDisks.AddHardDisks( &xmlHardDisk, nil, path.Dir(machine.Source)) if err != nil { return nil, err } } machine.HardDisks = make([]*uuid.UUID, len(xmlMachine.AttachedHardDisks)) for index, attachedImage := range xmlMachine.AttachedHardDisks { imageUUID, err := uuid.ParseHex(attachedImage.UUID) if err != nil { return nil, err } machine.HardDisks[index] = imageUUID } vbox.Machines[*machineUUID] = machine } return }