forked from Nixius/authelia
93 lines
2.9 KiB
Go
93 lines
2.9 KiB
Go
package handlers
|
|
|
|
import (
|
|
"log"
|
|
"net/http"
|
|
"net/url"
|
|
)
|
|
|
|
func (a *App) handleStackManage(w http.ResponseWriter, r *http.Request) {
|
|
acct, _, err := a.currentAccount(r)
|
|
if err != nil || acct == nil {
|
|
http.Redirect(w, r, a.cfg.IdentityURL, http.StatusSeeOther)
|
|
return
|
|
}
|
|
if !isSubscribedAccount(acct) {
|
|
http.Error(w, "forbidden", http.StatusForbidden)
|
|
return
|
|
}
|
|
inst, err := a.accounts.InstanceByAccountID(r.Context(), acct.ID)
|
|
if err != nil {
|
|
log.Printf("stack-manage instance lookup account=%d: %v", acct.ID, err)
|
|
redirectWithStackError(w, r, a.cfg.AppURL+"/dashboard", err)
|
|
return
|
|
}
|
|
|
|
action := r.FormValue("action")
|
|
|
|
switch action {
|
|
case "stop":
|
|
if err := a.swarm.ScaleStack(inst.StackName, 0); err != nil {
|
|
log.Printf("stack-manage stop %s: %v", inst.StackName, err)
|
|
redirectWithStackError(w, r, a.cfg.AppURL+"/dashboard", err)
|
|
return
|
|
}
|
|
_ = a.accounts.UpdateInstanceState(r.Context(), inst.StackName, "stopped", false)
|
|
|
|
case "start":
|
|
exists, _ := a.swarm.StackExists(inst.StackName)
|
|
if !exists {
|
|
if err := a.swarm.DeployStack(inst.StackName, inst.Slug, a.cfg.TraefikDomain); err != nil {
|
|
log.Printf("stack-manage start (deploy) %s: %v", inst.StackName, err)
|
|
redirectWithStackError(w, r, a.cfg.AppURL+"/dashboard", err)
|
|
return
|
|
}
|
|
} else {
|
|
if err := a.swarm.ScaleStack(inst.StackName, 1); err != nil {
|
|
log.Printf("stack-manage start (scale) %s: %v", inst.StackName, err)
|
|
redirectWithStackError(w, r, a.cfg.AppURL+"/dashboard", err)
|
|
return
|
|
}
|
|
}
|
|
_ = a.accounts.UpdateInstanceState(r.Context(), inst.StackName, "running", true)
|
|
|
|
case "restart":
|
|
if err := a.swarm.RestartStack(inst.StackName); err != nil {
|
|
log.Printf("stack-manage restart %s: %v", inst.StackName, err)
|
|
redirectWithStackError(w, r, a.cfg.AppURL+"/dashboard", err)
|
|
return
|
|
}
|
|
_ = a.accounts.UpdateInstanceState(r.Context(), inst.StackName, "running", true)
|
|
|
|
case "rebuild":
|
|
if err := a.swarm.DeployStack(inst.StackName, inst.Slug, a.cfg.TraefikDomain); err != nil {
|
|
log.Printf("stack-manage rebuild %s: %v", inst.StackName, err)
|
|
redirectWithStackError(w, r, a.cfg.AppURL+"/dashboard", err)
|
|
return
|
|
}
|
|
_ = a.accounts.UpdateInstanceState(r.Context(), inst.StackName, "running", true)
|
|
|
|
case "destroy":
|
|
if err := a.swarm.RemoveStack(inst.StackName); err != nil {
|
|
log.Printf("stack-manage destroy %s: %v", inst.StackName, err)
|
|
redirectWithStackError(w, r, a.cfg.AppURL+"/dashboard", err)
|
|
return
|
|
}
|
|
_ = a.accounts.UpdateInstanceState(r.Context(), inst.StackName, "destroyed", false)
|
|
|
|
default:
|
|
http.Error(w, "unknown action", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
http.Redirect(w, r, a.cfg.AppURL+"/dashboard", http.StatusSeeOther)
|
|
}
|
|
|
|
func redirectWithStackError(w http.ResponseWriter, r *http.Request, baseURL string, err error) {
|
|
u, _ := url.Parse(baseURL)
|
|
q := u.Query()
|
|
q.Set("stack_error", err.Error())
|
|
u.RawQuery = q.Encode()
|
|
http.Redirect(w, r, u.String(), http.StatusSeeOther)
|
|
}
|