2020-08-11 04:38:42 +00:00
|
|
|
package docker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/pkg/errors"
|
2020-09-07 22:53:44 +00:00
|
|
|
"github.com/pterodactyl/wings/environment"
|
2020-09-07 20:04:56 +00:00
|
|
|
"strconv"
|
2020-08-11 04:38:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type dockerLogLine struct {
|
|
|
|
Log string `json:"log"`
|
|
|
|
}
|
|
|
|
|
2020-09-27 00:35:11 +00:00
|
|
|
var ErrNotAttached = errors.New("not attached to instance")
|
|
|
|
|
2020-08-11 04:38:42 +00:00
|
|
|
func (e *Environment) setStream(s *types.HijackedResponse) {
|
|
|
|
e.mu.Lock()
|
|
|
|
e.stream = s
|
|
|
|
e.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sends the specified command to the stdin of the running container instance. There is no
|
|
|
|
// confirmation that this data is sent successfully, only that it gets pushed into the stdin.
|
|
|
|
func (e *Environment) SendCommand(c string) error {
|
|
|
|
if !e.IsAttached() {
|
2020-09-27 00:35:11 +00:00
|
|
|
return ErrNotAttached
|
2020-08-11 04:38:42 +00:00
|
|
|
}
|
|
|
|
|
2020-09-27 00:35:11 +00:00
|
|
|
e.mu.RLock()
|
|
|
|
defer e.mu.RUnlock()
|
|
|
|
|
2020-09-07 22:53:44 +00:00
|
|
|
if e.meta.Stop != nil {
|
|
|
|
// If the command being processed is the same as the process stop command then we want to mark
|
|
|
|
// the server as entering the stopping state otherwise the process will stop and Wings will think
|
|
|
|
// it has crashed and attempt to restart it.
|
|
|
|
if e.meta.Stop.Type == "command" && c == e.meta.Stop.Value {
|
|
|
|
e.Events().Publish(environment.StateChangeEvent, environment.ProcessStoppingState)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-11 04:38:42 +00:00
|
|
|
_, err := e.stream.Conn.Write([]byte(c + "\n"))
|
|
|
|
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reads the log file for the server. This does not care if the server is running or not, it will
|
|
|
|
// simply try to read the last X bytes of the file and return them.
|
2020-09-07 20:04:56 +00:00
|
|
|
func (e *Environment) Readlog(lines int) ([]string, error) {
|
|
|
|
r, err := e.client.ContainerLogs(context.Background(), e.Id, types.ContainerLogsOptions{
|
|
|
|
ShowStdout: true,
|
|
|
|
ShowStderr: true,
|
|
|
|
Tail: strconv.Itoa(lines),
|
|
|
|
})
|
2020-08-11 04:38:42 +00:00
|
|
|
if err != nil {
|
2020-09-07 20:04:56 +00:00
|
|
|
return nil, errors.WithStack(err)
|
2020-08-11 04:38:42 +00:00
|
|
|
}
|
2020-09-07 20:04:56 +00:00
|
|
|
defer r.Close()
|
2020-08-11 04:38:42 +00:00
|
|
|
|
2020-09-07 20:04:56 +00:00
|
|
|
var out []string
|
2020-08-11 04:38:42 +00:00
|
|
|
|
2020-09-07 20:04:56 +00:00
|
|
|
scanner := bufio.NewScanner(r)
|
|
|
|
for scanner.Scan() {
|
|
|
|
out = append(out, scanner.Text())
|
2020-08-11 04:38:42 +00:00
|
|
|
}
|
|
|
|
|
2020-09-07 20:04:56 +00:00
|
|
|
return out, nil
|
2020-08-11 04:38:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Docker stores the logs for server output in a JSON format. This function will iterate over the JSON
|
|
|
|
// that was read from the log file and parse it into a more human readable format.
|
|
|
|
func (e *Environment) parseLogToStrings(b []byte) ([]string, error) {
|
|
|
|
var hasError = false
|
|
|
|
var out []string
|
|
|
|
|
|
|
|
scanner := bufio.NewScanner(bytes.NewReader(b))
|
|
|
|
for scanner.Scan() {
|
|
|
|
var l dockerLogLine
|
2020-09-07 20:04:56 +00:00
|
|
|
|
2020-08-11 04:38:42 +00:00
|
|
|
// Unmarshal the contents and allow up to a single error before bailing out of the process. We
|
|
|
|
// do this because if you're arbitrarily reading a length of the file you'll likely end up
|
|
|
|
// with the first line in the output being improperly formatted JSON. In those cases we want to
|
|
|
|
// just skip over it. However if we see another error we're going to bail out because that is an
|
|
|
|
// abnormal situation.
|
|
|
|
if err := json.Unmarshal([]byte(scanner.Text()), &l); err != nil {
|
|
|
|
if hasError {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
hasError = true
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
out = append(out, l.Log)
|
|
|
|
}
|
|
|
|
|
|
|
|
return out, nil
|
|
|
|
}
|