code cleanup, starting and stopping
This commit is contained in:
188
server/deploy.go
188
server/deploy.go
@@ -1,14 +1,12 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"os/exec"
|
||||
"strings"
|
||||
|
||||
"github.com/juls0730/fluxd/models"
|
||||
)
|
||||
@@ -19,7 +17,7 @@ type DeployRequest struct {
|
||||
}
|
||||
|
||||
type DeployResponse struct {
|
||||
AppID int64 `json:"app_id"`
|
||||
App models.App `json:"app"`
|
||||
}
|
||||
|
||||
func (s *FluxServer) DeployHandler(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -98,109 +96,118 @@ func (s *FluxServer) DeployHandler(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
containerID, err := s.containerManager.DeployContainer(r.Context(), imageName, projectConfig.Name, projectPath, projectConfig)
|
||||
if err != nil {
|
||||
log.Printf("Failed to deploy container: %v\n", err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
var app models.App
|
||||
s.db.QueryRow("SELECT id FROM apps WHERE name = ?", projectConfig.Name).Scan(&app.ID)
|
||||
|
||||
deploymentResult, err := s.db.Exec("INSERT INTO deployments (urls) VALUES (?)", strings.Join(projectConfig.Urls, ","))
|
||||
if err != nil {
|
||||
log.Printf("Failed to insert deployment: %v\n", err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
deploymentID, err := deploymentResult.LastInsertId()
|
||||
if err != nil {
|
||||
log.Printf("Failed to get deployment id: %v\n", err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
_, err = s.db.Exec("INSERT INTO containers (container_id, deployment_id, status) VALUES (?, ?, ?)", containerID, deploymentID, "pending")
|
||||
if err != nil {
|
||||
log.Printf("Failed to get container id: %v\n", err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
var app struct {
|
||||
id int
|
||||
name string
|
||||
deployment_id int
|
||||
}
|
||||
s.db.QueryRow("SELECT id, name, deployment_id FROM apps WHERE name = ?", projectConfig.Name).Scan(&app.id, &app.name, &app.deployment_id)
|
||||
configBytes, err := json.Marshal(projectConfig)
|
||||
if err != nil {
|
||||
log.Printf("Failed to marshal project config: %v\n", err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
tx, err := s.db.Begin()
|
||||
if err != nil {
|
||||
log.Printf("Failed to begin transaction: %v\n", err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
var appResult sql.Result
|
||||
if app.id == 0 {
|
||||
// create app in the database
|
||||
appResult, err = tx.Exec("INSERT INTO apps (name, image, project_path, project_config, deployment_id) VALUES (?, ?, ?, ?, ?)", projectConfig.Name, imageName, projectPath, configBytes, deploymentID)
|
||||
if app.ID == 0 {
|
||||
configBytes, err := json.Marshal(projectConfig)
|
||||
if err != nil {
|
||||
log.Printf("Failed to marshal project config: %v\n", err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
containerID, err := s.containerManager.CreateContainer(r.Context(), imageName, projectPath, projectConfig)
|
||||
if err != nil {
|
||||
log.Printf("Failed to create container: %v\n", err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
deploymentID, err := s.CreateDeployment(r.Context(), projectConfig, containerID)
|
||||
if err != nil {
|
||||
log.Printf("Failed to create deployment: %v\n", err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
// create app in the database
|
||||
appResult, err := s.db.Exec("INSERT INTO apps (name, image, project_path, project_config, deployment_id) VALUES (?, ?, ?, ?, ?)", projectConfig.Name, imageName, projectPath, configBytes, deploymentID)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
log.Printf("Failed to insert app: %v\n", err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
appID, err := appResult.LastInsertId()
|
||||
if err != nil {
|
||||
log.Printf("Failed to get app id: %v\n", err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
s.db.QueryRow("SELECT id, name, deployment_id FROM apps WHERE id = ?", appID).Scan(&app.ID, &app.Name, &app.DeploymentID)
|
||||
} else {
|
||||
_, err = tx.Exec("DELETE FROM deployments WHERE id = ?", app.deployment_id)
|
||||
err = s.UpgradeDeployment(r.Context(), app.DeploymentID, projectConfig, imageName, projectPath)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
log.Printf("Failed to delete old deployment: %v\n", err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
_, err = tx.Exec("DELETE FROM containers WHERE deployment_id = ?", app.deployment_id)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
log.Printf("Failed to delete old containers: %v\n", err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
// update app in the database
|
||||
appResult, err = tx.Exec("UPDATE apps SET project_config = ?, deployment_id = ? WHERE name = ?", configBytes, deploymentID, projectConfig.Name)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
log.Printf("Failed to update app: %v\n", err)
|
||||
log.Printf("Failed to upgrade deployment: %v\n", err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if err := tx.Commit(); err != nil {
|
||||
log.Printf("Failed to commit transaction: %v\n", err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
appId, err := appResult.LastInsertId()
|
||||
err = s.StartDeployment(r.Context(), app.DeploymentID)
|
||||
if err != nil {
|
||||
log.Printf("Failed to get app id: %v\n", err)
|
||||
log.Printf("Failed to start deployment: %v\n", err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
log.Printf("App %s deployed successfully!\n", app.Name)
|
||||
|
||||
json.NewEncoder(w).Encode(DeployResponse{
|
||||
AppID: appId,
|
||||
App: app,
|
||||
})
|
||||
}
|
||||
|
||||
func (s *FluxServer) StartDeployHandler(w http.ResponseWriter, r *http.Request) {
|
||||
name := r.PathValue("name")
|
||||
|
||||
var app struct {
|
||||
id int64
|
||||
name string
|
||||
deployment_id int64
|
||||
}
|
||||
s.db.QueryRow("SELECT id, name, deployment_id FROM apps WHERE name = ?", name).Scan(&app.id, &app.name, &app.deployment_id)
|
||||
|
||||
if app.id == 0 {
|
||||
http.Error(w, "App not found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
err := s.StartDeployment(r.Context(), app.deployment_id)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
||||
func (s *FluxServer) StopDeployHandler(w http.ResponseWriter, r *http.Request) {
|
||||
name := r.PathValue("name")
|
||||
|
||||
var app struct {
|
||||
id int64
|
||||
name string
|
||||
deployment_id int64
|
||||
}
|
||||
s.db.QueryRow("SELECT id, name, deployment_id FROM apps WHERE name = ?", name).Scan(&app.id, &app.name, &app.deployment_id)
|
||||
|
||||
if app.id == 0 {
|
||||
http.Error(w, "App not found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
err := s.StopDeployment(r.Context(), app.deployment_id)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
||||
func (s *FluxServer) DeleteDeployHandler(w http.ResponseWriter, r *http.Request) {
|
||||
name := r.PathValue("name")
|
||||
|
||||
@@ -285,7 +292,6 @@ func (s *FluxServer) DeleteDeployHandler(w http.ResponseWriter, r *http.Request)
|
||||
}
|
||||
|
||||
func (s *FluxServer) ListAppsHandler(w http.ResponseWriter, r *http.Request) {
|
||||
// Implement app listing logic
|
||||
var apps []models.App
|
||||
rows, err := s.db.Query("SELECT * FROM apps")
|
||||
if err != nil {
|
||||
@@ -314,6 +320,18 @@ func (s *FluxServer) ListAppsHandler(w http.ResponseWriter, r *http.Request) {
|
||||
apps = append(apps, app)
|
||||
}
|
||||
|
||||
// for each app, get the deployment status
|
||||
for i, app := range apps {
|
||||
deploymentStatus, err := s.GetDeploymentStatus(r.Context(), app.DeploymentID)
|
||||
if err != nil {
|
||||
log.Printf("Failed to get deployment status: %v\n", err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
apps[i].DeploymentStatus = deploymentStatus
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(apps)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user