2019-12-28 22:57:19 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
|
|
|
"context"
|
2022-11-06 20:33:01 +00:00
|
|
|
"fmt"
|
2021-01-10 01:22:39 +00:00
|
|
|
"html/template"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2022-02-01 00:09:08 +00:00
|
|
|
"time"
|
2021-01-10 01:22:39 +00:00
|
|
|
|
2020-12-16 05:56:53 +00:00
|
|
|
"emperror.dev/errors"
|
2020-06-13 17:26:35 +00:00
|
|
|
"github.com/apex/log"
|
2019-12-28 22:57:19 +00:00
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/api/types/container"
|
|
|
|
"github.com/docker/docker/api/types/mount"
|
|
|
|
"github.com/docker/docker/client"
|
2022-10-06 15:58:42 +00:00
|
|
|
|
2020-04-17 21:27:06 +00:00
|
|
|
"github.com/pterodactyl/wings/config"
|
2020-08-20 01:58:48 +00:00
|
|
|
"github.com/pterodactyl/wings/environment"
|
2021-02-02 05:28:46 +00:00
|
|
|
"github.com/pterodactyl/wings/remote"
|
2020-12-26 01:04:18 +00:00
|
|
|
"github.com/pterodactyl/wings/system"
|
2019-12-28 22:57:19 +00:00
|
|
|
)
|
|
|
|
|
2022-02-05 16:56:17 +00:00
|
|
|
// Install executes the installation stack for a server process. Bubbles any
|
|
|
|
// errors up to the calling function which should handle contacting the panel to
|
|
|
|
// notify it of the server state.
|
2020-07-01 03:56:55 +00:00
|
|
|
//
|
2022-02-05 16:56:17 +00:00
|
|
|
// Pass true as the first argument in order to execute a server sync before the
|
|
|
|
// process to ensure the latest information is used.
|
2022-11-21 21:57:44 +00:00
|
|
|
func (s *Server) Install() error {
|
|
|
|
return s.install(false)
|
|
|
|
}
|
2020-07-01 03:56:55 +00:00
|
|
|
|
2022-11-21 21:57:44 +00:00
|
|
|
func (s *Server) install(reinstall bool) error {
|
2020-08-30 16:41:14 +00:00
|
|
|
var err error
|
|
|
|
if !s.Config().SkipEggScripts {
|
2022-11-21 21:57:44 +00:00
|
|
|
// Send the start event so the Panel can automatically update. We don't
|
|
|
|
// send this unless the process is actually going to run, otherwise all
|
|
|
|
// sorts of weird rapid UI behavior happens since there isn't an actual
|
|
|
|
// install process being executed.
|
2020-08-30 16:41:14 +00:00
|
|
|
s.Events().Publish(InstallStartedEvent, "")
|
2020-07-30 04:39:27 +00:00
|
|
|
|
2020-08-30 16:41:14 +00:00
|
|
|
err = s.internalInstall()
|
|
|
|
} else {
|
|
|
|
s.Log().Info("server configured to skip running installation scripts for this egg, not executing process")
|
|
|
|
}
|
2020-01-19 21:30:54 +00:00
|
|
|
|
2021-02-10 20:56:48 +00:00
|
|
|
s.Log().WithField("was_successful", err == nil).Debug("notifying panel of server install state")
|
2022-11-21 21:57:44 +00:00
|
|
|
if serr := s.SyncInstallState(err == nil, reinstall); serr != nil {
|
2020-06-23 03:41:45 +00:00
|
|
|
l := s.Log().WithField("was_successful", err == nil)
|
|
|
|
|
2022-11-21 21:57:44 +00:00
|
|
|
// If the request was successful but there was an error with this request,
|
|
|
|
// attach the error to this log entry. Otherwise, ignore it in this log
|
|
|
|
// since whatever is calling this function should handle the error and
|
|
|
|
// will end up logging the same one.
|
2020-06-23 03:41:45 +00:00
|
|
|
if err == nil {
|
2022-02-13 16:59:53 +00:00
|
|
|
l.WithField("error", err)
|
2020-06-23 03:41:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
l.Warn("failed to notify panel of server install state")
|
2020-01-19 21:30:54 +00:00
|
|
|
}
|
|
|
|
|
2022-11-21 21:57:44 +00:00
|
|
|
// Ensure that the server is marked as offline at this point, otherwise you
|
|
|
|
// end up with a blank value which is a bit confusing.
|
2020-11-07 05:53:00 +00:00
|
|
|
s.Environment.SetState(environment.ProcessOfflineState)
|
2020-08-04 23:19:13 +00:00
|
|
|
|
2022-11-21 21:57:44 +00:00
|
|
|
// Push an event to the websocket, so we can auto-refresh the information in
|
|
|
|
// the panel once the installation is completed.
|
2020-07-30 04:39:27 +00:00
|
|
|
s.Events().Publish(InstallCompletedEvent, "")
|
|
|
|
|
2022-02-13 16:59:53 +00:00
|
|
|
return errors.WithStackIf(err)
|
2020-01-19 21:30:54 +00:00
|
|
|
}
|
|
|
|
|
2022-11-21 21:57:44 +00:00
|
|
|
// Reinstall reinstalls a server's software by utilizing the installation script
|
|
|
|
// for the server egg. This does not touch any existing files for the server,
|
|
|
|
// other than what the script modifies.
|
2020-04-03 20:43:13 +00:00
|
|
|
func (s *Server) Reinstall() error {
|
2020-11-20 21:35:29 +00:00
|
|
|
if s.Environment.State() != environment.ProcessOfflineState {
|
2020-06-13 17:26:35 +00:00
|
|
|
s.Log().Debug("waiting for server instance to enter a stopped state")
|
2022-02-01 00:09:08 +00:00
|
|
|
if err := s.Environment.WaitForStop(s.Context(), time.Second*10, true); err != nil {
|
2022-02-13 16:59:53 +00:00
|
|
|
return errors.WrapIf(err, "install: failed to stop running environment")
|
2020-04-03 20:43:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-21 21:57:44 +00:00
|
|
|
s.Log().Info("syncing server state with remote source before executing re-installation process")
|
|
|
|
if err := s.Sync(); err != nil {
|
|
|
|
return errors.WrapIf(err, "install: failed to sync server state with Panel")
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.install(true)
|
2020-04-03 20:43:13 +00:00
|
|
|
}
|
|
|
|
|
2020-01-19 21:30:54 +00:00
|
|
|
// Internal installation function used to simplify reporting back to the Panel.
|
|
|
|
func (s *Server) internalInstall() error {
|
2021-08-02 21:07:00 +00:00
|
|
|
script, err := s.client.GetInstallationScript(s.Context(), s.ID())
|
2020-10-31 17:04:20 +00:00
|
|
|
if err != nil {
|
2021-05-02 22:41:02 +00:00
|
|
|
return err
|
2019-12-28 22:57:19 +00:00
|
|
|
}
|
|
|
|
p, err := NewInstallationProcess(s, &script)
|
|
|
|
if err != nil {
|
2020-11-28 23:57:10 +00:00
|
|
|
return err
|
2019-12-28 22:57:19 +00:00
|
|
|
}
|
|
|
|
|
2020-06-13 17:26:35 +00:00
|
|
|
s.Log().Info("beginning installation process for server")
|
2020-01-19 21:30:54 +00:00
|
|
|
if err := p.Run(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-01-19 21:05:49 +00:00
|
|
|
|
2020-06-13 17:26:35 +00:00
|
|
|
s.Log().Info("completed installation process for server")
|
2019-12-28 23:12:12 +00:00
|
|
|
return nil
|
2019-12-28 22:57:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type InstallationProcess struct {
|
|
|
|
Server *Server
|
2021-02-02 05:28:46 +00:00
|
|
|
Script *remote.InstallationScript
|
2022-02-05 16:56:17 +00:00
|
|
|
client *client.Client
|
2019-12-28 22:57:19 +00:00
|
|
|
}
|
|
|
|
|
2022-11-21 21:57:44 +00:00
|
|
|
// NewInstallationProcess returns a new installation process struct that will be
|
|
|
|
// used to create containers and otherwise perform installation commands for a
|
|
|
|
// server.
|
2021-02-02 05:28:46 +00:00
|
|
|
func NewInstallationProcess(s *Server, script *remote.InstallationScript) (*InstallationProcess, error) {
|
2019-12-28 22:57:19 +00:00
|
|
|
proc := &InstallationProcess{
|
|
|
|
Script: script,
|
|
|
|
Server: s,
|
|
|
|
}
|
|
|
|
|
2021-01-15 04:19:28 +00:00
|
|
|
if c, err := environment.Docker(); err != nil {
|
2020-11-28 23:57:10 +00:00
|
|
|
return nil, err
|
2019-12-28 22:57:19 +00:00
|
|
|
} else {
|
|
|
|
proc.client = c
|
|
|
|
}
|
|
|
|
|
|
|
|
return proc, nil
|
|
|
|
}
|
|
|
|
|
2022-11-21 21:57:44 +00:00
|
|
|
// IsInstalling returns if the server is actively running the installation
|
|
|
|
// process by checking the status of the installer lock.
|
2020-06-23 04:38:16 +00:00
|
|
|
func (s *Server) IsInstalling() bool {
|
2020-12-26 01:04:18 +00:00
|
|
|
return s.installing.Load()
|
2020-06-23 04:38:16 +00:00
|
|
|
}
|
|
|
|
|
2020-12-25 21:32:41 +00:00
|
|
|
func (s *Server) IsTransferring() bool {
|
2020-12-26 01:04:18 +00:00
|
|
|
return s.transferring.Load()
|
2020-12-25 21:32:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) SetTransferring(state bool) {
|
2020-12-26 01:04:18 +00:00
|
|
|
s.transferring.Store(state)
|
2020-12-25 21:32:41 +00:00
|
|
|
}
|
|
|
|
|
2021-03-12 23:19:35 +00:00
|
|
|
func (s *Server) IsRestoring() bool {
|
|
|
|
return s.restoring.Load()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) SetRestoring(state bool) {
|
|
|
|
s.restoring.Store(state)
|
|
|
|
}
|
|
|
|
|
2022-11-21 21:57:44 +00:00
|
|
|
// RemoveContainer removes the installation container for the server.
|
2020-12-26 01:04:18 +00:00
|
|
|
func (ip *InstallationProcess) RemoveContainer() error {
|
2022-02-05 16:56:17 +00:00
|
|
|
err := ip.client.ContainerRemove(ip.Server.Context(), ip.Server.ID()+"_installer", types.ContainerRemoveOptions{
|
2020-05-10 02:57:29 +00:00
|
|
|
RemoveVolumes: true,
|
|
|
|
Force: true,
|
|
|
|
})
|
|
|
|
if err != nil && !client.IsErrNotFound(err) {
|
2020-12-26 01:04:18 +00:00
|
|
|
return err
|
2020-05-10 02:57:29 +00:00
|
|
|
}
|
2020-12-26 01:04:18 +00:00
|
|
|
return nil
|
2020-05-10 02:57:29 +00:00
|
|
|
}
|
|
|
|
|
2022-02-05 16:56:17 +00:00
|
|
|
// Run runs the installation process, this is done as in a background thread.
|
|
|
|
// This will configure the required environment, and then spin up the
|
|
|
|
// installation container. Once the container finishes installing the results
|
|
|
|
// are stored in an installation log in the server's configuration directory.
|
2020-01-19 21:05:49 +00:00
|
|
|
func (ip *InstallationProcess) Run() error {
|
2020-06-23 04:38:16 +00:00
|
|
|
ip.Server.Log().Debug("acquiring installation process lock")
|
2020-12-26 01:04:18 +00:00
|
|
|
if !ip.Server.installing.SwapIf(true) {
|
|
|
|
return errors.New("install: cannot obtain installation lock")
|
2020-06-23 04:38:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// We now have an exclusive lock on this installation process. Ensure that whenever this
|
|
|
|
// process is finished that the semaphore is released so that other processes and be executed
|
2020-09-05 19:08:40 +00:00
|
|
|
// without encountering a wait timeout.
|
2020-06-23 04:38:16 +00:00
|
|
|
defer func() {
|
|
|
|
ip.Server.Log().Debug("releasing installation process lock")
|
2020-12-26 01:04:18 +00:00
|
|
|
ip.Server.installing.Store(false)
|
2020-06-23 04:38:16 +00:00
|
|
|
}()
|
|
|
|
|
2020-08-24 03:45:07 +00:00
|
|
|
if err := ip.BeforeExecute(); err != nil {
|
2020-11-28 23:57:10 +00:00
|
|
|
return err
|
2020-01-19 21:05:49 +00:00
|
|
|
}
|
2020-01-18 21:05:44 +00:00
|
|
|
|
2021-02-10 20:56:48 +00:00
|
|
|
cID, err := ip.Execute()
|
2020-01-19 21:05:49 +00:00
|
|
|
if err != nil {
|
2021-02-10 20:56:48 +00:00
|
|
|
_ = ip.RemoveContainer()
|
2020-11-28 23:57:10 +00:00
|
|
|
return err
|
2020-01-18 21:05:44 +00:00
|
|
|
}
|
|
|
|
|
2020-01-19 21:05:49 +00:00
|
|
|
// If this step fails, log a warning but don't exit out of the process. This is completely
|
|
|
|
// internal to the daemon's functionality, and does not affect the status of the server itself.
|
2021-02-10 20:56:48 +00:00
|
|
|
if err := ip.AfterExecute(cID); err != nil {
|
2020-06-13 17:26:35 +00:00
|
|
|
ip.Server.Log().WithField("error", err).Warn("failed to complete after-execute step of installation process")
|
2020-01-18 21:05:44 +00:00
|
|
|
}
|
|
|
|
|
2020-01-19 21:05:49 +00:00
|
|
|
return nil
|
2020-01-18 21:05:44 +00:00
|
|
|
}
|
|
|
|
|
2020-08-24 03:45:07 +00:00
|
|
|
// Returns the location of the temporary data for the installation process.
|
|
|
|
func (ip *InstallationProcess) tempDir() string {
|
2022-02-13 16:59:53 +00:00
|
|
|
return filepath.Join(config.Get().System.TmpDirectory, ip.Server.ID())
|
2020-08-24 03:45:07 +00:00
|
|
|
}
|
|
|
|
|
2019-12-28 22:57:19 +00:00
|
|
|
// Writes the installation script to a temporary file on the host machine so that it
|
|
|
|
// can be properly mounted into the installation container and then executed.
|
2020-08-24 03:45:07 +00:00
|
|
|
func (ip *InstallationProcess) writeScriptToDisk() error {
|
2020-05-04 04:04:16 +00:00
|
|
|
// Make sure the temp directory root exists before trying to make a directory within it. The
|
|
|
|
// ioutil.TempDir call expects this base to exist, it won't create it for you.
|
2021-11-15 17:37:56 +00:00
|
|
|
if err := os.MkdirAll(ip.tempDir(), 0o700); err != nil {
|
2020-11-28 23:57:10 +00:00
|
|
|
return errors.WithMessage(err, "could not create temporary directory for install process")
|
2020-05-04 04:04:16 +00:00
|
|
|
}
|
|
|
|
|
2021-11-15 17:37:56 +00:00
|
|
|
f, err := os.OpenFile(filepath.Join(ip.tempDir(), "install.sh"), os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0o644)
|
2019-12-28 22:57:19 +00:00
|
|
|
if err != nil {
|
2020-11-28 23:57:10 +00:00
|
|
|
return errors.WithMessage(err, "failed to write server installation script to disk before mount")
|
2019-12-28 22:57:19 +00:00
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
w := bufio.NewWriter(f)
|
|
|
|
|
|
|
|
scanner := bufio.NewScanner(bytes.NewReader([]byte(ip.Script.Script)))
|
|
|
|
for scanner.Scan() {
|
2019-12-28 23:12:12 +00:00
|
|
|
w.WriteString(scanner.Text() + "\n")
|
2019-12-28 22:57:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := scanner.Err(); err != nil {
|
2020-11-28 23:57:10 +00:00
|
|
|
return err
|
2019-12-28 22:57:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
w.Flush()
|
|
|
|
|
2020-08-24 03:45:07 +00:00
|
|
|
return nil
|
2019-12-28 22:57:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Pulls the docker image to be used for the installation container.
|
|
|
|
func (ip *InstallationProcess) pullInstallationImage() error {
|
2020-11-20 21:36:29 +00:00
|
|
|
// Get a registry auth configuration from the config.
|
|
|
|
var registryAuth *config.RegistryConfiguration
|
|
|
|
for registry, c := range config.Get().Docker.Registries {
|
|
|
|
if !strings.HasPrefix(ip.Script.ContainerImage, registry) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
log.WithField("registry", registry).Debug("using authentication for registry")
|
|
|
|
registryAuth = &c
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the ImagePullOptions.
|
|
|
|
imagePullOptions := types.ImagePullOptions{All: false}
|
|
|
|
if registryAuth != nil {
|
|
|
|
b64, err := registryAuth.Base64()
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Error("failed to get registry auth credentials")
|
|
|
|
}
|
|
|
|
|
|
|
|
// b64 is a string so if there is an error it will just be empty, not nil.
|
|
|
|
imagePullOptions.RegistryAuth = b64
|
|
|
|
}
|
|
|
|
|
2022-02-05 16:56:17 +00:00
|
|
|
r, err := ip.client.ImagePull(ip.Server.Context(), ip.Script.ContainerImage, imagePullOptions)
|
2019-12-28 22:57:19 +00:00
|
|
|
if err != nil {
|
2022-02-05 16:56:17 +00:00
|
|
|
images, ierr := ip.client.ImageList(ip.Server.Context(), types.ImageListOptions{})
|
2020-11-20 21:36:29 +00:00
|
|
|
if ierr != nil {
|
|
|
|
// Well damn, something has gone really wrong here, just go ahead and abort there
|
|
|
|
// isn't much anything we can do to try and self-recover from this.
|
|
|
|
return ierr
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, img := range images {
|
|
|
|
for _, t := range img.RepoTags {
|
|
|
|
if t != ip.Script.ContainerImage {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"image": ip.Script.ContainerImage,
|
|
|
|
"err": err.Error(),
|
|
|
|
}).Warn("unable to pull requested image from remote source, however the image exists locally")
|
|
|
|
|
|
|
|
// Okay, we found a matching container image, in that case just go ahead and return
|
|
|
|
// from this function, since there is nothing else we need to do here.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
2019-12-28 22:57:19 +00:00
|
|
|
}
|
2020-11-20 21:36:29 +00:00
|
|
|
defer r.Close()
|
|
|
|
|
|
|
|
log.WithField("image", ip.Script.ContainerImage).Debug("pulling docker image... this could take a bit of time")
|
2019-12-28 22:57:19 +00:00
|
|
|
|
2019-12-28 23:14:56 +00:00
|
|
|
// Block continuation until the image has been pulled successfully.
|
|
|
|
scanner := bufio.NewScanner(r)
|
|
|
|
for scanner.Scan() {
|
2020-06-13 17:26:35 +00:00
|
|
|
log.Debug(scanner.Text())
|
2019-12-28 23:14:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := scanner.Err(); err != nil {
|
2020-11-28 23:57:10 +00:00
|
|
|
return err
|
2019-12-28 22:57:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-02-05 16:56:17 +00:00
|
|
|
// BeforeExecute runs before the container is executed. This pulls down the
|
|
|
|
// required docker container image as well as writes the installation script to
|
|
|
|
// the disk. This process is executed in an async manner, if either one fails
|
|
|
|
// the error is returned.
|
2020-08-24 03:45:07 +00:00
|
|
|
func (ip *InstallationProcess) BeforeExecute() error {
|
|
|
|
if err := ip.writeScriptToDisk(); err != nil {
|
2020-11-28 23:57:10 +00:00
|
|
|
return errors.WithMessage(err, "failed to write installation script to disk")
|
2020-08-23 21:07:03 +00:00
|
|
|
}
|
|
|
|
if err := ip.pullInstallationImage(); err != nil {
|
2020-11-28 23:57:10 +00:00
|
|
|
return errors.WithMessage(err, "failed to pull updated installation container image for server")
|
2020-08-23 21:07:03 +00:00
|
|
|
}
|
2020-12-26 01:04:18 +00:00
|
|
|
if err := ip.RemoveContainer(); err != nil {
|
|
|
|
return errors.WithMessage(err, "failed to remove existing install container for server")
|
2019-12-28 22:57:19 +00:00
|
|
|
}
|
2020-08-24 03:45:07 +00:00
|
|
|
return nil
|
2019-12-28 22:57:19 +00:00
|
|
|
}
|
|
|
|
|
2022-11-21 21:57:44 +00:00
|
|
|
// GetLogPath returns the log path for the installation process.
|
2020-04-17 21:27:06 +00:00
|
|
|
func (ip *InstallationProcess) GetLogPath() string {
|
2021-08-02 21:07:00 +00:00
|
|
|
return filepath.Join(config.Get().System.LogDirectory, "/install", ip.Server.ID()+".log")
|
2020-04-17 21:27:06 +00:00
|
|
|
}
|
|
|
|
|
2022-11-21 21:57:44 +00:00
|
|
|
// AfterExecute cleans up after the execution of the installation process.
|
|
|
|
// This grabs the logs from the process to store in the server configuration
|
|
|
|
// directory, and then destroys the associated installation container.
|
2020-01-19 21:05:49 +00:00
|
|
|
func (ip *InstallationProcess) AfterExecute(containerId string) error {
|
2020-05-10 02:57:29 +00:00
|
|
|
defer ip.RemoveContainer()
|
2020-01-19 21:05:49 +00:00
|
|
|
|
2020-06-13 17:26:35 +00:00
|
|
|
ip.Server.Log().WithField("container_id", containerId).Debug("pulling installation logs for server")
|
2022-02-05 16:56:17 +00:00
|
|
|
reader, err := ip.client.ContainerLogs(ip.Server.Context(), containerId, types.ContainerLogsOptions{
|
2020-01-19 21:05:49 +00:00
|
|
|
ShowStdout: true,
|
|
|
|
ShowStderr: true,
|
|
|
|
Follow: false,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil && !client.IsErrNotFound(err) {
|
2020-11-28 23:57:10 +00:00
|
|
|
return err
|
2020-01-19 21:05:49 +00:00
|
|
|
}
|
|
|
|
|
2021-11-15 17:37:56 +00:00
|
|
|
f, err := os.OpenFile(ip.GetLogPath(), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o600)
|
2020-01-19 21:05:49 +00:00
|
|
|
if err != nil {
|
2020-11-28 23:57:10 +00:00
|
|
|
return err
|
2020-01-19 21:05:49 +00:00
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
// We write the contents of the container output to a more "permanent" file so that they
|
2020-07-01 03:45:36 +00:00
|
|
|
// can be referenced after this container is deleted. We'll also include the environment
|
|
|
|
// variables passed into the container to make debugging things a little easier.
|
|
|
|
ip.Server.Log().WithField("path", ip.GetLogPath()).Debug("writing most recent installation logs to disk")
|
|
|
|
|
|
|
|
tmpl, err := template.New("header").Parse(`Pterodactyl Server Installation Log
|
|
|
|
|
|
|
|
|
|
|
|
|
| Details
|
|
|
|
| ------------------------------
|
2021-08-02 21:07:00 +00:00
|
|
|
Server UUID: {{.Server.ID}}
|
2020-07-01 03:45:36 +00:00
|
|
|
Container Image: {{.Script.ContainerImage}}
|
|
|
|
Container Entrypoint: {{.Script.Entrypoint}}
|
|
|
|
|
|
|
|
|
|
|
|
|
| Environment Variables
|
|
|
|
| ------------------------------
|
|
|
|
{{ range $key, $value := .Server.GetEnvironmentVariables }} {{ $value }}
|
|
|
|
{{ end }}
|
|
|
|
|
|
|
|
|
|
|
|
|
| Script Output
|
|
|
|
| ------------------------------
|
|
|
|
`)
|
|
|
|
if err != nil {
|
2020-11-28 23:57:10 +00:00
|
|
|
return err
|
2020-07-01 03:45:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := tmpl.Execute(f, ip); err != nil {
|
2020-11-28 23:57:10 +00:00
|
|
|
return err
|
2020-07-01 03:45:36 +00:00
|
|
|
}
|
|
|
|
|
2020-01-19 21:05:49 +00:00
|
|
|
if _, err := io.Copy(f, reader); err != nil {
|
2020-11-28 23:57:10 +00:00
|
|
|
return err
|
2020-01-19 21:05:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-02-05 16:56:17 +00:00
|
|
|
// Execute executes the installation process inside a specially created docker
|
|
|
|
// container.
|
2020-08-24 03:45:07 +00:00
|
|
|
func (ip *InstallationProcess) Execute() (string, error) {
|
2020-12-26 01:04:18 +00:00
|
|
|
// Create a child context that is canceled once this function is done running. This
|
|
|
|
// will also be canceled if the parent context (from the Server struct) is canceled
|
|
|
|
// which occurs if the server is deleted.
|
2022-02-05 16:56:17 +00:00
|
|
|
ctx, cancel := context.WithCancel(ip.Server.Context())
|
2020-12-26 01:04:18 +00:00
|
|
|
defer cancel()
|
|
|
|
|
2019-12-28 22:57:19 +00:00
|
|
|
conf := &container.Config{
|
|
|
|
Hostname: "installer",
|
|
|
|
AttachStdout: true,
|
|
|
|
AttachStderr: true,
|
|
|
|
AttachStdin: true,
|
|
|
|
OpenStdin: true,
|
|
|
|
Tty: true,
|
2020-10-01 22:00:26 +00:00
|
|
|
Cmd: []string{ip.Script.Entrypoint, "/mnt/install/install.sh"},
|
2019-12-28 22:57:19 +00:00
|
|
|
Image: ip.Script.ContainerImage,
|
|
|
|
Env: ip.Server.GetEnvironmentVariables(),
|
|
|
|
Labels: map[string]string{
|
|
|
|
"Service": "Pterodactyl",
|
|
|
|
"ContainerType": "server_installer",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-11-06 20:33:01 +00:00
|
|
|
cfg := config.Get()
|
|
|
|
if cfg.System.User.Rootless.Enabled {
|
|
|
|
conf.User = fmt.Sprintf("%d:%d", cfg.System.User.Rootless.ContainerUID, cfg.System.User.Rootless.ContainerGID)
|
|
|
|
}
|
|
|
|
|
|
|
|
tmpfsSize := strconv.Itoa(int(cfg.Docker.TmpfsSize))
|
2019-12-28 22:57:19 +00:00
|
|
|
hostConf := &container.HostConfig{
|
|
|
|
Mounts: []mount.Mount{
|
|
|
|
{
|
|
|
|
Target: "/mnt/server",
|
2020-09-27 19:24:08 +00:00
|
|
|
Source: ip.Server.Filesystem().Path(),
|
2019-12-28 22:57:19 +00:00
|
|
|
Type: mount.TypeBind,
|
|
|
|
ReadOnly: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Target: "/mnt/install",
|
2020-08-24 03:45:07 +00:00
|
|
|
Source: ip.tempDir(),
|
2019-12-28 22:57:19 +00:00
|
|
|
Type: mount.TypeBind,
|
|
|
|
ReadOnly: false,
|
|
|
|
},
|
|
|
|
},
|
2021-06-21 00:21:51 +00:00
|
|
|
Resources: ip.resourceLimits(),
|
2019-12-28 22:57:19 +00:00
|
|
|
Tmpfs: map[string]string{
|
2020-08-23 21:07:03 +00:00
|
|
|
"/tmp": "rw,exec,nosuid,size=" + tmpfsSize + "M",
|
2019-12-28 22:57:19 +00:00
|
|
|
},
|
2022-11-06 20:33:01 +00:00
|
|
|
DNS: cfg.Docker.Network.Dns,
|
|
|
|
LogConfig: cfg.Docker.ContainerLogConfig(),
|
2019-12-28 22:57:19 +00:00
|
|
|
Privileged: true,
|
2022-11-06 20:33:01 +00:00
|
|
|
NetworkMode: container.NetworkMode(cfg.Docker.Network.Mode),
|
|
|
|
UsernsMode: container.UsernsMode(cfg.Docker.UsernsMode),
|
2019-12-28 22:57:19 +00:00
|
|
|
}
|
|
|
|
|
2021-01-22 04:58:52 +00:00
|
|
|
// Ensure the root directory for the server exists properly before attempting
|
|
|
|
// to trigger the reinstall of the server. It is possible the directory would
|
|
|
|
// not exist when this runs if Wings boots with a missing directory and a user
|
|
|
|
// triggers a reinstall before trying to start the server.
|
|
|
|
if err := ip.Server.EnsureDataDirectoryExists(); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2020-08-24 03:45:07 +00:00
|
|
|
ip.Server.Log().WithField("install_script", ip.tempDir()+"/install.sh").Info("creating install container for server process")
|
|
|
|
// Remove the temporary directory when the installation process finishes for this server container.
|
|
|
|
defer func() {
|
|
|
|
if err := os.RemoveAll(ip.tempDir()); err != nil {
|
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
ip.Server.Log().WithField("error", err).Warn("failed to remove temporary data directory after install process")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2021-08-02 21:07:00 +00:00
|
|
|
r, err := ip.client.ContainerCreate(ctx, conf, hostConf, nil, nil, ip.Server.ID()+"_installer")
|
2019-12-28 22:57:19 +00:00
|
|
|
if err != nil {
|
2020-11-28 23:57:10 +00:00
|
|
|
return "", err
|
2019-12-28 22:57:19 +00:00
|
|
|
}
|
|
|
|
|
2020-06-13 17:26:35 +00:00
|
|
|
ip.Server.Log().WithField("container_id", r.ID).Info("running installation script for server in container")
|
2020-12-26 01:04:18 +00:00
|
|
|
if err := ip.client.ContainerStart(ctx, r.ID, types.ContainerStartOptions{}); err != nil {
|
2020-01-18 21:05:44 +00:00
|
|
|
return "", err
|
2019-12-28 22:57:19 +00:00
|
|
|
}
|
|
|
|
|
2020-12-26 01:04:18 +00:00
|
|
|
// Process the install event in the background by listening to the stream output until the
|
|
|
|
// container has stopped, at which point we'll disconnect from it.
|
|
|
|
//
|
|
|
|
// If there is an error during the streaming output just report it and do nothing else, the
|
|
|
|
// install can still run, the console just won't have any output.
|
2020-01-18 21:05:44 +00:00
|
|
|
go func(id string) {
|
2020-01-18 22:04:26 +00:00
|
|
|
ip.Server.Events().Publish(DaemonMessageEvent, "Starting installation process, this could take a few minutes...")
|
2020-12-26 01:04:18 +00:00
|
|
|
if err := ip.StreamOutput(ctx, id); err != nil {
|
|
|
|
ip.Server.Log().WithField("error", err).Warn("error connecting to server install stream output")
|
2020-01-18 21:05:44 +00:00
|
|
|
}
|
|
|
|
}(r.ID)
|
|
|
|
|
2020-12-26 01:04:18 +00:00
|
|
|
sChan, eChan := ip.client.ContainerWait(ctx, r.ID, container.WaitConditionNotRunning)
|
2019-12-28 23:12:12 +00:00
|
|
|
select {
|
2020-09-05 19:08:40 +00:00
|
|
|
case err := <-eChan:
|
2020-12-26 01:04:18 +00:00
|
|
|
// Once the container has stopped running we can mark the install process as being completed.
|
|
|
|
if err == nil {
|
|
|
|
ip.Server.Events().Publish(DaemonMessageEvent, "Installation process completed.")
|
|
|
|
} else {
|
2020-11-28 23:57:10 +00:00
|
|
|
return "", err
|
2019-12-28 23:12:12 +00:00
|
|
|
}
|
2020-09-05 19:08:40 +00:00
|
|
|
case <-sChan:
|
2019-12-28 22:57:19 +00:00
|
|
|
}
|
|
|
|
|
2020-01-18 21:05:44 +00:00
|
|
|
return r.ID, nil
|
|
|
|
}
|
|
|
|
|
2022-01-23 14:57:25 +00:00
|
|
|
// StreamOutput streams the output of the installation process to a log file in
|
|
|
|
// the server configuration directory, as well as to a websocket listener so
|
|
|
|
// that the process can be viewed in the panel by administrators.
|
2020-12-26 01:04:18 +00:00
|
|
|
func (ip *InstallationProcess) StreamOutput(ctx context.Context, id string) error {
|
2022-02-05 16:56:17 +00:00
|
|
|
opts := types.ContainerLogsOptions{ShowStdout: true, ShowStderr: true, Follow: true}
|
|
|
|
reader, err := ip.client.ContainerLogs(ctx, id, opts)
|
2020-01-18 21:05:44 +00:00
|
|
|
if err != nil {
|
2020-11-28 23:57:10 +00:00
|
|
|
return err
|
2020-01-18 21:05:44 +00:00
|
|
|
}
|
|
|
|
defer reader.Close()
|
|
|
|
|
2022-02-03 00:16:34 +00:00
|
|
|
err = system.ScanReader(reader, ip.Server.Sink(system.InstallSink).Push)
|
2022-02-05 16:56:17 +00:00
|
|
|
if err != nil && !errors.Is(err, context.Canceled) {
|
2020-12-26 01:05:01 +00:00
|
|
|
ip.Server.Log().WithFields(log.Fields{"container_id": id, "error": err}).Warn("error processing install output lines")
|
2020-01-18 21:05:44 +00:00
|
|
|
}
|
2019-12-28 22:57:19 +00:00
|
|
|
return nil
|
|
|
|
}
|
2020-01-19 21:30:54 +00:00
|
|
|
|
2022-11-21 21:57:44 +00:00
|
|
|
// resourceLimits returns resource limits for the installation container. This
|
2021-06-21 00:21:51 +00:00
|
|
|
// looks at the globally defined install container limits and attempts to use
|
|
|
|
// the higher of the two (defined limits & server limits). This allows for servers
|
|
|
|
// with super low limits (e.g. Discord bots with 128Mb of memory) to perform more
|
|
|
|
// intensive installation processes if needed.
|
|
|
|
//
|
|
|
|
// This also avoids a server with limits such as 4GB of memory from accidentally
|
|
|
|
// consuming 2-5x the defined limits during the install process and causing
|
|
|
|
// system instability.
|
|
|
|
func (ip *InstallationProcess) resourceLimits() container.Resources {
|
|
|
|
limits := config.Get().Docker.InstallerLimits
|
|
|
|
|
2022-11-21 21:57:44 +00:00
|
|
|
// Create a copy of the configuration, so we're not accidentally making
|
|
|
|
// changes to the underlying server build data.
|
2021-06-21 00:21:51 +00:00
|
|
|
c := *ip.Server.Config()
|
|
|
|
cfg := c.Build
|
|
|
|
if cfg.MemoryLimit < limits.Memory {
|
|
|
|
cfg.MemoryLimit = limits.Memory
|
|
|
|
}
|
|
|
|
// Only apply the CPU limit if neither one is currently set to unlimited. If the
|
|
|
|
// installer CPU limit is unlimited don't even waste time with the logic, just
|
|
|
|
// set the config to unlimited for this.
|
|
|
|
if limits.Cpu == 0 {
|
|
|
|
cfg.CpuLimit = 0
|
|
|
|
} else if cfg.CpuLimit != 0 && cfg.CpuLimit < limits.Cpu {
|
|
|
|
cfg.CpuLimit = limits.Cpu
|
|
|
|
}
|
|
|
|
|
|
|
|
resources := cfg.AsContainerResources()
|
|
|
|
// Explicitly remove the PID limits for the installation container. These scripts are
|
|
|
|
// defined at an administrative level and users can't manually execute things like a
|
|
|
|
// fork bomb during this process.
|
|
|
|
resources.PidsLimit = nil
|
|
|
|
|
|
|
|
return resources
|
|
|
|
}
|
|
|
|
|
2022-11-21 21:57:44 +00:00
|
|
|
// SyncInstallState makes an HTTP request to the Panel instance notifying it that
|
2021-05-02 22:41:02 +00:00
|
|
|
// the server has completed the installation process, and what the state of the
|
2022-11-21 21:57:44 +00:00
|
|
|
// server is.
|
|
|
|
func (s *Server) SyncInstallState(successful, reinstall bool) error {
|
|
|
|
return s.client.SetInstallationStatus(s.Context(), s.ID(), remote.InstallStatusRequest{
|
|
|
|
Successful: successful,
|
|
|
|
Reinstall: reinstall,
|
|
|
|
})
|
2020-01-19 21:30:54 +00:00
|
|
|
}
|