2019-12-28 22:57:19 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
|
|
|
"context"
|
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"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/pterodactyl/wings/api"
|
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"
|
2020-06-23 04:38:16 +00:00
|
|
|
"golang.org/x/sync/semaphore"
|
2020-07-01 03:45:36 +00:00
|
|
|
"html/template"
|
2020-01-19 21:05:49 +00:00
|
|
|
"io"
|
2019-12-28 22:57:19 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2020-08-13 03:38:02 +00:00
|
|
|
"strconv"
|
2020-06-23 04:38:16 +00:00
|
|
|
"time"
|
2019-12-28 22:57:19 +00:00
|
|
|
)
|
|
|
|
|
2020-01-19 21:30:54 +00:00
|
|
|
// 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
|
|
|
//
|
|
|
|
// Pass true as the first arugment in order to execute a server sync before the process to
|
|
|
|
// ensure the latest information is used.
|
|
|
|
func (s *Server) Install(sync bool) error {
|
|
|
|
if sync {
|
|
|
|
s.Log().Info("syncing server state with remote source before executing installation process")
|
|
|
|
if err := s.Sync(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-30 04:39:27 +00:00
|
|
|
// Send the start event so the Panel can automatically update.
|
|
|
|
s.Events().Publish(InstallStartedEvent, "")
|
|
|
|
|
2020-01-19 21:30:54 +00:00
|
|
|
err := s.internalInstall()
|
|
|
|
|
2020-06-13 17:26:35 +00:00
|
|
|
s.Log().Debug("notifying panel of server install state")
|
2020-01-19 21:30:54 +00:00
|
|
|
if serr := s.SyncInstallState(err == nil); serr != nil {
|
2020-06-23 03:41:45 +00:00
|
|
|
l := s.Log().WithField("was_successful", err == nil)
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
if err == nil {
|
|
|
|
l.WithField("error", serr)
|
|
|
|
}
|
|
|
|
|
|
|
|
l.Warn("failed to notify panel of server install state")
|
2020-01-19 21:30:54 +00:00
|
|
|
}
|
|
|
|
|
2020-08-20 02:08:15 +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.
|
|
|
|
s.SetState(environment.ProcessOfflineState)
|
2020-08-04 23:19:13 +00:00
|
|
|
|
2020-07-30 04:39:27 +00:00
|
|
|
// Push an event to the websocket so we can auto-refresh the information in the panel once
|
|
|
|
// the install is completed.
|
|
|
|
s.Events().Publish(InstallCompletedEvent, "")
|
|
|
|
|
2020-01-19 21:30:54 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-04-03 20:43:13 +00:00
|
|
|
// Reinstalls a server's software by utilizing the install script for the server egg. This
|
|
|
|
// does not touch any existing files for the server, other than what the script modifies.
|
|
|
|
func (s *Server) Reinstall() error {
|
2020-08-20 01:58:48 +00:00
|
|
|
if s.GetState() != environment.ProcessOfflineState {
|
2020-06-13 17:26:35 +00:00
|
|
|
s.Log().Debug("waiting for server instance to enter a stopped state")
|
2020-04-03 20:43:13 +00:00
|
|
|
if err := s.Environment.WaitForStop(10, true); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-01 03:56:55 +00:00
|
|
|
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 {
|
2020-07-20 00:53:41 +00:00
|
|
|
script, rerr, err := api.NewRequester().GetInstallationScript(s.Id())
|
2019-12-28 22:57:19 +00:00
|
|
|
if err != nil || rerr != nil {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return errors.New(rerr.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
p, err := NewInstallationProcess(s, &script)
|
|
|
|
if err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
Script *api.InstallationScript
|
|
|
|
|
2020-06-23 04:38:16 +00:00
|
|
|
client *client.Client
|
|
|
|
context context.Context
|
2019-12-28 22:57:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Generates a new installation process struct that will be used to create containers,
|
|
|
|
// and otherwise perform installation commands for a server.
|
|
|
|
func NewInstallationProcess(s *Server, script *api.InstallationScript) (*InstallationProcess, error) {
|
|
|
|
proc := &InstallationProcess{
|
|
|
|
Script: script,
|
|
|
|
Server: s,
|
|
|
|
}
|
|
|
|
|
2020-06-23 04:38:16 +00:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
s.installer.cancel = &cancel
|
|
|
|
|
2019-12-28 22:57:19 +00:00
|
|
|
if c, err := client.NewClientWithOpts(client.FromEnv); err != nil {
|
|
|
|
return nil, errors.WithStack(err)
|
|
|
|
} else {
|
|
|
|
proc.client = c
|
2020-06-23 04:38:16 +00:00
|
|
|
proc.context = ctx
|
2019-12-28 22:57:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return proc, nil
|
|
|
|
}
|
|
|
|
|
2020-06-23 04:38:16 +00:00
|
|
|
// Try to obtain an exclusive lock on the installation process for the server. Waits up to 10
|
|
|
|
// seconds before aborting with a context timeout.
|
|
|
|
func (s *Server) acquireInstallationLock() error {
|
|
|
|
if s.installer.sem == nil {
|
|
|
|
s.installer.sem = semaphore.NewWeighted(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, _ := context.WithTimeout(context.Background(), time.Second*10)
|
|
|
|
|
|
|
|
return s.installer.sem.Acquire(ctx, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Determines if the server is actively running the installation process by checking the status
|
|
|
|
// of the semaphore lock.
|
|
|
|
func (s *Server) IsInstalling() bool {
|
|
|
|
if s.installer.sem == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.installer.sem.TryAcquire(1) {
|
|
|
|
// If we made it into this block it means we were able to obtain an exclusive lock
|
|
|
|
// on the semaphore. In that case, go ahead and release that lock immediately, and
|
|
|
|
// return false.
|
|
|
|
s.installer.sem.Release(1)
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Aborts the server installation process by calling the cancel function on the installer
|
|
|
|
// context.
|
|
|
|
func (s *Server) AbortInstallation() {
|
|
|
|
if !s.IsInstalling() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.installer.cancel != nil {
|
|
|
|
cancel := *s.installer.cancel
|
|
|
|
|
|
|
|
s.Log().Warn("aborting running installation process")
|
|
|
|
cancel()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-10 02:57:29 +00:00
|
|
|
// Removes the installer container for the server.
|
|
|
|
func (ip *InstallationProcess) RemoveContainer() {
|
2020-07-20 00:53:41 +00:00
|
|
|
err := ip.client.ContainerRemove(ip.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-06-13 17:26:35 +00:00
|
|
|
ip.Server.Log().WithField("error", errors.WithStack(err)).Warn("failed to delete server install container")
|
2020-05-10 02:57:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-18 21:05:44 +00:00
|
|
|
// Runs the installation process, this is done as a backgrounded thread. This will configure
|
|
|
|
// the required environment, and then spin up the installation container.
|
|
|
|
//
|
|
|
|
// Once the container finishes installing the results will be 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")
|
|
|
|
if err := ip.Server.acquireInstallationLock(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
// without encounting a wait timeout.
|
|
|
|
defer func() {
|
|
|
|
ip.Server.Log().Debug("releasing installation process lock")
|
|
|
|
ip.Server.installer.sem.Release(1)
|
|
|
|
ip.Server.installer.cancel = nil
|
|
|
|
}()
|
|
|
|
|
2020-08-24 03:45:07 +00:00
|
|
|
if err := ip.BeforeExecute(); err != nil {
|
2020-08-23 21:07:03 +00:00
|
|
|
return errors.WithStack(err)
|
2020-01-19 21:05:49 +00:00
|
|
|
}
|
2020-01-18 21:05:44 +00:00
|
|
|
|
2020-08-24 03:45:07 +00:00
|
|
|
cid, err := ip.Execute()
|
2020-01-19 21:05:49 +00:00
|
|
|
if err != nil {
|
2020-05-10 02:57:29 +00:00
|
|
|
ip.RemoveContainer()
|
|
|
|
|
2020-08-23 21:07:03 +00:00
|
|
|
return errors.WithStack(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.
|
|
|
|
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 {
|
|
|
|
return filepath.Join(os.TempDir(), "pterodactyl/", ip.Server.Id())
|
|
|
|
}
|
|
|
|
|
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.
|
2020-08-24 03:45:07 +00:00
|
|
|
if err := os.MkdirAll(ip.tempDir(), 0700); err != nil {
|
|
|
|
return errors.Wrap(err, "could not create temporary directory for install process")
|
2020-05-04 04:04:16 +00:00
|
|
|
}
|
|
|
|
|
2020-08-24 03:45:07 +00:00
|
|
|
f, err := os.OpenFile(filepath.Join(ip.tempDir(), "install.sh"), os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
|
2019-12-28 22:57:19 +00:00
|
|
|
if err != nil {
|
2020-08-24 03:45:07 +00:00
|
|
|
return errors.Wrap(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-08-24 03:45:07 +00:00
|
|
|
return errors.WithStack(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-06-23 04:38:16 +00:00
|
|
|
r, err := ip.client.ImagePull(ip.context, ip.Script.ContainerImage, types.ImagePullOptions{})
|
2019-12-28 22:57:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
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 {
|
2019-12-28 22:57:19 +00:00
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
|
|
|
return errors.Wrap(err, "failed to write installation script to disk")
|
2020-08-23 21:07:03 +00:00
|
|
|
}
|
2019-12-28 22:57:19 +00:00
|
|
|
|
2020-08-23 21:07:03 +00:00
|
|
|
if err := ip.pullInstallationImage(); err != nil {
|
2020-08-24 03:45:07 +00:00
|
|
|
return errors.Wrap(err, "failed to pull updated installation container image for server")
|
2020-08-23 21:07:03 +00:00
|
|
|
}
|
2019-12-28 22:57:19 +00:00
|
|
|
|
2020-08-23 21:07:03 +00:00
|
|
|
opts := types.ContainerRemoveOptions{
|
|
|
|
RemoveVolumes: true,
|
|
|
|
Force: true,
|
|
|
|
}
|
2019-12-28 22:57:19 +00:00
|
|
|
|
2020-08-23 21:07:03 +00:00
|
|
|
if err := ip.client.ContainerRemove(ip.context, ip.Server.Id()+"_installer", opts); err != nil {
|
|
|
|
if !client.IsErrNotFound(err) {
|
2020-08-24 03:45:07 +00:00
|
|
|
return errors.Wrap(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
|
|
|
}
|
|
|
|
|
2020-04-17 21:27:06 +00:00
|
|
|
// Returns the log path for the installation process.
|
|
|
|
func (ip *InstallationProcess) GetLogPath() string {
|
2020-07-20 00:53:41 +00:00
|
|
|
return filepath.Join(config.Get().System.GetInstallLogPath(), ip.Server.Id()+".log")
|
2020-04-17 21:27:06 +00:00
|
|
|
}
|
|
|
|
|
2020-01-19 21:05:49 +00:00
|
|
|
// 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.
|
|
|
|
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")
|
2020-06-23 04:38:16 +00:00
|
|
|
reader, err := ip.client.ContainerLogs(ip.context, containerId, types.ContainerLogsOptions{
|
2020-01-19 21:05:49 +00:00
|
|
|
ShowStdout: true,
|
|
|
|
ShowStderr: true,
|
|
|
|
Follow: false,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil && !client.IsErrNotFound(err) {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
2020-04-17 21:27:06 +00:00
|
|
|
f, err := os.OpenFile(ip.GetLogPath(), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
|
2020-01-19 21:05:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
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
|
|
|
|
| ------------------------------
|
2020-07-30 03:54:15 +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 {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := tmpl.Execute(f, ip); err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
2020-01-19 21:05:49 +00:00
|
|
|
if _, err := io.Copy(f, reader); err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-12-28 22:57:19 +00:00
|
|
|
// Executes the installation process inside a specially created docker container.
|
2020-08-24 03:45:07 +00:00
|
|
|
func (ip *InstallationProcess) Execute() (string, error) {
|
2019-12-28 22:57:19 +00:00
|
|
|
conf := &container.Config{
|
|
|
|
Hostname: "installer",
|
|
|
|
AttachStdout: true,
|
|
|
|
AttachStderr: true,
|
|
|
|
AttachStdin: true,
|
|
|
|
OpenStdin: true,
|
|
|
|
Tty: true,
|
|
|
|
Cmd: []string{ip.Script.Entrypoint, "./mnt/install/install.sh"},
|
|
|
|
Image: ip.Script.ContainerImage,
|
|
|
|
Env: ip.Server.GetEnvironmentVariables(),
|
|
|
|
Labels: map[string]string{
|
|
|
|
"Service": "Pterodactyl",
|
|
|
|
"ContainerType": "server_installer",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-08-13 03:38:02 +00:00
|
|
|
tmpfsSize := strconv.Itoa(int(config.Get().Docker.TmpfsSize))
|
2019-12-28 22:57:19 +00:00
|
|
|
hostConf := &container.HostConfig{
|
|
|
|
Mounts: []mount.Mount{
|
|
|
|
{
|
|
|
|
Target: "/mnt/server",
|
|
|
|
Source: ip.Server.Filesystem.Path(),
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
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
|
|
|
},
|
2020-05-10 02:57:29 +00:00
|
|
|
DNS: config.Get().Docker.Network.Dns,
|
2019-12-28 22:57:19 +00:00
|
|
|
LogConfig: container.LogConfig{
|
|
|
|
Type: "local",
|
|
|
|
Config: map[string]string{
|
2020-01-18 21:05:44 +00:00
|
|
|
"max-size": "5m",
|
2019-12-28 22:57:19 +00:00
|
|
|
"max-file": "1",
|
|
|
|
"compress": "false",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Privileged: true,
|
2020-05-10 02:57:29 +00:00
|
|
|
NetworkMode: container.NetworkMode(config.Get().Docker.Network.Mode),
|
2019-12-28 22:57:19 +00:00
|
|
|
}
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2020-07-20 00:53:41 +00:00
|
|
|
r, err := ip.client.ContainerCreate(ip.context, conf, hostConf, nil, ip.Server.Id()+"_installer")
|
2019-12-28 22:57:19 +00:00
|
|
|
if err != nil {
|
2020-01-18 21:05:44 +00:00
|
|
|
return "", errors.WithStack(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-06-23 04:38:16 +00:00
|
|
|
if err := ip.client.ContainerStart(ip.context, 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-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-01-18 21:05:44 +00:00
|
|
|
if err := ip.StreamOutput(id); err != nil {
|
2020-06-13 17:26:35 +00:00
|
|
|
ip.Server.Log().WithField("error", err).Error("error while handling output stream for server install process")
|
2020-01-18 21:05:44 +00:00
|
|
|
}
|
2020-01-18 22:04:26 +00:00
|
|
|
ip.Server.Events().Publish(DaemonMessageEvent, "Installation process completed.")
|
2020-01-18 21:05:44 +00:00
|
|
|
}(r.ID)
|
|
|
|
|
2020-06-23 04:38:16 +00:00
|
|
|
sChann, eChann := ip.client.ContainerWait(ip.context, r.ID, container.WaitConditionNotRunning)
|
2019-12-28 23:12:12 +00:00
|
|
|
select {
|
|
|
|
case err := <-eChann:
|
|
|
|
if err != nil {
|
2020-01-18 21:05:44 +00:00
|
|
|
return "", errors.WithStack(err)
|
2019-12-28 23:12:12 +00:00
|
|
|
}
|
|
|
|
case <-sChann:
|
2019-12-28 22:57:19 +00:00
|
|
|
}
|
|
|
|
|
2020-01-18 21:05:44 +00:00
|
|
|
return r.ID, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
func (ip *InstallationProcess) StreamOutput(id string) error {
|
2020-06-23 04:38:16 +00:00
|
|
|
reader, err := ip.client.ContainerLogs(ip.context, id, types.ContainerLogsOptions{
|
2020-01-18 21:05:44 +00:00
|
|
|
ShowStdout: true,
|
|
|
|
ShowStderr: true,
|
|
|
|
Follow: true,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer reader.Close()
|
|
|
|
|
|
|
|
s := bufio.NewScanner(reader)
|
|
|
|
for s.Scan() {
|
2020-01-18 22:04:26 +00:00
|
|
|
ip.Server.Events().Publish(InstallOutputEvent, s.Text())
|
2020-01-18 21:05:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.Err(); err != nil {
|
2020-06-13 17:26:35 +00:00
|
|
|
ip.Server.Log().WithFields(log.Fields{
|
|
|
|
"container_id": id,
|
2020-06-23 04:38:16 +00:00
|
|
|
"error": errors.WithStack(err),
|
2020-06-13 17:26:35 +00:00
|
|
|
}).Warn("error processing scanner line in installation output for server")
|
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
|
|
|
|
|
|
|
// Makes a HTTP request to the Panel instance notifying it that the server has
|
|
|
|
// completed the installation process, and what the state of the server is. A boolean
|
|
|
|
// value of "true" means everything was successful, "false" means something went
|
|
|
|
// wrong and the server must be deleted and re-created.
|
|
|
|
func (s *Server) SyncInstallState(successful bool) error {
|
|
|
|
r := api.NewRequester()
|
|
|
|
|
2020-07-20 00:53:41 +00:00
|
|
|
rerr, err := r.SendInstallationStatus(s.Id(), successful)
|
2020-01-19 21:30:54 +00:00
|
|
|
if rerr != nil || err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return errors.New(rerr.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|