wings/environment/docker.go

108 lines
3.0 KiB
Go
Raw Normal View History

package environment
import (
"context"
2020-10-17 21:09:02 +00:00
"strconv"
"sync"
"emperror.dev/errors"
2021-01-10 01:22:39 +00:00
"github.com/apex/log"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/network"
"github.com/docker/docker/client"
2021-01-15 04:19:28 +00:00
"github.com/pterodactyl/wings/config"
)
2021-11-15 17:37:56 +00:00
var (
_conce sync.Once
_client *client.Client
)
2021-01-15 04:19:28 +00:00
// Docker returns a docker client to be used throughout the codebase. Once a
// client has been created it will be returned for all subsequent calls to this
// function.
2021-01-15 04:19:28 +00:00
func Docker() (*client.Client, error) {
var err error
_conce.Do(func() {
_client, err = client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
})
return _client, errors.Wrap(err, "environment/docker: could not create client")
}
// ConfigureDocker configures the required network for the docker environment.
func ConfigureDocker(ctx context.Context) error {
// Ensure the required docker network exists on the system.
2021-01-15 04:19:28 +00:00
cli, err := Docker()
if err != nil {
return err
}
2021-01-15 04:19:28 +00:00
nw := config.Get().Docker.Network
resource, err := cli.NetworkInspect(ctx, nw.Name, types.NetworkInspectOptions{})
if err != nil {
if !client.IsErrNotFound(err) {
return err
}
log.Info("creating missing pterodactyl0 interface, this could take a few seconds...")
if err := createDockerNetwork(ctx, cli); err != nil {
return err
}
}
2021-01-15 04:19:28 +00:00
config.Update(func(c *config.Configuration) {
c.Docker.Network.Driver = resource.Driver
switch c.Docker.Network.Driver {
case "host":
c.Docker.Network.Interface = "127.0.0.1"
c.Docker.Network.ISPN = false
case "overlay":
fallthrough
case "weavemesh":
c.Docker.Network.Interface = ""
c.Docker.Network.ISPN = true
default:
c.Docker.Network.ISPN = false
}
})
return nil
}
// Creates a new network on the machine if one does not exist already.
func createDockerNetwork(ctx context.Context, cli *client.Client) error {
2021-01-15 04:19:28 +00:00
nw := config.Get().Docker.Network
_, err := cli.NetworkCreate(ctx, nw.Name, types.NetworkCreate{
Driver: nw.Driver,
EnableIPv6: true,
2021-01-15 04:19:28 +00:00
Internal: nw.IsInternal,
IPAM: &network.IPAM{
2021-01-15 04:19:28 +00:00
Config: []network.IPAMConfig{{
Subnet: nw.Interfaces.V4.Subnet,
Gateway: nw.Interfaces.V4.Gateway,
}, {
Subnet: nw.Interfaces.V6.Subnet,
Gateway: nw.Interfaces.V6.Gateway,
}},
},
Options: map[string]string{
"encryption": "false",
"com.docker.network.bridge.default_bridge": "false",
2021-01-15 04:19:28 +00:00
"com.docker.network.bridge.enable_icc": strconv.FormatBool(nw.EnableICC),
"com.docker.network.bridge.enable_ip_masquerade": "true",
"com.docker.network.bridge.host_binding_ipv4": "0.0.0.0",
"com.docker.network.bridge.name": "pterodactyl0",
"com.docker.network.driver.mtu": strconv.FormatInt(nw.NetworkMTU, 10),
},
})
2021-01-15 04:19:28 +00:00
if err != nil {
return err
}
if nw.Driver != "host" && nw.Driver != "overlay" && nw.Driver != "weavemesh" {
config.Update(func(c *config.Configuration) {
c.Docker.Network.Interface = c.Docker.Network.Interfaces.V4.Gateway
})
}
2021-01-15 04:19:28 +00:00
return nil
}