-
Notifications
You must be signed in to change notification settings - Fork 19
/
main.go
193 lines (181 loc) · 6.19 KB
/
main.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package main
import (
"crypto/tls"
"flag"
"fmt"
"net"
"os"
"time"
"github.com/Symantec/Dominator/lib/constants"
"github.com/Symantec/Dominator/lib/flags/loadflags"
"github.com/Symantec/Dominator/lib/log"
"github.com/Symantec/Dominator/lib/log/cmdlogger"
"github.com/Symantec/Dominator/lib/net/rrdialer"
"github.com/Symantec/Dominator/lib/srpc"
"github.com/Symantec/Dominator/lib/srpc/setupclient"
"github.com/Symantec/Dominator/lib/tags"
)
var (
fleetManagerHostname = flag.String("fleetManagerHostname", "",
"Hostname of Fleet Manager")
fleetManagerPortNum = flag.Uint("fleetManagerPortNum",
constants.FleetManagerPortNumber,
"Port number of Fleet Resource Manager")
hypervisorHostname = flag.String("hypervisorHostname", "",
"Hostname of hypervisor")
hypervisorPortNum = flag.Uint("hypervisorPortNum",
constants.HypervisorPortNumber, "Port number of hypervisor")
hypervisorTags tags.Tags
imageServerHostname = flag.String("imageServerHostname", "localhost",
"Hostname of image server")
imageServerPortNum = flag.Uint("imageServerPortNum",
constants.ImageServerPortNumber,
"Port number of image server")
installerImageStream = flag.String("installerImageStream", "",
"Name of default image stream for building bootable installer ISO")
installerPortNum = flag.Uint("installerPortNum",
constants.InstallerPortNumber, "Port number of installer")
location = flag.String("location", "",
"Location to search for hypervisors")
offerTimeout = flag.Duration("offerTimeout", time.Minute+time.Second,
"How long to offer DHCP OFFERs and ACKs")
netbootFiles tags.Tags
netbootFilesTimeout = flag.Duration("netbootFilesTimeout",
time.Minute+time.Second,
"How long to provide files via TFTP after last DHCP ACK")
netbootTimeout = flag.Duration("netbootTimeout", time.Minute,
"Time to wait for DHCP ACKs to be sent")
numAcknowledgementsToWaitFor = flag.Uint("numAcknowledgementsToWaitFor",
2, "Number of DHCP ACKs to wait for")
storageLayoutFilename = flag.String("storageLayoutFilename", "",
"Name of file containing storage layout for installing machine")
topologyDir = flag.String("topologyDir", "",
"Name of local topology directory in Git repository")
rrDialer *rrdialer.Dialer
)
func init() {
flag.Var(&hypervisorTags, "hypervisorTags", "Tags to apply to Hypervisor")
flag.Var(&netbootFiles, "netbootFiles", "Extra files served by TFTP server")
}
func printUsage() {
fmt.Fprintln(os.Stderr,
"Usage: hyper-control [flags...] command [args...]")
fmt.Fprintln(os.Stderr, "Common flags:")
flag.PrintDefaults()
fmt.Fprintln(os.Stderr, "Commands:")
fmt.Fprintln(os.Stderr, " add-address MACaddr [IPaddr]")
fmt.Fprintln(os.Stderr, " add-subnet ID IPgateway IPmask DNSserver...")
fmt.Fprintln(os.Stderr, " change-tags")
fmt.Fprintln(os.Stderr, " get-machine-info hostname")
fmt.Fprintln(os.Stderr, " get-updates")
fmt.Fprintln(os.Stderr, " installer-shell hostname")
fmt.Fprintln(os.Stderr, " make-installer-iso hostname dirname")
fmt.Fprintln(os.Stderr, " move-ip-address IPaddr")
fmt.Fprintln(os.Stderr, " netboot-host hostname")
fmt.Fprintln(os.Stderr, " netboot-machine MACaddr IPaddr [hostname]")
fmt.Fprintln(os.Stderr, " reinstall")
fmt.Fprintln(os.Stderr, " remove-excess-addresses MaxFreeAddr")
fmt.Fprintln(os.Stderr, " remove-ip-address IPaddr")
fmt.Fprintln(os.Stderr, " remove-mac-address MACaddr")
fmt.Fprintln(os.Stderr, " rollout-image name")
fmt.Fprintln(os.Stderr, " show-network-configuration")
fmt.Fprintln(os.Stderr, " update-network-configuration")
fmt.Fprintln(os.Stderr, " write-netboot-files hostname dirname")
}
type commandFunc func([]string, log.DebugLogger) error
type subcommand struct {
command string
minArgs int
maxArgs int
cmdFunc commandFunc
}
var subcommands = []subcommand{
{"add-address", 1, 2, addAddressSubcommand},
{"add-subnet", 4, -1, addSubnetSubcommand},
{"change-tags", 0, 0, changeTagsSubcommand},
{"get-machine-info", 1, 1, getMachineInfoSubcommand},
{"get-updates", 0, 0, getUpdatesSubcommand},
{"installer-shell", 1, 1, installerShellSubcommand},
{"make-installer-iso", 2, 2, makeInstallerIsoSubcommand},
{"move-ip-address", 1, 1, moveIpAddressSubcommand},
{"netboot-host", 1, 1, netbootHostSubcommand},
{"netboot-machine", 2, 3, netbootMachineSubcommand},
{"reinstall", 0, 0, reinstallSubcommand},
{"remove-excess-addresses", 1, 1, removeExcessAddressesSubcommand},
{"remove-ip-address", 1, 1, removeIpAddressSubcommand},
{"remove-mac-address", 1, 1, removeMacAddressSubcommand},
{"rollout-image", 1, 1, rolloutImageSubcommand},
{"show-network-configuration", 0, 0, showNetworkConfigurationSubcommand},
{"update-network-configuration", 0, 0,
updateNetworkConfigurationSubcommand},
{"write-netboot-files", 2, 2, writeNetbootFilesSubcommand},
}
func loadCerts() error {
err := setupclient.SetupTls(false)
if err == nil {
return nil
}
if !os.IsNotExist(err) {
return err
}
if os.Geteuid() != 0 {
return err
}
cert, e := tls.LoadX509KeyPair("/etc/ssl/hypervisor/cert.pem",
"/etc/ssl/hypervisor/key.pem")
if e != nil {
return err
}
srpc.RegisterClientTlsConfig(&tls.Config{
InsecureSkipVerify: true,
MinVersion: tls.VersionTLS12,
Certificates: []tls.Certificate{cert},
})
return nil
}
func doMain() int {
if err := loadflags.LoadForCli("hyper-control"); err != nil {
fmt.Fprintln(os.Stderr, err)
return 1
}
flag.Usage = printUsage
flag.Parse()
if flag.NArg() < 1 {
printUsage()
return 2
}
logger := cmdlogger.New()
if err := loadCerts(); err != nil {
fmt.Fprintln(os.Stderr, err)
return 1
}
var err error
rrDialer, err = rrdialer.New(&net.Dialer{Timeout: time.Second * 10}, "",
logger)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return 1
}
defer rrDialer.WaitForBackgroundResults(time.Second)
numSubcommandArgs := flag.NArg() - 1
for _, subcommand := range subcommands {
if flag.Arg(0) == subcommand.command {
if numSubcommandArgs < subcommand.minArgs ||
(subcommand.maxArgs >= 0 &&
numSubcommandArgs > subcommand.maxArgs) {
printUsage()
return 2
}
if err := subcommand.cmdFunc(flag.Args()[1:], logger); err != nil {
fmt.Fprintln(os.Stderr, err)
return 1
}
return 0
}
}
printUsage()
return 2
}
func main() {
os.Exit(doMain())
}