Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use docker volumes and not binding for services #419

Merged
merged 7 commits into from
Sep 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 6 additions & 13 deletions config/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package config

import (
"os"
"path/filepath"
"strings"

"github.com/mesg-foundation/core/version"
Expand All @@ -11,14 +10,12 @@ import (

// All the configuration keys.
const (
APIServerAddress = "Api.Server.Address"
APIClientTarget = "Api.Client.Target"
LogFormat = "Log.Format"
LogLevel = "Log.Level"
ServicePathHost = "Service.Path.Host"
ServicePathDocker = "Service.Path.Docker"
MESGPath = "MESG.Path"
CoreImage = "Core.Image"
APIServerAddress = "Api.Server.Address"
APIClientTarget = "Api.Client.Target"
LogFormat = "Log.Format"
LogLevel = "Log.Level"
MESGPath = "MESG.Path"
CoreImage = "Core.Image"
)

func setAPIDefault() {
Expand All @@ -34,10 +31,6 @@ func setAPIDefault() {
viper.SetDefault(LogFormat, "text")
viper.SetDefault(LogLevel, "info")

viper.SetDefault(ServicePathHost, filepath.Join(viper.GetString(MESGPath), "services"))
viper.SetDefault(ServicePathDocker, filepath.Join("/mesg", "services"))
os.MkdirAll(viper.GetString(ServicePathDocker), os.ModePerm)

// Keep only the first part if Version contains space
coreTag := strings.Split(version.Version, " ")
viper.SetDefault(CoreImage, "mesg/core:"+coreTag[0])
Expand Down
9 changes: 3 additions & 6 deletions config/api_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package config

import (
"path/filepath"
"testing"

"github.com/spf13/viper"
Expand All @@ -15,11 +14,9 @@ func assertViperDefault(t *testing.T, key string, expected string) {

func TestAPIDefault(t *testing.T) {
defaults := map[string]string{
APIServerAddress: ":50052",
LogFormat: "text",
LogLevel: "info",
ServicePathHost: filepath.Join(viper.GetString(MESGPath), "services"),
ServicePathDocker: filepath.Join("/mesg", "services"),
APIServerAddress: ":50052",
LogFormat: "text",
LogLevel: "info",
}
for key, defaultValue := range defaults {
assertViperDefault(t, key, defaultValue)
Expand Down
6 changes: 6 additions & 0 deletions container/service_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type Port struct {
type Mount struct {
Source string
Target string
Bind bool
}

func (options *ServiceOptions) toSwarmServiceSpec() swarm.ServiceSpec {
Expand Down Expand Up @@ -81,9 +82,14 @@ func (options *ServiceOptions) swarmMounts(force bool) []mount.Mount {
}
mounts := make([]mount.Mount, len(options.Mounts))
for i, m := range options.Mounts {
mountType := mount.TypeVolume
if m.Bind {
mountType = mount.TypeBind
}
mounts[i] = mount.Mount{
Source: m.Source,
Target: m.Target,
Type: mountType,
}
}
return mounts
Expand Down
2 changes: 2 additions & 0 deletions daemon/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package daemon
const (
name = "core"
dockerSocket = "/var/run/docker.sock"
volume = "mesg-core"
path = "/mesg"
)

// Namespace returns the namespace of the MESG Core.
Expand Down
14 changes: 6 additions & 8 deletions daemon/start.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package daemon

import (
"path/filepath"

"github.com/mesg-foundation/core/config"
"github.com/mesg-foundation/core/container"
"github.com/mesg-foundation/core/x/xnet"
Expand Down Expand Up @@ -37,19 +35,19 @@ func serviceSpec() (spec container.ServiceOptions, err error) {
Namespace: Namespace(),
Image: viper.GetString(config.CoreImage),
Env: container.MapToEnv(map[string]string{
config.ToEnv(config.MESGPath): "/mesg",
config.ToEnv(config.LogFormat): viper.GetString(config.LogFormat),
config.ToEnv(config.LogLevel): viper.GetString(config.LogLevel),
config.ToEnv(config.ServicePathHost): filepath.Join(viper.GetString(config.MESGPath), "services"),
config.ToEnv(config.MESGPath): path,
config.ToEnv(config.LogFormat): viper.GetString(config.LogFormat),
config.ToEnv(config.LogLevel): viper.GetString(config.LogLevel),
}),
Mounts: []container.Mount{
{
Source: dockerSocket,
Target: dockerSocket,
Bind: true,
},
{
Source: viper.GetString(config.MESGPath),
Target: "/mesg",
Source: volume,
Target: path,
},
},
Ports: []container.Port{
Expand Down
8 changes: 2 additions & 6 deletions daemon/start_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package daemon

import (
"path/filepath"
"testing"

"github.com/mesg-foundation/core/config"
"github.com/mesg-foundation/core/container"
"github.com/spf13/viper"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -57,14 +54,13 @@ func TestStartConfig(t *testing.T) {
require.True(t, contains(spec.Env, "MESG_MESG_PATH=/mesg"))
require.True(t, contains(spec.Env, "MESG_LOG_LEVEL=info"))
require.True(t, contains(spec.Env, "MESG_LOG_FORMAT=text"))
require.True(t, contains(spec.Env, "MESG_SERVICE_PATH_HOST="+filepath.Join(viper.GetString(config.MESGPath), "services")))
// Ensure that the port is shared
require.Equal(t, spec.Ports[0].Published, uint32(50052))
require.Equal(t, spec.Ports[0].Target, uint32(50052))
// Ensure that the docker socket is shared in the core
require.Equal(t, spec.Mounts[0].Source, dockerSocket)
require.Equal(t, spec.Mounts[0].Target, dockerSocket)
// Ensure that the host users folder is sync with the core
require.Equal(t, spec.Mounts[1].Source, viper.GetString(config.MESGPath))
require.Equal(t, spec.Mounts[1].Target, "/mesg")
require.Equal(t, spec.Mounts[1].Source, volume)
require.Equal(t, spec.Mounts[1].Target, path)
}
26 changes: 11 additions & 15 deletions service/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,12 @@ package service
import (
"errors"
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"sync"

"github.com/mesg-foundation/core/config"
"github.com/mesg-foundation/core/container"
"github.com/spf13/viper"
"github.com/mesg-foundation/core/x/xstructhash"
)

// Start starts the service.
Expand Down Expand Up @@ -117,33 +114,32 @@ func (dependency *DependencyFromService) extractVolumes() ([]container.Mount, er
if service == nil {
return nil, errors.New("Service is nil")
}
servicePath := strings.Join(service.namespace(), "-")
volumes := make([]container.Mount, 0)
for _, volume := range dependency.Volumes {
path := filepath.Join(servicePath, dependency.Name, volume)
source := filepath.Join(viper.GetString(config.ServicePathHost), path)
volumes = append(volumes, container.Mount{
Source: source,
Source: volumeKey(service, dependency.Name, volume),
Target: volume,
})
// TODO: move mkdir in container package
os.MkdirAll(filepath.Join(viper.GetString(config.ServicePathDocker), path), os.ModePerm)
}
for _, depName := range dependency.Volumesfrom {
dep := service.Dependencies[depName]
if dep == nil {
return nil, fmt.Errorf("Dependency %s do not exist", depName)
}
for _, volume := range dep.Volumes {
path := filepath.Join(servicePath, depName, volume)
source := filepath.Join(viper.GetString(config.ServicePathHost), path)
volumes = append(volumes, container.Mount{
Source: source,
Source: volumeKey(service, depName, volume),
Target: volume,
})
// TODO: move mkdir in container package
os.MkdirAll(filepath.Join(viper.GetString(config.ServicePathDocker), path), os.ModePerm)
}
}
return volumes, nil
}

func volumeKey(s *Service, dependency string, volume string) string {
return xstructhash.Hash([]string{
s.Hash(),
dependency,
volume,
}, 1)
}
54 changes: 54 additions & 0 deletions service/start_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,3 +200,57 @@ func TestServiceDependenciesListensFromSamePort(t *testing.T) {
require.NotZero(t, err)
require.Contains(t, err.Error(), `port '80' is already in use`)
}

func TestExtractVolumes(t *testing.T) {
dep := &DependencyFromService{}
_, err := dep.extractVolumes()
require.NotNil(t, err)

dep = &DependencyFromService{
Name: "test",
Service: &Service{},
Dependency: &Dependency{
Volumes: []string{"foo", "bar"},
},
}
volumes, err := dep.extractVolumes()
require.Nil(t, err)
require.Len(t, volumes, 2)
require.Equal(t, volumeKey(dep.Service, "test", "foo"), volumes[0].Source)
require.Equal(t, "foo", volumes[0].Target)
require.Equal(t, false, volumes[0].Bind)
require.Equal(t, volumeKey(dep.Service, "test", "bar"), volumes[1].Source)
require.Equal(t, "bar", volumes[1].Target)
require.Equal(t, false, volumes[1].Bind)

dep = &DependencyFromService{
Service: &Service{},
Dependency: &Dependency{
Volumesfrom: []string{"test"},
},
}
_, err = dep.extractVolumes()
require.NotNil(t, err)

dep = &DependencyFromService{
Service: &Service{
Dependencies: map[string]*Dependency{
"test": &Dependency{
Volumes: []string{"foo", "bar"},
},
},
},
Dependency: &Dependency{
Volumesfrom: []string{"test"},
},
}
volumes, err = dep.extractVolumes()
require.Nil(t, err)
require.Len(t, volumes, 2)
require.Equal(t, volumeKey(dep.Service, "test", "foo"), volumes[0].Source)
require.Equal(t, "foo", volumes[0].Target)
require.Equal(t, false, volumes[0].Bind)
require.Equal(t, volumeKey(dep.Service, "test", "bar"), volumes[1].Source)
require.Equal(t, "bar", volumes[1].Target)
require.Equal(t, false, volumes[1].Bind)
}