Add option to strip ansi colors and split the done line in egg startup configuration

This commit is contained in:
Matthew Penner 2020-08-04 17:39:54 -06:00
parent 234fbfa8ec
commit eb975e0cbd
2 changed files with 43 additions and 12 deletions

View File

@ -33,11 +33,15 @@ type ProcessConfiguration struct {
Startup struct { Startup struct {
Done string `json:"done"` Done string `json:"done"`
UserInteraction []string `json:"userInteraction"` UserInteraction []string `json:"userInteraction"`
SplitCharacter string `json:"splitCharacter"`
StripAnsi bool `json:"stripAnsi"`
} `json:"startup"` } `json:"startup"`
Stop struct { Stop struct {
Type string `json:"type"` Type string `json:"type"`
Value string `json:"value"` Value string `json:"value"`
} `json:"stop"` } `json:"stop"`
ConfigurationFiles []parser.ConfigurationFile `json:"configs"` ConfigurationFiles []parser.ConfigurationFile `json:"configs"`
} }

View File

@ -3,6 +3,7 @@ package server
import ( import (
"github.com/apex/log" "github.com/apex/log"
"github.com/pterodactyl/wings/api" "github.com/pterodactyl/wings/api"
"regexp"
"strings" "strings"
) )
@ -21,28 +22,54 @@ func (s *Server) AddEventListeners() {
}() }()
} }
var stripAnsiRegex = regexp.MustCompile("[\u001B\u009B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[a-zA-Z\\d]*)*)?\u0007)|(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PRZcf-ntqry=><~]))")
// Custom listener for console output events that will check if the given line // Custom listener for console output events that will check if the given line
// of output matches one that should mark the server as started or not. // of output matches one that should mark the server as started or not.
func (s *Server) onConsoleOutput(data string) { func (s *Server) onConsoleOutput(data string) {
// Get the server's process configuration.
processConfiguration := s.ProcessConfiguration()
// Check if the server is currently starting.
if s.GetState() == ProcessStartingState {
// If the specific line of output is one that would mark the server as started, // If the specific line of output is one that would mark the server as started,
// set the server to that state. Only do this if the server is not currently stopped // set the server to that state. Only do this if the server is not currently stopped
// or stopping. // or stopping.
match := s.ProcessConfiguration().Startup.Done
if s.GetState() == ProcessStartingState && strings.Contains(data, match) { var matches []string
if processConfiguration.Startup.SplitCharacter != "" {
matches = strings.Split(processConfiguration.Startup.Done, processConfiguration.Startup.SplitCharacter)
} else {
matches = []string{processConfiguration.Startup.Done}
}
// Check if we should strip ansi color codes.
if processConfiguration.Startup.StripAnsi {
// Strip ansi color codes from the data string.
data = stripAnsiRegex.ReplaceAllString(data, "")
}
// Iterate over all the matches.
for _, match := range matches {
if !strings.Contains(data, match) {
continue
}
s.Log().WithFields(log.Fields{ s.Log().WithFields(log.Fields{
"match": match, "match": match,
"against": data, "against": data,
}).Debug("detected server in running state based on console line output") }).Debug("detected server in running state based on console line output")
_ = s.SetState(ProcessRunningState) _ = s.SetState(ProcessRunningState)
break
}
} }
// If the command sent to the server is one that should stop the server we will need to // If the command sent to the server is one that should stop the server we will need to
// set the server to be in a stopping state, otherwise crash detection will kick in and // set the server to be in a stopping state, otherwise crash detection will kick in and
// cause the server to unexpectedly restart on the user. // cause the server to unexpectedly restart on the user.
if s.IsRunning() { if s.IsRunning() {
stop := s.ProcessConfiguration().Stop stop := processConfiguration.Stop
if stop.Type == api.ProcessStopCommand && data == stop.Value { if stop.Type == api.ProcessStopCommand && data == stop.Value {
_ = s.SetState(ProcessStoppingState) _ = s.SetState(ProcessStoppingState)