mirror of
https://github.com/caddyserver/caddy.git
synced 2024-12-23 22:27:38 -05:00
baf6db5b57
I am not a lawyer, but according to the appendix of the license, these boilerplate notices should be included with every source file.
87 lines
2.3 KiB
Go
87 lines
2.3 KiB
Go
// Copyright 2015 Light Code Labs, LLC
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package startupshutdown
|
|
|
|
import (
|
|
"log"
|
|
"os"
|
|
"os/exec"
|
|
"strings"
|
|
|
|
"github.com/mholt/caddy"
|
|
)
|
|
|
|
func init() {
|
|
caddy.RegisterPlugin("startup", caddy.Plugin{Action: Startup})
|
|
caddy.RegisterPlugin("shutdown", caddy.Plugin{Action: Shutdown})
|
|
}
|
|
|
|
// Startup registers a startup callback to execute during server start.
|
|
func Startup(c *caddy.Controller) error {
|
|
return registerCallback(c, c.OnFirstStartup)
|
|
}
|
|
|
|
// Shutdown registers a shutdown callback to execute during server stop.
|
|
func Shutdown(c *caddy.Controller) error {
|
|
return registerCallback(c, c.OnFinalShutdown)
|
|
}
|
|
|
|
// registerCallback registers a callback function to execute by
|
|
// using c to parse the directive. It registers the callback
|
|
// to be executed using registerFunc.
|
|
func registerCallback(c *caddy.Controller, registerFunc func(func() error)) error {
|
|
var funcs []func() error
|
|
|
|
for c.Next() {
|
|
args := c.RemainingArgs()
|
|
if len(args) == 0 {
|
|
return c.ArgErr()
|
|
}
|
|
|
|
nonblock := false
|
|
if len(args) > 1 && args[len(args)-1] == "&" {
|
|
// Run command in background; non-blocking
|
|
nonblock = true
|
|
args = args[:len(args)-1]
|
|
}
|
|
|
|
command, args, err := caddy.SplitCommandAndArgs(strings.Join(args, " "))
|
|
if err != nil {
|
|
return c.Err(err.Error())
|
|
}
|
|
|
|
fn := func() error {
|
|
cmd := exec.Command(command, args...)
|
|
cmd.Stdin = os.Stdin
|
|
cmd.Stdout = os.Stdout
|
|
cmd.Stderr = os.Stderr
|
|
if nonblock {
|
|
log.Printf("[INFO] Nonblocking Command:\"%s %s\"", command, strings.Join(args, " "))
|
|
return cmd.Start()
|
|
}
|
|
log.Printf("[INFO] Blocking Command:\"%s %s\"", command, strings.Join(args, " "))
|
|
return cmd.Run()
|
|
}
|
|
|
|
funcs = append(funcs, fn)
|
|
}
|
|
|
|
return c.OncePerServerBlock(func() error {
|
|
for _, fn := range funcs {
|
|
registerFunc(fn)
|
|
}
|
|
return nil
|
|
})
|
|
}
|