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

Add service sdk cosmos version step 2 #1252

Merged
merged 9 commits into from
Aug 27, 2019
10 changes: 5 additions & 5 deletions core/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func main() {
app := cosmos.NewApp(logger.TendermintLogger(), db)

// init sdk.
sdk, err = enginesdk.New(app, c, serviceDB, instanceDB, executionDB, workflowDB, cfg.Name, strconv.Itoa(port))
sdk, err = enginesdk.New(app, c, instanceDB, executionDB, workflowDB, cfg.Name, strconv.Itoa(port))
if err != nil {
logrus.WithField("module", "main").Fatalln(err)
}
Expand All @@ -173,11 +173,11 @@ func main() {
}
} else {
sdk = enginesdk.NewDeprecated(c, serviceDB, instanceDB, executionDB, workflowDB, cfg.Name, strconv.Itoa(port))
}

// init system services.
if err := deployCoreServices(cfg, sdk); err != nil {
logrus.WithField("module", "main").Fatalln(err)
// init system services.
if err := deployCoreServices(cfg, sdk); err != nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just a warning here, core services are just started when we run the deprecated sdk but are stopped all the time. We might have some errors when we stop them.

logrus.WithField("module", "main").Fatalln(err)
}
}

// init gRPC server.
Expand Down
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ require (
github.com/magiconair/properties v1.8.1 // indirect
github.com/mattn/go-isatty v0.0.8 // indirect
github.com/mitchellh/go-homedir v1.1.0
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.1 // indirect
github.com/mr-tron/base58 v1.1.1
github.com/mwitkow/go-proto-validators v0.0.0-20190212092829-1f388280e944 // indirect
github.com/onsi/ginkgo v1.8.0 // indirect
Expand Down
6 changes: 4 additions & 2 deletions sdk/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ type SDK struct {
}

// New creates a new SDK with given options.
func New(app *cosmos.App, c container.Container, serviceDB *database.ServiceDB, instanceDB database.InstanceDB, execDB database.ExecutionDB, workflowDB database.WorkflowDB, engineName, port string) (*SDK, error) {
func New(app *cosmos.App, c container.Container, instanceDB database.InstanceDB, execDB database.ExecutionDB, workflowDB database.WorkflowDB, engineName, port string) (*SDK, error) {
initDefaultAppModules(app)
ps := pubsub.New(0)
initDefaultAppModules(app)
serviceSDK := servicesdk.NewDeprecated(c, serviceDB)
serviceSDK := servicesdk.NewSDK(app)
servicesdk.NewModule(app, c)
instanceSDK := instancesdk.New(c, serviceSDK, instanceDB, engineName, port)
workflowSDK := workflowsdk.New(instanceSDK, workflowDB)
executionSDK := executionsdk.New(ps, serviceSDK, instanceSDK, workflowSDK, execDB)
Expand Down
61 changes: 1 addition & 60 deletions sdk/service/deprecated.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
package servicesdk

import (
"errors"
"io/ioutil"
"net/http"
"os"

"github.com/docker/docker/pkg/archive"
"github.com/mesg-foundation/engine/container"
"github.com/mesg-foundation/engine/database"
"github.com/mesg-foundation/engine/hash"
"github.com/mesg-foundation/engine/hash/dirhash"
"github.com/mesg-foundation/engine/service"
"github.com/mesg-foundation/engine/service/validator"
)

// deprecated exposes service APIs of MESG.
Expand All @@ -31,58 +23,7 @@ func NewDeprecated(c container.Container, serviceDB *database.ServiceDB) Service

// Create creates a new service from definition.
func (s *deprecated) Create(srv *service.Service) (*service.Service, error) {
if srv.Configuration == nil {
srv.Configuration = &service.Configuration{}
}
// download and untar service context into path.
path, err := ioutil.TempDir("", "mesg")
if err != nil {
return nil, err
}
defer os.RemoveAll(path)

resp, err := http.Get("http://ipfs.app.mesg.com:8080/ipfs/" + srv.Source)
if err != nil {
return nil, err
}
if resp.StatusCode != 200 {
return nil, errors.New("service's source code is not reachable")
}
defer resp.Body.Close()

if err := archive.Untar(resp.Body, path, nil); err != nil {
return nil, err
}

// calculate and apply hash to service.
dh := dirhash.New(path)
h, err := dh.Sum(hash.Dump(srv))
if err != nil {
return nil, err
}
srv.Hash = hash.Hash(h)

// check if service already exists.
if _, err := s.serviceDB.Get(srv.Hash); err == nil {
return nil, &AlreadyExistsError{Hash: srv.Hash}
}

// build service's Docker image.
_, err = s.container.Build(path)
if err != nil {
return nil, err
}
// TODO: the following test should be moved in New function
if srv.Sid == "" {
// make sure that sid doesn't have the same length with id.
srv.Sid = "_" + srv.Hash.String()
}

if err := validator.ValidateService(srv); err != nil {
return nil, err
}

return srv, s.serviceDB.Save(srv)
return create(s.container, s.serviceDB, srv)
}

// Delete deletes the service by hash.
Expand Down
98 changes: 98 additions & 0 deletions sdk/service/module.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package servicesdk

import (
"github.com/cosmos/cosmos-sdk/codec"
cosmostypes "github.com/cosmos/cosmos-sdk/types"
"github.com/mesg-foundation/engine/container"
"github.com/mesg-foundation/engine/cosmos"
"github.com/mesg-foundation/engine/database"
"github.com/mesg-foundation/engine/database/store"
"github.com/mesg-foundation/engine/hash"
"github.com/mesg-foundation/engine/service"
abci "github.com/tendermint/tendermint/abci/types"
)

// Module is the service module.
type Module struct {
container container.Container
name string
cdc *codec.Codec
storeKey *cosmostypes.KVStoreKey
}

// NewModule returns the module of the service sdk.
func NewModule(app *cosmos.App, c container.Container) *Module {
name := "service"
module := &Module{
container: c,
name: name,
cdc: app.Cdc(),
storeKey: cosmostypes.NewKVStoreKey(name),
}
appModuleBasic := cosmos.NewAppModuleBasic(name)
appModule := cosmos.NewAppModule(appModuleBasic, module.cdc, module.handler, module.querier)
app.RegisterModule(appModule)
app.RegisterStoreKey(module.storeKey)
return module
}

func (s *Module) db(request cosmostypes.Request) *database.ServiceDB {
return database.NewServiceDB(store.NewCosmosStore(request.KVStore(s.storeKey)))
}

func (s *Module) handler(request cosmostypes.Request, msg cosmostypes.Msg) cosmostypes.Result {
panic("to implement")
// switch msg := msg.(type) {
// case MsgCreateService:
// _, err := s.Create(request, msg.Service)
// if err != nil {
// return cosmostypes.ErrInternal(err.Error()).Result()
// }
// return cosmostypes.Result{}
// case MsgRemoveService:
// err := s.Delete(request, msg.Hash)
// if err != nil {
// return cosmostypes.ErrInternal(err.Error()).Result()
// }
// return cosmostypes.Result{}
// default:
// errmsg := fmt.Sprintf("Unrecognized service Msg type: %v", msg.Type())
// return cosmostypes.ErrUnknownRequest(errmsg).Result()
// }
}

func (s *Module) querier(request cosmostypes.Request, path []string, req abci.RequestQuery) (interface{}, error) {
panic("to implement")
// switch path[0] {
// case "get":
// hash, err := hash.Decode(path[1])
// if err != nil {
// return nil, err
// }
// return s.Get(request, hash)
// case "list":
// return s.All(request)
// default:
// return nil, fmt.Errorf("unknown service query endpoint %q", path[0])
// }
}

// Create creates a new service from definition.
func (s *Module) Create(request cosmostypes.Request, srv *service.Service) (*service.Service, error) {
return create(s.container, s.db(request), srv)
}

// Delete deletes the service by hash.
func (s *Module) Delete(request cosmostypes.Request, hash hash.Hash) error {
return s.db(request).Delete(hash)
}

// Get returns the service that matches given hash.
func (s *Module) Get(request cosmostypes.Request, hash hash.Hash) (*service.Service, error) {
return s.db(request).Get(hash)
}

// List returns all services.
func (s *Module) List(request cosmostypes.Request) ([]*service.Service, error) {
return s.db(request).All()
}
41 changes: 41 additions & 0 deletions sdk/service/sdk.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package servicesdk

import (
"fmt"

"github.com/mesg-foundation/engine/cosmos"
"github.com/mesg-foundation/engine/hash"
"github.com/mesg-foundation/engine/service"
)

type sdk struct {
app *cosmos.App
}

// NewSDK returns the service sdk.
func NewSDK(app *cosmos.App) Service {
sdk := &sdk{
app: app,
}
return sdk
}

// Create creates a new service from definition.
func (s *sdk) Create(srv *service.Service) (*service.Service, error) {
return nil, fmt.Errorf("create not implemented")
}

// Delete deletes the service by hash.
func (s *sdk) Delete(hash hash.Hash) error {
return fmt.Errorf("delete not implemented")
}

// Get returns the service that matches given hash.
func (s *sdk) Get(hash hash.Hash) (*service.Service, error) {
return nil, fmt.Errorf("get not implemented")
}

// List returns all services.
func (s *sdk) List() ([]*service.Service, error) {
return nil, fmt.Errorf("list not implemented")
}
70 changes: 70 additions & 0 deletions sdk/service/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package servicesdk

import (
"errors"
"io/ioutil"
"net/http"
"os"

"github.com/docker/docker/pkg/archive"
"github.com/mesg-foundation/engine/container"
"github.com/mesg-foundation/engine/database"
"github.com/mesg-foundation/engine/hash"
"github.com/mesg-foundation/engine/hash/dirhash"
"github.com/mesg-foundation/engine/service"
"github.com/mesg-foundation/engine/service/validator"
)

func create(container container.Container, db *database.ServiceDB, srv *service.Service) (*service.Service, error) {
if srv.Configuration == nil {
srv.Configuration = &service.Configuration{}
}
// download and untar service context into path.
path, err := ioutil.TempDir("", "mesg")
if err != nil {
return nil, err
}
defer os.RemoveAll(path)

resp, err := http.Get("http://ipfs.app.mesg.com:8080/ipfs/" + srv.Source)
if err != nil {
return nil, err
}
if resp.StatusCode != 200 {
return nil, errors.New("service's source code is not reachable")
}
defer resp.Body.Close()

if err := archive.Untar(resp.Body, path, nil); err != nil {
return nil, err
}

// calculate and apply hash to service.
dh := dirhash.New(path)
h, err := dh.Sum(hash.Dump(srv))
if err != nil {
return nil, err
}
srv.Hash = hash.Hash(h)

// check if service already exists.
if _, err := db.Get(srv.Hash); err == nil {
return nil, &AlreadyExistsError{Hash: srv.Hash}
}

// build service's Docker image.
_, err = container.Build(path)
if err != nil {
return nil, err
}
// TODO: the following test should be moved in New function
if srv.Sid == "" {
// make sure that sid doesn't have the same length with id.
srv.Sid = "_" + srv.Hash.String()
}

if err := validator.ValidateService(srv); err != nil {
return nil, err
}
return srv, db.Save(srv)
}