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

service: use custom Service, Event, Task, Output, Parameter types #414

Merged
merged 8 commits into from Sep 4, 2018
3 changes: 2 additions & 1 deletion api/deploy_deployer.go
Expand Up @@ -107,6 +107,7 @@ func (d *serviceDeployer) deploy(path string) (*service.Service, *importer.Valid
d.sendStatus(fmt.Sprintf("%s Image built with success.", aurora.Green("✔")), DONE)
d.adjustFields(s)
d.injectConfigurationInDependencies(s, imageHash)
s.ID = s.Hash()

d.sendStatus(fmt.Sprintf("%s Completed.", aurora.Green("✔")), DONE)
return s, nil, services.Save(s)
Expand Down Expand Up @@ -137,7 +138,7 @@ func (d *serviceDeployer) injectConfigurationInDependencies(s *service.Service,
Command: config.Command,
Ports: config.Ports,
Volumes: config.Volumes,
Volumesfrom: config.Volumesfrom,
VolumesFrom: config.VolumesFrom,
Image: imageHash,
}
if s.Dependencies == nil {
Expand Down
4 changes: 2 additions & 2 deletions api/deploy_test.go
Expand Up @@ -33,7 +33,7 @@ func TestDeployService(t *testing.T) {
service, validationError, err := a.DeployService(archive, DeployServiceStatusOption(statuses))
require.Nil(t, validationError)
require.Nil(t, err)
require.Equal(t, 1, structhash.Version(service.Id))
require.Equal(t, 1, structhash.Version(service.ID))
}()

require.Equal(t, DeployStatus{
Expand Down Expand Up @@ -119,7 +119,7 @@ func TestDeployServiceFromURL(t *testing.T) {
service, validationError, err := a.DeployServiceFromURL(url, DeployServiceStatusOption(statuses))
require.Nil(t, validationError)
require.Nil(t, err)
require.Equal(t, 1, structhash.Version(service.Id))
require.Equal(t, 1, structhash.Version(service.ID))
}()

require.Equal(t, DeployStatus{
Expand Down
2 changes: 1 addition & 1 deletion cmd/service/delete.go
Expand Up @@ -43,7 +43,7 @@ func deleteHandler(cmd *cobra.Command, args []string) {
return
}
for _, service := range reply.Services {
args = append(args, service.Hash())
args = append(args, service.ID)
}
}
if len(args) == 0 {
Expand Down
12 changes: 9 additions & 3 deletions cmd/service/execute.go
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/logrusorgru/aurora"
"github.com/mesg-foundation/core/cmd/utils"
"github.com/mesg-foundation/core/database/services"
"github.com/mesg-foundation/core/interface/grpc/core"
"github.com/mesg-foundation/core/service"
"github.com/mesg-foundation/core/x/xpflag"
Expand Down Expand Up @@ -51,7 +52,12 @@ func executeHandler(cmd *cobra.Command, args []string) {
})
utils.HandleError(err)
taskKey := getTaskKey(cmd, serviceReply.Service)
taskData, err := getData(cmd, taskKey, serviceReply.Service, executeData)

// TODO(ilgooz) rm this when we stop using internal methods of service in cmd.
sv, err := services.Get(serviceReply.Service.ID)
utils.HandleError(err)

taskData, err := getData(cmd, taskKey, &sv, executeData)
utils.HandleError(err)

// Create an unique tag that will be used to listen to the result of this exact execution
Expand Down Expand Up @@ -90,15 +96,15 @@ func executeTask(serviceID string, task string, data string, tags []string) (exe
})
}

func taskKeysFromService(s *service.Service) []string {
func taskKeysFromService(s *core.Service) []string {
var taskKeys []string
for key := range s.Tasks {
taskKeys = append(taskKeys, key)
}
return taskKeys
}

func getTaskKey(cmd *cobra.Command, s *service.Service) string {
func getTaskKey(cmd *cobra.Command, s *core.Service) string {
taskKey := cmd.Flag("task").Value.String()
if taskKey == "" {
if survey.AskOne(&survey.Select{
Expand Down
7 changes: 4 additions & 3 deletions cmd/service/execute_test.go
Expand Up @@ -3,6 +3,7 @@ package service
import (
"testing"

"github.com/mesg-foundation/core/interface/grpc/core"
"github.com/mesg-foundation/core/service"
"github.com/mesg-foundation/core/x/xpflag"
"github.com/spf13/cobra"
Expand All @@ -23,8 +24,8 @@ func TestReadJSONFile(t *testing.T) {
}

func TestTaskKeysFromService(t *testing.T) {
keys := taskKeysFromService(&service.Service{
Tasks: map[string]*service.Task{
keys := taskKeysFromService(&core.Service{
Tasks: map[string]*core.Task{
"task1": {},
},
})
Expand Down Expand Up @@ -100,7 +101,7 @@ func TestGetTaskKey(t *testing.T) {
cmd := cobra.Command{}
cmd.Flags().StringP("task", "", "", "")
cmd.Flags().Set("task", "test")
require.Equal(t, "test", getTaskKey(&cmd, &service.Service{}))
require.Equal(t, "test", getTaskKey(&cmd, &core.Service{}))
}

func TestExecutePreRun(t *testing.T) {
Expand Down
6 changes: 3 additions & 3 deletions cmd/service/list.go
Expand Up @@ -53,16 +53,16 @@ To have more details, see the [detail command](mesg-core_service_detail.md).`,
func listHandler(cmd *cobra.Command, args []string) {
reply, err := cli().ListServices(context.Background(), &core.ListServicesRequest{})
utils.HandleError(err)
status, err := servicesWithStatus(reply.Services)
status, err := servicesWithStatus(toServices(reply.Services))
utils.HandleError(err)
sort.Sort(byStatus(status))
for _, serviceStatus := range status {
fmt.Println(serviceStatus)
}
}

func servicesWithStatus(services []*service.Service) (status []serviceStatus, err error) {
for _, s := range services {
func servicesWithStatus(ss []*service.Service) (status []serviceStatus, err error) {
for _, s := range ss {
st, err := s.Status()
if err != nil {
break
Expand Down
8 changes: 7 additions & 1 deletion cmd/service/logs.go
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/docker/docker/pkg/stdcopy"
"github.com/mesg-foundation/core/cmd/utils"
"github.com/mesg-foundation/core/database/services"
"github.com/mesg-foundation/core/interface/grpc/core"
"github.com/mesg-foundation/core/x/xsignal"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -37,7 +38,12 @@ func showLogs(serviceID string, dependency string) func() {
ServiceID: serviceID,
})
utils.HandleError(err)
readers, err := reply.Service.Logs(dependency)

// TODO(ilgooz) rm this when we stop using internal methods of service in cmd.
s, err := services.Get(reply.Service.ID)
utils.HandleError(err)

readers, err := s.Logs(dependency)
utils.HandleError(err)
for _, reader := range readers {
go stdcopy.StdCopy(os.Stdout, os.Stderr, reader)
Expand Down
63 changes: 63 additions & 0 deletions cmd/service/service.go
@@ -0,0 +1,63 @@
package service

import (
"github.com/mesg-foundation/core/interface/grpc/core"
"github.com/mesg-foundation/core/service"
)

// TODO(ilgooz) rm this when we stop using internal methods of service in cmd.
func toServices(ss []*core.Service) []*service.Service {
services := make([]*service.Service, 0)
for _, s := range ss {
services = append(services, toService(s))
}
return services
}

// TODO(ilgooz) rm this when we stop using internal methods of service in cmd.
func toService(s *core.Service) *service.Service {
sv := &service.Service{
ID: s.ID,
Name: s.Name,
Description: s.Description,
Tasks: map[string]*service.Task{},
}

for taskKey, task := range s.Tasks {
t := &service.Task{
Key: task.Key,
Name: task.Name,
Description: task.Description,
ServiceName: task.ServiceName,
Inputs: toParameters(task.Inputs),
Outputs: map[string]*service.Output{},
}
for outputKey, output := range task.Outputs {
t.Outputs[outputKey] = &service.Output{
Key: output.Key,
Name: output.Name,
Description: output.Description,
TaskKey: output.TaskKey,
ServiceName: output.ServiceName,
Data: toParameters(output.Data),
}
}
sv.Tasks[taskKey] = t
}

return sv
}

// TODO(ilgooz) rm this when we stop using internal methods of service in cmd.
func toParameters(params map[string]*core.Parameter) map[string]*service.Parameter {
gParams := make(map[string]*service.Parameter, 0)
for key, param := range params {
gParams[key] = &service.Parameter{
Name: param.Name,
Description: param.Description,
Type: param.Type,
Optional: param.Optional,
}
}
return gParams
}
5 changes: 3 additions & 2 deletions database/services/all.go
@@ -1,7 +1,8 @@
package services

import (
"github.com/golang/protobuf/proto"
"encoding/json"

"github.com/mesg-foundation/core/service"
)

Expand All @@ -16,7 +17,7 @@ func All() ([]*service.Service, error) {
iter := db.NewIterator(nil, nil)
for iter.Next() {
var service service.Service
if err := proto.Unmarshal(iter.Value(), &service); err != nil {
if err := json.Unmarshal(iter.Value(), &service); err != nil {
antho1404 marked this conversation as resolved.
Show resolved Hide resolved
return nil, err
}
services = append(services, &service)
Expand Down
2 changes: 1 addition & 1 deletion database/services/all_test.go
Expand Up @@ -10,7 +10,7 @@ import (
func TestAll(t *testing.T) {
service := &service.Service{Name: "Service1"}
Save(service)
defer Delete(service.Id)
defer Delete(service.ID)
services, err := All()
founded := false
for _, s := range services {
Expand Down
4 changes: 2 additions & 2 deletions database/services/db_test.go
Expand Up @@ -23,11 +23,11 @@ func TestConcurrency(t *testing.T) {
Name: "TestConcurrency",
}
Save(service)
defer Delete(service.Id)
defer Delete(service.ID)
for i := 0; i < 5; i++ {
wg.Add(1)
go func() {
s, err := Get(service.Id)
s, err := Get(service.ID)
require.Nil(t, err)
require.Equal(t, s.Name, service.Name)
wg.Done()
Expand Down
2 changes: 1 addition & 1 deletion database/services/delete_test.go
Expand Up @@ -12,6 +12,6 @@ func TestDelete(t *testing.T) {
Name: "TestDelete",
}
Save(service)
err := Delete(service.Id)
err := Delete(service.ID)
require.Nil(t, err)
}
5 changes: 3 additions & 2 deletions database/services/get.go
@@ -1,7 +1,8 @@
package services

import (
"github.com/golang/protobuf/proto"
"encoding/json"

"github.com/mesg-foundation/core/service"
)

Expand All @@ -17,6 +18,6 @@ func Get(hash string) (service service.Service, err error) {
err = handleErrorNotFound(err, hash)
return
}
err = proto.Unmarshal(bytes, &service)
err = json.Unmarshal(bytes, &service)
return
}
4 changes: 2 additions & 2 deletions database/services/get_test.go
Expand Up @@ -12,8 +12,8 @@ func TestGet(t *testing.T) {
Name: "TestGet",
}
Save(service)
defer Delete(service.Id)
srv, err := Get(service.Id)
defer Delete(service.ID)
srv, err := Get(service.ID)
require.Nil(t, err)
require.Equal(t, srv.Name, "TestGet")
}
Expand Down
13 changes: 8 additions & 5 deletions database/services/save.go
@@ -1,13 +1,14 @@
package services

import (
"github.com/golang/protobuf/proto"
"encoding/json"

"github.com/mesg-foundation/core/service"
)

// Save stores a service in the database and returns a hash or an error.
func Save(service *service.Service) error {
bytes, err := proto.Marshal(service)
bytes, err := json.Marshal(service)
if err != nil {
return err
}
Expand All @@ -16,7 +17,9 @@ func Save(service *service.Service) error {
if err != nil {
return err
}
hash := service.Hash()
service.Id = hash
return db.Put([]byte(hash), bytes, nil)

// TODO(ilgooz) rm this when we have a New() initializer for Service type.
service.ID = service.Hash()

return db.Put([]byte(service.ID), bytes, nil)
}
9 changes: 4 additions & 5 deletions database/services/save_test.go
Expand Up @@ -11,20 +11,19 @@ func TestSaveReturningHash(t *testing.T) {
service := &service.Service{
Name: "TestSaveReturningHash",
}
calculatedHash := service.Hash()

err := Save(service)
defer Delete(service.Id)
defer Delete(service.ID)
require.Nil(t, err)
require.Equal(t, calculatedHash, service.Id)
require.Equal(t, service.Hash(), service.ID)
}

func TestSaveAndPresentInDB(t *testing.T) {
service := &service.Service{
Name: "TestSaveAndPresentInDB",
}
Save(service)
defer Delete(service.Id)
srv, _ := Get(service.Id)
defer Delete(service.ID)
srv, _ := Get(service.ID)
require.Equal(t, srv.Name, "TestSaveAndPresentInDB")
}
4 changes: 2 additions & 2 deletions docs/api/core.md
Expand Up @@ -1053,7 +1053,7 @@ The reply's data of the `ListServices` API.

| Field | Type | Description |
| ----- | ---- | ----------- |
| services | [service.Service](#service.Service)[] | The list of previously-deployed services' definitions. [Details here](./service-type.md). |
| services | [core.Service](#core.Service)[] | The list of previously-deployed services' definitions. [Details here](./service-type.md). |



Expand Down Expand Up @@ -1200,7 +1200,7 @@ The reply's data of the `GetService` API.

| Field | Type | Description |
| ----- | ---- | ----------- |
| service | [service.Service](#service.Service) | Service's definition. [Details here](./service-type.md). |
| service | [core.Service](#core.Service) | Service's definition. [Details here](./service-type.md). |



Expand Down