Skip to content

Commit

Permalink
Merge pull request #1032 from mesg-foundation/feature/configs
Browse files Browse the repository at this point in the history
Simplify configs package
  • Loading branch information
antho1404 authored Jun 7, 2019
2 parents 77f56b7 + dc22b05 commit b3e8dbb
Show file tree
Hide file tree
Showing 14 changed files with 134 additions and 215 deletions.
55 changes: 11 additions & 44 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@ import (
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"sync"

"github.com/kelseyhightower/envconfig"
"github.com/mesg-foundation/core/version"
"github.com/mesg-foundation/core/x/xstrings"
homedir "github.com/mitchellh/go-homedir"
"github.com/sirupsen/logrus"
Expand All @@ -29,11 +26,10 @@ var (

// Config contains all the configuration needed.
type Config struct {
Server struct {
Address string
}
Name string
Path string

Client struct {
Server struct {
Address string
}

Expand All @@ -43,25 +39,12 @@ type Config struct {
Level string
}

Core struct {
Image string
Name string
Path string

Database struct {
ServiceRelativePath string
ExecutionRelativePath string
}
Database struct {
ServiceRelativePath string
ExecutionRelativePath string
}

Service ServiceConfigGroup

Docker struct {
Socket string
Core struct {
Path string
}
}
}

// New creates a new config with default values.
Expand All @@ -73,17 +56,13 @@ func New() (*Config, error) {

var c Config
c.Server.Address = ":50052"
c.Client.Address = "localhost:50052"
c.Log.Format = "text"
c.Log.Level = "info"
c.Log.ForceColors = false
c.Core.Image = "mesg/engine:" + strings.Split(version.Version, " ")[0]
c.Core.Name = "engine"
c.Core.Path = filepath.Join(home, ".mesg")
c.Core.Database.ServiceRelativePath = filepath.Join("database", "services", serviceDBVersion)
c.Core.Database.ExecutionRelativePath = filepath.Join("database", "executions", executionDBVersion)
c.Docker.Core.Path = "/mesg"
c.Docker.Socket = "/var/run/docker.sock"
c.Name = "engine"
c.Path = filepath.Join(home, ".mesg")
c.Database.ServiceRelativePath = filepath.Join("database", "services", serviceDBVersion)
c.Database.ExecutionRelativePath = filepath.Join("database", "executions", executionDBVersion)
c.Service = c.getServiceConfigGroup()
return &c, nil
}
Expand Down Expand Up @@ -120,7 +99,7 @@ func (c *Config) Load() error {

// Prepare setups local directories or any other required thing based on config
func (c *Config) Prepare() error {
return os.MkdirAll(c.Core.Path, os.FileMode(0755))
return os.MkdirAll(c.Path, os.FileMode(0755))
}

// Validate checks values and return an error if any validation failed.
Expand All @@ -133,15 +112,3 @@ func (c *Config) Validate() error {
}
return nil
}

// DaemonEnv returns the needed environmental variable for the Daemon.
func (c *Config) DaemonEnv() map[string]string {
return map[string]string{
"MESG_SERVER_ADDRESS": c.Server.Address,
"MESG_LOG_FORMAT": c.Log.Format,
"MESG_LOG_LEVEL": c.Log.Level,
"MESG_LOG_FORCECOLORS": strconv.FormatBool(c.Log.ForceColors),
"MESG_CORE_NAME": c.Core.Name,
"MESG_CORE_PATH": c.Docker.Core.Path,
}
}
31 changes: 4 additions & 27 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package config
import (
"os"
"path/filepath"
"strconv"
"strings"
"testing"

homedir "github.com/mitchellh/go-homedir"
Expand All @@ -16,17 +14,13 @@ func TestDefaultValue(t *testing.T) {
c, err := New()
require.NoError(t, err)
require.Equal(t, ":50052", c.Server.Address)
require.Equal(t, "localhost:50052", c.Client.Address)
require.Equal(t, "text", c.Log.Format)
require.Equal(t, "info", c.Log.Level)
require.Equal(t, false, c.Log.ForceColors)
require.Equal(t, filepath.Join(home, ".mesg"), c.Core.Path)
require.Equal(t, filepath.Join("database", "services", serviceDBVersion), c.Core.Database.ServiceRelativePath)
require.Equal(t, filepath.Join("database", "executions", executionDBVersion), c.Core.Database.ExecutionRelativePath)
require.Equal(t, "engine", c.Core.Name)
require.Equal(t, "/mesg", c.Docker.Core.Path)
require.Equal(t, "/var/run/docker.sock", c.Docker.Socket)
require.True(t, strings.HasPrefix(c.Core.Image, "mesg/engine:"))
require.Equal(t, filepath.Join(home, ".mesg"), c.Path)
require.Equal(t, filepath.Join("database", "services", serviceDBVersion), c.Database.ServiceRelativePath)
require.Equal(t, filepath.Join("database", "executions", executionDBVersion), c.Database.ExecutionRelativePath)
require.Equal(t, "engine", c.Name)
}

func TestGlobal(t *testing.T) {
Expand All @@ -38,11 +32,9 @@ func TestGlobal(t *testing.T) {
func TestLoad(t *testing.T) {
snapsnot := map[string]string{
"MESG_SERVER_ADDRESS": "",
"MESG_CLIENT_ADDRESS": "",
"MESG_LOG_FORMAT": "",
"MESG_LOG_LEVEL": "",
"MESG_LOG_FORCECOLORS": "",
"MESG_CORE_IMAGE": "",
}
for key := range snapsnot {
snapsnot[key] = os.Getenv(key)
Expand All @@ -54,19 +46,15 @@ func TestLoad(t *testing.T) {
}()

os.Setenv("MESG_SERVER_ADDRESS", "test_server_address")
os.Setenv("MESG_CLIENT_ADDRESS", "test_client_address")
os.Setenv("MESG_LOG_FORMAT", "test_log_format")
os.Setenv("MESG_LOG_LEVEL", "test_log_level")
os.Setenv("MESG_LOG_FORCECOLORS", "true")
os.Setenv("MESG_CORE_IMAGE", "test_core_image")
c, _ := New()
c.Load()
require.Equal(t, "test_server_address", c.Server.Address)
require.Equal(t, "test_client_address", c.Client.Address)
require.Equal(t, "test_log_format", c.Log.Format)
require.Equal(t, "test_log_level", c.Log.Level)
require.Equal(t, true, c.Log.ForceColors)
require.Equal(t, "test_core_image", c.Core.Image)
}

func TestValidate(t *testing.T) {
Expand All @@ -81,14 +69,3 @@ func TestValidate(t *testing.T) {
c.Log.Level = "wrongValue"
require.Error(t, c.Validate())
}

func TestDaemonEnv(t *testing.T) {
c, _ := New()
env := c.DaemonEnv()
require.Equal(t, c.Server.Address, env["MESG_SERVER_ADDRESS"])
require.Equal(t, c.Log.Level, env["MESG_LOG_LEVEL"])
require.Equal(t, c.Log.Format, env["MESG_LOG_FORMAT"])
require.Equal(t, strconv.FormatBool(c.Log.ForceColors), env["MESG_LOG_FORCECOLORS"])
require.Equal(t, c.Core.Name, env["MESG_CORE_NAME"])
require.Equal(t, c.Docker.Core.Path, env["MESG_CORE_PATH"])
}
2 changes: 1 addition & 1 deletion container/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func TestNew(t *testing.T) {
}

ln := <-dt.LastNetworkCreate()
require.Equal(t, cfg.Core.Name, ln.Name)
require.Equal(t, cfg.Name, ln.Name)
require.Equal(t, types.NetworkCreate{
CheckDuplicate: true,
Driver: "overlay",
Expand Down
2 changes: 1 addition & 1 deletion container/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const namespaceSeparator string = "-"

// Namespace creates a namespace from a list of string.
func (c *DockerContainer) Namespace(ss []string) string {
ssWithPrefix := append([]string{c.config.Core.Name}, ss...)
ssWithPrefix := append([]string{c.config.Name}, ss...)
namespace := strings.Join(ssWithPrefix, namespaceSeparator)
namespace = strings.Replace(namespace, " ", namespaceSeparator, -1)
return namespace
Expand Down
4 changes: 2 additions & 2 deletions container/namespace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ func TestNamespace(t *testing.T) {
cfg, _ := config.Global()
c, _ := New()
namespace := c.Namespace([]string{"test"})
require.Equal(t, namespace, strings.Join([]string{cfg.Core.Name, "test"}, namespaceSeparator))
require.Equal(t, namespace, strings.Join([]string{cfg.Name, "test"}, namespaceSeparator))
}

func TestNamespaceReplaceSpace(t *testing.T) {
cfg, _ := config.Global()
c, _ := New()
namespace := c.Namespace([]string{"test foo"})
require.Equal(t, namespace, strings.Join([]string{cfg.Core.Name, "test-foo"}, namespaceSeparator))
require.Equal(t, namespace, strings.Join([]string{cfg.Name, "test-foo"}, namespaceSeparator))
}
4 changes: 2 additions & 2 deletions core/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ func initDependencies() (*dependencies, error) {
}

// init services db.
serviceDB, err := database.NewServiceDB(filepath.Join(config.Core.Path, config.Core.Database.ServiceRelativePath))
serviceDB, err := database.NewServiceDB(filepath.Join(config.Path, config.Database.ServiceRelativePath))
if err != nil {
return nil, err
}

// init execution db.
executionDB, err := database.NewExecutionDB(filepath.Join(config.Core.Path, config.Core.Database.ExecutionRelativePath))
executionDB, err := database.NewExecutionDB(filepath.Join(config.Path, config.Database.ExecutionRelativePath))
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion dev
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ docker service create \
--name engine \
--tty \
--label com.docker.stack.namespace=engine --label com.docker.stack.image=mesg/engine:$VERSION \
--mount type=bind,source=/var/run/docker.sock,destination=/var/run/docker.sock --mount type=bind,source=$HOME/.mesg,destination=/mesg \
--mount type=bind,source=/var/run/docker.sock,destination=/var/run/docker.sock --mount type=bind,source=$HOME/.mesg,destination=/home/root/.mesg \
--network engine --publish 50052:50052 \
mesg/engine:$VERSION

Expand Down
Loading

0 comments on commit b3e8dbb

Please sign in to comment.