2015-02-15 21:19:08 +00:00
|
|
|
package conf
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"github.com/dballard/warren/lib/warren"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
2015-03-29 17:45:43 +00:00
|
|
|
type MsgType int
|
|
|
|
const (
|
|
|
|
AddDeployment MsgType = iota
|
|
|
|
RmDeploymen
|
|
|
|
)
|
|
|
|
|
|
|
|
type Msg struct {
|
|
|
|
mType MsgType
|
|
|
|
reply chan error
|
2015-04-03 06:52:03 +00:00
|
|
|
Args map[string]string
|
2015-03-29 17:45:43 +00:00
|
|
|
}
|
|
|
|
|
2015-04-03 06:52:03 +00:00
|
|
|
func NewMsg(mType MsgType) *Msg {
|
|
|
|
var m = &Msg{mType, make(chan error), make(map[string]string)}
|
2015-03-29 17:45:43 +00:00
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Msg) Call() error {
|
|
|
|
confChan <- *m
|
|
|
|
err := <-m.reply
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-02-15 21:19:08 +00:00
|
|
|
// Conf package - thread safe conf
|
2015-04-07 06:19:31 +00:00
|
|
|
// All write ops are public API functions ( Msg.Call() )
|
2015-02-15 21:19:08 +00:00
|
|
|
// that internall use channels to thread safely queue writes
|
|
|
|
// all write ops cause FS conf writes so that state is preserved
|
|
|
|
// Reads return copies of data
|
|
|
|
|
2015-04-07 06:19:31 +00:00
|
|
|
type Pool struct {
|
|
|
|
High int
|
|
|
|
Low int
|
|
|
|
}
|
|
|
|
|
|
|
|
type Deployment struct {
|
|
|
|
Path string
|
|
|
|
Pool Pool
|
|
|
|
}
|
|
|
|
|
2015-02-15 21:19:08 +00:00
|
|
|
type Conf struct {
|
|
|
|
// name : path
|
2015-04-07 06:19:31 +00:00
|
|
|
Deployments map[string]Deployment
|
2015-02-15 21:19:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var conf *Conf = nil
|
|
|
|
var Init = make(chan bool)
|
|
|
|
|
2015-03-29 17:45:43 +00:00
|
|
|
var confChan = make(chan Msg)
|
|
|
|
|
2015-02-15 21:19:08 +00:00
|
|
|
|
|
|
|
func loadConf() {
|
|
|
|
file, err := os.Open(warren.WarrendConf)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error loading ", warren.WarrendConf, ": ", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
decoder := json.NewDecoder(file)
|
|
|
|
conf = new(Conf)
|
|
|
|
err = decoder.Decode(conf)
|
|
|
|
if err != nil {
|
|
|
|
conf = nil
|
|
|
|
fmt.Println("Error parsing ", warren.WarrendConf, ": ", err)
|
|
|
|
}
|
|
|
|
if conf.Deployments == nil {
|
2015-04-07 06:19:31 +00:00
|
|
|
conf.Deployments = make(map[string]Deployment)
|
2015-02-15 21:19:08 +00:00
|
|
|
}
|
2015-02-22 02:43:52 +00:00
|
|
|
// populate reverse map
|
2015-04-07 06:19:31 +00:00
|
|
|
for nick, dep := range conf.Deployments {
|
|
|
|
dirToNick[dep.Path] = nick
|
2015-02-22 02:43:52 +00:00
|
|
|
}
|
2015-02-15 21:19:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func saveConf() error {
|
|
|
|
file, err := os.Create(warren.WarrendConf)
|
|
|
|
if err != nil {
|
|
|
|
// TODO: Um, more than noting we might want to do something more drastic
|
|
|
|
return errors.New("Error: could not open conf file for saving: " + err.Error())
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
jdata, err := json.MarshalIndent(conf, "", "\t")
|
|
|
|
if err != nil {
|
|
|
|
return errors.New("Error: Could not enocde to json: " + err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := file.Write(jdata); err != nil {
|
|
|
|
return errors.New("Error: Could not write to file: " + err.Error())
|
|
|
|
}
|
|
|
|
|
2015-04-03 06:52:03 +00:00
|
|
|
return nil
|
2015-02-15 21:19:08 +00:00
|
|
|
}
|
|
|
|
|
2015-04-07 06:19:31 +00:00
|
|
|
// TODO: return copies
|
2015-02-15 21:19:08 +00:00
|
|
|
func GetConf() Conf {
|
|
|
|
return *conf
|
|
|
|
}
|
|
|
|
|
|
|
|
func Run() {
|
|
|
|
loadConf()
|
|
|
|
if conf == nil {
|
|
|
|
fmt.Println("failed to load conf")
|
|
|
|
Init <- false
|
|
|
|
}
|
|
|
|
|
|
|
|
// set up comm chans
|
|
|
|
|
|
|
|
Init <- true
|
|
|
|
// loop for Writes
|
|
|
|
for {
|
2015-03-29 17:45:43 +00:00
|
|
|
m := <- confChan
|
2015-04-04 00:24:57 +00:00
|
|
|
var err error = nil
|
2015-04-03 06:52:03 +00:00
|
|
|
switch (m.mType) {
|
|
|
|
case AddDeployment:
|
2015-04-04 00:24:57 +00:00
|
|
|
err = addDeployment(&m)
|
|
|
|
break
|
|
|
|
case RmDeploymen:
|
|
|
|
err = rmDeployment(&m)
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
err = errors.New("Unknown command")
|
|
|
|
break
|
2015-02-15 21:19:08 +00:00
|
|
|
}
|
2015-04-04 00:24:57 +00:00
|
|
|
m.reply <- err
|
2015-02-15 21:19:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|