1
0
mirror of https://github.com/rancher/os.git synced 2025-06-27 07:16:48 +00:00
os/cmd/respawn/respawn.go

172 lines
3.0 KiB
Go
Raw Normal View History

2015-02-14 16:34:31 +00:00
package respawn
import (
"fmt"
2015-02-20 03:05:43 +00:00
"io"
2015-02-14 16:34:31 +00:00
"io/ioutil"
"os"
"os/exec"
2015-03-15 04:31:31 +00:00
"os/signal"
"runtime"
2015-02-14 16:34:31 +00:00
"strings"
"sync"
2015-03-15 04:31:31 +00:00
"syscall"
2015-02-14 16:34:31 +00:00
"time"
2015-02-20 03:05:43 +00:00
"github.com/codegangsta/cli"
"github.com/rancher/os/config"
2018-09-16 04:55:26 +00:00
"github.com/rancher/os/pkg/log"
2015-02-14 16:34:31 +00:00
)
2015-03-15 04:31:31 +00:00
var (
2016-11-28 08:06:00 +00:00
running = true
processes = map[int]*os.Process{}
processLock = sync.Mutex{}
2015-03-15 04:31:31 +00:00
)
2015-02-14 16:34:31 +00:00
func Main() {
log.InitLogger()
runtime.GOMAXPROCS(1)
runtime.LockOSThread()
2015-02-20 03:05:43 +00:00
app := cli.NewApp()
app.Name = os.Args[0]
app.Usage = fmt.Sprintf("%s RancherOS\nbuilt: %s", app.Name, config.BuildDate)
app.Version = config.Version
app.Author = "Rancher Labs, Inc."
2015-02-20 03:05:43 +00:00
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "file, f",
Usage: "Optional config file to load",
},
}
app.Action = run
log.Infof("%s, %s", app.Usage, app.Version)
fmt.Printf("%s, %s", app.Usage, app.Version)
2015-02-20 03:05:43 +00:00
app.Run(os.Args)
}
2015-03-15 04:31:31 +00:00
func setupSigterm() {
sigtermChan := make(chan os.Signal)
signal.Notify(sigtermChan, syscall.SIGTERM)
go func() {
for range sigtermChan {
2015-03-15 04:31:31 +00:00
termPids()
}
}()
}
func run(c *cli.Context) error {
2015-03-15 04:31:31 +00:00
setupSigterm()
2015-02-20 03:05:43 +00:00
var stream io.Reader = os.Stdin
var err error
inputFileName := c.String("file")
if inputFileName != "" {
stream, err = os.Open(inputFileName)
if err != nil {
log.Fatal(err)
}
}
input, err := ioutil.ReadAll(stream)
2015-02-14 16:34:31 +00:00
if err != nil {
panic(err)
}
lines := strings.Split(string(input), "\n")
doneChannel := make(chan string, len(lines))
2015-02-14 16:34:31 +00:00
for _, line := range lines {
if strings.TrimSpace(line) == "" || strings.Index(strings.TrimSpace(line), "#") == 0 {
2015-02-23 03:56:37 +00:00
continue
}
go execute(line, doneChannel)
2015-02-14 16:34:31 +00:00
}
for i := 0; i < len(lines); i++ {
line := <-doneChannel
log.Infof("FINISHED: %s", line)
fmt.Printf("FINISHED: %s", line)
}
return nil
2015-03-15 04:31:31 +00:00
}
func addProcess(process *os.Process) {
processLock.Lock()
defer processLock.Unlock()
processes[process.Pid] = process
}
func removeProcess(process *os.Process) {
processLock.Lock()
defer processLock.Unlock()
delete(processes, process.Pid)
2015-02-14 16:34:31 +00:00
}
2015-03-15 04:31:31 +00:00
func termPids() {
running = false
processLock.Lock()
defer processLock.Unlock()
for _, process := range processes {
log.Infof("sending SIGTERM to %d", process.Pid)
2015-03-15 04:31:31 +00:00
process.Signal(syscall.SIGTERM)
}
}
func execute(line string, doneChannel chan string) {
defer func() { doneChannel <- line }()
2015-03-15 04:31:31 +00:00
2015-02-14 16:34:31 +00:00
start := time.Now()
count := 0
args := strings.Split(line, " ")
2015-02-14 16:34:31 +00:00
for {
cmd := exec.Command(args[0], args[1:]...)
2015-08-06 19:01:57 +00:00
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.SysProcAttr = &syscall.SysProcAttr{
Setsid: true,
}
2015-02-14 16:34:31 +00:00
err := cmd.Start()
if err != nil {
2015-03-15 04:31:31 +00:00
log.Errorf("%s : %v", line, err)
}
if err == nil {
addProcess(cmd.Process)
err = cmd.Wait()
removeProcess(cmd.Process)
2015-02-14 16:34:31 +00:00
}
if err != nil {
2015-03-15 04:31:31 +00:00
log.Errorf("%s : %v", line, err)
}
if !running {
2015-03-18 12:36:19 +00:00
log.Infof("%s : not restarting, exiting", line)
2015-03-15 04:31:31 +00:00
break
2015-02-14 16:34:31 +00:00
}
count++
if count > 10 {
if time.Now().Sub(start) <= (1 * time.Second) {
2015-02-14 16:34:31 +00:00
log.Errorf("%s : restarted too fast, not executing", line)
break
}
count = 0
start = time.Now()
}
}
}