This commit is contained in:
Denis Zheleztsov 2016-12-30 13:51:06 +03:00
parent 2acb818296
commit 8dcb652bee
3 changed files with 72 additions and 5 deletions

11
main.go
View File

@ -9,13 +9,15 @@ import (
var (
zk string
zk_path string
zkPath string
tick int
listen string
)
func init() {
flag.StringVar(&zk, "zk", "127.0.0.1:2181", "Zookeeper servers comma separated")
flag.StringVar(&zk_path, "zk_path", "/rbmd", "Zookeeper path")
flag.StringVar(&zkPath, "zkPath", "/rbmd", "Zookeeper path")
flag.StringVar(&listen, "listen", "0.0.0.0:9076", "HTTP API listen address")
flag.IntVar(&tick, "tick", 5, "Tick time loop")
flag.Parse()
}
@ -23,8 +25,9 @@ func init() {
func main() {
config := rbmd.Zk{
strings.Split(zk, ","),
zk_path,
zkPath,
tick,
}
rbmd.Run(config)
s := rbmd.ServerConf{listen}
rbmd.Run(config, s)
}

51
src/rbmd/http.go Normal file
View File

@ -0,0 +1,51 @@
package rbmd
import (
"net/http"
"log"
"encoding/json"
)
//ServerConf configuration of http api server
type ServerConf struct {
Addr string
}
//ServeHTTP start http server
func (s ServerConf) ServeHTTP(z ZooNode, fqdn string) {
// Return JSON of full quorum status
http.HandleFunc("/status", func(w http.ResponseWriter, r *http.Request) {
w.Write(z.GetState())
})
// Return string with quorum health check result
http.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(z.GetQuorumHealth()))
})
// Return JSON of node description
http.HandleFunc("/node", func(w http.ResponseWriter, r *http.Request) {
n := GetNodeState(fqdn)
state, err := json.Marshal(n)
if err != nil {
log.Fatal(err)
}
w.Write(state)
})
// Mount volume. Accept JSON. Return JSON.
http.HandleFunc("/mount", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Not implemented yet."))
})
// Umount volume. Accept JSON. Return JSON.
http.HandleFunc("/umount", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Not implemented yet."))
})
server := &http.Server{
Addr: s.Addr,
}
log.Fatal(server.ListenAndServe())
}

View File

@ -11,7 +11,7 @@ import (
)
//Run -- start main loop
func Run(zoo Zk) {
func Run(zoo Zk, s ServerConf) {
connection, err := zoo.InitConnection()
if err != nil {
log.Fatal(err)
@ -20,6 +20,9 @@ func Run(zoo Zk) {
z := ZooNode{zoo.Path, connection}
// Serve HTTP API
go s.ServeHTTP(z, fqdn)
for {
node, err := z.EnsureZooPath(strings.Join([]string{"cluster/", fqdn, "/state"}, ""))
if err != nil {
@ -52,4 +55,14 @@ func (z ZooNode) UpdateState(zkPath string, fqdn string) {
}
}
//GetState return cluster status
func (z ZooNode) GetState() []byte {
quorumStatePath := strings.Join([]string{z.Path, "/log/quorum"}, "")
stateJSON, _, err := z.Conn.Get(quorumStatePath)
if err != nil {
log.Fatal(err)
}
return stateJSON
}