Skip to content

Commit

Permalink
minor fixes and improvements
Browse files Browse the repository at this point in the history
* respect to protobuf field numbers
* add more docs
* return with an err in register() of api/ Server
* cmd/service don't os.Exit() in goroutine
* use -e on dev-core script
* update proto docs
  • Loading branch information
ilgooz committed Aug 17, 2018
1 parent a18ab8d commit 3261b15
Show file tree
Hide file tree
Showing 10 changed files with 164 additions and 137 deletions.
166 changes: 86 additions & 80 deletions api/core/api.pb.go

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions api/core/api.proto
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ service Core {

// Deploy a service to [Core](../guide/start-here/core.md). This will give you an unique identifier which is used to interact with the service.
rpc DeployService (stream DeployServiceRequest) returns (stream DeployServiceReply) {}

// Delete a service from Core. This function only deletes a deployed service in [Core](../guide/start-here/core.md). If the service's code is on your computer, the source code will not be deleted.
rpc DeleteService (DeleteServiceRequest) returns (DeleteServiceReply) {}

Expand Down Expand Up @@ -202,11 +202,11 @@ message DeployServiceRequest {
oneof value {
// Git repo url of service. If url provided, stream will be closed after
// first receive.
string url = 1;
string url = 2;

// Chunks of gzipped tar archive of service. If chunk provided, stream will
// be closed after all chunks sent.
bytes chunk = 2;
bytes chunk = 3;
}
}

Expand All @@ -221,15 +221,15 @@ message DeployServiceRequest {
message DeployServiceReply {
oneof value {
// status will be sent after each status change.
string status = 1;
string status = 2;

// serviceID will be sent as the last message of stream when
// service deployed successfully.
string serviceID = 2;
string serviceID = 3;

// validationError will be sent as the last message of stream when
// there is a validation error.
string validationError = 3;
string validationError = 4;
}
}

Expand Down
5 changes: 4 additions & 1 deletion api/core/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

// DeployService deploys a service from Git URL or service.tar.gz file. It'll send status
// events during the process and finishes with sending service id or validation error.
// events during the process and finish with sending service id or validation error.
func (s *Server) DeployService(stream Core_DeployServiceServer) error {
statuses := make(chan string, 0)
go sendDeployStatus(statuses, stream)
Expand Down Expand Up @@ -64,6 +64,9 @@ func newDeployServiceStreamReader(stream Core_DeployServiceServer) *deployServic

func (r *deployServiceStreamReader) GetURL() (url string, err error) {
message, err := r.stream.Recv()
if err != nil {
return "", err
}
r.data = message.GetChunk()
return message.GetUrl(), err
}
Expand Down
11 changes: 7 additions & 4 deletions api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ func (s *Server) Serve() error {
grpc_logrus.UnaryServerInterceptor(logrus.NewEntry(logrus.StandardLogger())),
)),
)
s.register()
if err := s.register(); err != nil {
return err
}

logrus.Info("Server listens on ", s.listener.Addr())

Expand All @@ -63,19 +65,20 @@ func (s *Server) Stop() {
}

// register all server
func (s *Server) register() {
func (s *Server) register() error {
m, err := mesg.New()
if err != nil {
logrus.Fatal(err)
return err
}

coreServer, err := core.NewServer(core.MESGOption(m))
if err != nil {
logrus.Fatal(err)
return err
}

service.RegisterServiceServer(s.instance, &service.Server{})
core.RegisterCoreServer(s.instance, coreServer)

reflection.Register(s.instance)
return nil
}
16 changes: 8 additions & 8 deletions api/service/api.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 16 additions & 10 deletions cmd/service/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,21 @@ To get more information, see the [deploy page from the documentation](https://do

func deployHandler(cmd *cobra.Command, args []string) {
path := defaultPath(args)
serviceID, err := deployService(path)
serviceID, isValid, err := deployService(path)
if !isValid {
os.Exit(1)
}
utils.HandleError(err)

fmt.Println("Service deployed with ID:", aurora.Green(serviceID))
fmt.Printf("To start it, run the command: mesg-core service start %s\n", serviceID)

}

func deployService(path string) (serviceID string, err error) {
func deployService(path string) (serviceID string, isValid bool, err error) {
stream, err := cli().DeployService(context.Background())
if err != nil {
return "", err
return "", false, err
}

deployment := make(chan deploymentResult)
Expand All @@ -51,20 +54,20 @@ func deployService(path string) (serviceID string, err error) {
if err := stream.Send(&core.DeployServiceRequest{
Value: &core.DeployServiceRequest_Url{Url: path},
}); err != nil {
return "", err
return "", false, err
}
} else {
if err := deployServiceSendServiceContext(path, stream); err != nil {
return "", err
return "", false, err
}
}

if err := stream.CloseSend(); err != nil {
return "", err
return "", false, err
}

result := <-deployment
return result.serviceID, result.err
return result.serviceID, result.isValid, result.err
}

func deployServiceSendServiceContext(path string, stream core.Core_DeployServiceClient) error {
Expand Down Expand Up @@ -98,10 +101,12 @@ func deployServiceSendServiceContext(path string, stream core.Core_DeployService
type deploymentResult struct {
serviceID string
err error
isValid bool
}

func readDeployReply(stream core.Core_DeployServiceClient, deployment chan deploymentResult) {
sp := spinner.New(utils.SpinnerCharset, utils.SpinnerDuration)
result := deploymentResult{isValid: true}

for {
message, err := stream.Recv()
Expand All @@ -125,14 +130,15 @@ func readDeployReply(stream core.Core_DeployServiceClient, deployment chan deplo
sp.Suffix = " " + status
case serviceID != "":
sp.Stop()
deployment <- deploymentResult{serviceID: serviceID}
result.serviceID = serviceID
deployment <- result
return
case validationError != "":
sp.Stop()
fmt.Println(aurora.Red(validationError))
fmt.Println("Run the command 'service validate' for more details")
os.Exit(0)
result.isValid = false
deployment <- result
}

}
}
16 changes: 10 additions & 6 deletions cmd/service/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package service
import (
"context"
"fmt"
"os"

"github.com/logrusorgru/aurora"
"github.com/mesg-foundation/core/api/core"
Expand All @@ -28,7 +29,10 @@ func init() {
}

func devHandler(cmd *cobra.Command, args []string) {
serviceID, err := createService(defaultPath(args))
serviceID, isValid, err := createService(defaultPath(args))
if !isValid {
os.Exit(1)
}
utils.HandleError(err)
fmt.Printf("%s Service started with success\n", aurora.Green("✔"))
fmt.Printf("Service ID: %s\n", aurora.Bold(serviceID))
Expand All @@ -48,18 +52,18 @@ func devHandler(cmd *cobra.Command, args []string) {
})
}

func createService(path string) (string, error) {
serviceID, err := deployService(path)
if err != nil {
return "", err
func createService(path string) (serviceID string, isValid bool, err error) {
serviceID, isValid, err = deployService(path)
if !isValid || err != nil {
return "", isValid, err
}

utils.ShowSpinnerForFunc(utils.SpinnerOptions{Text: "Starting service..."}, func() {
_, err = cli().StartService(context.Background(), &core.StartServiceRequest{
ServiceID: serviceID,
})
})
return serviceID, err
return serviceID, true, err
}

func listenEvents(serviceID string, filter string) {
Expand Down
10 changes: 4 additions & 6 deletions dev-core
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
#!/bin/bash
#!/bin/bash -e

# chain these to stop execution on failure
./scripts/build-test-core.sh && \
./dev-cli stop && \
./dev-cli start && \
./scripts/build-test-core.sh
./dev-cli stop
./dev-cli start
./dev-cli logs

./dev-cli stop
6 changes: 4 additions & 2 deletions docs/api/core.md
Original file line number Diff line number Diff line change
Expand Up @@ -751,8 +751,10 @@ The reply's data of `DeployService` API.
| Field | Type | Description |
| ----- | ---- | ----------- |
| status | [string](#string) | status will be sent after each status change. |
| serviceID | [string](#string) | serviceID will be sent as the last message of stream. |
| validationError | [string](#string) | |
| serviceID | [string](#string) | serviceID will be sent as the last message of stream when
service deployed successfully. |
| validationError | [string](#string) | validationError will be sent as the last message of stream when
there is a validation error. |



Expand Down
33 changes: 19 additions & 14 deletions mesg/deploy_deployer.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func newServiceDeployer(mesg *MESG) *serviceDeployer {
}
}

// FromGitURL deploys a service hosted at a Git url.
func (d *serviceDeployer) FromGitURL(url string) (*service.Service, *importer.ValidationError, error) {
d.sendStatus("Downloading service...")
path, err := d.createTempDir()
Expand All @@ -41,6 +42,22 @@ func (d *serviceDeployer) FromGitURL(url string) (*service.Service, *importer.Va
return d.deploy(path)
}

// FromGzippedTar deploys a service from a gzipped tarball.
func (d *serviceDeployer) FromGzippedTar(r io.Reader) (*service.Service, *importer.ValidationError, error) {
d.sendStatus("Sending service context to core daemon...")
path, err := d.createTempDir()
if err != nil {
return nil, nil, err
}
if err := archive.Untar(r, path, &archive.TarOptions{
Compression: archive.Gzip,
}); err != nil {
return nil, nil, err
}
return d.deploy(path)
}

// gitClone clones a repo hosted at repoURL to path.
func (d *serviceDeployer) gitClone(repoURL string, path string) error {
u, err := url.Parse(repoURL)
if err != nil {
Expand All @@ -59,20 +76,7 @@ func (d *serviceDeployer) gitClone(repoURL string, path string) error {
return err
}

func (d *serviceDeployer) FromGzippedTar(r io.Reader) (*service.Service, *importer.ValidationError, error) {
d.sendStatus("Sending service context to core daemon...")
path, err := d.createTempDir()
if err != nil {
return nil, nil, err
}
if err := archive.Untar(r, path, &archive.TarOptions{
Compression: archive.Gzip,
}); err != nil {
return nil, nil, err
}
return d.deploy(path)
}

// deploy deploys a service in path.
func (d *serviceDeployer) deploy(path string) (*service.Service, *importer.ValidationError, error) {
defer os.RemoveAll(path)

Expand Down Expand Up @@ -118,6 +122,7 @@ func (d *serviceDeployer) createTempDir() (path string, err error) {
return ioutil.TempDir("", "mesg-"+uuid.NewV4().String())
}

// sendStatus sends a status message.
func (d *serviceDeployer) sendStatus(message string) {
if d.Statuses != nil {
d.Statuses <- message
Expand Down

0 comments on commit 3261b15

Please sign in to comment.