mirror of
https://github.com/caddyserver/caddy.git
synced 2024-12-16 21:56:40 -05:00
cmd: New reload command
This commit is contained in:
parent
6bcba91fbe
commit
9429c843c8
3 changed files with 64 additions and 2 deletions
6
admin.go
6
admin.go
|
@ -44,7 +44,7 @@ type AdminConfig struct {
|
||||||
// DefaultAdminConfig is the default configuration
|
// DefaultAdminConfig is the default configuration
|
||||||
// for the administration endpoint.
|
// for the administration endpoint.
|
||||||
var DefaultAdminConfig = &AdminConfig{
|
var DefaultAdminConfig = &AdminConfig{
|
||||||
Listen: "localhost:2019",
|
Listen: DefaultAdminListen,
|
||||||
}
|
}
|
||||||
|
|
||||||
// StartAdmin starts Caddy's administration endpoint,
|
// StartAdmin starts Caddy's administration endpoint,
|
||||||
|
@ -192,6 +192,10 @@ func Load(r io.Reader) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DefaultAdminListen is the address for the admin
|
||||||
|
// listener, if none is specified at startup.
|
||||||
|
var DefaultAdminListen = "localhost:2019"
|
||||||
|
|
||||||
var bufPool = sync.Pool{
|
var bufPool = sync.Pool{
|
||||||
New: func() interface{} {
|
New: func() interface{} {
|
||||||
return new(bytes.Buffer)
|
return new(bytes.Buffer)
|
||||||
|
|
|
@ -15,12 +15,16 @@
|
||||||
package caddycmd
|
package caddycmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
|
"encoding/json"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
@ -200,6 +204,59 @@ func cmdStop() (int, error) {
|
||||||
return 0, nil
|
return 0, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func cmdReload() (int, error) {
|
||||||
|
reloadCmd := flag.NewFlagSet("load", flag.ExitOnError)
|
||||||
|
reloadCmdConfigFlag := reloadCmd.String("config", "", "Configuration file")
|
||||||
|
reloadCmdAddrFlag := reloadCmd.String("address", "", "Address of the administration listener, if different from config")
|
||||||
|
reloadCmd.Parse(os.Args[2:])
|
||||||
|
|
||||||
|
// a configuration is required
|
||||||
|
if *reloadCmdConfigFlag == "" {
|
||||||
|
return 1, fmt.Errorf("no configuration to load (use --config)")
|
||||||
|
}
|
||||||
|
|
||||||
|
// load the configuration file
|
||||||
|
config, err := ioutil.ReadFile(*reloadCmdConfigFlag)
|
||||||
|
if err != nil {
|
||||||
|
return 1, fmt.Errorf("reading config file: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// get the address of the admin listener and craft endpoint URL
|
||||||
|
adminAddr := *reloadCmdAddrFlag
|
||||||
|
if adminAddr == "" {
|
||||||
|
var tmpStruct struct {
|
||||||
|
Admin caddy.AdminConfig `json:"admin"`
|
||||||
|
}
|
||||||
|
err = json.Unmarshal(config, &tmpStruct)
|
||||||
|
if err != nil {
|
||||||
|
return 1, fmt.Errorf("unmarshaling admin listener address from config: %v", err)
|
||||||
|
}
|
||||||
|
adminAddr = tmpStruct.Admin.Listen
|
||||||
|
}
|
||||||
|
if adminAddr == "" {
|
||||||
|
adminAddr = caddy.DefaultAdminListen
|
||||||
|
}
|
||||||
|
adminEndpoint := fmt.Sprintf("http://%s/load", adminAddr)
|
||||||
|
|
||||||
|
// send the configuration to the instance
|
||||||
|
resp, err := http.Post(adminEndpoint, "application/json", bytes.NewReader(config))
|
||||||
|
if err != nil {
|
||||||
|
return 1, fmt.Errorf("sending configuration to instance: %v", err)
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
// if it didn't work, let the user know
|
||||||
|
if resp.StatusCode >= 400 {
|
||||||
|
respBody, err := ioutil.ReadAll(io.LimitReader(resp.Body, 1024*10))
|
||||||
|
if err != nil {
|
||||||
|
return 1, fmt.Errorf("HTTP %d: reading error message: %v", resp.StatusCode, err)
|
||||||
|
}
|
||||||
|
return 1, fmt.Errorf("caddy responded with error: HTTP %d: %s", resp.StatusCode, respBody)
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0, nil
|
||||||
|
}
|
||||||
|
|
||||||
func cmdVersion() (int, error) {
|
func cmdVersion() (int, error) {
|
||||||
goModule := caddy.GoModule()
|
goModule := caddy.GoModule()
|
||||||
if goModule.Sum != "" {
|
if goModule.Sum != "" {
|
||||||
|
|
|
@ -51,8 +51,9 @@ type commandFunc func() (int, error)
|
||||||
|
|
||||||
var commands = map[string]commandFunc{
|
var commands = map[string]commandFunc{
|
||||||
"start": cmdStart,
|
"start": cmdStart,
|
||||||
"stop": cmdStop,
|
|
||||||
"run": cmdRun,
|
"run": cmdRun,
|
||||||
|
"stop": cmdStop,
|
||||||
|
"reload": cmdReload,
|
||||||
"version": cmdVersion,
|
"version": cmdVersion,
|
||||||
"list-modules": cmdListModules,
|
"list-modules": cmdListModules,
|
||||||
"environ": cmdEnviron,
|
"environ": cmdEnviron,
|
||||||
|
|
Loading…
Reference in a new issue