Skip to content
Permalink
Browse files

Refactoring for new configuration system

  • Loading branch information...
Depado committed May 21, 2016
1 parent d4ab14a commit 3b22a057ac238a982df6d9bd707b70799f63b98c
Showing with 51 additions and 57 deletions.
  1. +4 −7 main.go
  2. +13 −10 models/conf.go
  3. +34 −40 models/service.go
11 main.go
@@ -1,13 +1,13 @@
package main

import (
"fmt"
"log"

"github.com/gin-gonic/gin"

"github.com/Depado/gomonit/admin"
"github.com/Depado/gomonit/auth"
"github.com/Depado/gomonit/conf"
"github.com/Depado/gomonit/models"
"github.com/Depado/gomonit/views"
)
@@ -52,20 +52,17 @@ func main() {
var err error

// Configuration parsing and services initialization
if err = conf.Load("conf.yml"); err != nil {
log.Fatal(err)
}
if err = models.ParseConf(); err != nil {
if err = models.ParseConfiguration("conf.yml"); err != nil {
log.Fatal(err)
}
// Starting monitoring of services
go models.All.Monitor()

// Gin initialization
if !conf.C.Debug {
if !models.C.Debug {
gin.SetMode(gin.ReleaseMode)
}

router := SetupRouter()
router.Run(conf.C.Listen)
router.Run(fmt.Sprintf("%s:%s", models.C.Host, models.C.Port))
}
@@ -35,7 +35,6 @@ type rawConfiguration struct {
}

func (r *rawConfiguration) ParseServices() (Services, error) {
var err error
services := make(Services, len(r.Services))
for i, s := range r.Services {
p, err := s.Parse()
@@ -74,12 +73,12 @@ func (r *rawService) Parse() (Service, error) {
var si time.Duration

if r.ID == "" || r.Name == "" {
return s, fmt.Errorf("Each service needs a 'name' and an 'id' fields.")
return s, fmt.Errorf("Configuration Error : Each service needs a 'name' and an 'id' fields.")
}
s.Name = r.Name
s.ID = r.ID
if r.RepoType != "" && r.RepoType != "github" { // TODO Modify when other types of repo are added
return s, fmt.Errorf("Service %s : %s repo type isn't supported yet", r.Name, r.RepoType)
return s, fmt.Errorf("Configuration Error : Service %s : %s repo type isn't supported yet", r.Name, r.RepoType)
}
if r.Repo != "" {
s.Repo = r.Repo
@@ -88,16 +87,16 @@ func (r *rawService) Parse() (Service, error) {
s.RepoURL = fmt.Sprintf("%s/%s", strings.TrimSuffix(r.RepoHost, "/"), r.Repo)
if r.RepoInterval != "" {
if ri, err = time.ParseDuration(r.RepoInterval); err != nil {
return s, fmt.Errorf("Service %s : Can't parse 'repo_interval' : %s", r.Name, err)
return s, fmt.Errorf("Configuration Error : Service %s : Can't parse 'repo_interval' : %s", r.Name, err)
}
s.RepoInterval = ri
}
if r.CIType != "" {
if r.CIType != "drone" {
return s, fmt.Errorf("Service %s : %s ci type isn't supported yet", r.Name, r.CIType)
return s, fmt.Errorf("Configuration Error : Service %s : %s ci type isn't supported yet", r.Name, r.CIType)
}
if r.CIHost == "" {
return s, fmt.Errorf("Service %s : Missing 'ci_host' field", r.Name)
return s, fmt.Errorf("Configuration Error : Service %s : Missing 'ci_host' field", r.Name)
}
s.BuildAPI = fmt.Sprintf("%s/api/repos/%s/builds", strings.TrimSuffix(r.CIHost, "/"), r.Repo)
s.BuildURL = fmt.Sprintf("%s/%s", strings.TrimSuffix(r.CIHost, "/"), r.Repo)
@@ -120,8 +119,8 @@ func (r *rawService) Parse() (Service, error) {
return s, nil
}

// Load loads the given fp (file path) to the C global configuration variable.
func Load(fp string) error {
// ParseConfiguration loads the given fp (file path) to the C global configuration variable.
func ParseConfiguration(fp string) error {
var err error
var raw rawConfiguration
var conf []byte
@@ -134,11 +133,14 @@ func Load(fp string) error {
if err = yaml.Unmarshal(conf, &raw); err != nil {
return err
}
if raw.DefaultServiceInterval == "" || raw.DefaultRepoInterval == "" {
return fmt.Errorf("Configuration Error : Both 'default_service_interval' and 'default_repo_interval' are mandatory fields")
}
if sd, err = time.ParseDuration(raw.DefaultServiceInterval); err != nil {
return err
return fmt.Errorf("Configuration Error : Could not parse 'default_service_interval' (%s) : %s", raw.DefaultServiceInterval, err)
}
if rd, err = time.ParseDuration(raw.DefaultRepoInterval); err != nil {
return err
return fmt.Errorf("Configuration Error : Could not parse 'default_repo_interval' (%s) : %s", raw.DefaultRepoInterval, err)
}
ss, err := raw.ParseServices()
if err != nil {
@@ -153,5 +155,6 @@ func Load(fp string) error {
Debug: raw.Debug,
Services: ss,
}
All = ss
return nil
}
@@ -7,10 +7,7 @@ import (
"log"
"net/http"
"strings"
"sync"
"time"

"github.com/Depado/govue/conf"
)

// All represents all the services
@@ -71,8 +68,7 @@ func (s *Service) FetchStatus() {
}

// FetchBuilds checks the last build
func (s *Service) FetchBuilds(wg *sync.WaitGroup) {
defer wg.Done()
func (s *Service) FetchBuilds() {
resp, err := http.Get(s.BuildAPI)
if err != nil {
log.Printf("[%s][ERROR] While requesting build status : %v\n", s.Name, err)
@@ -92,17 +88,16 @@ func (s *Service) FetchBuilds(wg *sync.WaitGroup) {
}

// FetchCommits fetches the last commits associated to the repository
func (s *Service) FetchCommits(wg *sync.WaitGroup) {
defer wg.Done()
func (s *Service) FetchCommits() {
u := strings.Split(s.RepoURL, "/")
url := fmt.Sprintf("https://api.github.com/repos/%s/%s/commits", u[len(u)-2], u[len(u)-1])
client := &http.Client{}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
log.Printf("[%s][ERROR][COMMITS] Couldn't create request : %v\n", s.Name, err)
}
if conf.C.GithubOAuthToken != "" {
req.Header.Add("Authorization", "token "+conf.C.GithubOAuthToken)
if C.GithubOAuthToken != "" {
req.Header.Add("Authorization", "token "+C.GithubOAuthToken)
}
res, err := client.Do(req)
if err != nil {
@@ -119,17 +114,16 @@ func (s *Service) FetchCommits(wg *sync.WaitGroup) {
}

// FetchRepoInfos fetches the repository information
func (s *Service) FetchRepoInfos(wg *sync.WaitGroup) {
defer wg.Done()
func (s *Service) FetchRepoInfos() {
u := strings.Split(s.RepoURL, "/")
url := fmt.Sprintf("https://api.github.com/repos/%s/%s", u[len(u)-2], u[len(u)-1])
client := &http.Client{}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
log.Printf("[%s][ERROR][REPO] Couldn't create request : %v\n", s.Name, err)
}
if conf.C.GithubOAuthToken != "" {
req.Header.Add("Authorization", "token "+conf.C.GithubOAuthToken)
if C.GithubOAuthToken != "" {
req.Header.Add("Authorization", "token "+C.GithubOAuthToken)
}
res, err := client.Do(req)
if err != nil {
@@ -153,39 +147,39 @@ type Services []*Service

// Monitor allows to monitor Services every interval delay
func (ss Services) Monitor() {
var work sync.Mutex
var wg sync.WaitGroup

go func() {
rtc := time.NewTicker(conf.C.RepoInterval)
for {
work.Lock()
for _, s := range ss {
for _, s := range ss {
go func(s *Service) {
var rtc *time.Ticker
if s.RepoInterval != 0 {
rtc = time.NewTicker(s.RepoInterval)
} else {
rtc = time.NewTicker(C.DefaultRepoInterval)
}
for {
if s.BuildAPI != "" {
wg.Add(1)
go s.FetchBuilds(&wg)
go s.FetchBuilds()
}
if s.RepoURL != "" {
wg.Add(2)
go s.FetchCommits(&wg)
go s.FetchRepoInfos(&wg)
go s.FetchCommits()
go s.FetchRepoInfos()
}
<-rtc.C
}
wg.Wait()
work.Unlock()
<-rtc.C
}
}()
stc := time.NewTicker(conf.C.ServiceInterval)
for {
work.Lock()
for _, s := range ss {
if s.URL != "" {
s.FetchStatus()
}(s)
go func(s *Service) {
var rtc *time.Ticker
if s.ServiceInterval != 0 {
rtc = time.NewTicker(s.ServiceInterval)
} else {
rtc = time.NewTicker(C.DefaultServiceInterval)
}
for {
if s.URL != "" {
s.FetchStatus()
}
<-rtc.C
}
}
work.Unlock()
<-stc.C
}(s)
}
}

0 comments on commit 3b22a05

Please sign in to comment.
You can’t perform that action at this time.