-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcli.go
More file actions
178 lines (161 loc) · 5.16 KB
/
cli.go
File metadata and controls
178 lines (161 loc) · 5.16 KB
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
// Copyright 2016 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package cli implements the commandline interface for Pixiecore.
package cli // import "github.com/metal-stack/pixie/cli"
import (
"fmt"
"log/slog"
"os"
"github.com/metal-stack/pixie/pixiecore"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
// Ipxe is the set of ipxe binaries for supported firmware.
//
// Can be set externally before calling CLI(), and set/extended by
// command line processing in CLI().
var Ipxe = map[pixiecore.Firmware][]byte{}
// CLI runs the Pixiecore commandline.
//
// This function always exits back to the OS when finished.
func CLI() {
cobra.OnInitialize(initConfig)
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(-1)
}
os.Exit(0)
}
// This represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "pixiecore",
Short: "All-in-one network booting",
Long: `Pixiecore is a tool to make network booting easy.`,
}
func initConfig() {
viper.SetEnvPrefix("pixiecore")
viper.AutomaticEnv() // read in environment variables that match
}
func fatalf(msg string, args ...any) {
fmt.Printf(msg+"\n", args...)
os.Exit(1)
}
func serverConfigFlags(cmd *cobra.Command) {
cmd.Flags().BoolP("debug", "d", false, "Log more things that aren't directly related to booting a recognized client")
cmd.Flags().StringP("listen-addr", "l", "0.0.0.0", "IPv4 address to listen on")
cmd.Flags().IntP("port", "p", 80, "Port to listen on for HTTP")
cmd.Flags().String("metrics-listen-addr", "0.0.0.0", "IPv4 address of the metrics server to listen on")
cmd.Flags().Int("metrics-port", 2113, "Metrics server port")
cmd.Flags().Int("status-port", 0, "HTTP port for status information (can be the same as --port)")
cmd.Flags().Bool("dhcp-no-bind", false, "Handle DHCP traffic without binding to the DHCP server port")
cmd.Flags().String("ipxe-bios", "", "Path to an iPXE binary for BIOS/UNDI")
cmd.Flags().String("ipxe-ipxe", "", "Path to an iPXE binary for chainloading from another iPXE")
cmd.Flags().String("ipxe-efi32", "", "Path to an iPXE binary for 32-bit UEFI")
cmd.Flags().String("ipxe-efi64", "", "Path to an iPXE binary for 64-bit UEFI")
}
func mustFile(path string) []byte {
bs, err := os.ReadFile(path)
if err != nil {
fatalf("couldn't read file %q: %s", path, err)
}
return bs
}
func serverFromFlags(cmd *cobra.Command) *pixiecore.Server {
debug, err := cmd.Flags().GetBool("debug")
if err != nil {
fatalf("Error reading flag: %s", err)
}
addr, err := cmd.Flags().GetString("listen-addr")
if err != nil {
fatalf("Error reading flag: %s", err)
}
httpPort, err := cmd.Flags().GetInt("port")
if err != nil {
fatalf("Error reading flag: %s", err)
}
metricsAddr, err := cmd.Flags().GetString("metrics-listen-addr")
if err != nil {
fatalf("Error reading flag: %s", err)
}
metricsPort, err := cmd.Flags().GetInt("metrics-port")
if err != nil {
fatalf("Error reading flag: %s", err)
}
httpStatusPort, err := cmd.Flags().GetInt("status-port")
if err != nil {
fatalf("Error reading flag: %s", err)
}
dhcpNoBind, err := cmd.Flags().GetBool("dhcp-no-bind")
if err != nil {
fatalf("Error reading flag: %s", err)
}
ipxeBios, err := cmd.Flags().GetString("ipxe-bios")
if err != nil {
fatalf("Error reading flag: %s", err)
}
ipxeIpxe, err := cmd.Flags().GetString("ipxe-ipxe")
if err != nil {
fatalf("Error reading flag: %s", err)
}
ipxeEFI32, err := cmd.Flags().GetString("ipxe-efi32")
if err != nil {
fatalf("Error reading flag: %s", err)
}
ipxeEFI64, err := cmd.Flags().GetString("ipxe-efi64")
if err != nil {
fatalf("Error reading flag: %s", err)
}
if httpPort <= 0 {
fatalf("HTTP port must be >0")
}
ret := &pixiecore.Server{
Ipxe: map[pixiecore.Firmware][]byte{},
Log: getLogger(debug),
HTTPPort: httpPort,
HTTPStatusPort: httpStatusPort,
MetricsPort: metricsPort,
MetricsAddress: metricsAddr,
DHCPNoBind: dhcpNoBind,
}
for fwtype, bs := range Ipxe {
ret.Ipxe[fwtype] = bs
}
if ipxeBios != "" {
ret.Ipxe[pixiecore.FirmwareX86PC] = mustFile(ipxeBios)
}
if ipxeIpxe != "" {
ret.Ipxe[pixiecore.FirmwareX86Ipxe] = mustFile(ipxeIpxe)
}
if ipxeEFI32 != "" {
ret.Ipxe[pixiecore.FirmwareEFI32] = mustFile(ipxeEFI32)
}
if ipxeEFI64 != "" {
ret.Ipxe[pixiecore.FirmwareEFI64] = mustFile(ipxeEFI64)
ret.Ipxe[pixiecore.FirmwareEFIBC] = ret.Ipxe[pixiecore.FirmwareEFI64]
}
if addr != "" {
ret.Address = addr
}
return ret
}
func getLogger(debug bool) *slog.Logger {
opts := &slog.HandlerOptions{
Level: slog.LevelInfo,
AddSource: debug,
}
if debug {
opts.Level = slog.LevelDebug
}
return slog.New(slog.NewJSONHandler(os.Stdout, opts))
}