Skip to content

Commit

Permalink
Feat/deploy serviceset (#241)
Browse files Browse the repository at this point in the history
* deployed service proto update

* deploye service set cmd implementation

* name correction

* lint changes

* pr changes

* pr changes
  • Loading branch information
yashjaind11 authored Jun 14, 2024
1 parent 556fb98 commit 2280c6a
Show file tree
Hide file tree
Showing 8 changed files with 1,261 additions and 592 deletions.
71 changes: 71 additions & 0 deletions cmd/deploy/service-set.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package deploy

import (
"encoding/json"
"os"

serviceDto "github.com/dream11/odin/proto/gen/go/dream11/od/dto/v1"
serviceProto "github.com/dream11/odin/proto/gen/go/dream11/od/service/v1"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

var serviceSetName string
var serviceSetDeployCmd = &cobra.Command{
Use: "service-set",
Short: "Deploy service-set",
Args: func(cmd *cobra.Command, args []string) error {
return cobra.NoArgs(cmd, args)
},
Long: "Deploy service-set using files or service set name",
Run: func(cmd *cobra.Command, args []string) {
executeDeployServiceSet(cmd)
},
}

func init() {

serviceSetDeployCmd.Flags().StringVar(&env, "env", "", "environment for deploying the service-set")
serviceSetDeployCmd.Flags().StringVar(&provisioningFile, "file", "", "path to the service set provisioning file")
serviceSetDeployCmd.Flags().StringVar(&serviceSetName, "name", "", "released service set name")

err := serviceSetDeployCmd.MarkFlagRequired("env")
if err != nil {
log.Fatal("Error marking 'env' flag as required:", err)
}

deployCmd.AddCommand(serviceSetDeployCmd)
}

func executeDeployServiceSet(cmd *cobra.Command) {
ctx := cmd.Context()
if serviceSetName == "" && provisioningFile == "" {
log.Fatal("Please provide either --name or --file.")
}
if provisioningFile != "" && serviceSetName != "" {
log.Fatal("--name should not be provided when --file is provided.")
}
var deployServiceSetRequestDTO serviceDto.ServiceSet
var deployServiceSetRequest serviceProto.DeployServiceSetRequest

deployServiceSetRequest.EnvName = env

if provisioningFile != "" {
provisioningData, err := os.ReadFile(provisioningFile)
if err != nil {
log.Fatal("Error while reading provisioning file ", err)
}
if err := json.Unmarshal(provisioningData, &deployServiceSetRequestDTO); err != nil {
log.Fatal("error unmarshalling provisioning file: %w", err)
}
deployServiceSetRequest = serviceClient.ConvertToDeployServiceSetRequest(&deployServiceSetRequestDTO, env)
}
if serviceSetName != "" {
deployServiceSetRequest.Name = serviceSetName
}

err := serviceClient.DeployServiceSet(&ctx, &deployServiceSetRequest)
if err != nil {
log.Fatal("Failed to deploy service ", err)
}
}
8 changes: 5 additions & 3 deletions cmd/deploy/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,11 @@ func execute(cmd *cobra.Command) {
} else if (serviceName != "" && serviceVersion != "") && (definitionFile == "" && provisioningFile == "") {
log.Info("deploying service :", serviceName, ":", serviceVersion, " in env :", env)
err := serviceClient.DeployReleasedService(&ctx, &serviceProto.DeployReleasedServiceRequest{
EnvName: env,
ServiceName: serviceName,
ServiceVersion: serviceVersion,
EnvName: env,
ServiceIdentifier: &serviceProto.ServiceIdentifier{
ServiceName: serviceName,
ServiceVersion: serviceVersion,
},
})

if err != nil {
Expand Down
71 changes: 68 additions & 3 deletions internal/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/briandowns/spinner"
"github.com/dream11/odin/pkg/constant"
serviceDto "github.com/dream11/odin/proto/gen/go/dream11/od/dto/v1"
serviceProto "github.com/dream11/odin/proto/gen/go/dream11/od/service/v1"
log "github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -60,6 +61,53 @@ func (e *Service) DeployService(ctx *context.Context, request *serviceProto.Depl
return err
}

// DeployServiceSet deploys service-set
func (e *Service) DeployServiceSet(ctx *context.Context, request *serviceProto.DeployServiceSetRequest) error {
conn, requestCtx, err := grpcClient(ctx)
if err != nil {
return err
}
client := serviceProto.NewServiceServiceClient(conn)
stream, err := client.DeployServiceSet(*requestCtx, request)
if err != nil {
return err
}

log.Info("Deploying Service Set..")
spinnerInstance := spinner.New(spinner.CharSets[constant.SpinnerType], constant.SpinnerDelay)
err = spinnerInstance.Color(constant.SpinnerColor, constant.SpinnerStyle)
if err != nil {
return err
}

var message string
for {
response, err := stream.Recv()
spinnerInstance.Stop()
if err != nil {
if errors.Is(err, context.Canceled) || err == io.EOF {
break
}
return err
}

if response != nil {

for _, serviceRespose := range response.GetServices() {
message = fmt.Sprintf("\n Service %s %s %s", serviceRespose.ServiceIdentifier, serviceRespose.ServiceResponse.ServiceStatus, serviceRespose.ServiceResponse.Message)
for _, compMessage := range serviceRespose.ServiceResponse.ComponentsStatus {
message += fmt.Sprintf("\n Component %s %s %s", compMessage.ComponentName, compMessage.ComponentAction, compMessage.ComponentStatus)
}
}
spinnerInstance.Prefix = fmt.Sprintf(" %s ", message)
spinnerInstance.Start()
}
}

log.Info(message)
return err
}

// DeployReleasedService deploys service
func (e *Service) DeployReleasedService(ctx *context.Context, request *serviceProto.DeployReleasedServiceRequest) error {
conn, requestCtx, err := grpcClient(ctx)
Expand Down Expand Up @@ -91,9 +139,9 @@ func (e *Service) DeployReleasedService(ctx *context.Context, request *servicePr
}

if response != nil {
message = response.Message
message += fmt.Sprintf("\n Service %s %s", response.ServiceStatus.ServiceAction, response.ServiceStatus.ServiceStatus)
for _, compMessage := range response.ComponentsStatus {
message = response.GetServiceResponse().Message
message += fmt.Sprintf("\n Service %s %s", response.GetServiceResponse().ServiceStatus.ServiceAction, response.GetServiceResponse().ServiceStatus.ServiceStatus)
for _, compMessage := range response.GetServiceResponse().ComponentsStatus {
message += fmt.Sprintf("\n Component %s %s %s", compMessage.ComponentName, compMessage.ComponentAction, compMessage.ComponentStatus)
}
spinnerInstance.Prefix = fmt.Sprintf(" %s ", message)
Expand Down Expand Up @@ -201,3 +249,20 @@ func (e *Service) ListService(ctx *context.Context, request *serviceProto.ListSe
response, err := client.ListService(*requestCtx, request)
return response, err
}

// ConvertToDeployServiceSetRequest converts service set to deploy service set request
func (e *Service) ConvertToDeployServiceSetRequest(serviceSet *serviceDto.ServiceSet, env string) serviceProto.DeployServiceSetRequest {
var services []*serviceProto.ServiceIdentifier
for _, service := range serviceSet.Services {
services = append(services, &serviceProto.ServiceIdentifier{
ServiceName: service.Name,
ServiceVersion: service.Version,
})
}

return serviceProto.DeployServiceSetRequest{
EnvName: env,
Name: serviceSet.Name,
Services: services,
}
}
12 changes: 11 additions & 1 deletion proto/dream11/od/dto/v1/service.proto
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ message ServiceDefinition {
repeated ComponentDefinition components = 4;
}

message ServiceIdentifier {
string name = 1;
string version = 2;
}

message ServiceSet {
string name = 1;
repeated ServiceIdentifier services = 2;
}

message ComponentProvisioningConfig {
string component_name = 1;
string deployment_type = 2;
Expand All @@ -41,5 +51,5 @@ message ServiceMetadata {
string created_at = 7;
string updated_at = 8;
string tags = 9; // JSON encoded string
string labels = 10; // JSON encoded string
string labels = 10; // comma seperated string
}
64 changes: 41 additions & 23 deletions proto/dream11/od/service/v1/service.proto
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,55 @@ option go_package = "github.com/dream11/odin/proto/gen/go/dream11/od/service/v1"
service ServiceService {
rpc DeployService(DeployServiceRequest) returns (stream DeployServiceResponse) {}
rpc ReleaseService(ReleaseServiceRequest) returns (stream ReleaseServiceResponse) {}
rpc DeployReleasedService(DeployReleasedServiceRequest) returns (stream DeployReleasedServiceResponse) {}
rpc DeployServiceSet(DeployServiceSetRequest) returns (stream DeployServiceSetResponse) {}
rpc OperateService(OperateServiceRequest) returns (stream OperateServiceResponse) {}
rpc UndeployService(UndeployServiceRequest) returns (stream UndeployServiceResponse) {}
rpc DeployReleasedService(DeployReleasedServiceRequest) returns (stream DeployReleasedServiceResponse) {}
rpc ListService(ListServiceRequest) returns (ListServiceResponse) {}
}

message DeployServiceRequest {
string env_name = 1;
dream11.od.dto.v1.ServiceDefinition service_definition = 2;
dream11.od.dto.v1.ProvisioningConfig provisioning_config = 3;
dto.v1.ServiceDefinition service_definition = 2;
dto.v1.ProvisioningConfig provisioning_config = 3;
}

message DeployServiceResponse {
message ServiceResponse {
ServiceStatus service_status = 1;
repeated ComponentStatus components_status = 2;
string message = 3;
}

message DeployReleasedServiceResponse {
ServiceResponse service_response = 1;
}

message UndeployServiceRequest {
message ServiceIdentifier {
string service_name = 1;
string service_version = 2;
}

message DeployReleasedServiceRequest {
ServiceIdentifier service_identifier = 1;
string env_name = 3;
}

message DeployServiceSetRequest {
string env_name = 1;
string service_name = 2;
string name = 2;
repeated ServiceIdentifier services = 3;
}

message UndeployServiceResponse {
message DeployServiceSetResponse {
repeated DeployServiceSetServiceResponse services = 1;
}

message DeployServiceSetServiceResponse {
ServiceIdentifier service_identifier = 1;
ServiceResponse service_response = 3;
}

message DeployServiceResponse {
ServiceResponse service_response = 1;
}

Expand All @@ -48,8 +75,8 @@ message ComponentStatus {
}

message ReleaseServiceRequest {
dream11.od.dto.v1.ServiceDefinition service_definition = 1;
map<string, dream11.od.dto.v1.ProvisioningConfig> provisioning_configs = 2;
dto.v1.ServiceDefinition service_definition = 1;
map<string, dto.v1.ProvisioningConfig> provisioning_configs = 2;
}

message ReleaseServiceResponse {
Expand All @@ -59,12 +86,6 @@ message ReleaseServiceResponse {
string message = 4;
}

message ServiceResponse {
ServiceStatus service_status = 1;
repeated ComponentStatus components_status = 2;
string message = 3;
}

message OperateServiceRequest {
string env_name = 1;
string service_name = 2;
Expand All @@ -78,16 +99,13 @@ message OperateServiceResponse {
ServiceResponse service_response = 1;
}

message DeployReleasedServiceResponse {
ServiceStatus service_status = 1;
repeated ComponentStatus components_status = 2;
string message = 3;
message UndeployServiceRequest {
string env_name = 1;
string service_name = 2;
}

message DeployReleasedServiceRequest {
string service_name = 1;
string service_version = 2;
string env_name = 3;
message UndeployServiceResponse {
ServiceResponse service_response = 1;
}

message ListServiceResponse {
Expand Down
Loading

0 comments on commit 2280c6a

Please sign in to comment.