Commit 31f0d678 authored by Lukas Burgey's avatar Lukas Burgey
Browse files

Implement the config sync with the backend

parent ca191370
......@@ -99,7 +99,9 @@ func (c *consumer) connect() (deliveries <-chan amqp.Delivery, err error) {
// all the bindings from our queue to the topic exchange
for _, exchange := range c.exchanges {
log.Printf("[Conn] Binding to exchange %s with routing keys %v", exchange.name, exchange.bindingKeys)
if *brokerDebugging {
log.Printf("[Conn] Binding to exchange %s with routing keys %v", exchange.name, exchange.bindingKeys)
}
for _, bindingKey := range exchange.bindingKeys {
if err = c.channel.QueueBind(
c.queue.Name, // queue name
......
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
......@@ -19,7 +21,6 @@ type (
fetchedConfig struct {
RabbitMQConfig rabbitMQConfig `json:"rabbitmq_config"`
Services []service `json:"services"`
Site string `json:"site"`
}
......@@ -45,6 +46,12 @@ type (
RabbitMQConfig rabbitMQConfig
Site string
}
// strippedConfig is sent to the backend on startup
strippedConfig struct {
Services []service `json:"services"`
GroupToServices map[string]([]service) `json:"group_to_services"`
}
)
var (
......@@ -60,6 +67,7 @@ var (
configFile = app.Arg("config", "Config file to file to use.").Required().String()
scriptDebugging = app.Flag("debug-scripts", "Display debugging info concerning executed scripts").Bool()
backendDebugging = app.Flag("debug-backend", "Display debugging info concerning the backend").Bool()
brokerDebugging = app.Flag("debug-broker", "Display debugging info concerning the message broker").Bool()
)
func logError(err error, msg string) {
......@@ -68,13 +76,28 @@ func logError(err error, msg string) {
}
}
func (c *config) fetchConfig() (err error) {
req, err := http.NewRequest("GET", "https://"+c.Host+"/backend/clientapi/config", nil)
func (c *config) syncConfig() (err error) {
// we inform the backend which services we provide
strippedConfigBytes, err := json.Marshal(strippedConfig{
Services: c.Services,
GroupToServices: c.GroupToServices,
})
if err != nil {
return
}
// update the services tracked by the backend
req, err := http.NewRequest(
"PUT",
"https://"+c.Host+"/backend/clientapi/config",
bytes.NewReader(strippedConfigBytes),
)
if err != nil {
return
}
req.SetBasicAuth(c.Username, c.Password)
req.Header.Set("Content-Type", "application/json")
resp, err := client.Do(req)
if err != nil {
return
......@@ -85,6 +108,12 @@ func (c *config) fetchConfig() (err error) {
if err != nil {
return
}
if resp.StatusCode != 200 {
err = fmt.Errorf("Unable to sync configuration (backend returned %v)", resp.Status)
return
}
var fetchedConfig fetchedConfig
err = json.Unmarshal(body, &fetchedConfig)
if err != nil {
......@@ -95,18 +124,20 @@ func (c *config) fetchConfig() (err error) {
c.RabbitMQConfig = fetchedConfig.RabbitMQConfig
c.Site = fetchedConfig.Site
permittedServices := fetchedConfig.Services
log.Printf("[Conf] Permitted services: %s", permittedServices)
// we *disabled* service filtering as clients can push the services to the backend
/*
permittedServices := fetchedConfig.Services
log.Printf("[Conf] Permitted services: %s", permittedServices)
if len(c.Services) == 0 {
log.Printf("[Conf] Config specifies no services. Using all permitted services")
} else {
// if the config specifies services we check if they are all permitted
c.Services = filterPermitted(permittedServices, c.Services)
if len(c.Services) == 0 {
log.Printf("[Conf] Config specifies no services. Using all permitted services")
} else {
// if the config specifies services we check if they are all permitted
c.Services = filterPermitted(permittedServices, c.Services)
}
log.Printf("[Conf] Services: %s", c.Services)
}
*/
log.Printf("[Conf] Groups: %s", c.GroupToServices)
log.Printf("[Conf] Synchronised with backend")
return
}
......@@ -169,12 +200,15 @@ func getConfig(configFile string) (c config, err error) {
}
// fetch the remote configuration
err = c.fetchConfig()
err = c.syncConfig()
if err != nil {
log.Fatalf("[Conf] Error fetching remote config: %s", err)
return
}
log.Printf("[Conf] Services: %s", c.Services)
log.Printf("[Conf] Groups: %s", c.GroupToServices)
// initialize the task queues
c.NewTasks = make(chan task)
c.DoneTasks = make(chan taskExecution)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment