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-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-01-13 05:14:57 +00:00
|
|
|
"github.com/spf13/viper"
|
2019-12-08 01:31:12 +00:00
|
|
|
)
|
|
|
|
|
2021-01-13 05:14:57 +00:00
|
|
|
var _conce sync.Once
|
2020-10-17 21:04:56 +00:00
|
|
|
var _client *client.Client
|
|
|
|
|
2021-01-13 05:14:57 +00:00
|
|
|
// DockerClient 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.
|
2020-10-17 21:04:56 +00:00
|
|
|
func DockerClient() (*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())
|
|
|
|
})
|
2020-10-17 21:04:56 +00:00
|
|
|
return _client, err
|
|
|
|
}
|
|
|
|
|
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.
|
2020-10-17 21:04:56 +00:00
|
|
|
cli, err := DockerClient()
|
2019-12-08 01:31:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-01-13 05:14:57 +00:00
|
|
|
nw := viper.Sub("docker.network")
|
|
|
|
resource, err := cli.NetworkInspect(ctx, nw.GetString("name"), types.NetworkInspectOptions{})
|
|
|
|
if err != nil {
|
|
|
|
if client.IsErrNotFound(err) {
|
|
|
|
log.Info("creating missing pterodactyl0 interface, this could take a few seconds...")
|
|
|
|
if err := createDockerNetwork(ctx, cli); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
nw.Set("driver", resource.Driver)
|
2019-12-08 01:31:12 +00:00
|
|
|
}
|
|
|
|
|
2021-01-13 05:14:57 +00:00
|
|
|
switch nw.GetString("driver") {
|
2019-12-08 01:31:12 +00:00
|
|
|
case "host":
|
2021-01-13 05:14:57 +00:00
|
|
|
nw.Set("interface", "127.0.0.1")
|
|
|
|
nw.Set("ispn", false)
|
2019-12-08 01:31:12 +00:00
|
|
|
case "overlay":
|
2021-01-13 05:14:57 +00:00
|
|
|
fallthrough
|
2019-12-08 01:31:12 +00:00
|
|
|
case "weavemesh":
|
2021-01-13 05:14:57 +00:00
|
|
|
nw.Set("interface", "")
|
|
|
|
nw.Set("ispn", true)
|
2019-12-08 01:31:12 +00:00
|
|
|
default:
|
2021-01-13 05:14:57 +00:00
|
|
|
nw.Set("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 {
|
|
|
|
nw := viper.Sub("docker.network")
|
|
|
|
_, err := cli.NetworkCreate(ctx, nw.GetString("name"), types.NetworkCreate{
|
|
|
|
Driver: nw.GetString("driver"),
|
2019-12-08 01:31:12 +00:00
|
|
|
EnableIPv6: true,
|
2021-01-13 05:14:57 +00:00
|
|
|
Internal: nw.GetBool("is_internal"),
|
2019-12-08 01:31:12 +00:00
|
|
|
IPAM: &network.IPAM{
|
|
|
|
Config: []network.IPAMConfig{
|
|
|
|
{
|
2021-01-13 05:14:57 +00:00
|
|
|
Subnet: nw.GetString("interfaces.v4.subnet"),
|
|
|
|
Gateway: nw.GetString("interfaces.v4.gateway"),
|
2019-12-08 01:31:12 +00:00
|
|
|
},
|
|
|
|
{
|
2021-01-13 05:14:57 +00:00
|
|
|
Subnet: nw.GetString("interfaces.v6.subnet"),
|
|
|
|
Gateway: nw.GetString("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-13 05:14:57 +00:00
|
|
|
"com.docker.network.bridge.enable_icc": strconv.FormatBool(nw.GetBool("enable_icc")),
|
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",
|
|
|
|
"com.docker.network.driver.mtu": "1500",
|
|
|
|
},
|
|
|
|
})
|
2021-01-13 05:14:57 +00:00
|
|
|
driver := nw.GetString("driver")
|
|
|
|
if driver != "host" && driver != "overlay" && driver != "weavemesh" {
|
|
|
|
nw.Set("interface", nw.GetString("interfaces.v4.gateway"))
|
2019-12-08 01:31:12 +00:00
|
|
|
}
|
2021-01-13 05:14:57 +00:00
|
|
|
return err
|
2019-12-08 01:31:12 +00:00
|
|
|
}
|