package main import ( "encoding/json" "errors" "fmt" "io/ioutil" "sync" "time" log "github.com/sirupsen/logrus" ) type Config struct { ScheduleDuration map[string]string `json:"schedule"` Box map[string]BoxConfig `json:"box"` Email EmailConfig `json:"email"` Apps []AppConfig `json:"apps"` Timezone string `json:"timezone"` Debug bool `json:"debug"` Admin AdminConfig `json:"admin"` box map[string]*Box `json:"-"` apps map[string]*App `json:"-"` timezone *time.Location `json:"-"` } type AdminConfig struct { Addr string `json:"addr"` Username string `json:"username"` Password string `json:"password"` } type BoxConfig struct { Addr string `json:"addr"` User string `json:"user"` Key string `json:"key"` } type AppConfig struct { Name string `json:"name"` Schedule []string `json:"schedule"` Sources []string `json:"src"` Destinations []string `json:"dest"` Before map[string]string `json:"before"` After map[string]string `json:"after"` Active bool `json:"active"` } // Load config from file func (c *Config) LoadFile(path string) error { log.WithFields(log.Fields{"path": path}).Debugf("starting") defer log.WithFields(log.Fields{"path": path}).Debugf("done") b, err := ioutil.ReadFile(path) if err != nil { log.WithFields(log.Fields{"path": path, "error": err, "call": "ioutil.ReadFile"}).Errorf("") return err } err = json.Unmarshal(b, &c) if err != nil { log.WithFields(log.Fields{"path": path, "error": err, "call": "json.Unmarshal"}).Errorf("") return err } c.timezone, err = time.LoadLocation(cfg.Timezone) if err != nil { log.WithFields(log.Fields{"path": path, "error": err, "call": "time.LoadLocation", "attr": cfg.Timezone}).Errorf("") return err } if len(cfg.Email.SmtpHost) == 0 { err := fmt.Errorf("no smtp") log.WithFields(log.Fields{"path": path, "error": err}).Errorf("") return err } if len(cfg.Email.FromEmail) == 0 { err := fmt.Errorf("no email from") log.WithFields(log.Fields{"path": path, "error": err}).Errorf("") return err } if len(cfg.Email.ToEmail) == 0 { err := fmt.Errorf("no email to") log.WithFields(log.Fields{"path": path, "error": err}).Errorf("") return err } for k, v := range c.ScheduleDuration { switch k { case "hourly": case "daily": case "weekly": case "monthly": case "yearly": if _, err := Expiration(time.Now(), v); err != nil { log.WithFields(log.Fields{"path": path, "schedule": k, "deadline": v, "error": err}).Errorf("") return err } default: err := errors.New("invalid schedule") log.WithFields(log.Fields{"path": path, "schedule": k, "deadline": v, "error": err}).Errorf("") return err } } c.box = make(map[string]*Box) for k, v := range c.Box { if b, err := c.NewBox(k, v.Addr, v.User, v.Key); err != nil { log.WithFields(log.Fields{"path": path, "call": "NewBox", "attr": k, "error": err}).Errorf("") return err } else { if _, ok := c.box[k]; ok { err := errors.New("already exists") log.WithFields(log.Fields{"path": path, "attr": k, "error": err}).Errorf("") return err } c.box[k] = b } } c.apps = make(map[string]*App) for _, v := range c.Apps { if a, err := c.NewApp(v.Name, v.Sources, v.Destinations, v.Schedule, v.Before, v.After); err != nil { log.WithFields(log.Fields{"path": path, "call": "NewApp", "attr": v.Name, "error": err}).Errorf("") return err } else { if _, ok := c.apps[v.Name]; ok { err := errors.New("app already exists") log.WithFields(log.Fields{"path": path, "app": v.Name, "error": err}).Errorf("") return err } c.apps[v.Name] = a } } return nil } func (c *Config) Start(e *Email) { log.WithFields(log.Fields{}).Debugf("starting") defer log.WithFields(log.Fields{}).Debugf("done") var wg sync.WaitGroup for _, b := range c.box { wg.Add(1) go func(box *Box) { defer wg.Done() if err := box.Open(); err != nil { log.WithFields(log.Fields{"name": box.name, "call": "Open", "error": err}).Errorf("") if e != nil { e.AddItem(fmt.Sprintf(" - Box : %s is down", box.name)) } return } }(b) } wg.Wait() } // Run config func (c *Config) Run(e *Email) { log.WithFields(log.Fields{}).Debugf("starting") defer log.WithFields(log.Fields{}).Debugf("done") var wg sync.WaitGroup for _, a := range cfg.apps { wg.Add(1) go func(app *App) { if err := app.Run(e.startTime); err != nil { e.AddItem(fmt.Sprintf(" - App : Error running %s (%s)", app.name, err)) } wg.Done() }(a) } wg.Wait() return } func (c *Config) Stop(e *Email) { log.WithFields(log.Fields{}).Debugf("starting") defer log.WithFields(log.Fields{}).Debugf("done") for _, b := range c.box { if err := b.Close(); err != nil { log.WithFields(log.Fields{"name": b.name, "call": "Close", "error": err}).Errorf("") } } if len(e.items) > 0 { if err := e.Send(); err != nil { log.WithFields(log.Fields{"call": "email.Send", "error": err}).Errorf("") } } }