Providers refactoring
This commit is contained in:
parent
8fcbd73edc
commit
bf37869b8f
4 changed files with 169 additions and 67 deletions
65
docker.go
Normal file
65
docker.go
Normal file
|
@ -0,0 +1,65 @@
|
||||||
|
package main
|
||||||
|
import(
|
||||||
|
"github.com/fsouza/go-dockerclient"
|
||||||
|
"fmt"
|
||||||
|
"github.com/leekchan/gtf"
|
||||||
|
"bytes"
|
||||||
|
"github.com/BurntSushi/toml"
|
||||||
|
)
|
||||||
|
|
||||||
|
type DockerProvider struct {
|
||||||
|
dockerClient *docker.Client
|
||||||
|
}
|
||||||
|
|
||||||
|
func (provider *DockerProvider) Provide(serviceChan chan<- Service){
|
||||||
|
endpoint := "unix:///var/run/docker.sock"
|
||||||
|
provider.dockerClient, _ = docker.NewClient(endpoint)
|
||||||
|
dockerEvents := make(chan *docker.APIEvents)
|
||||||
|
provider.dockerClient.AddEventListener(dockerEvents)
|
||||||
|
go func() {
|
||||||
|
for {
|
||||||
|
event := <-dockerEvents
|
||||||
|
fmt.Println("Event receveived", event)
|
||||||
|
service:= provider.loadDockerConfig()
|
||||||
|
serviceChan <- *service
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
service:= provider.loadDockerConfig()
|
||||||
|
fmt.Println("Sending service")
|
||||||
|
serviceChan <- *service
|
||||||
|
}
|
||||||
|
|
||||||
|
func (provider *DockerProvider) loadDockerConfig() *Service {
|
||||||
|
service := new(Service)
|
||||||
|
containerList, _ := provider.dockerClient.ListContainers(docker.ListContainersOptions{})
|
||||||
|
containersInspected := []docker.Container{}
|
||||||
|
for _, container := range containerList {
|
||||||
|
containerInspected, _ := provider.dockerClient.InspectContainer(container.ID)
|
||||||
|
containersInspected = append(containersInspected, *containerInspected)
|
||||||
|
}
|
||||||
|
containers := struct {
|
||||||
|
Containers []docker.Container
|
||||||
|
}{
|
||||||
|
containersInspected,
|
||||||
|
}
|
||||||
|
tmpl, err := gtf.New("docker.tmpl").ParseFiles("docker.tmpl")
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
var buffer bytes.Buffer
|
||||||
|
|
||||||
|
err = tmpl.Execute(&buffer, containers)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println(buffer.String())
|
||||||
|
|
||||||
|
if _, err := toml.Decode(buffer.String(), service); err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return service
|
||||||
|
}
|
5
provider.go
Normal file
5
provider.go
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
type Provider interface {
|
||||||
|
Provide(chan<- Service)
|
||||||
|
}
|
25
service.go
Normal file
25
service.go
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
|
||||||
|
type Backend struct {
|
||||||
|
Servers map[string]Server
|
||||||
|
}
|
||||||
|
|
||||||
|
type Server struct {
|
||||||
|
Url string
|
||||||
|
}
|
||||||
|
|
||||||
|
type Rule struct {
|
||||||
|
Category string
|
||||||
|
Value string
|
||||||
|
}
|
||||||
|
|
||||||
|
type Route struct {
|
||||||
|
Backends []string
|
||||||
|
Rules map[string]Rule
|
||||||
|
}
|
||||||
|
|
||||||
|
type Service struct {
|
||||||
|
Backends map[string]Backend
|
||||||
|
Routes map[string]Route
|
||||||
|
}
|
125
tortuous.go
125
tortuous.go
|
@ -1,64 +1,57 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/gorilla/mux"
|
"bytes"
|
||||||
"github.com/tylerb/graceful"
|
|
||||||
"net/http"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"github.com/BurntSushi/toml"
|
||||||
|
"github.com/fsouza/go-dockerclient"
|
||||||
|
"github.com/gorilla/mux"
|
||||||
|
"github.com/leekchan/gtf"
|
||||||
"github.com/mailgun/oxy/forward"
|
"github.com/mailgun/oxy/forward"
|
||||||
"github.com/mailgun/oxy/roundrobin"
|
"github.com/mailgun/oxy/roundrobin"
|
||||||
"time"
|
"github.com/tylerb/graceful"
|
||||||
"net"
|
|
||||||
"os/signal"
|
|
||||||
"syscall"
|
|
||||||
"github.com/BurntSushi/toml"
|
|
||||||
"reflect"
|
|
||||||
"net/url"
|
|
||||||
"github.com/fsouza/go-dockerclient"
|
|
||||||
"github.com/leekchan/gtf"
|
|
||||||
"bytes"
|
|
||||||
"github.com/unrolled/render"
|
"github.com/unrolled/render"
|
||||||
|
"net"
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
"os"
|
||||||
|
"os/signal"
|
||||||
|
"reflect"
|
||||||
|
"syscall"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Backend struct {
|
|
||||||
Servers map[string]Server
|
|
||||||
}
|
|
||||||
|
|
||||||
type Server struct {
|
|
||||||
Url string
|
|
||||||
}
|
|
||||||
|
|
||||||
type Rule struct {
|
|
||||||
Category string
|
|
||||||
Value string
|
|
||||||
}
|
|
||||||
|
|
||||||
type Route struct {
|
|
||||||
Backends []string
|
|
||||||
Rules map[string]Rule
|
|
||||||
}
|
|
||||||
|
|
||||||
type Config struct {
|
|
||||||
Backends map[string]Backend
|
|
||||||
Routes map[string]Route
|
|
||||||
}
|
|
||||||
|
|
||||||
var srv *graceful.Server
|
var srv *graceful.Server
|
||||||
var userRouter *mux.Router
|
var userRouter *mux.Router
|
||||||
var config = new(Config)
|
|
||||||
var renderer = render.New()
|
var renderer = render.New()
|
||||||
|
var currentService = new(Service)
|
||||||
|
var serviceChan = make(chan Service)
|
||||||
|
var providers = []Provider{}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
providers = append(providers, new(DockerProvider))
|
||||||
|
|
||||||
sigs := make(chan os.Signal, 1)
|
sigs := make(chan os.Signal, 1)
|
||||||
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
|
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
|
||||||
|
|
||||||
systemRouter := mux.NewRouter()
|
systemRouter := mux.NewRouter()
|
||||||
systemRouter.Methods("POST").Path("/restart").HandlerFunc(RestartHandler)
|
|
||||||
systemRouter.Methods("POST").Path("/reload").HandlerFunc(ReloadConfigHandler)
|
systemRouter.Methods("POST").Path("/reload").HandlerFunc(ReloadConfigHandler)
|
||||||
systemRouter.Methods("GET").Path("/").HandlerFunc(GetConfigHandler)
|
systemRouter.Methods("GET").Path("/").HandlerFunc(GetConfigHandler)
|
||||||
go http.ListenAndServe(":8000", systemRouter)
|
go http.ListenAndServe(":8000", systemRouter)
|
||||||
|
|
||||||
userRouter = LoadConfig()
|
go func() {
|
||||||
|
for {
|
||||||
|
service := <-serviceChan
|
||||||
|
fmt.Println("Service receveived", service)
|
||||||
|
currentService = &service
|
||||||
|
userRouter = LoadConfig(service)
|
||||||
|
srv.Stop(10 * time.Second)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
for _, provider := range providers {
|
||||||
|
provider.Provide(serviceChan)
|
||||||
|
}
|
||||||
|
|
||||||
goAway := false
|
goAway := false
|
||||||
go func() {
|
go func() {
|
||||||
|
@ -69,7 +62,7 @@ func main() {
|
||||||
}()
|
}()
|
||||||
|
|
||||||
for {
|
for {
|
||||||
if (goAway){
|
if goAway {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
srv = &graceful.Server{
|
srv = &graceful.Server{
|
||||||
|
@ -93,9 +86,7 @@ func main() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func LoadDockerConfig(){
|
func LoadDockerConfig(client *docker.Client, service Service) {
|
||||||
endpoint := "unix:///var/run/docker.sock"
|
|
||||||
client, _ := docker.NewClient(endpoint)
|
|
||||||
containerList, _ := client.ListContainers(docker.ListContainersOptions{})
|
containerList, _ := client.ListContainers(docker.ListContainersOptions{})
|
||||||
containersInspected := []docker.Container{}
|
containersInspected := []docker.Container{}
|
||||||
for _, container := range containerList {
|
for _, container := range containerList {
|
||||||
|
@ -108,35 +99,48 @@ func LoadDockerConfig(){
|
||||||
containersInspected,
|
containersInspected,
|
||||||
}
|
}
|
||||||
tmpl, err := gtf.New("docker.tmpl").ParseFiles("docker.tmpl")
|
tmpl, err := gtf.New("docker.tmpl").ParseFiles("docker.tmpl")
|
||||||
if err != nil { panic(err) }
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
var buffer bytes.Buffer
|
var buffer bytes.Buffer
|
||||||
|
|
||||||
err = tmpl.Execute(&buffer, containers)
|
err = tmpl.Execute(&buffer, containers)
|
||||||
if err != nil { panic(err) }
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
fmt.Println(buffer.String())
|
fmt.Println(buffer.String())
|
||||||
|
|
||||||
if _, err := toml.Decode(buffer.String(), config); err != nil {
|
if _, err := toml.Decode(buffer.String(), service); err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func LoadFileConfig(){
|
func LoadFileConfig(service Service) {
|
||||||
if _, err := toml.DecodeFile("tortuous.toml", config); err != nil {
|
if _, err := toml.DecodeFile("tortuous.toml", service); err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func LoadConfig(service Service) *mux.Router {
|
||||||
func LoadConfig() *mux.Router{
|
/*endpoint := "unix:///var/run/docker.sock"
|
||||||
//LoadDockerConfig()
|
client, _ := docker.NewClient(endpoint)
|
||||||
LoadFileConfig()
|
dockerEvents := make(chan *docker.APIEvents)
|
||||||
|
LoadDockerConfig(client)
|
||||||
|
client.AddEventListener(dockerEvents)
|
||||||
|
go func() {
|
||||||
|
for {
|
||||||
|
event := <-dockerEvents
|
||||||
|
fmt.Println("Event receveived", event)
|
||||||
|
}
|
||||||
|
}()*/
|
||||||
|
//LoadFileConfig()
|
||||||
|
|
||||||
router := mux.NewRouter()
|
router := mux.NewRouter()
|
||||||
for routeName, route := range config.Routes {
|
for routeName, route := range service.Routes {
|
||||||
fmt.Println("Creating route", routeName)
|
fmt.Println("Creating route", routeName)
|
||||||
fwd, _ := forward.New()
|
fwd, _ := forward.New()
|
||||||
newRoutes := []*mux.Route{}
|
newRoutes := []*mux.Route{}
|
||||||
|
@ -150,7 +154,7 @@ func LoadConfig() *mux.Router{
|
||||||
fmt.Println("Creating backend", backendName)
|
fmt.Println("Creating backend", backendName)
|
||||||
lb, _ := roundrobin.New(fwd)
|
lb, _ := roundrobin.New(fwd)
|
||||||
rb, _ := roundrobin.NewRebalancer(lb)
|
rb, _ := roundrobin.NewRebalancer(lb)
|
||||||
for serverName, server := range config.Backends[backendName].Servers {
|
for serverName, server := range service.Backends[backendName].Servers {
|
||||||
fmt.Println("Creating server", serverName)
|
fmt.Println("Creating server", serverName)
|
||||||
url, _ := url.Parse(server.Url)
|
url, _ := url.Parse(server.Url)
|
||||||
rb.UpsertServer(url)
|
rb.UpsertServer(url)
|
||||||
|
@ -163,18 +167,22 @@ func LoadConfig() *mux.Router{
|
||||||
return router
|
return router
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func DeployService() {
|
||||||
|
userRouter = LoadConfig(*currentService)
|
||||||
|
}
|
||||||
|
|
||||||
func ReloadConfigHandler(rw http.ResponseWriter, r *http.Request) {
|
func ReloadConfigHandler(rw http.ResponseWriter, r *http.Request) {
|
||||||
userRouter = LoadConfig()
|
DeployService()
|
||||||
|
srv.Stop(10 * time.Second)
|
||||||
renderer.JSON(rw, http.StatusOK, map[string]interface{}{"status": "reloaded"})
|
renderer.JSON(rw, http.StatusOK, map[string]interface{}{"status": "reloaded"})
|
||||||
}
|
}
|
||||||
|
|
||||||
func RestartHandler(rw http.ResponseWriter, r *http.Request) {
|
func RestartHandler(rw http.ResponseWriter, r *http.Request) {
|
||||||
srv.Stop(10 * time.Second)
|
|
||||||
renderer.JSON(rw, http.StatusOK, map[string]interface{}{"status": "restarted"})
|
renderer.JSON(rw, http.StatusOK, map[string]interface{}{"status": "restarted"})
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetConfigHandler(rw http.ResponseWriter, r *http.Request) {
|
func GetConfigHandler(rw http.ResponseWriter, r *http.Request) {
|
||||||
renderer.JSON(rw, http.StatusOK, config)
|
renderer.JSON(rw, http.StatusOK, currentService)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Invoke(any interface{}, name string, args ...interface{}) []reflect.Value {
|
func Invoke(any interface{}, name string, args ...interface{}) []reflect.Value {
|
||||||
|
@ -184,4 +192,3 @@ func Invoke(any interface{}, name string, args... interface{}) []reflect.Value {
|
||||||
}
|
}
|
||||||
return reflect.ValueOf(any).MethodByName(name).Call(inputs)
|
return reflect.ValueOf(any).MethodByName(name).Call(inputs)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue