Skip to content

Commit

Permalink
Load config file via AWS S3 closes #58 (#60)
Browse files Browse the repository at this point in the history
* Load config file via AWS S3 closes #58

* Add AWS client deps
  • Loading branch information
jonog authored Oct 14, 2017
1 parent 6735785 commit 6aef4e0
Show file tree
Hide file tree
Showing 7 changed files with 210 additions and 11 deletions.
14 changes: 11 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,17 +131,25 @@ Available Commands:
Flags:
-d, --config-db string config database url
-f, --config-file string config file (default "config.json")
-s, --config-s3 string config S3
-u, --config-url string config url
-h, --help help for redalert
-p, --port int port to run web server (default 8888)
-r, --rpc-port int port to run RPC server (default 8889)
Use "redalert [command] --help" for more information about a command.
```

#### Monitoring Configuration
Configure servers to monitor & alert settings via `config.json`.
#### Configuration

##### Simple config.json
Configure servers to monitor & alert settings via a configuration file:
* a local file (specified by `-f` or `--config-file`) - defaults to `config.json`
* a file remotely accessible via HTTP (specified by `-u` or `--config-url`)
* a file hosted in an AWS S3 bucket (specified by `-s` or `--config-s3`)

TODO: document Postgres configuration option

##### Example config.json
```
{
"checks":[
Expand Down
2 changes: 2 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func Execute() {
var cfgFile string
var cfgDb string
var cfgURL string
var cfgS3 string
var webPort int
var rpcPort int
var disableBrand bool
Expand All @@ -50,6 +51,7 @@ func init() {
RootCmd.PersistentFlags().StringVarP(&cfgFile, "config-file", "f", "config.json", "config file")
RootCmd.PersistentFlags().StringVarP(&cfgDb, "config-db", "d", "", "config database url")
RootCmd.PersistentFlags().StringVarP(&cfgURL, "config-url", "u", "", "config url")
RootCmd.PersistentFlags().StringVarP(&cfgS3, "config-s3", "s", "", "config S3")
RootCmd.PersistentFlags().IntVarP(&webPort, "port", "p", 8888, "port to run web server")
RootCmd.PersistentFlags().IntVarP(&rpcPort, "rpc-port", "r", 8889, "port to run RPC server")
}
7 changes: 7 additions & 0 deletions cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ var serverCmd = &cobra.Command{
if err != nil {
log.Fatal("URL config error via :", configURL, " Error: ", err)
}
} else if cmd.Flag("config-s3").Changed {
log.Println("Config via S3")
configS3 := cmd.Flag("config-s3").Value.String()
configStore, err = config.NewS3Store(configS3)
if err != nil {
log.Fatal("S3 config error via :", configS3, " Error: ", err)
}
} else {
log.Println("Config via file")
configFile := cmd.Flag("config-file").Value.String()
Expand Down
102 changes: 102 additions & 0 deletions config/s3_store.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package config

import (
"bytes"
"encoding/json"
"io"
"log"
"net/url"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/jonog/redalert/checks"
"github.com/jonog/redalert/notifiers"
)

type S3Store struct {
URL string
data S3StoreData
}

type S3StoreData struct {
Checks []checks.Config `json:"checks"`
Notifications []notifiers.Config `json:"notifications"`
Preferences Preferences `json:"preferences"`
}

func NewS3Store(URL string) (*S3Store, error) {
config := &S3Store{URL: URL}
err := config.read()
if err != nil {
return nil, err
}

// create check ID if not present
for i := range config.data.Checks {
if config.data.Checks[i].ID == "" {
config.data.Checks[i].ID = generateID(8)
}
}

// create notification ID if not present
for i := range config.data.Notifications {
if config.data.Notifications[i].ID == "" {
config.data.Notifications[i].ID = generateID(8)
}
}

return config, nil
}

func (s *S3Store) read() error {

u, err := url.Parse(s.URL)
if err != nil {
return err
}
log.Printf("bucket: %q, key: %q", u.Host, u.Path)

sess := session.Must(session.NewSession())
body, err := getS3File(sess, u.Host, u.Path)
if err != nil {
return err
}

var data S3StoreData
err = json.Unmarshal(body, &data)
if err != nil {
return err
}
s.data = data
return nil
}

func getS3File(s *session.Session, bucket, key string) (value []byte, err error) {
results, err := s3.New(s).GetObject(&s3.GetObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(key),
})
if err != nil {
return nil, err
}
defer results.Body.Close()

buf := bytes.NewBuffer(nil)
if _, err := io.Copy(buf, results.Body); err != nil {
return nil, err
}
return buf.Bytes(), nil
}

func (s *S3Store) Notifications() ([]notifiers.Config, error) {
return s.data.Notifications, nil
}

func (s *S3Store) Checks() ([]checks.Config, error) {
return s.data.Checks, nil
}

func (s *S3Store) Preferences() (Preferences, error) {
return s.data.Preferences, nil
}
60 changes: 52 additions & 8 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ import:
- package: google.golang.org/grpc
version: ^1.0.5
- package: gopkg.in/gorp.v1
- package: github.com/aws/aws-sdk-go
version: ^1.12.10
subpackages:
- aws
- aws/session
- service/s3
testImport:
- package: github.com/docker/go-connections
subpackages:
Expand Down
30 changes: 30 additions & 0 deletions uploaded_config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"checks": [
{
"name": "AWS Bravo Adapter",
"type": "web-ping",
"send_alerts": [
"stderr"
],
"backoff": {
"type": "constant",
"interval": 10
},
"config": {
"address": "https://uat01.webapi.tab.com.au/v1/bravo-adapter/status"
},
"assertions": [
{
"source": "metadata",
"identifier": "status_code",
"comparison": "==",
"target": "200"
}
]
}
],
"notifications": [],
"preferences": {
"notifications": {}
}
}

0 comments on commit 6aef4e0

Please sign in to comment.