Cleanup command running a bit
This commit is contained in:
parent
b2eaa3f7f8
commit
948d927eb9
96
cmd/root.go
96
cmd/root.go
|
@ -21,6 +21,7 @@ import (
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"golang.org/x/crypto/acme"
|
"golang.org/x/crypto/acme"
|
||||||
"golang.org/x/crypto/acme/autocert"
|
"golang.org/x/crypto/acme/autocert"
|
||||||
|
log2 "log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
@ -29,39 +30,51 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
profiler = ""
|
|
||||||
configPath = config.DefaultLocation
|
configPath = config.DefaultLocation
|
||||||
debug = false
|
debug = false
|
||||||
useAutomaticTls = false
|
|
||||||
tlsHostname = ""
|
|
||||||
showVersion = false
|
|
||||||
ignoreCertificateErrors = false
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var root = &cobra.Command{
|
var rootCommand = &cobra.Command{
|
||||||
Use: "wings",
|
Use: "wings",
|
||||||
Short: "The wings of the pterodactyl game management panel",
|
Short: "Runs the API server allowing programatic control of game servers for Pterodactyl Panel.",
|
||||||
Long: ``,
|
|
||||||
PreRun: func(cmd *cobra.Command, args []string) {
|
PreRun: func(cmd *cobra.Command, args []string) {
|
||||||
if useAutomaticTls && len(tlsHostname) == 0 {
|
if tls, _ := cmd.Flags().GetBool("auto-tls"); tls {
|
||||||
|
if host, _ := cmd.Flags().GetString("tls-hostname"); host == "" {
|
||||||
fmt.Println("A TLS hostname must be provided when running wings with automatic TLS, e.g.:\n\n ./wings --auto-tls --tls-hostname my.example.com")
|
fmt.Println("A TLS hostname must be provided when running wings with automatic TLS, e.g.:\n\n ./wings --auto-tls --tls-hostname my.example.com")
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
},
|
},
|
||||||
Run: rootCmdRun,
|
Run: rootCmdRun,
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
var versionCommand = &cobra.Command{
|
||||||
root.PersistentFlags().BoolVar(&showVersion, "version", false, "show the version and exit")
|
Use: "version",
|
||||||
root.PersistentFlags().StringVar(&configPath, "config", config.DefaultLocation, "set the location for the configuration file")
|
Short: "Prints the current executable version and exits.",
|
||||||
root.PersistentFlags().BoolVar(&debug, "debug", false, "pass in order to run wings in debug mode")
|
Run: func(cmd *cobra.Command, _ []string) {
|
||||||
root.PersistentFlags().StringVar(&profiler, "profiler", "", "the profiler to run for this instance")
|
fmt.Printf("wings v%s\nCopyright © 2018 - 2021 Dane Everitt & Contributors\n", system.Version)
|
||||||
root.PersistentFlags().BoolVar(&useAutomaticTls, "auto-tls", false, "pass in order to have wings generate and manage it's own SSL certificates using Let's Encrypt")
|
},
|
||||||
root.PersistentFlags().StringVar(&tlsHostname, "tls-hostname", "", "required with --auto-tls, the FQDN for the generated SSL certificate")
|
}
|
||||||
root.PersistentFlags().BoolVar(&ignoreCertificateErrors, "ignore-certificate-errors", false, "if passed any SSL certificate errors will be ignored by wings")
|
|
||||||
|
|
||||||
root.AddCommand(configureCmd)
|
func Execute() {
|
||||||
root.AddCommand(diagnosticsCmd)
|
if err := rootCommand.Execute(); err != nil {
|
||||||
|
log2.Fatalf("failed to execute command: %s", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
rootCommand.PersistentFlags().StringVar(&configPath, "config", config.DefaultLocation, "set the location for the configuration file")
|
||||||
|
rootCommand.PersistentFlags().BoolVar(&debug, "debug", false, "pass in order to run wings in debug mode")
|
||||||
|
|
||||||
|
// Flags specifically used when running the API.
|
||||||
|
rootCommand.Flags().String("profiler", "", "the profiler to run for this instance")
|
||||||
|
rootCommand.Flags().Bool("auto-tls", false, "pass in order to have wings generate and manage it's own SSL certificates using Let's Encrypt")
|
||||||
|
rootCommand.Flags().String("tls-hostname", "", "required with --auto-tls, the FQDN for the generated SSL certificate")
|
||||||
|
rootCommand.Flags().Bool("ignore-certificate-errors", false, "ignore certificate verification errors when executing API calls")
|
||||||
|
|
||||||
|
rootCommand.AddCommand(versionCommand)
|
||||||
|
rootCommand.AddCommand(configureCmd)
|
||||||
|
rootCommand.AddCommand(diagnosticsCmd)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the configuration path based on the arguments provided.
|
// Get the configuration path based on the arguments provided.
|
||||||
|
@ -85,13 +98,8 @@ func readConfiguration() (*config.Configuration, error) {
|
||||||
return config.ReadConfiguration(p)
|
return config.ReadConfiguration(p)
|
||||||
}
|
}
|
||||||
|
|
||||||
func rootCmdRun(*cobra.Command, []string) {
|
func rootCmdRun(cmd *cobra.Command, _ []string) {
|
||||||
if showVersion {
|
switch cmd.Flag("profiler").Value.String() {
|
||||||
fmt.Println(system.Version)
|
|
||||||
os.Exit(0)
|
|
||||||
}
|
|
||||||
|
|
||||||
switch profiler {
|
|
||||||
case "cpu":
|
case "cpu":
|
||||||
defer profile.Start(profile.CPUProfile).Stop()
|
defer profile.Start(profile.CPUProfile).Stop()
|
||||||
case "mem":
|
case "mem":
|
||||||
|
@ -117,7 +125,6 @@ func rootCmdRun(*cobra.Command, []string) {
|
||||||
if errors.Is(err, os.ErrNotExist) {
|
if errors.Is(err, os.ErrNotExist) {
|
||||||
exitWithConfigurationNotice()
|
exitWithConfigurationNotice()
|
||||||
}
|
}
|
||||||
|
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -141,7 +148,7 @@ func rootCmdRun(*cobra.Command, []string) {
|
||||||
log.Debug("running in debug mode")
|
log.Debug("running in debug mode")
|
||||||
}
|
}
|
||||||
|
|
||||||
if ignoreCertificateErrors {
|
if ok, _ := cmd.Flags().GetBool("ignore-certificate-errors"); ok {
|
||||||
log.Warn("running with --ignore-certificate-errors: TLS certificate host chains and name will not be verified")
|
log.Warn("running with --ignore-certificate-errors: TLS certificate host chains and name will not be verified")
|
||||||
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{
|
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{
|
||||||
InsecureSkipVerify: true,
|
InsecureSkipVerify: true,
|
||||||
|
@ -280,9 +287,15 @@ func rootCmdRun(*cobra.Command, []string) {
|
||||||
log.WithField("error", err).Error("failed to create backup directory")
|
log.WithField("error", err).Error("failed to create backup directory")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
autotls, _ := cmd.Flags().GetBool("auto-tls")
|
||||||
|
tlshostname, _ := cmd.Flags().GetString("tls-hostname")
|
||||||
|
if autotls && tlshostname == "" {
|
||||||
|
autotls = false
|
||||||
|
}
|
||||||
|
|
||||||
log.WithFields(log.Fields{
|
log.WithFields(log.Fields{
|
||||||
"use_ssl": c.Api.Ssl.Enabled,
|
"use_ssl": c.Api.Ssl.Enabled,
|
||||||
"use_auto_tls": useAutomaticTls && len(tlsHostname) > 0,
|
"use_auto_tls": autotls,
|
||||||
"host_address": c.Api.Host,
|
"host_address": c.Api.Host,
|
||||||
"host_port": c.Api.Port,
|
"host_port": c.Api.Port,
|
||||||
}).Info("configuring internal webserver")
|
}).Info("configuring internal webserver")
|
||||||
|
@ -293,7 +306,6 @@ func rootCmdRun(*cobra.Command, []string) {
|
||||||
s := &http.Server{
|
s := &http.Server{
|
||||||
Addr: fmt.Sprintf("%s:%d", c.Api.Host, c.Api.Port),
|
Addr: fmt.Sprintf("%s:%d", c.Api.Host, c.Api.Port),
|
||||||
Handler: r,
|
Handler: r,
|
||||||
|
|
||||||
TLSConfig: &tls.Config{
|
TLSConfig: &tls.Config{
|
||||||
NextProtos: []string{"h2", "http/1.1"},
|
NextProtos: []string{"h2", "http/1.1"},
|
||||||
// @see https://blog.cloudflare.com/exposing-go-on-the-internet
|
// @see https://blog.cloudflare.com/exposing-go-on-the-internet
|
||||||
|
@ -313,14 +325,14 @@ func rootCmdRun(*cobra.Command, []string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if the server should run with TLS but using autocert.
|
// Check if the server should run with TLS but using autocert.
|
||||||
if useAutomaticTls && len(tlsHostname) > 0 {
|
if autotls {
|
||||||
m := autocert.Manager{
|
m := autocert.Manager{
|
||||||
Prompt: autocert.AcceptTOS,
|
Prompt: autocert.AcceptTOS,
|
||||||
Cache: autocert.DirCache(path.Join(c.System.RootDirectory, "/.tls-cache")),
|
Cache: autocert.DirCache(path.Join(c.System.RootDirectory, "/.tls-cache")),
|
||||||
HostPolicy: autocert.HostWhitelist(tlsHostname),
|
HostPolicy: autocert.HostWhitelist(tlshostname),
|
||||||
}
|
}
|
||||||
|
|
||||||
log.WithField("hostname", tlsHostname).
|
log.WithField("hostname", tlshostname).
|
||||||
Info("webserver is now listening with auto-TLS enabled; certificates will be automatically generated by Let's Encrypt")
|
Info("webserver is now listening with auto-TLS enabled; certificates will be automatically generated by Let's Encrypt")
|
||||||
|
|
||||||
// Hook autocert into the main http server.
|
// Hook autocert into the main http server.
|
||||||
|
@ -336,7 +348,7 @@ func rootCmdRun(*cobra.Command, []string) {
|
||||||
|
|
||||||
// Start the main http server with TLS using autocert.
|
// Start the main http server with TLS using autocert.
|
||||||
if err := s.ListenAndServeTLS("", ""); err != nil {
|
if err := s.ListenAndServeTLS("", ""); err != nil {
|
||||||
log.WithFields(log.Fields{"auto_tls": true, "tls_hostname": tlsHostname, "error": err}).
|
log.WithFields(log.Fields{"auto_tls": true, "tls_hostname": tlshostname, "error": err}).
|
||||||
Fatal("failed to configure HTTP server using auto-tls")
|
Fatal("failed to configure HTTP server using auto-tls")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -364,11 +376,6 @@ func rootCmdRun(*cobra.Command, []string) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Execute calls cobra to handle cli commands
|
|
||||||
func Execute() error {
|
|
||||||
return root.Execute()
|
|
||||||
}
|
|
||||||
|
|
||||||
// Configures the global logger for Zap so that we can call it from any location
|
// Configures the global logger for Zap so that we can call it from any location
|
||||||
// in the code without having to pass around a logger instance.
|
// in the code without having to pass around a logger instance.
|
||||||
func configureLogging(logDir string, debug bool) error {
|
func configureLogging(logDir string, debug bool) error {
|
||||||
|
@ -379,20 +386,15 @@ func configureLogging(logDir string, debug bool) error {
|
||||||
p := filepath.Join(logDir, "/wings.log")
|
p := filepath.Join(logDir, "/wings.log")
|
||||||
w, err := logrotate.NewFile(p)
|
w, err := logrotate.NewFile(p)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(errors.WithMessage(err, "failed to open process log file"))
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log.SetLevel(log.InfoLevel)
|
||||||
if debug {
|
if debug {
|
||||||
log.SetLevel(log.DebugLevel)
|
log.SetLevel(log.DebugLevel)
|
||||||
} else {
|
|
||||||
log.SetLevel(log.InfoLevel)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
log.SetHandler(multi.New(
|
log.SetHandler(multi.New(cli.Default, cli.New(w.File, false)))
|
||||||
cli.Default,
|
|
||||||
cli.New(w.File, false),
|
|
||||||
))
|
|
||||||
|
|
||||||
log.WithField("path", p).Info("writing log files to disk")
|
log.WithField("path", p).Info("writing log files to disk")
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|
|
@ -2,5 +2,5 @@ package system
|
||||||
|
|
||||||
var (
|
var (
|
||||||
// The current version of this software.
|
// The current version of this software.
|
||||||
Version = "v0.0.1"
|
Version = "0.0.1"
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user