diff --git a/docker/types.go b/docker/types.go index 45ff332..54cdd76 100644 --- a/docker/types.go +++ b/docker/types.go @@ -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 { diff --git a/handlers.go b/handlers.go index ad527ff..0d5dea3 100644 --- a/handlers.go +++ b/handlers.go @@ -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"}) }