Always clean up the installer containers, even if there is an error during the process; closes pterodactyl/panel#2015

This commit is contained in:
Dane Everitt 2020-05-09 19:57:29 -07:00
parent 0bd28a4480
commit 804f3d5ca9
No known key found for this signature in database
GPG Key ID: EEA66103B3D71F53

View File

@ -104,6 +104,18 @@ func NewInstallationProcess(s *Server, script *api.InstallationScript) (*Install
return proc, nil return proc, nil
} }
// Removes the installer container for the server.
func (ip *InstallationProcess) RemoveContainer() {
err := ip.client.ContainerRemove(context.Background(), ip.Server.Uuid + "_installer", types.ContainerRemoveOptions{
RemoveVolumes: true,
Force: true,
})
if err != nil && !client.IsErrNotFound(err) {
zap.S().Warnw("failed to delete server installer container", zap.String("server", ip.Server.Uuid), zap.Error(errors.WithStack(err)))
}
}
// Runs the installation process, this is done as a backgrounded thread. This will configure // Runs the installation process, this is done as a backgrounded thread. This will configure
// the required environment, and then spin up the installation container. // the required environment, and then spin up the installation container.
// //
@ -117,6 +129,8 @@ func (ip *InstallationProcess) Run() error {
cid, err := ip.Execute(installPath) cid, err := ip.Execute(installPath)
if err != nil { if err != nil {
ip.RemoveContainer()
return err return err
} }
@ -249,6 +263,7 @@ func (ip *InstallationProcess) GetLogPath() string {
// installation container. // installation container.
func (ip *InstallationProcess) AfterExecute(containerId string) error { func (ip *InstallationProcess) AfterExecute(containerId string) error {
ctx := context.Background() ctx := context.Background()
defer ip.RemoveContainer()
zap.S().Debugw("pulling installation logs for server", zap.String("server", ip.Server.Uuid), zap.String("container_id", containerId)) zap.S().Debugw("pulling installation logs for server", zap.String("server", ip.Server.Uuid), zap.String("container_id", containerId))
reader, err := ip.client.ContainerLogs(ctx, containerId, types.ContainerLogsOptions{ reader, err := ip.client.ContainerLogs(ctx, containerId, types.ContainerLogsOptions{
@ -273,17 +288,6 @@ func (ip *InstallationProcess) AfterExecute(containerId string) error {
return errors.WithStack(err) return errors.WithStack(err)
} }
zap.S().Debugw("removing server installation container", zap.String("server", ip.Server.Uuid), zap.String("container_id", containerId))
rErr := ip.client.ContainerRemove(ctx, containerId, types.ContainerRemoveOptions{
RemoveVolumes: true,
RemoveLinks: false,
Force: true,
})
if rErr != nil && !client.IsErrNotFound(rErr) {
return errors.WithStack(rErr)
}
return nil return nil
} }
@ -331,7 +335,7 @@ func (ip *InstallationProcess) Execute(installPath string) (string, error) {
Tmpfs: map[string]string{ Tmpfs: map[string]string{
"/tmp": "rw,exec,nosuid,size=50M", "/tmp": "rw,exec,nosuid,size=50M",
}, },
DNS: []string{"1.1.1.1", "8.8.8.8"}, DNS: config.Get().Docker.Network.Dns,
LogConfig: container.LogConfig{ LogConfig: container.LogConfig{
Type: "local", Type: "local",
Config: map[string]string{ Config: map[string]string{
@ -341,7 +345,7 @@ func (ip *InstallationProcess) Execute(installPath string) (string, error) {
}, },
}, },
Privileged: true, Privileged: true,
NetworkMode: "pterodactyl_nw", NetworkMode: container.NetworkMode(config.Get().Docker.Network.Mode),
} }
zap.S().Infow("creating installer container for server process", zap.String("server", ip.Server.Uuid)) zap.S().Infow("creating installer container for server process", zap.String("server", ip.Server.Uuid))