Write host template on save

This commit is contained in:
Jamie Curnow
2022-07-21 18:02:07 +10:00
parent 5b6dbaf43e
commit 8d37f5df8d
5 changed files with 182 additions and 33 deletions

View File

@ -1,13 +1,51 @@
package nginx
import "npm/internal/entity/host"
import (
"fmt"
"npm/internal/config"
"npm/internal/entity/host"
"npm/internal/logger"
)
// ConfigureHost will attempt to write nginx conf and reload nginx
func ConfigureHost(h host.Model) error {
// nolint: errcheck, gosec
h.Expand([]string{"certificate"})
h.Expand([]string{"certificate", "hosttemplate"})
data := TemplateData{
ConfDir: fmt.Sprintf("%s/nginx/hosts", config.Configuration.DataFolder),
DataDir: config.Configuration.DataFolder,
CertsDir: config.Configuration.Acmesh.CertHome,
Host: &h,
Certificate: h.Certificate,
}
filename := fmt.Sprintf("%s/host_%d.conf", data.ConfDir, h.ID)
// Write the config to disk
err := writeTemplate(filename, h.HostTemplate.Template, data)
if err != nil {
// this configuration failed somehow
h.Status = host.StatusError
h.ErrorMessage = fmt.Sprintf("Template generation failed: %s", err.Error())
logger.Debug(h.ErrorMessage)
return h.Save(true)
}
// nolint: errcheck, gosec
reloadNginx()
return nil
if err := reloadNginx(); err != nil {
// reloading nginx failed, likely due to this host having a problem
h.Status = host.StatusError
h.ErrorMessage = fmt.Sprintf("Nginx configuation error: %s", err.Error())
writeConfigFile(filename, fmt.Sprintf("# %s", h.ErrorMessage))
logger.Debug(h.ErrorMessage)
} else {
// All good
h.Status = host.StatusOK
h.ErrorMessage = ""
logger.Debug("ConfigureHost OK: %+v", h)
}
return h.Save(true)
}