Delete fixes

* Deletion happens in background
* Fix: Problem of deleting app when container doesn't exist
* Fix: Start of application creates container if it doesn't exist
* Fix: Stoping containers won't fail if container doesn't exist
This commit is contained in:
Adam Štrauch 2020-10-05 11:43:09 +02:00
parent 9e858935c9
commit be1ef03328
Signed by: cx
GPG Key ID: 018304FFA8988F8D
2 changed files with 54 additions and 19 deletions

View File

@ -160,11 +160,22 @@ func (c *Container) Destroy() error {
// Delete removes both data and the container
func (c *Container) Delete() error {
err := c.Destroy()
status, err := c.Status()
if err != nil {
return err
}
// It's questionable to have this here. The problem is this method
// does two things, deleting the container and the data and when
// the deleted container doesn't exist we actually don't care
// and we can continue to remove the data.
if status != "no-container" {
err = c.Destroy()
if err != nil {
return err
}
}
volumePath := path.Join("/srv", c.App.Name)
err = removeDirectory(volumePath)
if err != nil {

View File

@ -2,6 +2,7 @@ package main
import (
"io/ioutil"
"log"
"net/http"
"github.com/labstack/echo"
@ -150,11 +151,19 @@ func stopAppHandler(c echo.Context) error {
App: app,
}
err = container.Stop()
status, err := container.Status()
if err != nil {
return c.JSONPretty(http.StatusInternalServerError, Message{Message: err.Error()}, JSONIndent)
}
// Stop the container only when it exists
if status != "no-container" {
err = container.Stop()
if err != nil {
return c.JSONPretty(http.StatusInternalServerError, Message{Message: err.Error()}, JSONIndent)
}
}
return c.JSON(http.StatusOK, Message{Message: "ok"})
}
@ -171,6 +180,17 @@ func startAppHandler(c echo.Context) error {
App: app,
}
status, err := container.Status()
if err != nil {
return err
}
if status == "no-container" {
err = container.Create()
if err != nil {
return c.JSONPretty(http.StatusInternalServerError, Message{Message: err.Error()}, JSONIndent)
}
}
err = container.Start()
if err != nil {
return c.JSONPretty(http.StatusInternalServerError, Message{Message: err.Error()}, JSONIndent)
@ -381,6 +401,7 @@ func deleteLabelHandler(c echo.Context) error {
}
// Delete one app
// This is async function.
func deleteAppHandler(c echo.Context) error {
name := c.Param("name")
@ -389,25 +410,28 @@ func deleteAppHandler(c echo.Context) error {
return c.JSONPretty(http.StatusInternalServerError, Message{Message: err.Error()}, JSONIndent)
}
container := docker.Container{
App: app,
}
status, err := container.Status()
if err != nil {
return c.JSONPretty(http.StatusInternalServerError, Message{Message: err.Error()}, JSONIndent)
}
if status != "no-container" {
err = container.Delete()
if err != nil {
return c.JSONPretty(http.StatusInternalServerError, Message{Message: err.Error()}, JSONIndent)
go func(app *apps.App) {
container := docker.Container{
App: app,
}
}
err = apps.Delete(name)
if err != nil {
return c.JSONPretty(http.StatusInternalServerError, Message{Message: err.Error()}, JSONIndent)
}
status, err := container.Status()
if err != nil {
log.Println("ERROR delete application problem: " + err.Error())
}
if status != "no-container" {
err = container.Delete()
if err != nil {
log.Println("ERROR delete application problem: " + err.Error())
}
}
err = apps.Delete(app.Name)
if err != nil {
log.Println("ERROR delete application problem: " + err.Error())
}
}(app)
return c.JSON(http.StatusOK, Message{Message: "deleted"})
}