Commit 179436df authored by Lukas Burgey's avatar Lukas Burgey
Browse files

Fix the error reporting

parent 45174c33
......@@ -175,7 +175,7 @@ func (c *consumer) reconnect() {
func (c *consumer) startConsuming() (err error) {
deliveries, err := c.connect()
if err != nil {
log.Fatalf("[Conn] Error connecting: %s", err)
log.Printf("[Conn] Error connecting: %s", err)
return
}
......
......@@ -79,8 +79,18 @@ func logError(err error, msg string) {
}
func (c *config) syncConfig() (err error) {
log.Printf("[Conf] Synchronising configuration with %v", c.Host)
var (
strippedConfigBytes []byte
req *http.Request
resp *http.Response
fetchedConfig fetchedConfig
)
// we inform the backend which services we provide
strippedConfigBytes, err := json.Marshal(strippedConfig{
strippedConfigBytes, err = json.Marshal(strippedConfig{
Services: c.Services,
GroupToServices: c.GroupToServices,
})
......@@ -89,7 +99,7 @@ func (c *config) syncConfig() (err error) {
}
// update the services tracked by the backend
req, err := http.NewRequest(
req, err = http.NewRequest(
"PUT",
"https://"+c.Host+"/backend/clientapi/config",
bytes.NewReader(strippedConfigBytes),
......@@ -100,7 +110,7 @@ func (c *config) syncConfig() (err error) {
req.SetBasicAuth(c.Username, c.Password)
req.Header.Set("Content-Type", "application/json")
resp, err := client.Do(req)
resp, err = client.Do(req)
if err != nil {
return
}
......@@ -110,51 +120,20 @@ func (c *config) syncConfig() (err error) {
if err != nil {
return
}
if resp.StatusCode != 200 {
err = fmt.Errorf("Unable to sync configuration (backend returned %v)", resp.Status)
err = fmt.Errorf("Unable to sync configuration (response: %v)", resp.Status)
return
}
var fetchedConfig fetchedConfig
err = json.Unmarshal(body, &fetchedConfig)
if err != nil {
log.Fatalf("Unable to parse: %s %s", err, body)
err = fmt.Errorf("Unable to parse remote configuration: %s %s", err, body)
return
}
c.RabbitMQConfig = fetchedConfig.RabbitMQConfig
c.Site = fetchedConfig.Site
// 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)
}
*/
c.Log("Synchronised with backend")
return
}
func filterPermitted(permitted []service, wanted []service) (remainder []service) {
serviceLoop:
for _, service := range wanted {
for _, permittedService := range permitted {
if service.Name == permittedService.Name {
remainder = append(remainder, service)
continue serviceLoop
}
}
log.Printf("[Conn] Skipping unpermitted service %s", service)
}
return
}
......@@ -204,7 +183,7 @@ func getConfig(configFile string) (c config, err error) {
// fetch the remote configuration
err = c.syncConfig()
if err != nil {
log.Fatalf("[Conf] Error fetching remote config: %s", err)
log.Printf("[Conf] Error fetching remote config: %s", err)
return
}
......@@ -246,7 +225,7 @@ func main() {
// read the config file
c, err := getConfig(*configFile)
if err != nil {
log.Fatalf("[Err] No valid config. Exiting")
log.Fatalf("[Exit] No valid config. Exiting")
}
if len(c.Services) == 0 && len(c.GroupToServices) == 0 {
......
......@@ -27,12 +27,13 @@ type (
}
task struct {
ID int `json:"id"`
StateTarget s.State `json:"state_target"`
User s.User `json:"user"`
Key s.SSHKey `json:"key"`
Questionnaire map[string]string `json:"questionnaire"`
Group group `json:"group"`
ID int `json:"id"`
StateTarget s.State `json:"state_target"`
User s.User `json:"user"`
Key s.SSHKey `json:"key"`
Credentials map[string][]s.Credential `json:"credentials"`
Questionnaire map[string]string `json:"questionnaire"`
Group group `json:"group"`
// maybe overwritten by scheduleTask
Service service `json:"service,omitempty"`
......@@ -234,6 +235,11 @@ func (c *config) fetchTasks() (err error) {
}
defer resp.Body.Close()
if resp.StatusCode != 400 {
err = fmt.Errorf("Unable to fetch tasks (response: %v)", resp.Status)
return
}
var newTasks []task
body, err := ioutil.ReadAll(resp.Body)
......@@ -286,6 +292,7 @@ func (c *config) handleTask(t task) (err error) {
User: t.User,
Key: t.Key,
Questionnaire: t.Questionnaire,
Credentials: t.Credentials,
}
iBytes, err := input.Marshal()
if err != nil {
......
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