|
@@ -7,39 +7,33 @@ import (
|
|
|
|
|
|
"git.linuxforward.com/byop/byop-engine/dbstore"
|
|
|
"git.linuxforward.com/byop/byop-engine/models"
|
|
|
- "github.com/google/uuid"
|
|
|
)
|
|
|
|
|
|
// DeploymentService handles business logic for deployments
|
|
|
type DeploymentService struct {
|
|
|
- store *dbstore.DeploymentStore
|
|
|
- appStore *dbstore.AppStore
|
|
|
- templateStore *dbstore.TemplateStore
|
|
|
- clientStore *dbstore.ClientStore
|
|
|
+ store *dbstore.DeploymentStore
|
|
|
+ componentStore *dbstore.ComponentStore // Renamed from appStore
|
|
|
+ appStore *dbstore.AppStore // Renamed from templateStore
|
|
|
+ clientStore *dbstore.ClientStore
|
|
|
}
|
|
|
|
|
|
// NewDeploymentService creates a new DeploymentService
|
|
|
func NewDeploymentService(
|
|
|
store *dbstore.DeploymentStore,
|
|
|
+ componentStore *dbstore.ComponentStore,
|
|
|
appStore *dbstore.AppStore,
|
|
|
- templateStore *dbstore.TemplateStore,
|
|
|
clientStore *dbstore.ClientStore,
|
|
|
) *DeploymentService {
|
|
|
return &DeploymentService{
|
|
|
- store: store,
|
|
|
- appStore: appStore,
|
|
|
- templateStore: templateStore,
|
|
|
- clientStore: clientStore,
|
|
|
+ store: store,
|
|
|
+ componentStore: componentStore,
|
|
|
+ appStore: appStore,
|
|
|
+ clientStore: clientStore,
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// CreateDeployment creates a new deployment
|
|
|
func (s *DeploymentService) CreateDeployment(deployment *models.Deployment) error {
|
|
|
- // Generate UUID if not provided
|
|
|
- if deployment.ID == "" {
|
|
|
- deployment.ID = uuid.New().String()
|
|
|
- }
|
|
|
-
|
|
|
// Validate the deployment
|
|
|
if err := s.validateDeployment(deployment); err != nil {
|
|
|
return fmt.Errorf("invalid deployment: %w", err)
|
|
@@ -70,7 +64,7 @@ func (s *DeploymentService) CreateDeployment(deployment *models.Deployment) erro
|
|
|
}
|
|
|
|
|
|
// GetDeployment retrieves a deployment by ID
|
|
|
-func (s *DeploymentService) GetDeployment(id string) (*models.Deployment, error) {
|
|
|
+func (s *DeploymentService) GetDeployment(id int64) (*models.Deployment, error) {
|
|
|
deployment, err := s.store.GetByID(id)
|
|
|
if err != nil {
|
|
|
return nil, fmt.Errorf("failed to retrieve deployment: %w", err)
|
|
@@ -89,7 +83,7 @@ func (s *DeploymentService) GetDeployment(id string) (*models.Deployment, error)
|
|
|
// UpdateDeployment updates an existing deployment
|
|
|
func (s *DeploymentService) UpdateDeployment(deployment *models.Deployment) error {
|
|
|
// Validate the deployment ID
|
|
|
- if deployment.ID == "" {
|
|
|
+ if deployment.ID == 0 {
|
|
|
return fmt.Errorf("deployment ID is required for update")
|
|
|
}
|
|
|
|
|
@@ -99,7 +93,7 @@ func (s *DeploymentService) UpdateDeployment(deployment *models.Deployment) erro
|
|
|
return fmt.Errorf("failed to check if deployment exists: %w", err)
|
|
|
}
|
|
|
if existingDeployment == nil {
|
|
|
- return fmt.Errorf("deployment with ID %s not found", deployment.ID)
|
|
|
+ return fmt.Errorf("deployment with ID %d not found", deployment.ID)
|
|
|
}
|
|
|
|
|
|
// Prevent updates to deployed apps if deployment is not in the right state
|
|
@@ -134,14 +128,14 @@ func (s *DeploymentService) UpdateDeployment(deployment *models.Deployment) erro
|
|
|
}
|
|
|
|
|
|
// DeleteDeployment deletes a deployment by ID
|
|
|
-func (s *DeploymentService) DeleteDeployment(id string) error {
|
|
|
+func (s *DeploymentService) DeleteDeployment(id int64) error {
|
|
|
// Check if deployment exists
|
|
|
deployment, err := s.store.GetByID(id)
|
|
|
if err != nil {
|
|
|
return fmt.Errorf("failed to check if deployment exists: %w", err)
|
|
|
}
|
|
|
if deployment == nil {
|
|
|
- return fmt.Errorf("deployment with ID %s not found", id)
|
|
|
+ return fmt.Errorf("deployment with ID %d not found", id)
|
|
|
}
|
|
|
|
|
|
// Set status to deleting
|
|
@@ -175,14 +169,14 @@ func (s *DeploymentService) ListDeployments(filter map[string]interface{}) ([]*m
|
|
|
}
|
|
|
|
|
|
// GetDeploymentsByClientID retrieves deployments for a specific client
|
|
|
-func (s *DeploymentService) GetDeploymentsByClientID(clientID string) ([]*models.Deployment, error) {
|
|
|
+func (s *DeploymentService) GetDeploymentsByClientID(clientID int64) ([]*models.Deployment, error) {
|
|
|
// Check if client exists
|
|
|
client, err := s.clientStore.GetByID(clientID)
|
|
|
if err != nil {
|
|
|
return nil, fmt.Errorf("failed to check if client exists: %w", err)
|
|
|
}
|
|
|
if client == nil {
|
|
|
- return nil, fmt.Errorf("client with ID %s not found", clientID)
|
|
|
+ return nil, fmt.Errorf("client with ID %d not found", clientID)
|
|
|
}
|
|
|
|
|
|
deployments, err := s.store.GetByClientID(clientID)
|
|
@@ -217,20 +211,20 @@ func (s *DeploymentService) GetDeploymentsByUserID(userID string) ([]*models.Dep
|
|
|
return deployments, nil
|
|
|
}
|
|
|
|
|
|
-// GetDeploymentsByTemplateID retrieves deployments based on a specific template
|
|
|
-func (s *DeploymentService) GetDeploymentsByTemplateID(templateID string) ([]*models.Deployment, error) {
|
|
|
- // Check if template exists
|
|
|
- template, err := s.templateStore.GetByID(templateID)
|
|
|
+// GetDeploymentsByAppID retrieves deployments based on a specific app (was template)
|
|
|
+func (s *DeploymentService) GetDeploymentsByAppID(appID int64) ([]*models.Deployment, error) {
|
|
|
+ // Check if app exists
|
|
|
+ app, err := s.appStore.GetByID(appID)
|
|
|
if err != nil {
|
|
|
- return nil, fmt.Errorf("failed to check if template exists: %w", err)
|
|
|
+ return nil, fmt.Errorf("failed to check if app exists: %w", err)
|
|
|
}
|
|
|
- if template == nil {
|
|
|
- return nil, fmt.Errorf("template with ID %s not found", templateID)
|
|
|
+ if app == nil {
|
|
|
+ return nil, fmt.Errorf("app with ID %d not found", appID)
|
|
|
}
|
|
|
|
|
|
- deployments, err := s.store.GetByTemplateID(templateID)
|
|
|
+ deployments, err := s.store.GetByAppID(appID)
|
|
|
if err != nil {
|
|
|
- return nil, fmt.Errorf("failed to retrieve deployments for template %s: %w", templateID, err)
|
|
|
+ return nil, fmt.Errorf("failed to retrieve deployments for app %s: %w", appID, err)
|
|
|
}
|
|
|
|
|
|
// Deserialize config fields for each deployment
|
|
@@ -244,7 +238,7 @@ func (s *DeploymentService) GetDeploymentsByTemplateID(templateID string) ([]*mo
|
|
|
}
|
|
|
|
|
|
// UpdateDeploymentStatus updates the status of a deployment
|
|
|
-func (s *DeploymentService) UpdateDeploymentStatus(id string, status string) error {
|
|
|
+func (s *DeploymentService) UpdateDeploymentStatus(id int64, status string) error {
|
|
|
deployment, err := s.store.GetByID(id)
|
|
|
if err != nil {
|
|
|
return fmt.Errorf("failed to retrieve deployment: %w", err)
|
|
@@ -276,7 +270,7 @@ func (s *DeploymentService) validateDeployment(deployment *models.Deployment) er
|
|
|
}
|
|
|
|
|
|
// Validate relationships
|
|
|
- if deployment.ClientID == "" {
|
|
|
+ if deployment.ClientID == 0 {
|
|
|
return fmt.Errorf("client ID is required")
|
|
|
}
|
|
|
client, err := s.clientStore.GetByID(deployment.ClientID)
|
|
@@ -284,18 +278,18 @@ func (s *DeploymentService) validateDeployment(deployment *models.Deployment) er
|
|
|
return fmt.Errorf("failed to check client: %w", err)
|
|
|
}
|
|
|
if client == nil {
|
|
|
- return fmt.Errorf("client with ID %s not found", deployment.ClientID)
|
|
|
+ return fmt.Errorf("client with ID %d not found", deployment.ClientID)
|
|
|
}
|
|
|
|
|
|
- if deployment.TemplateID == "" {
|
|
|
- return fmt.Errorf("template ID is required")
|
|
|
+ if deployment.AppID == 0 {
|
|
|
+ return fmt.Errorf("app ID is required")
|
|
|
}
|
|
|
- template, err := s.templateStore.GetByID(deployment.TemplateID)
|
|
|
+ app, err := s.appStore.GetByID(deployment.AppID)
|
|
|
if err != nil {
|
|
|
- return fmt.Errorf("failed to check template: %w", err)
|
|
|
+ return fmt.Errorf("failed to check app: %w", err)
|
|
|
}
|
|
|
- if template == nil {
|
|
|
- return fmt.Errorf("template with ID %s not found", deployment.TemplateID)
|
|
|
+ if app == nil {
|
|
|
+ return fmt.Errorf("app with ID %s not found", deployment.AppID)
|
|
|
}
|
|
|
|
|
|
return nil
|
|
@@ -308,46 +302,45 @@ func (s *DeploymentService) setupDeployedApps(deployment *models.Deployment) err
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
- // Get the template
|
|
|
- template, err := s.templateStore.GetByID(deployment.TemplateID)
|
|
|
+ // Get the app
|
|
|
+ app, err := s.appStore.GetByID(deployment.AppID)
|
|
|
if err != nil {
|
|
|
- return fmt.Errorf("failed to retrieve template: %w", err)
|
|
|
+ return fmt.Errorf("failed to retrieve app: %w", err)
|
|
|
}
|
|
|
- if template == nil {
|
|
|
- return fmt.Errorf("template with ID %s not found", deployment.TemplateID)
|
|
|
+ if app == nil {
|
|
|
+ return fmt.Errorf("app with ID %d not found", deployment.AppID)
|
|
|
}
|
|
|
|
|
|
- // Use the template config to set up deployed apps
|
|
|
- var templateConfig models.TemplateConfig
|
|
|
- if err := json.Unmarshal([]byte(template.ConfigJSON), &templateConfig); err != nil {
|
|
|
- return fmt.Errorf("failed to parse template config: %w", err)
|
|
|
+ // Use the app config to set up deployed apps
|
|
|
+ var appConfig models.AppConfig
|
|
|
+ if err := json.Unmarshal([]byte(app.ConfigJSON), &appConfig); err != nil {
|
|
|
+ return fmt.Errorf("failed to parse app config: %w", err)
|
|
|
}
|
|
|
|
|
|
- // Create deployed apps for each app in the template
|
|
|
- for _, appConfig := range templateConfig.Apps {
|
|
|
- // Get the app
|
|
|
- app, err := s.appStore.GetByID(appConfig.ID)
|
|
|
+ // Create deployed apps for each component in the app
|
|
|
+ for _, componentConfig := range appConfig.Components {
|
|
|
+ // Get the component
|
|
|
+ component, err := s.componentStore.GetByID(componentConfig.ID)
|
|
|
if err != nil {
|
|
|
- return fmt.Errorf("failed to retrieve app: %w", err)
|
|
|
+ return fmt.Errorf("failed to retrieve component: %w", err)
|
|
|
}
|
|
|
- if app == nil {
|
|
|
- return fmt.Errorf("app with ID %s not found", appConfig.ID)
|
|
|
+ if component == nil {
|
|
|
+ return fmt.Errorf("component with ID %d not found", componentConfig.ID)
|
|
|
}
|
|
|
|
|
|
- // Create a deployed app
|
|
|
+ // Create a deployed app (GORM will auto-generate ID)
|
|
|
deployedApp := models.DeployedApp{
|
|
|
- ID: uuid.New().String(),
|
|
|
DeploymentID: deployment.ID,
|
|
|
- AppID: app.ID,
|
|
|
+ ComponentID: component.ID,
|
|
|
Status: string(models.PENDING_APP),
|
|
|
- Version: app.Version,
|
|
|
+ Version: component.Version,
|
|
|
URL: "", // Will be set during deployment
|
|
|
- PodCount: appConfig.Autoscaling.MinReplicas,
|
|
|
+ PodCount: componentConfig.Autoscaling.MinReplicas,
|
|
|
HealthStatus: string(models.HEALTHY),
|
|
|
Resources: models.ResourceAllocation{
|
|
|
- CPU: appConfig.Resources.CPU,
|
|
|
- Memory: appConfig.Resources.Memory,
|
|
|
- Storage: appConfig.Resources.Storage,
|
|
|
+ CPU: componentConfig.Resources.CPU,
|
|
|
+ Memory: componentConfig.Resources.Memory,
|
|
|
+ Storage: componentConfig.Resources.Storage,
|
|
|
},
|
|
|
}
|
|
|
|
|
@@ -359,7 +352,7 @@ func (s *DeploymentService) setupDeployedApps(deployment *models.Deployment) err
|
|
|
}
|
|
|
|
|
|
// processDeployment handles the actual deployment process
|
|
|
-func (s *DeploymentService) processDeployment(deploymentID string) {
|
|
|
+func (s *DeploymentService) processDeployment(deploymentID int64) {
|
|
|
// This would be an async process in a real system
|
|
|
// For now, we just update the status after a short delay to simulate the process
|
|
|
|
|
@@ -373,6 +366,13 @@ func (s *DeploymentService) processDeployment(deploymentID string) {
|
|
|
// 4. Setup monitoring
|
|
|
// etc.
|
|
|
|
|
|
+ // Logging the deployment process
|
|
|
+ fmt.Printf("Processing deployment %d...\n", deploymentID)
|
|
|
+ for i := 0; i < 5; i++ {
|
|
|
+ fmt.Printf("Deploying app %d/%d...\n", i+1, 5)
|
|
|
+ time.Sleep(500 * time.Millisecond) // Simulate work
|
|
|
+ }
|
|
|
+
|
|
|
// For this demo, we'll just update the status after a short delay
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
|
@@ -385,7 +385,7 @@ func (s *DeploymentService) processDeployment(deploymentID string) {
|
|
|
}
|
|
|
|
|
|
// processDeploymentCleanup handles the cleanup process for deleted deployments
|
|
|
-func (s *DeploymentService) processDeploymentCleanup(deploymentID string) {
|
|
|
+func (s *DeploymentService) processDeploymentCleanup(deploymentID int64) {
|
|
|
// This would be an async process in a real system
|
|
|
// In a real system, this would:
|
|
|
// 1. Deprovision infrastructure
|