diff --git a/config/config.go b/config/config.go index 170296357..f8358cc4f 100644 --- a/config/config.go +++ b/config/config.go @@ -38,6 +38,7 @@ type Config struct { Core struct { Image string + Name string Path string } @@ -62,6 +63,7 @@ func New() (*Config, error) { c.Log.Format = "text" c.Log.Level = "info" c.Core.Image = "mesg/core:" + strings.Split(version.Version, " ")[0] + c.Core.Name = "core" c.Core.Path = filepath.Join(home, ".mesg") c.Docker.Core.Path = "/mesg" c.Docker.Socket = "/var/run/docker.sock" @@ -117,8 +119,10 @@ func (c *Config) Validate() error { // DaemonEnv returns the needed environmental variable for the Daemon. func (c *Config) DaemonEnv() map[string]string { return map[string]string{ - "MESG_LOG_FORMAT": c.Log.Format, - "MESG_LOG_LEVEL": c.Log.Level, - "MESG_CORE_PATH": c.Docker.Core.Path, + "MESG_SERVER_ADDRESS": c.Server.Address, + "MESG_LOG_FORMAT": c.Log.Format, + "MESG_LOG_LEVEL": c.Log.Level, + "MESG_CORE_NAME": c.Core.Name, + "MESG_CORE_PATH": c.Docker.Core.Path, } } diff --git a/config/config_test.go b/config/config_test.go index 47dc6b0cf..e000e4195 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -19,6 +19,7 @@ func TestDefaultValue(t *testing.T) { require.Equal(t, "text", c.Log.Format) require.Equal(t, "info", c.Log.Level) require.Equal(t, filepath.Join(home, ".mesg"), c.Core.Path) + require.Equal(t, "core", 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/core:")) @@ -77,6 +78,9 @@ func TestValidate(t *testing.T) { func TestDaemonEnv(t *testing.T) { c, _ := New() env := c.DaemonEnv() - require.Equal(t, c.Log.Format, env["MESG_LOG_FORMAT"]) + 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, c.Core.Name, env["MESG_CORE_NAME"]) + require.Equal(t, c.Docker.Core.Path, env["MESG_CORE_PATH"]) } diff --git a/container/container.go b/container/container.go index 16f101a2c..720f0229a 100644 --- a/container/container.go +++ b/container/container.go @@ -8,6 +8,7 @@ import ( "github.com/docker/docker/api/types/filters" "github.com/docker/docker/api/types/swarm" docker "github.com/docker/docker/client" + "github.com/mesg-foundation/core/config" ) // Container provides high level interactions with Docker API for MESG. @@ -17,6 +18,8 @@ type Container struct { // callTimeout is the timeout value for Docker API calls. callTimeout time.Duration + + config *config.Config } // Option is a configuration func for Container. @@ -31,6 +34,11 @@ func New(options ...Option) (*Container, error) { option(c) } var err error + cfg, err := config.Global() + if err != nil { + return nil, err + } + c.config = cfg if c.client == nil { c.client, err = docker.NewEnvClient() if err != nil { @@ -89,7 +97,7 @@ func (c *Container) FindContainer(namespace []string) (types.ContainerJSON, erro containers, err := c.client.ContainerList(ctx, types.ContainerListOptions{ Filters: filters.NewArgs(filters.KeyValuePair{ Key: "label", - Value: "com.docker.stack.namespace=" + Namespace(namespace), + Value: "com.docker.stack.namespace=" + c.Namespace(namespace), }), Limit: 1, }) diff --git a/container/container_test.go b/container/container_test.go index f315dd6a8..e75ba9132 100644 --- a/container/container_test.go +++ b/container/container_test.go @@ -8,6 +8,7 @@ import ( "github.com/docker/docker/api/types/filters" "github.com/docker/docker/api/types/swarm" + "github.com/mesg-foundation/core/config" "github.com/mesg-foundation/core/container/dockertest" "github.com/stretchr/testify/require" ) @@ -15,6 +16,7 @@ import ( func TestNew(t *testing.T) { dt := dockertest.New() c, err := New(ClientOption(dt.Client())) + cfg, _ := config.Global() require.Nil(t, err) require.NotNil(t, c) @@ -33,7 +35,7 @@ func TestNew(t *testing.T) { require.Equal(t, "0.0.0.0:2377", (<-dt.LastSwarmInit()).Request.ListenAddr) ln := <-dt.LastNetworkCreate() - require.Equal(t, "mesg-shared", ln.Name) + require.Equal(t, cfg.Core.Name, ln.Name) require.Equal(t, types.NetworkCreate{ CheckDuplicate: true, Driver: "overlay", @@ -72,7 +74,7 @@ func TestFindContainerNonExistent(t *testing.T) { require.Equal(t, types.ContainerListOptions{ Filters: filters.NewArgs(filters.KeyValuePair{ Key: "label", - Value: "com.docker.stack.namespace=" + Namespace(namespace), + Value: "com.docker.stack.namespace=" + c.Namespace(namespace), }), Limit: 1, }, (<-dt.LastContainerList()).Options) @@ -103,7 +105,7 @@ func TestFindContainer(t *testing.T) { require.Equal(t, types.ContainerListOptions{ Filters: filters.NewArgs(filters.KeyValuePair{ Key: "label", - Value: "com.docker.stack.namespace=" + Namespace(namespace), + Value: "com.docker.stack.namespace=" + c.Namespace(namespace), }), Limit: 1, }, (<-dt.LastContainerList()).Options) @@ -125,7 +127,7 @@ func TestNonExistentContainerStatus(t *testing.T) { require.Equal(t, STOPPED, status) resp := <-dt.LastServiceInspectWithRaw() - require.Equal(t, Namespace(namespace), resp.ServiceID) + require.Equal(t, c.Namespace(namespace), resp.ServiceID) require.Equal(t, types.ServiceInspectOptions{false}, resp.Options) } diff --git a/container/namespace.go b/container/namespace.go index dd74c7607..c73df34ba 100644 --- a/container/namespace.go +++ b/container/namespace.go @@ -4,14 +4,11 @@ import ( "strings" ) -const ( - namespacePrefix string = "mesg" - namespaceSeparator string = "-" -) +const namespaceSeparator string = "-" // Namespace creates a namespace from a list of string. -func Namespace(ss []string) string { - ssWithPrefix := append([]string{namespacePrefix}, ss...) +func (c *Container) Namespace(ss []string) string { + ssWithPrefix := append([]string{c.config.Core.Name}, ss...) namespace := strings.Join(ssWithPrefix, namespaceSeparator) namespace = strings.Replace(namespace, " ", namespaceSeparator, -1) return namespace diff --git a/container/namespace_test.go b/container/namespace_test.go index a853e1861..70dd46421 100644 --- a/container/namespace_test.go +++ b/container/namespace_test.go @@ -4,15 +4,20 @@ import ( "strings" "testing" + "github.com/mesg-foundation/core/config" "github.com/stretchr/testify/require" ) func TestNamespace(t *testing.T) { - namespace := Namespace([]string{"test"}) - require.Equal(t, namespace, strings.Join([]string{namespacePrefix, "test"}, namespaceSeparator)) + cfg, _ := config.Global() + c, _ := New() + namespace := c.Namespace([]string{"test"}) + require.Equal(t, namespace, strings.Join([]string{cfg.Core.Name, "test"}, namespaceSeparator)) } func TestNamespaceReplaceSpace(t *testing.T) { - namespace := Namespace([]string{"test foo"}) - require.Equal(t, namespace, strings.Join([]string{namespacePrefix, "test-foo"}, namespaceSeparator)) + cfg, _ := config.Global() + c, _ := New() + namespace := c.Namespace([]string{"test foo"}) + require.Equal(t, namespace, strings.Join([]string{cfg.Core.Name, "test-foo"}, namespaceSeparator)) } diff --git a/container/network.go b/container/network.go index d8785e5c8..7f456a9b0 100644 --- a/container/network.go +++ b/container/network.go @@ -17,7 +17,7 @@ func (c *Container) CreateNetwork(namespace []string) (id string, err error) { if network.ID != "" { return network.ID, nil } - namespaceFlat := Namespace(namespace) + namespaceFlat := c.Namespace(namespace) ctx, cancel := context.WithTimeout(context.Background(), c.callTimeout) defer cancel() response, err := c.client.NetworkCreate(ctx, namespaceFlat, types.NetworkCreate{ @@ -69,5 +69,5 @@ func (c *Container) DeleteNetwork(namespace []string) error { func (c *Container) FindNetwork(namespace []string) (types.NetworkResource, error) { ctx, cancel := context.WithTimeout(context.Background(), c.callTimeout) defer cancel() - return c.client.NetworkInspect(ctx, Namespace(namespace), types.NetworkInspectOptions{}) + return c.client.NetworkInspect(ctx, c.Namespace(namespace), types.NetworkInspectOptions{}) } diff --git a/container/network_test.go b/container/network_test.go index cd7ef9203..5d66da7d7 100644 --- a/container/network_test.go +++ b/container/network_test.go @@ -29,12 +29,12 @@ func TestCreateNetwork(t *testing.T) { require.Equal(t, id, networkID) li := <-dt.LastNetworkCreate() - require.Equal(t, Namespace(namespace), li.Name) + require.Equal(t, c.Namespace(namespace), li.Name) require.Equal(t, types.NetworkCreate{ CheckDuplicate: true, Driver: "overlay", Labels: map[string]string{ - "com.docker.stack.namespace": Namespace(namespace), + "com.docker.stack.namespace": c.Namespace(namespace), }, }, li.Options) } @@ -57,7 +57,7 @@ func TestCreateAlreadyExistingNetwork(t *testing.T) { require.Equal(t, id, networkID) li := <-dt.LastNetworkInspect() - require.Equal(t, Namespace(namespace), li.Network) + require.Equal(t, c.Namespace(namespace), li.Network) require.Equal(t, types.NetworkInspectOptions{}, li.Options) select { @@ -88,7 +88,7 @@ func TestDeleteNetwork(t *testing.T) { require.Nil(t, c.DeleteNetwork(namespace)) li := <-dt.LastNetworkInspect() - require.Equal(t, Namespace(namespace), li.Network) + require.Equal(t, c.Namespace(namespace), li.Network) require.Equal(t, types.NetworkInspectOptions{}, li.Options) require.Equal(t, id, (<-dt.LastNetworkRemove()).Network) @@ -150,7 +150,7 @@ func TestFindNetwork(t *testing.T) { require.Equal(t, id, network.ID) li := <-dt.LastNetworkInspect() - require.Equal(t, Namespace(namespace), li.Network) + require.Equal(t, c.Namespace(namespace), li.Network) require.Equal(t, types.NetworkInspectOptions{}, li.Options) } diff --git a/container/service.go b/container/service.go index 1f149e76a..a81704ad7 100644 --- a/container/service.go +++ b/container/service.go @@ -12,14 +12,18 @@ import ( ) // ListServices returns existing docker services matching a specific label name. -func (c *Container) ListServices(label string) ([]swarm.Service, error) { +func (c *Container) ListServices(labels ...string) ([]swarm.Service, error) { + args := make([]filters.KeyValuePair, 0) + for _, label := range labels { + args = append(args, filters.KeyValuePair{ + Key: "label", + Value: label, + }) + } ctx, cancel := context.WithTimeout(context.Background(), c.callTimeout) defer cancel() return c.client.ServiceList(ctx, types.ServiceListOptions{ - Filters: filters.NewArgs(filters.KeyValuePair{ - Key: "label", - Value: label, - }), + Filters: filters.NewArgs(args...), }) } @@ -27,7 +31,7 @@ func (c *Container) ListServices(label string) ([]swarm.Service, error) { func (c *Container) FindService(namespace []string) (swarm.Service, error) { ctx, cancel := context.WithTimeout(context.Background(), c.callTimeout) defer cancel() - service, _, err := c.client.ServiceInspectWithRaw(ctx, Namespace(namespace), + service, _, err := c.client.ServiceInspectWithRaw(ctx, c.Namespace(namespace), types.ServiceInspectOptions{}, ) return service, err @@ -35,7 +39,7 @@ func (c *Container) FindService(namespace []string) (swarm.Service, error) { // StartService starts a docker service. func (c *Container) StartService(options ServiceOptions) (serviceID string, err error) { - service := options.toSwarmServiceSpec() + service := options.toSwarmServiceSpec(c) ctx, cancel := context.WithTimeout(context.Background(), c.callTimeout) defer cancel() response, err := c.client.ServiceCreate(ctx, service, types.ServiceCreateOptions{}) @@ -53,7 +57,7 @@ func (c *Container) StopService(namespace []string) (err error) { if err != nil { return err } - if err := c.client.ServiceRemove(ctx, Namespace(namespace)); err != nil && !docker.IsErrNotFound(err) { + if err := c.client.ServiceRemove(ctx, c.Namespace(namespace)); err != nil && !docker.IsErrNotFound(err) { return err } timeout := 1 * time.Second @@ -68,7 +72,7 @@ func (c *Container) StopService(namespace []string) (err error) { // ServiceLogs returns the logs of a service. func (c *Container) ServiceLogs(namespace []string) (io.ReadCloser, error) { - return c.client.ServiceLogs(context.Background(), Namespace(namespace), + return c.client.ServiceLogs(context.Background(), c.Namespace(namespace), types.ContainerLogsOptions{ ShowStdout: true, ShowStderr: true, diff --git a/container/service_options.go b/container/service_options.go index f4c63acb3..e0a14beaf 100644 --- a/container/service_options.go +++ b/container/service_options.go @@ -33,8 +33,8 @@ type Mount struct { Bind bool } -func (options *ServiceOptions) toSwarmServiceSpec() swarm.ServiceSpec { - namespace := Namespace(options.Namespace) +func (options *ServiceOptions) toSwarmServiceSpec(c *Container) swarm.ServiceSpec { + namespace := c.Namespace(options.Namespace) return swarm.ServiceSpec{ Annotations: swarm.Annotations{ Name: namespace, diff --git a/container/service_options_test.go b/container/service_options_test.go index c0f61991e..bc9bdfb26 100644 --- a/container/service_options_test.go +++ b/container/service_options_test.go @@ -11,8 +11,9 @@ func TestServiceOptionNamespace(t *testing.T) { options := &ServiceOptions{ Namespace: namespace, } - expectedNamespace := Namespace(namespace) - service := options.toSwarmServiceSpec() + c, _ := New() + expectedNamespace := c.Namespace(namespace) + service := options.toSwarmServiceSpec(c) require.Equal(t, expectedNamespace, service.Annotations.Name) require.Equal(t, expectedNamespace, service.Annotations.Labels["com.docker.stack.namespace"]) require.Equal(t, expectedNamespace, service.TaskTemplate.ContainerSpec.Labels["com.docker.stack.namespace"]) @@ -23,7 +24,8 @@ func TestServiceOptionImage(t *testing.T) { options := &ServiceOptions{ Image: image, } - service := options.toSwarmServiceSpec() + c, _ := New() + service := options.toSwarmServiceSpec(c) require.Equal(t, image, service.Annotations.Labels["com.docker.stack.image"]) require.Equal(t, image, service.TaskTemplate.ContainerSpec.Image) } @@ -52,7 +54,8 @@ func TestServiceOptionLabels(t *testing.T) { "label2": "bar", }, } - service := options.toSwarmServiceSpec() + c, _ := New() + service := options.toSwarmServiceSpec(c) require.Equal(t, "foo", service.Annotations.Labels["label1"]) require.Equal(t, "bar", service.Annotations.Labels["label2"]) } @@ -97,7 +100,8 @@ func TestServiceOptionEnv(t *testing.T) { options := &ServiceOptions{ Env: []string{"env1", "env2"}, } - service := options.toSwarmServiceSpec() + c, _ := New() + service := options.toSwarmServiceSpec(c) env := service.TaskTemplate.ContainerSpec.Env require.Equal(t, 2, len(env)) require.Equal(t, "env1", env[0]) diff --git a/container/service_test.go b/container/service_test.go index 28193e4a4..a36b0f11c 100644 --- a/container/service_test.go +++ b/container/service_test.go @@ -30,7 +30,7 @@ func TestStartService(t *testing.T) { require.Equal(t, containerID, id) ls := <-dt.LastServiceCreate() - require.Equal(t, options.toSwarmServiceSpec(), ls.Service) + require.Equal(t, options.toSwarmServiceSpec(c), ls.Service) require.Equal(t, types.ServiceCreateOptions{}, ls.Options) } @@ -56,7 +56,7 @@ func TestStopService(t *testing.T) { }() require.Nil(t, c.StopService(namespace)) - require.Equal(t, Namespace(namespace), (<-dt.LastServiceRemove()).ServiceID) + require.Equal(t, c.Namespace(namespace), (<-dt.LastServiceRemove()).ServiceID) ls := <-dt.LastContainerStop() require.Equal(t, containerID, ls.Container) @@ -98,7 +98,7 @@ func TestFindService(t *testing.T) { require.Equal(t, swarmService.ID, service.ID) li := <-dt.LastServiceInspectWithRaw() - require.Equal(t, Namespace(namespace), li.ServiceID) + require.Equal(t, c.Namespace(namespace), li.ServiceID) require.Equal(t, types.ServiceInspectOptions{}, li.Options) } @@ -114,7 +114,7 @@ func TestFindServiceNotExisting(t *testing.T) { require.Equal(t, dockertest.NotFoundErr{}, err) li := <-dt.LastServiceInspectWithRaw() - require.Equal(t, Namespace(namespace), li.ServiceID) + require.Equal(t, c.Namespace(namespace), li.ServiceID) require.Equal(t, types.ServiceInspectOptions{}, li.Options) } @@ -122,21 +122,20 @@ func TestListServices(t *testing.T) { namespace := []string{"namespace"} namespace1 := []string{"namespace"} label := "1" - swarmServices := []swarm.Service{ - {Spec: swarm.ServiceSpec{Annotations: swarm.Annotations{Name: Namespace(namespace)}}}, - {Spec: swarm.ServiceSpec{Annotations: swarm.Annotations{Name: Namespace(namespace1)}}}, - } - dt := dockertest.New() c, _ := New(ClientOption(dt.Client())) + swarmServices := []swarm.Service{ + {Spec: swarm.ServiceSpec{Annotations: swarm.Annotations{Name: c.Namespace(namespace)}}}, + {Spec: swarm.ServiceSpec{Annotations: swarm.Annotations{Name: c.Namespace(namespace1)}}}, + } dt.ProvideServiceList(swarmServices, nil) services, err := c.ListServices(label) require.Nil(t, err) require.Equal(t, 2, len(services)) - require.Equal(t, Namespace(namespace), services[0].Spec.Name) - require.Equal(t, Namespace(namespace1), services[1].Spec.Name) + require.Equal(t, c.Namespace(namespace), services[0].Spec.Name) + require.Equal(t, c.Namespace(namespace1), services[1].Spec.Name) require.Equal(t, types.ServiceListOptions{ Filters: filters.NewArgs(filters.KeyValuePair{ @@ -164,7 +163,7 @@ func TestServiceLogs(t *testing.T) { require.Equal(t, data, bytes) ll := <-dt.LastServiceLogs() - require.Equal(t, Namespace(namespace), ll.ServiceID) + require.Equal(t, c.Namespace(namespace), ll.ServiceID) require.Equal(t, types.ContainerLogsOptions{ ShowStdout: true, ShowStderr: true, diff --git a/container/shared_network.go b/container/shared_network.go index fdd04ad97..7ae232bb4 100644 --- a/container/shared_network.go +++ b/container/shared_network.go @@ -7,8 +7,6 @@ import ( docker "github.com/docker/docker/client" ) -var sharedNetworkNamespace = []string{"shared"} - // SharedNetworkID returns the ID of the shared network. func (c *Container) SharedNetworkID() (networkID string, err error) { network, err := c.sharedNetwork() @@ -31,7 +29,7 @@ func (c *Container) createSharedNetworkIfNeeded() error { defer cancel() // Create the new network needed to run containers. - namespace := Namespace(sharedNetworkNamespace) + namespace := c.Namespace([]string{}) _, err = c.client.NetworkCreate(ctx, namespace, types.NetworkCreate{ CheckDuplicate: true, Driver: "overlay", @@ -46,5 +44,5 @@ func (c *Container) createSharedNetworkIfNeeded() error { func (c *Container) sharedNetwork() (network types.NetworkResource, err error) { ctx, cancel := context.WithTimeout(context.Background(), c.callTimeout) defer cancel() - return c.client.NetworkInspect(ctx, Namespace(sharedNetworkNamespace), types.NetworkInspectOptions{}) + return c.client.NetworkInspect(ctx, c.Namespace([]string{}), types.NetworkInspectOptions{}) } diff --git a/container/shared_network_test.go b/container/shared_network_test.go index 7b60e4705..278384907 100644 --- a/container/shared_network_test.go +++ b/container/shared_network_test.go @@ -25,7 +25,7 @@ func TestSharedNetwork(t *testing.T) { require.Equal(t, id, network.ID) li := <-dt.LastNetworkInspect() - require.Equal(t, Namespace(sharedNetworkNamespace), li.Network) + require.Equal(t, c.Namespace([]string{}), li.Network) require.Equal(t, types.NetworkInspectOptions{}, li.Options) } @@ -42,12 +42,12 @@ func TestCreateSharedNetworkIfNeeded(t *testing.T) { require.Nil(t, c.createSharedNetworkIfNeeded()) lc := <-dt.LastNetworkCreate() - require.Equal(t, Namespace(sharedNetworkNamespace), lc.Name) + require.Equal(t, c.Namespace([]string{}), lc.Name) require.Equal(t, types.NetworkCreate{ CheckDuplicate: true, Driver: "overlay", Labels: map[string]string{ - "com.docker.stack.namespace": Namespace(sharedNetworkNamespace), + "com.docker.stack.namespace": c.Namespace([]string{}), }, }, lc.Options) } @@ -89,6 +89,6 @@ func TestSharedNetworkID(t *testing.T) { require.Equal(t, network, id) li := <-dt.LastNetworkInspect() - require.Equal(t, Namespace(sharedNetworkNamespace), li.Network) + require.Equal(t, c.Namespace([]string{}), li.Network) require.Equal(t, types.NetworkInspectOptions{}, li.Options) } diff --git a/container/task.go b/container/task.go index ff2ab32ed..526a77ede 100644 --- a/container/task.go +++ b/container/task.go @@ -15,7 +15,7 @@ func (c *Container) ListTasks(namespace []string) ([]swarm.Task, error) { return c.client.TaskList(ctx, types.TaskListOptions{ Filters: filters.NewArgs(filters.KeyValuePair{ Key: "label", - Value: "com.docker.stack.namespace=" + Namespace(namespace), + Value: "com.docker.stack.namespace=" + c.Namespace(namespace), }), }) } diff --git a/container/task_test.go b/container/task_test.go index 1ac41f494..9c81e14ee 100644 --- a/container/task_test.go +++ b/container/task_test.go @@ -31,7 +31,7 @@ func TestListTasks(t *testing.T) { require.Equal(t, types.TaskListOptions{ Filters: filters.NewArgs(filters.KeyValuePair{ Key: "label", - Value: "com.docker.stack.namespace=" + Namespace(namespace), + Value: "com.docker.stack.namespace=" + c.Namespace(namespace), }), }, (<-dt.LastTaskList()).Options) } diff --git a/daemon/const.go b/daemon/const.go deleted file mode 100644 index f0c88651c..000000000 --- a/daemon/const.go +++ /dev/null @@ -1,10 +0,0 @@ -package daemon - -const ( - name = "core" -) - -// Namespace returns the namespace of the MESG Core. -func Namespace() []string { - return []string{name} -} diff --git a/daemon/logs.go b/daemon/logs.go index 77e362781..117c4be16 100644 --- a/daemon/logs.go +++ b/daemon/logs.go @@ -6,5 +6,5 @@ import ( // Logs returns the core's docker service logs. func Logs() (io.ReadCloser, error) { - return defaultContainer.ServiceLogs(Namespace()) + return defaultContainer.ServiceLogs([]string{}) } diff --git a/daemon/start.go b/daemon/start.go index ceebf6fc4..858febbf1 100644 --- a/daemon/start.go +++ b/daemon/start.go @@ -33,7 +33,7 @@ func serviceSpec() (spec container.ServiceOptions, err error) { return container.ServiceOptions{}, err } return container.ServiceOptions{ - Namespace: Namespace(), + Namespace: []string{}, Image: c.Core.Image, Env: container.MapToEnv(c.DaemonEnv()), Mounts: []container.Mount{ diff --git a/daemon/start_test.go b/daemon/start_test.go index 64a412f0d..5b2d73ee9 100644 --- a/daemon/start_test.go +++ b/daemon/start_test.go @@ -23,7 +23,7 @@ func startForTest() { panic(err) } _, err = defaultContainer.StartService(container.ServiceOptions{ - Namespace: Namespace(), + Namespace: []string{}, Image: "http-server", NetworksID: []string{sharedNetworkID}, }) @@ -37,6 +37,7 @@ func TestStartConfig(t *testing.T) { c, _ := config.Global() spec, err := serviceSpec() require.NoError(t, err) + require.Equal(t, []string{}, spec.Namespace) // Make sure that the config directory is passed in parameter to write on the same folder require.Contains(t, spec.Env, "MESG_LOG_LEVEL=info") require.Contains(t, spec.Env, "MESG_LOG_FORMAT=text") diff --git a/daemon/status.go b/daemon/status.go index 0e7bbf9a1..de982c516 100644 --- a/daemon/status.go +++ b/daemon/status.go @@ -6,5 +6,5 @@ import ( // Status returns the Status of the docker service of the daemon. func Status() (container.StatusType, error) { - return defaultContainer.Status(Namespace()) + return defaultContainer.Status([]string{}) } diff --git a/daemon/stop.go b/daemon/stop.go index 246b32aac..3e395bcf8 100644 --- a/daemon/stop.go +++ b/daemon/stop.go @@ -10,5 +10,5 @@ func Stop() error { if err != nil || status == container.STOPPED { return err } - return defaultContainer.StopService(Namespace()) + return defaultContainer.StopService([]string{}) } diff --git a/service/start.go b/service/start.go index 42df8e46c..1266887a7 100644 --- a/service/start.go +++ b/service/start.go @@ -68,12 +68,13 @@ func (d *Dependency) Start(networkID string) (containerServiceID string, err err return "", err } _, port, err := xnet.SplitHostPort(c.Server.Address) - endpoint := "mesg-core:" + strconv.Itoa(port) // TODO: should get this from daemon namespace and config + endpoint := c.Core.Name + ":" + strconv.Itoa(port) return d.service.docker.StartService(container.ServiceOptions{ Namespace: d.namespace(), Labels: map[string]string{ "mesg.service": d.service.Name, "mesg.hash": d.service.ID, + "mesg.core": c.Core.Name, }, Image: d.Image, Args: strings.Fields(d.Command), diff --git a/service/start_test.go b/service/start_test.go index 3d1e94bf4..6b786f7c3 100644 --- a/service/start_test.go +++ b/service/start_test.go @@ -5,7 +5,6 @@ import ( "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/swarm" - "github.com/mesg-foundation/core/container" "github.com/mesg-foundation/core/container/dockertest" "github.com/mesg-foundation/core/x/xstrings" "github.com/stretchr/testify/require" @@ -109,7 +108,7 @@ func TestStartService(t *testing.T) { lc := <-dt.LastServiceCreate() require.Equal(t, types.ServiceCreateOptions{}, lc.Options) - require.Equal(t, container.Namespace([]string{s.ID, dependencyKey}), lc.Service.Name) + require.Equal(t, s.docker.Namespace([]string{s.ID, dependencyKey}), lc.Service.Name) } func TestStartWith2Dependencies(t *testing.T) { diff --git a/service/status.go b/service/status.go index bcc2b6d6f..f144df2e9 100644 --- a/service/status.go +++ b/service/status.go @@ -1,6 +1,7 @@ package service import ( + "github.com/mesg-foundation/core/config" "github.com/mesg-foundation/core/container" ) @@ -70,16 +71,18 @@ func (d *Dependency) Status() (container.StatusType, error) { // ListRunning returns all the running services.2 // TODO: should move to another file func ListRunning() ([]string, error) { + cfg, err := config.Global() // TODO(ilgooz): remove this line after ListRunning refactored. c, err := container.New() if err != nil { return nil, err } - services, err := c.ListServices("mesg.hash") + services, err := c.ListServices("mesg.hash", "mesg.core="+cfg.Core.Name) if err != nil { return nil, err } + // Make service list unique. One mesg service can have multiple docker service. mapRes := make(map[string]uint) for _, service := range services { serviceName := service.Spec.Annotations.Labels["mesg.hash"]