2021-06-20 23:01:44 +02:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/kardianos/service"
|
2021-08-17 09:14:21 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
2021-06-20 23:01:44 +02:00
|
|
|
"github.com/spf13/cobra"
|
2021-10-17 21:34:07 +02:00
|
|
|
"github.com/wiretrustee/wiretrustee/util"
|
2021-10-17 22:15:38 +02:00
|
|
|
"time"
|
2021-06-20 23:01:44 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func (p *program) Start(s service.Service) error {
|
2021-10-18 13:29:26 +02:00
|
|
|
|
2021-06-20 23:01:44 +02:00
|
|
|
// Start should not block. Do the actual work async.
|
2021-08-17 09:14:21 +02:00
|
|
|
log.Info("starting service") //nolint
|
2021-08-18 13:35:42 +02:00
|
|
|
go func() {
|
2021-11-01 09:34:06 +01:00
|
|
|
err := runClient()
|
2021-08-18 13:35:42 +02:00
|
|
|
if err != nil {
|
2021-11-01 09:34:06 +01:00
|
|
|
log.Errorf("stopped Wiretrustee client app due to error: %v", err)
|
2021-08-18 13:35:42 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}()
|
2021-06-20 23:01:44 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *program) Stop(s service.Service) error {
|
2021-10-18 13:29:26 +02:00
|
|
|
go func() {
|
|
|
|
stopCh <- 1
|
|
|
|
}()
|
2021-10-17 22:15:38 +02:00
|
|
|
|
|
|
|
select {
|
|
|
|
case <-cleanupCh:
|
|
|
|
case <-time.After(time.Second * 10):
|
|
|
|
log.Warnf("failed waiting for service cleanup, terminating")
|
|
|
|
}
|
|
|
|
log.Info("stopped Wiretrustee service") //nolint
|
2021-06-20 23:01:44 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
runCmd = &cobra.Command{
|
|
|
|
Use: "run",
|
|
|
|
Short: "runs wiretrustee as service",
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2021-10-17 22:15:38 +02:00
|
|
|
|
2021-10-17 21:34:07 +02:00
|
|
|
err := util.InitLog(logLevel, logFile)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("failed initializing log %v", err)
|
|
|
|
return
|
|
|
|
}
|
2021-10-17 22:15:38 +02:00
|
|
|
|
|
|
|
SetupCloseHandler()
|
|
|
|
|
2021-06-20 23:01:44 +02:00
|
|
|
prg := &program{
|
|
|
|
cmd: cmd,
|
|
|
|
args: args,
|
|
|
|
}
|
|
|
|
|
|
|
|
s, err := newSVC(prg, newSVCConfig())
|
|
|
|
if err != nil {
|
2021-06-22 01:07:12 +02:00
|
|
|
cmd.PrintErrln(err)
|
|
|
|
return
|
2021-06-20 23:01:44 +02:00
|
|
|
}
|
|
|
|
err = s.Run()
|
|
|
|
if err != nil {
|
2021-06-22 01:07:12 +02:00
|
|
|
cmd.PrintErrln(err)
|
|
|
|
return
|
2021-06-20 23:01:44 +02:00
|
|
|
}
|
2021-06-22 01:07:12 +02:00
|
|
|
cmd.Printf("Wiretrustee service is running")
|
2021-06-20 23:01:44 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
startCmd = &cobra.Command{
|
|
|
|
Use: "start",
|
|
|
|
Short: "starts wiretrustee service",
|
2021-10-17 21:34:07 +02:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
err := util.InitLog(logLevel, logFile)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("failed initializing log %v", err)
|
|
|
|
return err
|
|
|
|
}
|
2021-06-20 23:01:44 +02:00
|
|
|
s, err := newSVC(&program{}, newSVCConfig())
|
|
|
|
if err != nil {
|
2021-06-22 01:07:12 +02:00
|
|
|
cmd.PrintErrln(err)
|
2021-10-17 21:34:07 +02:00
|
|
|
return err
|
2021-06-20 23:01:44 +02:00
|
|
|
}
|
|
|
|
err = s.Start()
|
|
|
|
if err != nil {
|
2021-06-22 01:07:12 +02:00
|
|
|
cmd.PrintErrln(err)
|
2021-10-17 21:34:07 +02:00
|
|
|
return err
|
2021-06-20 23:01:44 +02:00
|
|
|
}
|
2021-10-17 21:34:07 +02:00
|
|
|
cmd.Println("Wiretrustee service has been started")
|
|
|
|
return nil
|
2021-06-20 23:01:44 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
stopCmd = &cobra.Command{
|
|
|
|
Use: "stop",
|
|
|
|
Short: "stops wiretrustee service",
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2021-10-17 21:34:07 +02:00
|
|
|
err := util.InitLog(logLevel, logFile)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("failed initializing log %v", err)
|
|
|
|
}
|
2021-06-20 23:01:44 +02:00
|
|
|
s, err := newSVC(&program{}, newSVCConfig())
|
|
|
|
if err != nil {
|
2021-06-22 01:07:12 +02:00
|
|
|
cmd.PrintErrln(err)
|
|
|
|
return
|
2021-06-20 23:01:44 +02:00
|
|
|
}
|
|
|
|
err = s.Stop()
|
|
|
|
if err != nil {
|
2021-06-22 01:07:12 +02:00
|
|
|
cmd.PrintErrln(err)
|
|
|
|
return
|
2021-06-20 23:01:44 +02:00
|
|
|
}
|
2021-10-17 21:34:07 +02:00
|
|
|
cmd.Println("Wiretrustee service has been stopped")
|
2021-06-20 23:01:44 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
restartCmd = &cobra.Command{
|
|
|
|
Use: "restart",
|
|
|
|
Short: "restarts wiretrustee service",
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2021-10-17 21:34:07 +02:00
|
|
|
err := util.InitLog(logLevel, logFile)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("failed initializing log %v", err)
|
|
|
|
}
|
2021-06-20 23:01:44 +02:00
|
|
|
s, err := newSVC(&program{}, newSVCConfig())
|
|
|
|
if err != nil {
|
2021-06-22 01:07:12 +02:00
|
|
|
cmd.PrintErrln(err)
|
|
|
|
return
|
2021-06-20 23:01:44 +02:00
|
|
|
}
|
|
|
|
err = s.Restart()
|
|
|
|
if err != nil {
|
2021-06-22 01:07:12 +02:00
|
|
|
cmd.PrintErrln(err)
|
|
|
|
return
|
2021-06-20 23:01:44 +02:00
|
|
|
}
|
2021-10-17 21:34:07 +02:00
|
|
|
cmd.Println("Wiretrustee service has been restarted")
|
2021-06-20 23:01:44 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
}
|