fix steaming, stale data, proxy bugs and more

This commit is contained in:
Zoe
2024-12-13 03:33:04 -06:00
parent e46bb05b39
commit 7689999413
13 changed files with 798 additions and 658 deletions

View File

@@ -12,10 +12,10 @@ import (
)
type App struct {
ID int64 `json:"id,omitempty"`
Deployment Deployment `json:"deployment,omitempty"`
Name string `json:"name,omitempty"`
DeploymentID int64 `json:"deployment_id,omitempty"`
ID int64 `json:"id,omitempty"`
Deployment *Deployment `json:"deployment,omitempty"`
Name string `json:"name,omitempty"`
DeploymentID int64 `json:"deployment_id,omitempty"`
}
func CreateApp(ctx context.Context, imageName string, projectPath string, projectConfig pkg.ProjectConfig) (*App, error) {
@@ -24,51 +24,36 @@ func CreateApp(ctx context.Context, imageName string, projectPath string, projec
}
log.Printf("Creating deployment %s...\n", app.Name)
container, err := CreateDockerContainer(ctx, imageName, projectPath, projectConfig)
if err != nil || container == nil {
return nil, fmt.Errorf("Failed to create container: %v", err)
}
deployment, err := CreateDeployment(*container, projectConfig.Port, projectConfig.Url, Flux.db)
deployment, err := CreateDeployment(projectConfig.Port, projectConfig.Url, Flux.db)
app.Deployment = deployment
if err != nil {
log.Printf("Failed to create deployment: %v", err)
return nil, err
}
container, err := CreateContainer(ctx, imageName, projectPath, projectConfig, true, deployment)
if err != nil || container == nil {
return nil, fmt.Errorf("failed to create container: %v", err)
}
if appInsertStmt == nil {
appInsertStmt, err = Flux.db.Prepare("INSERT INTO apps (name, deployment_id) VALUES ($1, $2) RETURNING id, name, deployment_id")
if err != nil {
return nil, fmt.Errorf("Failed to prepare statement: %v", err)
return nil, fmt.Errorf("failed to prepare statement: %v", err)
}
}
// create app in the database
err = appInsertStmt.QueryRow(projectConfig.Name, deployment.ID).Scan(&app.ID, &app.Name, &app.DeploymentID)
if err != nil {
return nil, fmt.Errorf("Failed to insert app: %v", err)
return nil, fmt.Errorf("failed to insert app: %v", err)
}
err = deployment.Start(ctx)
if err != nil {
return nil, fmt.Errorf("Failed to start deployment: %v", err)
return nil, fmt.Errorf("failed to start deployment: %v", err)
}
var headContainer *Container
for _, container := range deployment.Containers {
if container.Head {
headContainer = &container
}
}
deployment.Proxy, err = NewDeploymentProxy(&deployment, headContainer)
if err != nil {
return nil, fmt.Errorf("Failed to create deployment proxy: %v", err)
}
Flux.proxy.AddDeployment(&deployment)
Flux.appManager.AddApp(app.Name, app)
return app, nil
@@ -79,16 +64,20 @@ func (app *App) Upgrade(ctx context.Context, projectConfig pkg.ProjectConfig, im
// if deploy is not started, start it
deploymentStatus, err := app.Deployment.Status(ctx)
if deploymentStatus != "running" || err != nil {
if err != nil {
return fmt.Errorf("failed to get deployment status: %v", err)
}
if deploymentStatus != "running" {
err = app.Deployment.Start(ctx)
if err != nil {
return fmt.Errorf("Failed to start deployment: %v", err)
return fmt.Errorf("failed to start deployment: %v", err)
}
}
err = app.Deployment.Upgrade(ctx, projectConfig, imageName, projectPath)
if err != nil {
return fmt.Errorf("Failed to upgrade deployment: %v", err)
return fmt.Errorf("failed to upgrade deployment: %v", err)
}
return nil
@@ -110,7 +99,7 @@ func (app *App) Remove(ctx context.Context) error {
projectPath := filepath.Join(Flux.rootDir, "apps", app.Name)
err = os.RemoveAll(projectPath)
if err != nil {
return fmt.Errorf("Failed to remove project directory: %v", err)
return fmt.Errorf("failed to remove project directory: %v", err)
}
return nil
@@ -141,13 +130,17 @@ func (am *AppManager) GetAllApps() []*App {
}
func (am *AppManager) AddApp(name string, app *App) {
if app.Deployment.Containers == nil || app.Deployment.Head == nil || len(app.Deployment.Containers) == 0 {
panic("nil containers")
}
am.Store(name, app)
}
func (am *AppManager) DeleteApp(name string) error {
app := am.GetApp(name)
if app == nil {
return fmt.Errorf("App not found")
return fmt.Errorf("app not found")
}
err := app.Remove(context.Background())
@@ -185,10 +178,10 @@ func (am *AppManager) Init() {
}
for _, app := range apps {
var deployment Deployment
deployment := &Deployment{}
var headContainer *Container
Flux.db.QueryRow("SELECT id, url, port FROM deployments WHERE id = ?", app.DeploymentID).Scan(&deployment.ID, &deployment.URL, &deployment.Port)
deployment.Containers = make([]Container, 0)
deployment.Containers = make([]*Container, 0)
rows, err = Flux.db.Query("SELECT id, container_id, deployment_id, head FROM containers WHERE deployment_id = ?", app.DeploymentID)
if err != nil {
@@ -201,19 +194,18 @@ func (am *AppManager) Init() {
var container Container
var containerIDString string
rows.Scan(&container.ID, &containerIDString, &container.DeploymentID, &container.Head)
container.Deployment = &deployment
container.Deployment = deployment
copy(container.ContainerID[:], containerIDString)
if container.Head {
if headContainer != nil {
log.Fatalf("Several containers are marked as head")
}
headContainer = &container
}
deployment.Containers = append(deployment.Containers, container)
}
for i, container := range deployment.Containers {
var volumes []Volume
rows, err := Flux.db.Query("SELECT id, volume_id, container_id FROM volumes WHERE container_id = ?", container.ID)
rows, err := Flux.db.Query("SELECT id, volume_id, container_id, mountpoint FROM volumes WHERE container_id = ?", container.ContainerID[:])
if err != nil {
log.Printf("Failed to query volumes: %v\n", err)
return
@@ -222,17 +214,32 @@ func (am *AppManager) Init() {
for rows.Next() {
var volume Volume
rows.Scan(&volume.ID, &volume.VolumeID, &volume.ContainerID)
volumes = append(volumes, volume)
rows.Scan(&volume.ID, &volume.VolumeID, &volume.ContainerID, &volume.Mountpoint)
container.Volumes = append(container.Volumes, volume)
}
deployment.Containers[i].Volumes = volumes
deployment.Containers = append(deployment.Containers, &container)
}
deployment.Proxy, _ = NewDeploymentProxy(&deployment, headContainer)
if headContainer == nil {
log.Fatalf("head container is nil!")
}
deployment.Head = headContainer
app.Deployment = deployment
am.AddApp(app.Name, &app)
status, err := deployment.Status(context.Background())
if err != nil {
log.Printf("Failed to get deployment status: %v\n", err)
continue
}
if status != "running" {
continue
}
deployment.Proxy, _ = deployment.NewDeploymentProxy()
Flux.proxy.AddDeployment(deployment)
}
}