Add nginx exec
This commit is contained in:
parent
b8008606fd
commit
5b6dbaf43e
@ -10,6 +10,8 @@ import (
|
||||
"npm/internal/api/middleware"
|
||||
"npm/internal/api/schema"
|
||||
"npm/internal/entity/certificate"
|
||||
"npm/internal/jobqueue"
|
||||
"npm/internal/logger"
|
||||
)
|
||||
|
||||
// GetCertificates will return a list of Certificates
|
||||
@ -73,6 +75,8 @@ func CreateCertificate() func(http.ResponseWriter, *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
configureCertificate(newCertificate)
|
||||
|
||||
h.ResultResponseJSON(w, r, http.StatusOK, newCertificate)
|
||||
}
|
||||
}
|
||||
@ -119,6 +123,8 @@ func UpdateCertificate() func(http.ResponseWriter, *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
configureCertificate(certificateObject)
|
||||
|
||||
h.ResultResponseJSON(w, r, http.StatusOK, certificateObject)
|
||||
}
|
||||
}
|
||||
@ -143,3 +149,13 @@ func DeleteCertificate() func(http.ResponseWriter, *http.Request) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func configureCertificate(c certificate.Model) {
|
||||
err := jobqueue.AddJob(jobqueue.Job{
|
||||
Name: "RequestCertificate",
|
||||
Action: c.Request,
|
||||
})
|
||||
if err != nil {
|
||||
logger.Error("ConfigureCertificateError", err)
|
||||
}
|
||||
}
|
||||
|
@ -9,6 +9,9 @@ import (
|
||||
h "npm/internal/api/http"
|
||||
"npm/internal/api/middleware"
|
||||
"npm/internal/entity/host"
|
||||
"npm/internal/jobqueue"
|
||||
"npm/internal/logger"
|
||||
"npm/internal/nginx"
|
||||
"npm/internal/validator"
|
||||
)
|
||||
|
||||
@ -80,6 +83,8 @@ func CreateHost() func(http.ResponseWriter, *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
configureHost(newHost)
|
||||
|
||||
h.ResultResponseJSON(w, r, http.StatusOK, newHost)
|
||||
}
|
||||
}
|
||||
@ -114,6 +119,8 @@ func UpdateHost() func(http.ResponseWriter, *http.Request) {
|
||||
// nolint: errcheck,gosec
|
||||
hostObject.Expand(getExpandFromContext(r))
|
||||
|
||||
configureHost(hostObject)
|
||||
|
||||
h.ResultResponseJSON(w, r, http.StatusOK, hostObject)
|
||||
}
|
||||
}
|
||||
@ -138,3 +145,15 @@ func DeleteHost() func(http.ResponseWriter, *http.Request) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func configureHost(h host.Model) {
|
||||
err := jobqueue.AddJob(jobqueue.Job{
|
||||
Name: "NginxConfigureHost",
|
||||
Action: func() error {
|
||||
return nginx.ConfigureHost(h)
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
logger.Error("ConfigureHostError", err)
|
||||
}
|
||||
}
|
||||
|
@ -11,7 +11,7 @@ var (
|
||||
worker *Worker
|
||||
)
|
||||
|
||||
// Start ...
|
||||
// Start will intantiate the queue and start doing work
|
||||
func Start() {
|
||||
ctx, cancel = context.WithCancel(context.Background())
|
||||
q := &Queue{
|
||||
@ -27,6 +27,15 @@ func Start() {
|
||||
go worker.doWork()
|
||||
}
|
||||
|
||||
// Shutdown will gracefully stop the queue
|
||||
func Shutdown() error {
|
||||
if cancel == nil {
|
||||
return errors.New("Unable to shutdown, jobqueue has not been started")
|
||||
}
|
||||
cancel()
|
||||
return nil
|
||||
}
|
||||
|
||||
// AddJob adds a job to the queue for processing
|
||||
func AddJob(j Job) error {
|
||||
if worker == nil {
|
||||
@ -35,12 +44,3 @@ func AddJob(j Job) error {
|
||||
worker.Queue.AddJob(j)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Shutdown ...
|
||||
func Shutdown() error {
|
||||
if cancel == nil {
|
||||
return errors.New("Unable to shutdown, jobqueue has not been started")
|
||||
}
|
||||
cancel()
|
||||
return nil
|
||||
}
|
||||
|
@ -2,7 +2,6 @@ package jobqueue
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"sync"
|
||||
)
|
||||
|
||||
@ -42,13 +41,10 @@ func (q *Queue) AddJobs(jobs []Job) {
|
||||
// AddJob sends job to the channel.
|
||||
func (q *Queue) AddJob(job Job) {
|
||||
q.jobs <- job
|
||||
log.Printf("New job %s added to queue", job.Name)
|
||||
}
|
||||
|
||||
// Run performs job execution.
|
||||
func (j Job) Run() error {
|
||||
log.Printf("Job running: %s", j.Name)
|
||||
|
||||
err := j.Action()
|
||||
if err != nil {
|
||||
return err
|
||||
|
13
backend/internal/nginx/control.go
Normal file
13
backend/internal/nginx/control.go
Normal file
@ -0,0 +1,13 @@
|
||||
package nginx
|
||||
|
||||
import "npm/internal/entity/host"
|
||||
|
||||
// ConfigureHost will attempt to write nginx conf and reload nginx
|
||||
func ConfigureHost(h host.Model) error {
|
||||
// nolint: errcheck, gosec
|
||||
h.Expand([]string{"certificate"})
|
||||
|
||||
// nolint: errcheck, gosec
|
||||
reloadNginx()
|
||||
return nil
|
||||
}
|
43
backend/internal/nginx/exec.go
Normal file
43
backend/internal/nginx/exec.go
Normal file
@ -0,0 +1,43 @@
|
||||
package nginx
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os/exec"
|
||||
|
||||
"npm/internal/logger"
|
||||
)
|
||||
|
||||
func reloadNginx() error {
|
||||
_, err := shExec([]string{"-s", "reload"})
|
||||
return err
|
||||
}
|
||||
|
||||
func getNginxFilePath() (string, error) {
|
||||
path, err := exec.LookPath("nginx")
|
||||
if err != nil {
|
||||
return path, fmt.Errorf("Cannot find nginx execuatable script in PATH")
|
||||
}
|
||||
return path, nil
|
||||
}
|
||||
|
||||
// shExec executes nginx with arguments
|
||||
func shExec(args []string) (string, error) {
|
||||
ng, err := getNginxFilePath()
|
||||
if err != nil {
|
||||
logger.Error("NginxError", err)
|
||||
return "", err
|
||||
}
|
||||
|
||||
logger.Debug("CMD: %s %v", ng, args)
|
||||
// nolint: gosec
|
||||
c := exec.Command(ng, args...)
|
||||
|
||||
b, e := c.Output()
|
||||
|
||||
if e != nil {
|
||||
logger.Error("NginxError", fmt.Errorf("Command error: %s -- %v\n%+v", ng, args, e))
|
||||
logger.Warn(string(b))
|
||||
}
|
||||
|
||||
return string(b), e
|
||||
}
|
Loading…
Reference in New Issue
Block a user