Update remaining logic using viper

This commit is contained in:
Dane Everitt 2021-01-14 20:19:28 -08:00
parent 80faea3286
commit 05c04c4350
No known key found for this signature in database
GPG Key ID: EEA66103B3D71F53
5 changed files with 46 additions and 43 deletions

View File

@ -188,7 +188,7 @@ func diagnosticsCmdRun(cmd *cobra.Command, args []string) {
} }
func getDockerInfo() (types.Version, types.Info, error) { func getDockerInfo() (types.Version, types.Info, error) {
cli, err := environment.DockerClient() cli, err := environment.Docker()
if err != nil { if err != nil {
return types.Version{}, types.Info{}, err return types.Version{}, types.Info{}, err
} }

View File

@ -340,8 +340,8 @@ func Get() *Configuration {
// the global configuration. // the global configuration.
func Update(callback func(c *Configuration)) { func Update(callback func(c *Configuration)) {
mu.Lock() mu.Lock()
defer mu.Unlock()
callback(_config) callback(_config)
mu.Unlock()
} }
// GetJwtAlgorithm returns the in-memory JWT algorithm. // GetJwtAlgorithm returns the in-memory JWT algorithm.

View File

@ -9,15 +9,16 @@ import (
"github.com/docker/docker/api/types" "github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/network" "github.com/docker/docker/api/types/network"
"github.com/docker/docker/client" "github.com/docker/docker/client"
"github.com/pterodactyl/wings/config"
) )
var _conce sync.Once var _conce sync.Once
var _client *client.Client var _client *client.Client
// DockerClient returns a Docker client to be used throughout the codebase. Once // Docker returns a docker client to be used throughout the codebase. Once a
// a client has been created it will be returned for all subsequent calls to this // client has been created it will be returned for all subsequent calls to this
// function. // function.
func DockerClient() (*client.Client, error) { func Docker() (*client.Client, error) {
var err error var err error
_conce.Do(func() { _conce.Do(func() {
_client, err = client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation()) _client, err = client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
@ -28,13 +29,13 @@ func DockerClient() (*client.Client, error) {
// ConfigureDocker configures the required network for the docker environment. // ConfigureDocker configures the required network for the docker environment.
func ConfigureDocker(ctx context.Context) error { func ConfigureDocker(ctx context.Context) error {
// Ensure the required docker network exists on the system. // Ensure the required docker network exists on the system.
cli, err := DockerClient() cli, err := Docker()
if err != nil { if err != nil {
return err return err
} }
nw := viper.Sub("docker.network") nw := config.Get().Docker.Network
resource, err := cli.NetworkInspect(ctx, nw.GetString("name"), types.NetworkInspectOptions{}) resource, err := cli.NetworkInspect(ctx, nw.Name, types.NetworkInspectOptions{})
if err != nil { if err != nil {
if client.IsErrNotFound(err) { if client.IsErrNotFound(err) {
log.Info("creating missing pterodactyl0 interface, this could take a few seconds...") log.Info("creating missing pterodactyl0 interface, this could take a few seconds...")
@ -43,57 +44,59 @@ func ConfigureDocker(ctx context.Context) error {
} }
} }
return err return err
} else {
nw.Set("driver", resource.Driver)
} }
switch nw.GetString("driver") { config.Update(func(c *config.Configuration) {
c.Docker.Network.Driver = resource.Driver
switch c.Docker.Network.Driver {
case "host": case "host":
nw.Set("interface", "127.0.0.1") c.Docker.Network.Interface = "127.0.0.1"
nw.Set("ispn", false) c.Docker.Network.ISPN = false
case "overlay": case "overlay":
fallthrough fallthrough
case "weavemesh": case "weavemesh":
nw.Set("interface", "") c.Docker.Network.Interface = ""
nw.Set("ispn", true) c.Docker.Network.ISPN = true
default: default:
nw.Set("ispn", false) c.Docker.Network.ISPN = false
} }
})
return nil return nil
} }
// Creates a new network on the machine if one does not exist already. // Creates a new network on the machine if one does not exist already.
func createDockerNetwork(ctx context.Context, cli *client.Client) error { func createDockerNetwork(ctx context.Context, cli *client.Client) error {
nw := viper.Sub("docker.network") nw := config.Get().Docker.Network
_, err := cli.NetworkCreate(ctx, nw.GetString("name"), types.NetworkCreate{ _, err := cli.NetworkCreate(ctx, nw.Name, types.NetworkCreate{
Driver: nw.GetString("driver"), Driver: nw.Driver,
EnableIPv6: true, EnableIPv6: true,
Internal: nw.GetBool("is_internal"), Internal: nw.IsInternal,
IPAM: &network.IPAM{ IPAM: &network.IPAM{
Config: []network.IPAMConfig{ Config: []network.IPAMConfig{{
{ Subnet: nw.Interfaces.V4.Subnet,
Subnet: nw.GetString("interfaces.v4.subnet"), Gateway: nw.Interfaces.V4.Gateway,
Gateway: nw.GetString("interfaces.v4.gateway"), }, {
}, Subnet: nw.Interfaces.V6.Subnet,
{ Gateway: nw.Interfaces.V6.Gateway,
Subnet: nw.GetString("interfaces.v6.subnet"), }},
Gateway: nw.GetString("interfaces.v6.gateway"),
},
},
}, },
Options: map[string]string{ Options: map[string]string{
"encryption": "false", "encryption": "false",
"com.docker.network.bridge.default_bridge": "false", "com.docker.network.bridge.default_bridge": "false",
"com.docker.network.bridge.enable_icc": strconv.FormatBool(nw.GetBool("enable_icc")), "com.docker.network.bridge.enable_icc": strconv.FormatBool(nw.EnableICC),
"com.docker.network.bridge.enable_ip_masquerade": "true", "com.docker.network.bridge.enable_ip_masquerade": "true",
"com.docker.network.bridge.host_binding_ipv4": "0.0.0.0", "com.docker.network.bridge.host_binding_ipv4": "0.0.0.0",
"com.docker.network.bridge.name": "pterodactyl0", "com.docker.network.bridge.name": "pterodactyl0",
"com.docker.network.driver.mtu": "1500", "com.docker.network.driver.mtu": "1500",
}, },
}) })
driver := nw.GetString("driver") if err != nil {
if driver != "host" && driver != "overlay" && driver != "weavemesh" {
nw.Set("interface", nw.GetString("interfaces.v4.gateway"))
}
return err 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
})
}
return nil
} }

View File

@ -55,7 +55,7 @@ type Environment struct {
// reference the container from here on out. This should be unique per-server (we use the UUID // reference the container from here on out. This should be unique per-server (we use the UUID
// by default). The container does not need to exist at this point. // by default). The container does not need to exist at this point.
func New(id string, m *Metadata, c *environment.Configuration) (*Environment, error) { func New(id string, m *Metadata, c *environment.Configuration) (*Environment, error) {
cli, err := environment.DockerClient() cli, err := environment.Docker()
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -127,7 +127,7 @@ func NewInstallationProcess(s *Server, script *api.InstallationScript) (*Install
Server: s, Server: s,
} }
if c, err := environment.DockerClient(); err != nil { if c, err := environment.Docker(); err != nil {
return nil, err return nil, err
} else { } else {
proc.client = c proc.client = c