When mounting timezone data, check for the path first to avoid a fatal error

This commit is contained in:
Dane Everitt 2019-12-24 16:49:08 -08:00
parent 32e389db21
commit 06f495682c
No known key found for this signature in database
GPG Key ID: EEA66103B3D71F53
2 changed files with 34 additions and 10 deletions

View File

@ -166,7 +166,7 @@ type DockerConfiguration struct {
// Defines the location of the timezone file on the host system that should // Defines the location of the timezone file on the host system that should
// be mounted into the created containers so that they all use the same time. // be mounted into the created containers so that they all use the same time.
TimezonePath string `yaml:"timezone_path"` TimezonePath string `default:"/etc/localtime" yaml:"timezone_path"`
} }
// Defines the configuration for the internal API that is exposed by the // Defines the configuration for the internal API that is exposed by the

View File

@ -554,9 +554,7 @@ func (d *DockerEnvironment) Create() error {
PortBindings: d.portBindings(), PortBindings: d.portBindings(),
// Configure the mounts for this container. First mount the server data directory // Configure the mounts for this container. First mount the server data directory
// into the container as a r/w bind. Additionally mount the host timezone data into // into the container as a r/w bind.
// the container as a readonly bind so that software running in the container uses
// the same time as the host system.
Mounts: []mount.Mount{ Mounts: []mount.Mount{
{ {
Target: "/home/container", Target: "/home/container",
@ -564,12 +562,6 @@ func (d *DockerEnvironment) Create() error {
Type: mount.TypeBind, Type: mount.TypeBind,
ReadOnly: false, ReadOnly: false,
}, },
{
Target: config.Get().System.TimezonePath,
Source: config.Get().System.TimezonePath,
Type: mount.TypeBind,
ReadOnly: true,
},
}, },
// Configure the /tmp folder mapping in containers. This is necessary for some // Configure the /tmp folder mapping in containers. This is necessary for some
@ -606,6 +598,14 @@ func (d *DockerEnvironment) Create() error {
NetworkMode: "pterodactyl_nw", NetworkMode: "pterodactyl_nw",
} }
if err := mountTimezoneData(hostConf); err != nil {
if os.IsNotExist(err) {
zap.S().Warnw("the timezone data path configured does not exist on the system", zap.Error(errors.WithStack(err)))
} else {
zap.S().Warnw("failed to mount timezone data into container", zap.Error(errors.WithStack(err)))
}
}
if _, err := cli.ContainerCreate(ctx, conf, hostConf, nil, d.Server.Uuid); err != nil { if _, err := cli.ContainerCreate(ctx, conf, hostConf, nil, d.Server.Uuid); err != nil {
return errors.WithStack(err) return errors.WithStack(err)
} }
@ -613,6 +613,30 @@ func (d *DockerEnvironment) Create() error {
return nil return nil
} }
// Given a host configuration mount, also mount the timezone data into it.
func mountTimezoneData(c *container.HostConfig) error {
p := config.Get().System.TimezonePath
// Check for the timezone file, if it exists use it assuming it isn't also a directory,
// otherwise bubble the error back up the stack.
if s, err := os.Stat(p); err != nil {
return err
} else {
if s.IsDir() {
return errors.New("attempting to mount directory as timezone file")
}
}
c.Mounts = append(c.Mounts, mount.Mount{
Target: p,
Source: p,
Type: mount.TypeBind,
ReadOnly: true,
})
return nil
}
// Sends the specified command to the stdin of the running container instance. There is no // 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. // confirmation that this data is sent successfully, only that it gets pushed into the stdin.
func (d *DockerEnvironment) SendCommand(c string) error { func (d *DockerEnvironment) SendCommand(c string) error {