2019-11-02 14:32:58 +00:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"os/signal"
|
2019-11-24 20:27:59 +00:00
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2019-11-02 14:32:58 +00:00
|
|
|
"sync"
|
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2020-05-02 05:06:39 +00:00
|
|
|
// Command create a command at the project root.
|
2019-11-24 20:27:59 +00:00
|
|
|
func Command(name string, args ...string) *exec.Cmd {
|
|
|
|
cmd := exec.Command(name, args...)
|
|
|
|
|
2020-05-02 05:06:39 +00:00
|
|
|
// By default set the working directory to the project root directory.
|
2019-11-24 20:27:59 +00:00
|
|
|
wd, _ := os.Getwd()
|
|
|
|
for !strings.HasSuffix(wd, "authelia") {
|
|
|
|
wd = filepath.Dir(wd)
|
|
|
|
}
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2019-11-24 20:27:59 +00:00
|
|
|
cmd.Dir = wd
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2019-11-24 20:27:59 +00:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2020-05-02 05:06:39 +00:00
|
|
|
// CommandWithStdout create a command forwarding stdout and stderr to the OS streams.
|
2019-11-02 14:32:58 +00:00
|
|
|
func CommandWithStdout(name string, args ...string) *exec.Cmd {
|
2019-11-24 20:27:59 +00:00
|
|
|
cmd := Command(name, args...)
|
2019-11-30 16:49:52 +00:00
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stderr = os.Stderr
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2019-11-02 14:32:58 +00:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2020-05-02 05:06:39 +00:00
|
|
|
// Shell create a shell command.
|
2019-11-02 14:32:58 +00:00
|
|
|
func Shell(command string) *exec.Cmd {
|
|
|
|
return CommandWithStdout("bash", "-c", command)
|
|
|
|
}
|
|
|
|
|
2020-05-02 05:06:39 +00:00
|
|
|
// RunCommandUntilCtrlC run a command until ctrl-c is hit.
|
2019-11-02 14:32:58 +00:00
|
|
|
func RunCommandUntilCtrlC(cmd *exec.Cmd) {
|
|
|
|
mutex := sync.Mutex{}
|
|
|
|
cond := sync.NewCond(&mutex)
|
2021-06-11 00:30:53 +00:00
|
|
|
signalChannel := make(chan os.Signal, 1)
|
2019-11-02 14:32:58 +00:00
|
|
|
signal.Notify(signalChannel, os.Interrupt, syscall.SIGTERM)
|
|
|
|
|
|
|
|
mutex.Lock()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
mutex.Lock()
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2019-11-02 14:32:58 +00:00
|
|
|
f := bufio.NewWriter(os.Stdout)
|
|
|
|
defer f.Flush()
|
|
|
|
|
|
|
|
fmt.Println("Hit Ctrl+C to shutdown...")
|
|
|
|
|
|
|
|
err := cmd.Run()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
cond.Broadcast()
|
|
|
|
mutex.Unlock()
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2019-11-02 14:32:58 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
<-signalChannel
|
|
|
|
cond.Broadcast()
|
|
|
|
mutex.Unlock()
|
|
|
|
}()
|
|
|
|
|
|
|
|
cond.Wait()
|
|
|
|
}
|
|
|
|
|
2020-05-02 05:06:39 +00:00
|
|
|
// RunFuncUntilCtrlC run a function until ctrl-c is hit.
|
2019-11-02 14:32:58 +00:00
|
|
|
func RunFuncUntilCtrlC(fn func() error) error {
|
|
|
|
mutex := sync.Mutex{}
|
|
|
|
cond := sync.NewCond(&mutex)
|
|
|
|
errorChannel := make(chan error)
|
2021-06-11 00:30:53 +00:00
|
|
|
signalChannel := make(chan os.Signal, 1)
|
2019-11-02 14:32:58 +00:00
|
|
|
signal.Notify(signalChannel, os.Interrupt, syscall.SIGTERM)
|
|
|
|
|
|
|
|
mutex.Lock()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
mutex.Lock()
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2019-11-02 14:32:58 +00:00
|
|
|
f := bufio.NewWriter(os.Stdout)
|
|
|
|
defer f.Flush()
|
|
|
|
|
|
|
|
fmt.Println("Hit Ctrl+C to shutdown...")
|
|
|
|
|
|
|
|
err := fn()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
errorChannel <- err
|
|
|
|
fmt.Println(err)
|
|
|
|
cond.Broadcast()
|
|
|
|
mutex.Unlock()
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2019-11-02 14:32:58 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
errorChannel <- nil
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2019-11-02 14:32:58 +00:00
|
|
|
<-signalChannel
|
|
|
|
cond.Broadcast()
|
|
|
|
mutex.Unlock()
|
|
|
|
}()
|
|
|
|
|
|
|
|
cond.Wait()
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2019-11-02 14:32:58 +00:00
|
|
|
return <-errorChannel
|
|
|
|
}
|
|
|
|
|
|
|
|
// RunCommandWithTimeout run a command with timeout.
|
|
|
|
func RunCommandWithTimeout(cmd *exec.Cmd, timeout time.Duration) error {
|
|
|
|
// Start a process:
|
|
|
|
if err := cmd.Start(); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for the process to finish or kill it after a timeout (whichever happens first):
|
|
|
|
done := make(chan error, 1)
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2019-11-02 14:32:58 +00:00
|
|
|
go func() {
|
|
|
|
done <- cmd.Wait()
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-time.After(timeout):
|
|
|
|
fmt.Printf("Timeout of %ds reached... Killing process...\n", int64(timeout/time.Second))
|
|
|
|
|
2019-11-16 19:50:58 +00:00
|
|
|
if err := cmd.Process.Kill(); err != nil {
|
2019-11-02 14:32:58 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2019-11-30 16:49:52 +00:00
|
|
|
return ErrTimeoutReached
|
2019-11-02 14:32:58 +00:00
|
|
|
case err := <-done:
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2021-05-05 07:09:31 +00:00
|
|
|
|
|
|
|
// RunFuncWithRetry run a function for n attempts with a sleep of n duration between each attempt.
|
|
|
|
func RunFuncWithRetry(attempts int, sleep time.Duration, f func() error) (err error) {
|
|
|
|
for i := 0; ; i++ {
|
|
|
|
err = f()
|
|
|
|
if err == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if i >= (attempts - 1) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(sleep)
|
|
|
|
|
|
|
|
log.Printf("Retrying after error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("Failed after %d attempts, last error: %s", attempts, err)
|
|
|
|
}
|