Skip to content

Commit

Permalink
Merge pull request #1102 from mesg-foundation/fix/instance
Browse files Browse the repository at this point in the history
Fix instance start API
  • Loading branch information
krhubert authored Jun 25, 2019
2 parents 979b4f5 + de2b830 commit f7c63a4
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 67 deletions.
5 changes: 1 addition & 4 deletions sdk/instance/helpers.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
package instancesdk

import (
"crypto/sha1"
"strconv"
"strings"

"github.com/mesg-foundation/core/container"
"github.com/mesg-foundation/core/hash"
"github.com/mesg-foundation/core/service"
"github.com/mr-tron/base58"
)

// instanceNamespace returns the namespace of the service.
func instanceNamespace(hash hash.Hash) []string {
sum := sha1.Sum(hash)
return []string{base58.Encode(sum[:])}
return []string{hash.String()}
}

// dependencyNamespace builds the namespace of a dependency.
Expand Down
10 changes: 5 additions & 5 deletions sdk/instance/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (i *Instance) Create(serviceHash hash.Hash, env []string) (*instance.Instan
}

// build service's Docker image and apply to service.
_, err = i.container.Build(path)
imageHash, err := i.container.Build(path)
if err != nil {
return nil, err
}
Expand All @@ -102,16 +102,16 @@ func (i *Instance) Create(serviceHash hash.Hash, env []string) (*instance.Instan
}

