set deploy statuses in app service (#354)

This commit is contained in:
Asim Aslam
2022-01-20 15:09:04 +00:00
committed by GitHub
parent cffc586609
commit 574a0d376c
2 changed files with 88 additions and 53 deletions

10
app/domain/domain.go Normal file
View File

@@ -0,0 +1,10 @@
package domain
type Status string
const (
StatusDeploying = "Deploying"
StatusUpdating = "Updating"
StatusDeleting = "Deleting"
StatusDeploymentError = "DeploymentError"
)

View File

@@ -17,6 +17,7 @@ import (
log "github.com/micro/micro/v3/service/logger" log "github.com/micro/micro/v3/service/logger"
"github.com/micro/micro/v3/service/runtime/source/git" "github.com/micro/micro/v3/service/runtime/source/git"
"github.com/micro/micro/v3/service/store" "github.com/micro/micro/v3/service/store"
"github.com/micro/services/app/domain"
pb "github.com/micro/services/app/proto" pb "github.com/micro/services/app/proto"
"github.com/micro/services/pkg/tenant" "github.com/micro/services/pkg/tenant"
"github.com/teris-io/shortid" "github.com/teris-io/shortid"
@@ -309,7 +310,7 @@ func (e *GoogleApp) Run(ctx context.Context, req *pb.RunRequest, rsp *pb.RunResp
Branch: req.Branch, Branch: req.Branch,
Region: req.Region, Region: req.Region,
Port: req.Port, Port: req.Port,
Status: "Deploying", Status: domain.StatusDeploying,
EnvVars: req.EnvVars, EnvVars: req.EnvVars,
Created: time.Now().Format(time.RFC3339Nano), Created: time.Now().Format(time.RFC3339Nano),
} }
@@ -321,15 +322,21 @@ func (e *GoogleApp) Run(ctx context.Context, req *pb.RunRequest, rsp *pb.RunResp
OwnerKey + id + "/" + req.Name, OwnerKey + id + "/" + req.Name,
} }
// function to update records
updateRecords := func(service *pb.Service) {
// write the keys for the service // write the keys for the service
for _, key := range keys { for _, key := range keys {
rec := store.NewRecord(key, service) rec := store.NewRecord(key, service)
if err := store.Write(rec); err != nil { if err := store.Write(rec); err != nil {
log.Error(err) log.Error(err)
return err return
} }
} }
}
// write the record
updateRecords(service)
// make copy // make copy
srv := new(pb.Service) srv := new(pb.Service)
@@ -385,7 +392,7 @@ func (e *GoogleApp) Run(ctx context.Context, req *pb.RunRequest, rsp *pb.RunResp
log.Error(fmt.Errorf(errString)) log.Error(fmt.Errorf(errString))
// set the error status // set the error status
service.Status = "DeploymentError" service.Status = domain.StatusDeploymentError
if strings.Contains(errString, "Failed to start and then listen on the port defined by the PORT environment variable") { if strings.Contains(errString, "Failed to start and then listen on the port defined by the PORT environment variable") {
service.Status += ": Failed to start and listen on port " + fmt.Sprintf("%d", req.Port) service.Status += ": Failed to start and listen on port " + fmt.Sprintf("%d", req.Port)
@@ -393,22 +400,8 @@ func (e *GoogleApp) Run(ctx context.Context, req *pb.RunRequest, rsp *pb.RunResp
service.Status += ": Build failed" service.Status += ": Build failed"
} }
keys := []string{ // write the records
// service key updateRecords(service)
ServiceKey + service.Id,
// owner key
OwnerKey + id + "/" + req.Name,
}
// write the keys for the service
for _, key := range keys {
rec := store.NewRecord(key, service)
if err := store.Write(rec); err != nil {
log.Error(err)
return
}
}
}(service) }(service)
return nil return nil
@@ -449,6 +442,13 @@ func (e *GoogleApp) Update(ctx context.Context, req *pb.UpdateRequest, rsp *pb.U
return err return err
} }
// check the status
switch srv.Status {
case domain.StatusUpdating, domain.StatusDeploying, domain.StatusDeleting:
log.Errorf("Won't update: % is %s", req.Name, srv.Status)
return errors.BadRequest("app.update", "% status: %s", req.Name, srv.Status)
}
// checkout the code // checkout the code
gitter := git.NewGitter(map[string]string{}) gitter := git.NewGitter(map[string]string{})
if err := gitter.Checkout(srv.Repo, srv.Branch); err != nil { if err := gitter.Checkout(srv.Repo, srv.Branch); err != nil {
@@ -465,6 +465,31 @@ func (e *GoogleApp) Update(ctx context.Context, req *pb.UpdateRequest, rsp *pb.U
envVars = append(envVars, k+"="+v) envVars = append(envVars, k+"="+v)
} }
keys := []string{
// service key
ServiceKey + srv.Id,
// owner key
OwnerKey + id + "/" + req.Name,
}
// function to update records
updateRecords := func(service *pb.Service) {
// write the keys for the service
for _, key := range keys {
rec := store.NewRecord(key, service)
if err := store.Write(rec); err != nil {
log.Error(err)
return
}
}
}
srv.Status = domain.StatusUpdating
// update immediately with status
updateRecords(srv)
go func(service *pb.Service) { go func(service *pb.Service) {
// https://jsoverson.medium.com/how-to-deploy-node-js-functions-to-google-cloud-8bba05e9c10a // https://jsoverson.medium.com/how-to-deploy-node-js-functions-to-google-cloud-8bba05e9c10a
cmd := exec.Command("gcloud", "--project", e.project, "--quiet", "--format", "json", "run", "deploy", cmd := exec.Command("gcloud", "--project", e.project, "--quiet", "--format", "json", "run", "deploy",
@@ -505,7 +530,7 @@ func (e *GoogleApp) Update(ctx context.Context, req *pb.UpdateRequest, rsp *pb.U
log.Error(fmt.Errorf(errString)) log.Error(fmt.Errorf(errString))
// set the error status // set the error status
service.Status = "DeploymentError" service.Status = domain.StatusDeploymentError
if strings.Contains(errString, "Failed to start and then listen on the port defined by the PORT environment variable") { if strings.Contains(errString, "Failed to start and then listen on the port defined by the PORT environment variable") {
service.Status += ": Failed to start and listen on port " + fmt.Sprintf("%d", service.Port) service.Status += ": Failed to start and listen on port " + fmt.Sprintf("%d", service.Port)
@@ -513,22 +538,8 @@ func (e *GoogleApp) Update(ctx context.Context, req *pb.UpdateRequest, rsp *pb.U
service.Status += ": Build failed" service.Status += ": Build failed"
} }
keys := []string{ // update the records
// service key updateRecords(service)
ServiceKey + service.Id,
// owner key
OwnerKey + id + "/" + req.Name,
}
// write the keys for the service
for _, key := range keys {
rec := store.NewRecord(key, service)
if err := store.Write(rec); err != nil {
log.Error(err)
return
}
}
}(srv) }(srv)
return err return err
@@ -564,8 +575,31 @@ func (e *GoogleApp) Delete(ctx context.Context, req *pb.DeleteRequest, rsp *pb.D
return err return err
} }
if srv.Status == "Deploying" { // check the status
return errors.BadRequest("app.run", "app is being deployed") switch srv.Status {
case domain.StatusUpdating, domain.StatusDeploying, domain.StatusDeleting:
log.Errorf("Won't delete: % is %s", req.Name, srv.Status)
return errors.BadRequest("app.delete", "% status: %s", req.Name, srv.Status)
}
// delete from the db
keys := []string{
// service key
ServiceKey + srv.Id,
// owner key
OwnerKey + id + "/" + req.Name,
}
// set the delete status
srv.Status = domain.StatusDeleting
// update the status
for _, key := range keys {
rec := store.NewRecord(key, srv)
if err := store.Write(rec); err != nil {
log.Error(err)
return err
}
} }
// execute the delete async // execute the delete async
@@ -577,22 +611,13 @@ func (e *GoogleApp) Delete(ctx context.Context, req *pb.DeleteRequest, rsp *pb.D
return return
} }
// delete from the db // delete the records
keys := []string{
// service key
ServiceKey + srv.Id,
// owner key
OwnerKey + id + "/" + req.Name,
}
// delete the keys for the service
for _, key := range keys { for _, key := range keys {
if err := store.Delete(key); err != nil { if err := store.Delete(key); err != nil {
log.Error(err) log.Error(err)
return return
} }
} }
}(srv) }(srv)
return nil return nil