forked from Cloud-Foundations/Dominator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
netbootMachine.go
60 lines (56 loc) · 1.61 KB
/
netbootMachine.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package main
import (
"fmt"
"io/ioutil"
"net"
"github.com/Cloud-Foundations/Dominator/lib/errors"
"github.com/Cloud-Foundations/Dominator/lib/log"
"github.com/Cloud-Foundations/Dominator/lib/srpc"
proto "github.com/Cloud-Foundations/Dominator/proto/hypervisor"
)
func netbootMachineSubcommand(args []string, logger log.DebugLogger) error {
var hostname string
if len(args) > 2 {
hostname = args[2]
}
err := netbootMachine(args[0], args[1], hostname, logger)
if err != nil {
return fmt.Errorf("error netbooting machine: %s", err)
}
return nil
}
func netbootMachine(macAddr, ipAddr, hostname string,
logger log.DebugLogger) error {
request := proto.NetbootMachineRequest{
Address: proto.Address{
MacAddress: macAddr,
IpAddress: net.ParseIP(ipAddr),
},
Files: make(map[string][]byte,
len(netbootFiles)),
FilesExpiration: *netbootFilesTimeout,
Hostname: hostname,
NumAcknowledgementsToWaitFor: *numAcknowledgementsToWaitFor,
OfferExpiration: *offerTimeout,
WaitTimeout: *netbootTimeout,
}
for tftpFilename, localFilename := range netbootFiles {
if data, err := ioutil.ReadFile(localFilename); err != nil {
return err
} else {
request.Files[tftpFilename] = data
}
}
var reply proto.NetbootMachineResponse
clientName := fmt.Sprintf("%s:%d", *hypervisorHostname, *hypervisorPortNum)
client, err := srpc.DialHTTP("tcp", clientName, 0)
if err != nil {
return err
}
defer client.Close()
err = client.RequestReply("Hypervisor.NetbootMachine", request, &reply)
if err != nil {
return err
}
return errors.New(reply.Error)
}