2020-04-10 13:12:18 +00:00
|
|
|
package environment
|
2019-12-08 01:31:12 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-10-17 21:09:02 +00:00
|
|
|
"strconv"
|
2020-10-17 21:04:56 +00:00
|
|
|
"sync"
|
2020-04-10 13:12:18 +00:00
|
|
|
|
2021-03-04 04:56:18 +00:00
|
|
|
"emperror.dev/errors"
|
2021-01-10 01:22:39 +00:00
|
|
|
"github.com/apex/log"
|
2019-12-08 01:31:12 +00:00
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/api/types/network"
|
|
|
|
"github.com/docker/docker/client"
|
2021-08-02 21:07:00 +00:00
|
|
|
|
2021-01-15 04:19:28 +00:00
|
|
|
"github.com/pterodactyl/wings/config"
|
2019-12-08 01:31:12 +00:00
|
|
|
)
|
|
|
|
|
2021-11-15 17:37:56 +00:00
|
|
|
var (
|
|
|
|
_conce sync.Once
|
|
|
|
_client *client.Client
|
|
|
|
)
|
2020-10-17 21:04:56 +00:00
|
|
|
|
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
|
2021-01-13 05:14:57 +00:00
|
|
|
// function.
|
2021-01-15 04:19:28 +00:00
|
|
|
func Docker() (*client.Client, error) {
|
2021-01-13 05:14:57 +00:00
|
|
|
var err error
|
|
|
|
_conce.Do(func() {
|
|
|
|
_client, err = client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
|
|
|
|
})
|
2021-03-04 04:56:18 +00:00
|
|
|
return _client, errors.Wrap(err, "environment/docker: could not create client")
|
2020-10-17 21:04:56 +00:00
|
|
|
}
|
|
|
|
|
2021-01-13 05:14:57 +00:00
|
|
|
// ConfigureDocker configures the required network for the docker environment.
|
|
|
|
func ConfigureDocker(ctx context.Context) error {
|
2019-12-08 01:31:12 +00:00
|
|
|
// Ensure the required docker network exists on the system.
|
2021-01-15 04:19:28 +00:00
|
|
|
cli, err := Docker()
|
2019-12-08 01:31:12 +00:00
|
|
|
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{})
|
2021-01-13 05:14:57 +00:00
|
|
|
if err != nil {
|
2022-09-26 00:49:48 +00:00
|
|
|
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 {
|
2021-01-22 04:45:56 +00:00
|
|
|
return err
|
2021-01-13 05:14:57 +00:00
|
|
|
}
|
2019-12-08 01:31:12 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
})
|
2019-12-08 01:31:12 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Creates a new network on the machine if one does not exist already.
|
2021-01-13 05:14:57 +00:00
|
|
|
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,
|
2019-12-08 01:31:12 +00:00
|
|
|
EnableIPv6: true,
|
2021-01-15 04:19:28 +00:00
|
|
|
Internal: nw.IsInternal,
|
2019-12-08 01:31:12 +00:00
|
|
|
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,
|
|
|
|
}},
|
2019-12-08 01:31:12 +00:00
|
|
|
},
|
|
|
|
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),
|
2019-12-08 01:31:12 +00:00
|
|
|
"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",
|
2022-07-09 22:08:52 +00:00
|
|
|
"com.docker.network.driver.mtu": strconv.FormatInt(nw.NetworkMTU, 10),
|
2019-12-08 01:31:12 +00:00
|
|
|
},
|
|
|
|
})
|
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
|
|
|
|
})
|
2019-12-08 01:31:12 +00:00
|
|
|
}
|
2021-01-15 04:19:28 +00:00
|
|
|
return nil
|
2019-12-08 01:31:12 +00:00
|
|
|
}
|