This repository has been archived by the owner on Dec 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
main.go
194 lines (167 loc) · 7.68 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
194
package main
import (
"bytes"
"flag"
"fmt"
"log"
"os"
"path/filepath"
"github.com/giantswarm/micrologger"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/giantswarm/mayu/fs"
"github.com/giantswarm/mayu/hostmgr"
"github.com/giantswarm/mayu/pxemgr"
)
var (
globalFlags = MayuFlags{}
mainCmd = &cobra.Command{
Use: "mayu",
Short: "Manage your bare metal machines",
Long: "",
Run: mainRun,
}
projectVersion string = "1.1.0"
projectBuild string = "git"
)
func init() {
// Map any flags registered in the standard "flag" package into the
// top-level mayu command (eg. log flags)
pf := mainCmd.PersistentFlags()
flag.VisitAll(func(f *flag.Flag) {
pf.AddFlag(pflag.PFlagFromGoFlag(f))
})
pf.BoolVarP(&globalFlags.debug, "debug", "d", false, "Print debug output")
pf.BoolVar(&globalFlags.version, "version", false, "Show the version of Mayu")
pf.BoolVar(&globalFlags.help, "help", false, "Show mayu usage")
pf.StringVar(&globalFlags.configFile, "config", DefaultConfigFile, "Path to the configuration file")
pf.StringVar(&globalFlags.clusterDir, "cluster-directory", DefaultClusterDirectory, "Path to the cluster directory")
pf.BoolVar(&globalFlags.showTemplates, "show-templates", DefaultShowTemplates, "Show the templates and quit")
pf.BoolVar(&globalFlags.noGit, "no-git", DefaultNoGit, "Disable git operations")
pf.BoolVar(&globalFlags.noTLS, "no-tls", DefaultNoTLS, "Disable tls")
pf.StringVar(&globalFlags.tFTPRoot, "tftproot", DefaultTFTPRoot, "Path to the tftproot")
pf.StringVar(&globalFlags.fileServerPath, "file-server-path", DefaultFileServerPath, "Path to fileserver dir.")
pf.StringVar(&globalFlags.ignitionConfig, "ignition-config", DefaultIgnitionConfig, "Final ignition config file that is used to boot the machine")
pf.StringVar(&globalFlags.dnsmasqTemplate, "dnsmasq-template", DefaultDnsmasqTemplate, "Dnsmasq config template")
pf.StringVar(&globalFlags.templateSnippets, "template-snippets", DefaultTemplateSnippets, "Cloudconfig or Ignition template snippets (eg storage or network configuration)")
pf.StringVar(&globalFlags.dnsmasq, "dnsmasq", DefaultDNSMasq, "Path to dnsmasq binary")
pf.StringVar(&globalFlags.imagesCacheDir, "images-cache-dir", DefaultImagesCacheDir, "Directory for Container Linux images")
pf.StringVar(&globalFlags.filesDir, "files-dir", DefaultFilesDir, "Directory for file templates")
pf.IntVar(&globalFlags.apiPort, "api-port", DefaultAPIPort, "API HTTP port Mayu listens on")
pf.IntVar(&globalFlags.pxePort, "pxe-port", DefaultPXEPort, "PXE HTTP port Mayu listens on")
pf.StringVar(&globalFlags.bindAddress, "http-bind-address", DefaultHTTPBindAddress, "HTTP address Mayu listens on")
pf.StringVar(&globalFlags.tlsCertFile, "tls-cert-file", DefaultTLSCertFile, "Path to tls certificate file")
pf.StringVar(&globalFlags.tlsKeyFile, "tls-key-file", DefaultTLSKeyFile, "Path to tls key file")
pf.BoolVar(&globalFlags.useInternalEtcdDiscovery, "use-internal-etcd-discovery", DefaultUseInternalEtcdDiscovery, "Use the internal etcd discovery")
pf.IntVar(&globalFlags.etcdQuorumSize, "etcd-quorum-size", DefaultEtcdQuorumSize, "Default quorum of the etcd clusters")
pf.StringVar(&globalFlags.etcdDiscoveryUrl, "etcd-discovery", DefaultEtcdDiscoveryUrl, "External etcd discovery base url (eg https://discovery.etcd.io). Note: This should be the base URL of the discovery without a specific token. Mayu itself creates a token for the etcd clusters.")
pf.StringVar(&globalFlags.etcdEndpoint, "etcd-endpoint", DefaultEtcdEndpoint, "The etcd endpoint for the internal discovery feature (you must also specify protocol).")
pf.StringVar(&globalFlags.etcdCAfile, "etcd-cafile", DefaultEtcdCA, "The etcd CA file, if etcd is using non-trustred root CA certificate")
pf.BoolVar(&globalFlags.flatcarAutologin, "flatcar-autologin", DefaultFlatcarAutologin, "Sets kernel boot param 'flatcar.autologin'. This is handy for debugging. Do NOT use for production!")
pf.BoolVar(&globalFlags.consoleTTY, "console-tty", DefaultConsoleTTY, "Sets kernel boot param 'console=ttyS0'. This is handy for debugging.")
pf.BoolVar(&globalFlags.systemdShell, "systemd-shell", DefaultSystemdShell, "Sets kernel boot param 'rd.shell'. This will be activated if the initramfs fails to boot successfully.")
globalFlags.filesystem = fs.DefaultFilesystem
}
func main() {
log.SetFlags(0)
log.SetPrefix("mayu: ")
if globalFlags.version {
printVersion()
os.Exit(0)
}
_ = mainCmd.Execute()
}
func mainRun(cmd *cobra.Command, args []string) {
if os.Args[1] == "version" {
println("Mayu build: ", projectBuild)
println("Mayu version: ", projectVersion)
return
}
if globalFlags.help {
cmd.PersistentFlags().Usage()
return
}
var err error
var logger micrologger.Logger
{
logger, err = micrologger.New(micrologger.Config{})
if err != nil {
println("ERROR: failed to init logger")
os.Exit(1)
}
}
_ = logger.Log("level", "info", "message", fmt.Sprintf("Starting mayu version %s", projectVersion))
// hack to make some dnsmasq versions happy
globalFlags.tFTPRoot, err = filepath.Abs(globalFlags.tFTPRoot)
if err != nil {
log.Fatal(err)
}
if ok, err := globalFlags.Validate(); !ok {
log.Fatal(err)
}
var cluster *hostmgr.Cluster
if fileExists(fmt.Sprintf("%s/cluster.json", globalFlags.clusterDir)) {
cluster, err = hostmgr.OpenCluster(globalFlags.clusterDir, logger)
} else {
cluster, err = hostmgr.NewCluster(globalFlags.clusterDir, logger)
}
if err != nil {
_ = logger.Log("level", "error", "message", "unable to get a cluster", "stack", err)
os.Exit(1)
}
globalFlags.templateSnippets = DefaultTemplateSnippets
pxeManager, err := pxemgr.PXEManager(pxemgr.PXEManagerConfiguration{
ConfigFile: globalFlags.configFile,
UseInternalEtcdDiscovery: globalFlags.useInternalEtcdDiscovery,
EtcdQuorumSize: globalFlags.etcdQuorumSize,
EtcdDiscoveryUrl: globalFlags.etcdDiscoveryUrl,
EtcdEndpoint: globalFlags.etcdEndpoint,
EtcdCAFile: globalFlags.etcdCAfile,
DNSmasqExecutable: globalFlags.dnsmasq,
DNSmasqTemplate: globalFlags.dnsmasqTemplate,
TFTPRoot: globalFlags.tFTPRoot,
NoTLS: globalFlags.noTLS,
APIPort: globalFlags.apiPort,
PXEPort: globalFlags.pxePort,
BindAddress: globalFlags.bindAddress,
TLSCertFile: globalFlags.tlsCertFile,
TLSKeyFile: globalFlags.tlsKeyFile,
FileServerPath: globalFlags.fileServerPath,
StaticHTMLPath: globalFlags.staticHTMLPath,
TemplateSnippets: globalFlags.templateSnippets,
IgnitionConfig: globalFlags.ignitionConfig,
ImagesCacheDir: globalFlags.imagesCacheDir,
FilesDir: globalFlags.filesDir,
FlatcarAutologin: globalFlags.flatcarAutologin,
ConsoleTTY: globalFlags.consoleTTY,
SystemdShell: globalFlags.systemdShell,
Version: projectVersion,
Logger: logger,
}, cluster)
if err != nil {
_ = logger.Log("level", "error", "message", "unable to create a pxe manager", "stack", err)
os.Exit(1)
}
if globalFlags.showTemplates {
placeholderHost := hostmgr.Host{}
b := bytes.NewBuffer(nil)
if err := pxeManager.WriteIgnitionConfig(placeholderHost, b); err != nil {
_ = logger.Log("level", "error", "message", "error found while checking generated ignition config ", "stack", err)
os.Exit(1)
}
os.Stdout.WriteString("ignition config:\n")
os.Stdout.WriteString(b.String())
os.Exit(0)
}
err = pxeManager.Start()
if err != nil {
_ = logger.Log("level", "error", "message", err)
os.Exit(1)
}
}
func fileExists(path string) bool {
if _, err := os.Stat(path); err == nil {
return true
}
return false
}