2017-09-18 09:21:58 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
2017-09-28 10:09:33 +00:00
|
|
|
"path"
|
2017-09-18 09:21:58 +00:00
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
|
2017-09-21 08:38:05 +00:00
|
|
|
lfcc "git.loafle.net/commons_go/config"
|
2017-09-18 09:21:58 +00:00
|
|
|
"git.loafle.net/commons_go/logging"
|
2017-12-01 13:01:46 +00:00
|
|
|
oocc "git.loafle.net/overflow/overflow_commons_go/config"
|
|
|
|
ooccp "git.loafle.net/overflow/overflow_commons_go/config/probe"
|
2017-09-21 08:38:05 +00:00
|
|
|
"git.loafle.net/overflow/overflow_probes/auth"
|
2017-12-01 13:01:46 +00:00
|
|
|
"git.loafle.net/overflow/overflow_probes/commons"
|
2017-09-21 08:38:05 +00:00
|
|
|
"git.loafle.net/overflow/overflow_probes/config"
|
2017-12-03 05:50:23 +00:00
|
|
|
"git.loafle.net/overflow/overflow_probes/probe"
|
2017-09-18 09:21:58 +00:00
|
|
|
)
|
|
|
|
|
2017-09-28 10:09:33 +00:00
|
|
|
/*
|
|
|
|
/bin/
|
|
|
|
probe.exe
|
|
|
|
/config/
|
|
|
|
/sensor/
|
|
|
|
noauthprobe.json
|
|
|
|
central.json
|
|
|
|
probe.json
|
|
|
|
logging.json
|
2017-09-18 09:21:58 +00:00
|
|
|
|
2017-09-28 10:09:33 +00:00
|
|
|
*/
|
2017-09-18 09:21:58 +00:00
|
|
|
|
2017-09-29 12:30:27 +00:00
|
|
|
// const (
|
|
|
|
// // name of the service
|
|
|
|
// serviceName = "Probe"
|
|
|
|
// serviceDescription = "Probe Service of overFlow"
|
|
|
|
// )
|
2017-09-18 09:21:58 +00:00
|
|
|
|
2017-09-21 08:38:05 +00:00
|
|
|
var (
|
2017-12-01 13:01:46 +00:00
|
|
|
configDir *string
|
2017-09-21 08:38:05 +00:00
|
|
|
)
|
|
|
|
|
2017-09-18 09:21:58 +00:00
|
|
|
func init() {
|
2017-09-29 12:30:27 +00:00
|
|
|
// flag.Usage = func() {
|
|
|
|
// fmt.Printf("Usage of %s\n", os.Args[0])
|
|
|
|
// fmt.Printf(" [install | remove | start | stop | status]\n")
|
|
|
|
// flag.PrintDefaults()
|
|
|
|
// }
|
2017-09-18 09:21:58 +00:00
|
|
|
|
2017-12-01 13:01:46 +00:00
|
|
|
configDir = oocc.FlagConfigDir()
|
2017-09-18 09:21:58 +00:00
|
|
|
flag.Parse()
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2017-12-01 13:01:46 +00:00
|
|
|
defer logging.Logger().Sync()
|
2017-09-18 09:21:58 +00:00
|
|
|
|
2017-09-28 10:09:33 +00:00
|
|
|
printBanner()
|
2017-12-01 13:01:46 +00:00
|
|
|
loadConfig()
|
2017-09-18 09:21:58 +00:00
|
|
|
|
2017-12-01 13:01:46 +00:00
|
|
|
var instance interface{}
|
2017-09-21 08:38:05 +00:00
|
|
|
|
2017-09-18 09:21:58 +00:00
|
|
|
go func() {
|
2017-12-15 08:18:32 +00:00
|
|
|
if ooccp.ProbeStateTypeNotAuthorized == config.Config.Probe.State() {
|
2017-12-01 13:01:46 +00:00
|
|
|
var err error
|
|
|
|
instance = auth.New()
|
|
|
|
|
|
|
|
authDoneChan := make(chan error, 1)
|
|
|
|
defer close(authDoneChan)
|
|
|
|
|
|
|
|
if err := instance.(commons.EndableStarter).EndableStart(authDoneChan); err != nil {
|
|
|
|
logging.Logger().Error(err.Error())
|
2017-12-01 13:20:01 +00:00
|
|
|
os.Exit(1)
|
2017-10-13 05:32:34 +00:00
|
|
|
}
|
2017-12-01 13:01:46 +00:00
|
|
|
err = <-authDoneChan
|
|
|
|
if nil != err {
|
|
|
|
logging.Logger().Error(err.Error())
|
2017-12-01 13:20:01 +00:00
|
|
|
os.Exit(1)
|
2017-10-13 05:32:34 +00:00
|
|
|
}
|
2017-10-12 10:02:38 +00:00
|
|
|
}
|
2017-12-03 05:50:23 +00:00
|
|
|
instance = probe.New()
|
|
|
|
if err := instance.(commons.Starter).Start(); err != nil {
|
|
|
|
logging.Logger().Error(err.Error())
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2017-10-12 10:02:38 +00:00
|
|
|
|
2017-09-18 09:21:58 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
// // Set up channel on which to send signal notifications.
|
|
|
|
// // We must use a buffered channel or risk missing the signal
|
|
|
|
// // if we're not ready to receive when the signal is sent.
|
|
|
|
interrupt := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(interrupt,
|
|
|
|
syscall.SIGKILL,
|
|
|
|
syscall.SIGSTOP,
|
|
|
|
syscall.SIGHUP,
|
|
|
|
syscall.SIGINT,
|
|
|
|
syscall.SIGTERM,
|
|
|
|
syscall.SIGQUIT)
|
|
|
|
|
|
|
|
<-interrupt
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
|
|
defer cancel()
|
2017-10-12 10:02:38 +00:00
|
|
|
if err := instance.(commons.Shutdowner).Shutdown(ctx); err != nil {
|
2017-12-01 13:01:46 +00:00
|
|
|
logging.Logger().Error(fmt.Sprintf("Probe error: %v", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadConfig() {
|
|
|
|
if dir, err := lfcc.ABSPathify(*configDir); nil != err {
|
|
|
|
logging.Logger().Panic(fmt.Sprintf("Config path[%s] is not valid", *configDir))
|
|
|
|
} else {
|
|
|
|
logging.Logger().Debug(fmt.Sprintf("Config path: %s", dir))
|
|
|
|
config.ConfigDir = &dir
|
|
|
|
}
|
|
|
|
cfp := path.Join(*config.ConfigDir, ooccp.ConfigFileName)
|
|
|
|
config.ConfigFilePath = &cfp
|
|
|
|
|
|
|
|
conf := lfcc.New()
|
|
|
|
config.Config = &ooccp.Config{}
|
|
|
|
if err := conf.Load(config.Config, *config.ConfigFilePath); nil != err {
|
|
|
|
logging.Logger().Panic(fmt.Sprintf("Config is not valid: %v", err))
|
2017-09-18 09:21:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2017-09-28 10:09:33 +00:00
|
|
|
|
|
|
|
const (
|
|
|
|
version = "1.0.0"
|
|
|
|
website = "https://www.overflow.cloud"
|
|
|
|
banner = `
|
|
|
|
|
|
|
|
██████╗ ██╗ ██╗███████╗██████╗ ███████╗██╗ ██████╗ ██╗ ██╗
|
|
|
|
██╔═══██╗██║ ██║██╔════╝██╔══██╗██╔════╝██║ ██╔═══██╗██║ ██║
|
|
|
|
██║ ██║██║ ██║█████╗ ██████╔╝█████╗ ██║ ██║ ██║██║ █╗ ██║
|
|
|
|
██║ ██║╚██╗ ██╔╝██╔══╝ ██╔══██╗██╔══╝ ██║ ██║ ██║██║███╗██║
|
|
|
|
╚██████╔╝ ╚████╔╝ ███████╗██║ ██║██║ ███████╗╚██████╔╝╚███╔███╔╝
|
2017-12-01 13:01:46 +00:00
|
|
|
╚═════╝ ╚═══╝ ╚══════╝╚═╝ ╚═╝╚═╝ ╚══════╝ ╚═════╝ ╚══╝╚══╝
|
|
|
|
|
2017-09-28 10:09:33 +00:00
|
|
|
`
|
|
|
|
)
|
|
|
|
|
|
|
|
func printBanner() {
|
|
|
|
fmt.Println(banner)
|
|
|
|
fmt.Printf("Version: %s\n", version)
|
|
|
|
fmt.Printf("URL: %s\n", website)
|
|
|
|
fmt.Println()
|
|
|
|
|
|
|
|
}
|