// save & start instance.
o := &instance.Instance{
inst := &instance.Instance{
Hash: instanceHash,
ServiceHash: srv.Hash,
}
if err := i.instanceDB.Save(o); err != nil {
if err := i.instanceDB.Save(inst); err != nil {
return nil, err
}

_, err = i.start(o, xos.EnvMapToSlice(instanceEnv))
return o, err
_, err = i.start(inst, imageHash, xos.EnvMapToSlice(instanceEnv))
return inst, err
}

// Delete deletes an instance.
Expand Down
86 changes: 56 additions & 30 deletions sdk/instance/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ import (
)

// Start starts the service.
func (i *Instance) start(inst *instance.Instance, env []string) (serviceIDs []string, err error) {
func (i *Instance) start(inst *instance.Instance, imageHash string, env []string) (serviceIDs []string, err error) {
srv, err := i.service.Get(inst.ServiceHash)
if err != nil {
return nil, err
}
sNamespace := instanceNamespace(inst.Hash)
networkID, err := i.container.CreateNetwork(sNamespace)
instNamespace := instanceNamespace(inst.Hash)
networkID, err := i.container.CreateNetwork(instNamespace)
if err != nil {
return nil, err
}
Expand All @@ -33,47 +33,73 @@ func (i *Instance) start(inst *instance.Instance, env []string) (serviceIDs []st
endpoint := conf.Name + ":" + strconv.Itoa(port)
// BUG: https://github.com/mesg-foundation/core/issues/382
// After solving this by docker, switch back to deploy in parallel
serviceIDs = make([]string, 0)
for _, d := range append(srv.Dependencies, srv.Configuration) {
// Service.Configuration can be nil so, here is a check for it.
if d == nil {
continue
}
configs := make([]container.ServiceOptions, 0)

labels := map[string]string{
"mesg.service": srv.Name,
"mesg.hash": inst.Hash.String(),
"mesg.sid": srv.Sid,
"mesg.engine": conf.Name,
}

// Create dependency container configs
for _, d := range srv.Dependencies {
volumes := extractVolumes(srv, d)
volumesFrom, err := extractVolumesFrom(srv, d)
if err != nil {
return nil, err
}

serviceID, err := i.container.StartService(container.ServiceOptions{
Namespace: dependencyNamespace(sNamespace, d.Key),
Labels: map[string]string{
"mesg.service": srv.Name,
"mesg.hash": inst.Hash.String(),
"mesg.sid": srv.Sid,
"mesg.engine": conf.Name,
},
Image: d.Image,
Args: d.Args,
Command: d.Command,
Env: xos.EnvMergeSlices(env, []string{
"MESG_TOKEN=" + inst.Hash.String(),
"MESG_ENDPOINT=" + endpoint,
"MESG_ENDPOINT_TCP=" + endpoint,
}),
Mounts: append(volumes, volumesFrom...),
Ports: extractPorts(d),
configs = append(configs, container.ServiceOptions{
Namespace: dependencyNamespace(instNamespace, d.Key),
Labels: labels,
Image: d.Image,
Args: d.Args,
Command: d.Command,
Env: d.Env,
Mounts: append(volumes, volumesFrom...),
Ports: extractPorts(d),
Networks: []container.Network{
{ID: networkID, Alias: d.Key},
{ID: sharedNetworkID},
{ID: sharedNetworkID}, // TODO: to remove
},
})
if err != nil {
}

// Create configuration container config
volumes := extractVolumes(srv, srv.Configuration)
volumesFrom, err := extractVolumesFrom(srv, srv.Configuration)
if err != nil {
return nil, err
}
configs = append(configs, container.ServiceOptions{
Namespace: dependencyNamespace(instNamespace, srv.Configuration.Key),
Labels: labels,
Image: imageHash,
Args: srv.Configuration.Args,
Command: srv.Configuration.Command,
Env: xos.EnvMergeSlices(env, []string{
"MESG_TOKEN=" + inst.Hash.String(),
"MESG_ENDPOINT=" + endpoint,
"MESG_ENDPOINT_TCP=" + endpoint,
}),
Mounts: append(volumes, volumesFrom...),
Ports: extractPorts(srv.Configuration),
Networks: []container.Network{
{ID: networkID, Alias: srv.Configuration.Key},
{ID: sharedNetworkID},
},
})

// Start
serviceIDs = make([]string, 0)
for _, c := range configs {
serviceID, err := i.container.StartService(c)
if err != nil {
i.stop(inst)
return nil, err
}
serviceIDs = append(serviceIDs, serviceID)
}

return serviceIDs, nil
}
15 changes: 10 additions & 5 deletions server/grpc/api/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,17 @@ func NewInstanceServer(sdk *sdk.SDK) *InstanceServer {

// List instances.
func (s *InstanceServer) List(ctx context.Context, request *protobuf_api.ListInstancesRequest) (*protobuf_api.ListInstancesResponse, error) {
hash, err := hash.Decode(request.ServiceHash)
if err != nil {
return nil, err
var (
h hash.Hash
err error
)
if request.ServiceHash != "" {
h, err = hash.Decode(request.ServiceHash)
if err != nil {
return nil, err
}
}

instances, err := s.sdk.Instance.List(&instancesdk.Filter{ServiceHash: hash})
instances, err := s.sdk.Instance.List(&instancesdk.Filter{ServiceHash: h})
if err != nil {
return nil, err
}
Expand Down
13 changes: 3 additions & 10 deletions server/grpc/api/mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ func fromProtoParameters(params []*definition.Parameter) []*service.Parameter {

func fromProtoConfiguration(configuration *definition.Configuration) *service.Dependency {
if configuration == nil {
return nil
return &service.Dependency{
Key: service.MainServiceKey,
}
}
return &service.Dependency{
Key: service.MainServiceKey,
Expand All @@ -79,9 +81,6 @@ func fromProtoConfiguration(configuration *definition.Configuration) *service.De
}

func fromProtoDependency(dep *definition.Dependency) *service.Dependency {
if dep == nil {
return nil
}
return &service.Dependency{
Key: dep.Key,
Image: dep.Image,
Expand Down Expand Up @@ -162,9 +161,6 @@ func toProtoParameters(params []*service.Parameter) []*definition.Parameter {
}

func toProtoConfiguration(configuration *service.Dependency) *definition.Configuration {
if configuration == nil {
return nil
}
return &definition.Configuration{
Args: configuration.Args,
Command: configuration.Command,
Expand All @@ -176,9 +172,6 @@ func toProtoConfiguration(configuration *service.Dependency) *definition.Configu
}

func toProtoDependency(dep *service.Dependency) *definition.Dependency {
if dep == nil {
return nil
}
return &definition.Dependency{
Key: dep.Key,
Image: dep.Image,
Expand Down
24 changes: 11 additions & 13 deletions service/service_validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func ValidateService(service *Service) error {
}

// validate configuration image
if service.Configuration != nil && service.Configuration.Image != "" {
if service.Configuration.Image != "" {
errs = append(errs, errors.New("configuration.image is not allowed"))
}

Expand All @@ -49,20 +49,18 @@ func ValidateService(service *Service) error {
}

// validate configuration volumes
if service.Configuration != nil {
for _, depVolumeKey := range service.Configuration.VolumesFrom {
found := false
for _, s := range service.Dependencies {
if s.Key == depVolumeKey {
found = true
break
}
}
if !found {
err := fmt.Errorf("configuration.volumesFrom is invalid: dependency %q does not exist", depVolumeKey)
errs = append(errs, err)
for _, depVolumeKey := range service.Configuration.VolumesFrom {
found := false
for _, s := range service.Dependencies {
if s.Key == depVolumeKey {
found = true
break
}
}
if !found {
err := fmt.Errorf("configuration.volumesFrom is invalid: dependency %q does not exist", depVolumeKey)
errs = append(errs, err)
}
}

// validate dependencies volumes
Expand Down

0 comments on commit f7c63a4

Please sign in to comment.