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) {
cli, err := environment.DockerClient()
cli, err := environment.Docker()
if err != nil {
return types.Version{}, types.Info{}, err
}

View File

@ -340,8 +340,8 @@ func Get() *Configuration {
// the global configuration.
func Update(callback func(c *Configuration)) {
mu.Lock()
defer mu.Unlock()
callback(_config)
mu.Unlock()
}
// 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/network"
"github.com/docker/docker/client"
"github.com/pterodactyl/wings/config"
)
var _conce sync.Once
var _client *client.Client
// 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
// 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.
func DockerClient() (*client.Client, error) {
func Docker() (*client.Client, error) {
var err error
_conce.Do(func() {
_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.
func ConfigureDocker(ctx context.Context) error {
// Ensure the required docker network exists on the system.
cli, err := DockerClient()
cli, err := Docker()
if err != nil {
return err
}
nw := viper.Sub("docker.network")
resource, err := cli.NetworkInspect(ctx, nw.GetString("name"), types.NetworkInspectOptions{})
nw := config.Get().Docker.Network
resource, err := cli.NetworkInspect(ctx, nw.Name, types.NetworkInspectOptions{})
if err != nil {
if client.IsErrNotFound(err) {
log.Info("creating missing pterodactyl0 interface, this could take a few seconds...")
@ -43,57 +44,59 @@ func ConfigureDocker(ctx context.Context) error {
}
}
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":
nw.Set("interface", "127.0.0.1")
nw.Set("ispn", false)
c.Docker.Network.Interface = "127.0.0.1"
c.Docker.Network.ISPN = false
case "overlay":
fallthrough
case "weavemesh":
nw.Set("interface", "")
nw.Set("ispn", true)
c.Docker.Network.Interface = ""
c.Docker.Network.ISPN = true
default:
nw.Set("ispn", false)
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 {
nw := viper.Sub("docker.network")
_, err := cli.NetworkCreate(ctx, nw.GetString("name"), types.NetworkCreate{
Driver: nw.GetString("driver"),
nw := config.Get().Docker.Network
_, err := cli.NetworkCreate(ctx, nw.Name, types.NetworkCreate{
Driver: nw.Driver,
EnableIPv6: true,
Internal: nw.GetBool("is_internal"),
Internal: nw.IsInternal,
IPAM: &network.IPAM{
Config: []network.IPAMConfig{
{
Subnet: nw.GetString("interfaces.v4.subnet"),
Gateway: nw.GetString("interfaces.v4.gateway"),
},
{
Subnet: nw.GetString("interfaces.v6.subnet"),
Gateway: nw.GetString("interfaces.v6.gateway"),
},
},
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",
"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.host_binding_ipv4": "0.0.0.0",
"com.docker.network.bridge.name": "pterodactyl0",
"com.docker.network.driver.mtu": "1500",
},
})
driver := nw.GetString("driver")
if driver != "host" && driver != "overlay" && driver != "weavemesh" {
nw.Set("interface", nw.GetString("interfaces.v4.gateway"))
}
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
})
}
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
// by default). The container does not need to exist at this point.
func New(id string, m *Metadata, c *environment.Configuration) (*Environment, error) {
cli, err := environment.DockerClient()
cli, err := environment.Docker()
if err != nil {
return nil, err
}

View File

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