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:
parent
9e858935c9
commit
be1ef03328
@ -160,11 +160,22 @@ func (c *Container) Destroy() error {
|
|||||||
|
|
||||||
// Delete removes both data and the container
|
// Delete removes both data and the container
|
||||||
func (c *Container) Delete() error {
|
func (c *Container) Delete() error {
|
||||||
err := c.Destroy()
|
status, err := c.Status()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
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)
|
volumePath := path.Join("/srv", c.App.Name)
|
||||||
err = removeDirectory(volumePath)
|
err = removeDirectory(volumePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
60
handlers.go
60
handlers.go
@ -2,6 +2,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/labstack/echo"
|
"github.com/labstack/echo"
|
||||||
@ -150,11 +151,19 @@ func stopAppHandler(c echo.Context) error {
|
|||||||
App: app,
|
App: app,
|
||||||
}
|
}
|
||||||
|
|
||||||
err = container.Stop()
|
status, err := container.Status()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return c.JSONPretty(http.StatusInternalServerError, Message{Message: err.Error()}, JSONIndent)
|
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"})
|
return c.JSON(http.StatusOK, Message{Message: "ok"})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -171,6 +180,17 @@ func startAppHandler(c echo.Context) error {
|
|||||||
App: app,
|
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()
|
err = container.Start()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return c.JSONPretty(http.StatusInternalServerError, Message{Message: err.Error()}, JSONIndent)
|
return c.JSONPretty(http.StatusInternalServerError, Message{Message: err.Error()}, JSONIndent)
|
||||||
@ -381,6 +401,7 @@ func deleteLabelHandler(c echo.Context) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Delete one app
|
// Delete one app
|
||||||
|
// This is async function.
|
||||||
func deleteAppHandler(c echo.Context) error {
|
func deleteAppHandler(c echo.Context) error {
|
||||||
name := c.Param("name")
|
name := c.Param("name")
|
||||||
|
|
||||||
@ -389,25 +410,28 @@ func deleteAppHandler(c echo.Context) error {
|
|||||||
return c.JSONPretty(http.StatusInternalServerError, Message{Message: err.Error()}, JSONIndent)
|
return c.JSONPretty(http.StatusInternalServerError, Message{Message: err.Error()}, JSONIndent)
|
||||||
}
|
}
|
||||||
|
|
||||||
container := docker.Container{
|
go func(app *apps.App) {
|
||||||
App: app,
|
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)
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
err = apps.Delete(name)
|
status, err := container.Status()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return c.JSONPretty(http.StatusInternalServerError, Message{Message: err.Error()}, JSONIndent)
|
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"})
|
return c.JSON(http.StatusOK, Message{Message: "deleted"})
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user