Change default config location (again); support auto-locating and moving old configs

This commit is contained in:
Dane Everitt 2020-05-09 15:37:49 -07:00
parent 483b652087
commit fd83424ee2
No known key found for this signature in database
GPG Key ID: EEA66103B3D71F53
3 changed files with 94 additions and 2 deletions

60
cmd/config_finder.go Normal file
View File

@ -0,0 +1,60 @@
package cmd
import (
"github.com/pterodactyl/wings/config"
"os"
"path/filepath"
)
// We've gone through a couple of iterations of where the configuration is stored. This
// helpful little function will look through the three areas it might have ended up, and
// return it.
//
// We only run this if the configuration flag for the instance is not actually passed in
// via the command line. Once found, the configuration is moved into the expected default
// location. Only errors are returned from this function, you can safely assume that after
// running this the configuration can be found in the correct default location.
func RelocateConfiguration() error {
var match string
check := []string{
config.DefaultLocation,
"/var/lib/pterodactyl/config.yml",
"/etc/wings/config.yml",
}
// Loop over all of the configuration paths, and return which one we found, if
// any.
for _, p := range check {
if s, err := os.Stat(p); err != nil {
if !os.IsNotExist(err) {
return err
}
} else if !s.IsDir() {
match = p
break
}
}
// Just return a generic not exist error at this point if we didn't have a match, this
// will allow the caller to handle displaying a more friendly error to the user. If we
// did match in the default location, go ahead and return successfully.
if match == "" {
return os.ErrNotExist
} else if match == config.DefaultLocation {
return nil
}
// The rest of this function simply creates the new default location and moves the
// old configuration file over to the new location, then sets the permissions on the
// file correctly so that only the user running this process can read it.
p, _ := filepath.Split(config.DefaultLocation)
if err := os.MkdirAll(p, 0755); err != nil {
return err
}
if err := os.Rename(match, config.DefaultLocation); err != nil {
return err
}
return os.Chmod(config.DefaultLocation, 0600)
}

View File

@ -3,6 +3,7 @@ package cmd
import ( import (
"crypto/tls" "crypto/tls"
"fmt" "fmt"
"github.com/mitchellh/colorstring"
"net/http" "net/http"
"os" "os"
"path" "path"
@ -62,11 +63,22 @@ func readConfiguration() (*config.Configuration, error) {
} }
func rootCmdRun(*cobra.Command, []string) { func rootCmdRun(*cobra.Command, []string) {
// Profile wings in production!!!!
if shouldRunProfiler { if shouldRunProfiler {
defer profile.Start().Stop() defer profile.Start().Stop()
} }
// Only attempt configuration file relocation if a custom location has not
// been specified in the command startup.
if configPath == config.DefaultLocation {
if err := RelocateConfiguration(); err != nil {
if os.IsNotExist(err) {
exitWithConfigurationNotice()
}
panic(err)
}
}
c, err := readConfiguration() c, err := readConfiguration()
if err != nil { if err != nil {
panic(err) panic(err)
@ -259,3 +271,23 @@ func printLogo() {
fmt.Println(`Copyright © 2018 - 2020 Dane Everitt & Contributors`) fmt.Println(`Copyright © 2018 - 2020 Dane Everitt & Contributors`)
fmt.Println() fmt.Println()
} }
func exitWithConfigurationNotice() {
fmt.Print(colorstring.Color(`
[_red_][white][bold]Error: Configuration File Not Found[reset]
Wings was not able to locate your configuration file, and therefore is not
able to complete its boot process.
Please ensure you have copied your instance configuration file into
the default location, or have provided the --config flag to use a
custom location.
Default Location: /etc/pterodactyl/config.yml
[yellow]This is not a bug with this software. Please do not make a bug report
for this issue, it will be closed.[reset]
`))
os.Exit(1)
}

View File

@ -19,7 +19,7 @@ import (
"sync" "sync"
) )
const DefaultLocation = "/var/lib/pterodactyl/config.yml" const DefaultLocation = "/etc/pterodactyl/config.yml"
type Configuration struct { type Configuration struct {
sync.RWMutex `json:"-" yaml:"-"` sync.RWMutex `json:"-" yaml:"-"`