2018-02-07 19:32:11 +01:00
|
|
|
package daemon
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
|
|
|
|
2018-02-10 13:34:42 +01:00
|
|
|
log "github.com/sirupsen/logrus"
|
2018-02-07 19:32:11 +01:00
|
|
|
|
|
|
|
"dev.sum7.eu/genofire/golang-lib/file"
|
|
|
|
"dev.sum7.eu/genofire/golang-lib/worker"
|
|
|
|
"dev.sum7.eu/genofire/yaja/client"
|
|
|
|
"dev.sum7.eu/genofire/yaja/daemon/tester"
|
|
|
|
"dev.sum7.eu/genofire/yaja/messages"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2018-02-10 13:34:42 +01:00
|
|
|
var (
|
|
|
|
configTester = &tester.Config{}
|
|
|
|
testerInstance = tester.NewTester()
|
|
|
|
testerWorker *worker.Worker
|
|
|
|
)
|
2018-02-07 19:32:11 +01:00
|
|
|
|
|
|
|
// TesterCMD represents the serve command
|
|
|
|
var TesterCMD = &cobra.Command{
|
|
|
|
Use: "tester",
|
|
|
|
Short: "runs xmpp tester server",
|
|
|
|
Example: "yaja daemon tester -c /etc/yaja.conf",
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
|
|
|
|
if err := file.ReadTOML(configPath, configTester); err != nil {
|
|
|
|
log.Fatal("unable to load config file:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.SetLevel(configTester.Logging)
|
|
|
|
|
2018-02-10 13:34:42 +01:00
|
|
|
if err := file.ReadJSON(configTester.AccountsPath, testerInstance); err != nil {
|
2018-02-07 19:32:11 +01:00
|
|
|
log.Warn("unable to load state file:", err)
|
|
|
|
}
|
|
|
|
|
2018-02-11 09:07:07 +01:00
|
|
|
mainClient, err := client.NewClientProtocolDuration(configTester.Client.JID, configTester.Client.Password, "tcp", configTester.Timeout.Duration)
|
2018-02-07 19:32:11 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal("unable to connect with main jabber client: ", err)
|
|
|
|
}
|
2018-02-10 13:34:42 +01:00
|
|
|
defer mainClient.Close()
|
2018-02-07 19:32:11 +01:00
|
|
|
|
|
|
|
for _, admin := range configTester.Admins {
|
2018-02-10 13:34:42 +01:00
|
|
|
mainClient.Send(&messages.MessageClient{
|
|
|
|
To: admin,
|
2018-02-07 19:32:11 +01:00
|
|
|
Type: "chat",
|
|
|
|
Body: "yaja tester starts",
|
|
|
|
})
|
|
|
|
}
|
2018-02-11 09:07:07 +01:00
|
|
|
testerInstance.Timeout = configTester.Timeout.Duration
|
2018-02-10 13:34:42 +01:00
|
|
|
testerInstance.Start(mainClient, configTester.Client.Password)
|
|
|
|
testerInstance.CheckStatus()
|
2018-02-11 09:07:07 +01:00
|
|
|
testerWorker = worker.NewWorker(configTester.Interval.Duration, func() {
|
2018-02-10 13:34:42 +01:00
|
|
|
testerInstance.CheckStatus()
|
|
|
|
file.SaveJSON(configTester.AccountsPath, testerInstance)
|
|
|
|
file.SaveJSON(configTester.OutputPath, testerInstance.Output())
|
|
|
|
})
|
|
|
|
|
|
|
|
go testerWorker.Start()
|
2018-02-07 19:32:11 +01:00
|
|
|
|
2018-02-10 13:34:42 +01:00
|
|
|
log.Info("yaja tester started ")
|
2018-02-07 19:32:11 +01:00
|
|
|
|
|
|
|
// Wait for INT/TERM
|
|
|
|
sigs := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(sigs, syscall.SIGTERM, syscall.SIGHUP, syscall.SIGQUIT)
|
|
|
|
for sig := range sigs {
|
|
|
|
log.Infoln("received", sig)
|
|
|
|
switch sig {
|
|
|
|
case syscall.SIGTERM:
|
|
|
|
log.Panic("terminated")
|
2018-02-11 11:15:11 +01:00
|
|
|
quitTester()
|
2018-02-07 19:32:11 +01:00
|
|
|
os.Exit(0)
|
|
|
|
case syscall.SIGQUIT:
|
|
|
|
quitTester()
|
|
|
|
case syscall.SIGHUP:
|
|
|
|
quitTester()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func quitTester() {
|
2018-02-10 13:34:42 +01:00
|
|
|
testerWorker.Close()
|
|
|
|
testerInstance.Close()
|
2018-02-07 19:32:11 +01:00
|
|
|
srv.Close()
|
|
|
|
|
2018-02-10 13:34:42 +01:00
|
|
|
file.SaveJSON(configTester.AccountsPath, db)
|
2018-02-07 19:32:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
TesterCMD.Flags().StringVarP(&configPath, "config", "c", "yaja-tester.conf", "Path to configuration file")
|
|
|
|
|
|
|
|
}
|