fix dial errors (I fucking did it)

This commit is contained in:
Zoe
2024-12-07 03:03:38 -06:00
parent d27cc71f1d
commit f88a5b3db5
5 changed files with 60 additions and 41 deletions

View File

@@ -371,7 +371,7 @@ func main() {
os.Exit(0) os.Exit(0)
} }
req, err := http.NewRequest("DELETE", config.DeamonURL+"/deploy/"+projectName, nil) req, err := http.NewRequest("DELETE", config.DeamonURL+"/deployments/"+projectName, nil)
if err != nil { if err != nil {
fmt.Printf("Failed to delete app: %v\n", err) fmt.Printf("Failed to delete app: %v\n", err)
os.Exit(1) os.Exit(1)

View File

@@ -80,7 +80,8 @@ func CreateDockerContainer(ctx context.Context, imageName, projectPath string, p
}, },
}, },
&container.HostConfig{ &container.HostConfig{
NetworkMode: "bridge", RestartPolicy: container.RestartPolicy{Name: container.RestartPolicyUnlessStopped},
NetworkMode: "bridge",
Mounts: []mount.Mount{ Mounts: []mount.Mount{
{ {
Type: mount.TypeVolume, Type: mount.TypeVolume,

View File

@@ -140,6 +140,22 @@ func (s *FluxServer) DeployHandler(w http.ResponseWriter, r *http.Request) {
return return
} }
var headContainer *Container
for _, container := range deployment.Containers {
if container.Head {
headContainer = &container
}
}
deployment.Proxy, err = NewDeploymentProxy(&deployment, headContainer)
if err != nil {
log.Printf("Failed to create deployment proxy: %v\n", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
ReverseProxy.AddDeployment(&deployment)
Apps.AddApp(app.Name, app) Apps.AddApp(app.Name, app)
} else { } else {
log.Printf("Upgrading deployment %s...\n", app.Name) log.Printf("Upgrading deployment %s...\n", app.Name)

View File

@@ -6,7 +6,6 @@ import (
"fmt" "fmt"
"log" "log"
"sync" "sync"
"time"
"github.com/juls0730/fluxd/pkg" "github.com/juls0730/fluxd/pkg"
) )
@@ -114,12 +113,12 @@ func (am *AppManager) Init() {
deployment.Containers = append(deployment.Containers, container) deployment.Containers = append(deployment.Containers, container)
} }
deployment.Proxy = &DeploymentProxy{ deployment.Proxy, err = NewDeploymentProxy(&deployment, headContainer)
deployment: &deployment, if err != nil {
currentHead: headContainer, log.Printf("Failed to create deployment proxy: %v\n", err)
gracePeriod: time.Second * 30, return
activeRequests: 0,
} }
app.Deployment = deployment app.Deployment = deployment
Apps.AddApp(app.Name, &app) Apps.AddApp(app.Name, &app)
@@ -162,16 +161,8 @@ func CreateDeployment(containerID string, port uint16, appUrl string, db *sql.DB
} }
copy(container.ContainerID[:], containerIDString) copy(container.ContainerID[:], containerIDString)
deployment.Proxy = &DeploymentProxy{
deployment: &deployment,
currentHead: &container,
gracePeriod: time.Second * 30,
activeRequests: 0,
}
container.Deployment = &deployment container.Deployment = &deployment
deployment.Containers = append(deployment.Containers, container) deployment.Containers = append(deployment.Containers, container)
ReverseProxy.AddDeployment(&deployment)
return deployment, nil return deployment, nil
} }
@@ -252,11 +243,10 @@ func (deployment *Deployment) Upgrade(ctx context.Context, projectConfig pkg.Pro
// Create a new proxy that points to the new head, and replace the old one, but ensure that the old one is gracefully shutdown // Create a new proxy that points to the new head, and replace the old one, but ensure that the old one is gracefully shutdown
oldProxy := deployment.Proxy oldProxy := deployment.Proxy
deployment.Proxy = &DeploymentProxy{ deployment.Proxy, err = NewDeploymentProxy(deployment, &container)
deployment: deployment, if err != nil {
currentHead: &container, log.Printf("Failed to create deployment proxy: %v\n", err)
gracePeriod: time.Second * 30, return err
activeRequests: 0,
} }
var containers []Container var containers []Container

View File

@@ -35,22 +35,30 @@ func (p *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
atomic.AddInt64(&deployment.(*Deployment).Proxy.activeRequests, 1) atomic.AddInt64(&deployment.(*Deployment).Proxy.activeRequests, 1)
container := deployment.(*Deployment).Proxy.currentHead deployment.(*Deployment).Proxy.proxy.ServeHTTP(w, r)
if container == nil { }
http.Error(w, "No active container found", http.StatusNotFound)
return type DeploymentProxy struct {
deployment *Deployment
currentHead *Container
proxy *httputil.ReverseProxy
gracePeriod time.Duration
activeRequests int64
}
func NewDeploymentProxy(deployment *Deployment, head *Container) (*DeploymentProxy, error) {
containerJSON, err := dockerClient.ContainerInspect(context.Background(), string(head.ContainerID[:]))
if err != nil {
return nil, err
} }
containerJSON, err := dockerClient.ContainerInspect(context.Background(), string(container.ContainerID[:])) if containerJSON.NetworkSettings.IPAddress == "" {
if err != nil { return nil, fmt.Errorf("No IP address found for container %s", head.ContainerID[0:12])
http.Error(w, err.Error(), http.StatusInternalServerError)
return
} }
containerUrl, err := url.Parse(fmt.Sprintf("http://%s:%d", containerJSON.NetworkSettings.IPAddress, container.Deployment.Port)) containerUrl, err := url.Parse(fmt.Sprintf("http://%s:%d", containerJSON.NetworkSettings.IPAddress, deployment.Port))
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) return nil, err
return
} }
proxy := &httputil.ReverseProxy{ proxy := &httputil.ReverseProxy{
@@ -58,20 +66,24 @@ func (p *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
req.URL = containerUrl req.URL = containerUrl
req.Host = containerUrl.Host req.Host = containerUrl.Host
}, },
Transport: &http.Transport{
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
MaxIdleConnsPerHost: 100,
},
ModifyResponse: func(resp *http.Response) error { ModifyResponse: func(resp *http.Response) error {
atomic.AddInt64(&deployment.(*Deployment).Proxy.activeRequests, -1) atomic.AddInt64(&deployment.Proxy.activeRequests, -1)
return nil return nil
}, },
} }
proxy.ServeHTTP(w, r) return &DeploymentProxy{
} deployment: deployment,
currentHead: head,
type DeploymentProxy struct { proxy: proxy,
deployment *Deployment gracePeriod: time.Second * 30,
currentHead *Container activeRequests: 0,
gracePeriod time.Duration }, nil
activeRequests int64
} }
func (dp *DeploymentProxy) GracefulShutdown(oldContainers []*Container) { func (dp *DeploymentProxy) GracefulShutdown(oldContainers []*Container) {