weird nil pointer deref
ci/woodpecker/push/woodpecker Pipeline failed Details

main
Johannes Bülow 2024-02-05 09:45:28 +01:00
parent 37f2801d53
commit d12722cb6b
Signed by untrusted user who does not match committer: jmb
GPG Key ID: B56971CF7B8F83A6
3 changed files with 71 additions and 22 deletions

View File

@ -11,3 +11,4 @@
5533801d-70e3-4c21-9942-82d20930c789 5533801d-70e3-4c21-9942-82d20930c789
5533801d-70e3-4c21-9942-82d20930c789 5533801d-70e3-4c21-9942-82d20930c789
5533801d-70e3-4c21-9942-82d20930c789 5533801d-70e3-4c21-9942-82d20930c789
5533801d-70e3-4c21-9942-82d20930c789

18
web/responses.go Normal file
View File

@ -0,0 +1,18 @@
package web
var clientReloadContent = `
<!DOCTYPE html>
<html>
<head>
<title> Reloading </title>
</head>
<body>
reloading
</body>
<script>
setTimeout(() => {
document.location.reload();
}, 3000);
</script>
</html>
`

View File

@ -29,15 +29,15 @@ func createReverseProxy(backendService string) (*httputil.ReverseProxy, error) {
request.SetURL(backendURL) request.SetURL(backendURL)
request.Out.Host = request.In.Host request.Out.Host = request.In.Host
}, },
ModifyResponse: func(response *http.Response) error { // ModifyResponse: func(response *http.Response) error {
if response.StatusCode == http.StatusBadGateway { // if response.StatusCode == http.StatusBadGateway {
time.Sleep(time.Second) // waitForAnswer(backendURL.String())
response.StatusCode = 307 // response.StatusCode = 200
response.Header.Set("Location", "/") // response.Header.Set("Location", "/")
} // }
return nil // return nil
}, // },
} }
return proxy, err return proxy, err
@ -46,7 +46,7 @@ func createReverseProxy(backendService string) (*httputil.ReverseProxy, error) {
func containerProxy(c *gin.Context) { func containerProxy(c *gin.Context) {
session := sessions.Default(c) session := sessions.Default(c)
sessionID := "" sessionID := ""
if session.Get("ct") == nil && session.Get("ready") == nil { if session.Get("ct") == nil {
session.Set("ready", false) session.Set("ready", false)
session.Save() session.Save()
log.Println("Creating Container for Session ", sessionID) log.Println("Creating Container for Session ", sessionID)
@ -58,6 +58,7 @@ func containerProxy(c *gin.Context) {
session.Delete("ct") session.Delete("ct")
session.Save() session.Save()
c.Abort() c.Abort()
return
} }
err = pods.StartContainer(ct) err = pods.StartContainer(ct)
if err != nil { if err != nil {
@ -65,6 +66,8 @@ func containerProxy(c *gin.Context) {
session.Delete("ct") session.Delete("ct")
session.Save() session.Save()
c.Abort() c.Abort()
return
} }
ctip, err := pods.GetContainerIP(ct) ctip, err := pods.GetContainerIP(ct)
@ -74,11 +77,13 @@ func containerProxy(c *gin.Context) {
session.Delete("ct") session.Delete("ct")
session.Save() session.Save()
c.Abort() c.Abort()
return
} }
// Soft fail Skel // Soft fail Skel
// _ = pods.CopySkelToContainer(ct) // _ = pods.CopySkelToContainer(ct)
err = waitForAnswer(fmt.Sprintf("http://%s:%d", ctip, viper.GetInt("container_port")))
proxies[ct], err = createReverseProxy( proxies[ct], err = createReverseProxy(
fmt.Sprintf("http://%s:%d", ctip, viper.GetInt("container_port")), fmt.Sprintf("http://%s:%d", ctip, viper.GetInt("container_port")),
) )
@ -92,25 +97,28 @@ func containerProxy(c *gin.Context) {
session.Delete("ct") session.Delete("ct")
session.Save() session.Save()
c.Abort() c.Abort()
return
}
if err != nil {
c.HTML(
500,
"Error",
fmt.Sprintf("[%s] Timed out waiting for Container: %v", sessionID, err),
)
session.Delete("ct")
session.Save()
c.Abort()
return
} }
session.Set("ready", true) session.Set("ready", true)
session.Save() session.Save()
c.Redirect(307, "/") c.HTML(200, "", clientReloadContent)
return
} else { } else {
sessionCT := session.Get("ct")
switch sessionCT.(type) {
case string:
default:
c.HTML(500, "Error", "Session Container ID is not a string")
session.Delete("ct")
session.Delete("ready")
session.Save()
c.Abort()
}
if session.Get("ready").(bool) == false { if session.Get("ready").(bool) == false {
time.Sleep(time.Second) time.Sleep(2 * time.Second)
c.Redirect(307, "/") c.Redirect(307, "/")
return
} }
id := session.Get("ct").(string) id := session.Get("ct").(string)
proxy := proxies[id] proxy := proxies[id]
@ -121,8 +129,30 @@ func containerProxy(c *gin.Context) {
session.Delete("ready") session.Delete("ready")
session.Save() session.Save()
c.Abort() c.Abort()
return
} }
} }
} }
func waitForAnswer(url string) error {
retries := 0
var err error
for retries < 50 {
err = nil
response, err := http.Get(url)
if err != nil {
log.Printf("Error connecting to %s: %v", url, err)
}
if response.StatusCode == 200 {
return nil
}
retries++
time.Sleep(500 * time.Millisecond)
}
log.Println("Timed out waiting for Container")
return err
}