Example #1
0
func leader(hc *http.Client, masters []string) (types.Host, error) {
	var host types.Host

	master := utils.PickRandomFromList(masters)

	leaderId, err := query(hc, master)
	if err != nil {
		return host, err
	}

	return parseLeader(leaderId)
}
Example #2
0
// Queries a Marathon master to receive running applications and tasks and generates a list of services.
func (g *Generator) Generate() ([]*types.Service, error) {
	var apps Apps
	var tasks Tasks

	server := utils.PickRandomFromList(g.marathonServers)

	log.AppLog.Debug("Querying Marathon server at '%s'", server)

	req, _ := http.NewRequest("GET", fmt.Sprintf("%s%s", server, appsEndpoint), nil)
	req.Header.Add("Accept", "application/json")

	resp, err := g.httpClient.Do(req)
	if err != nil {
		return []*types.Service{}, err
	}
	defer resp.Body.Close()

	data, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return []*types.Service{}, err
	}

	err = json.Unmarshal(data, &apps)
	if err != nil {
		return []*types.Service{}, err
	}

	req, _ = http.NewRequest("GET", fmt.Sprintf("%s%s", server, tasksEndpoint), nil)
	req.Header.Add("Accept", "application/json")

	resp, err = g.httpClient.Do(req)
	if err != nil {
		return []*types.Service{}, err
	}
	defer resp.Body.Close()

	data, err = ioutil.ReadAll(resp.Body)
	if err != nil {
		return []*types.Service{}, err
	}

	err = json.Unmarshal(data, &tasks)
	if err != nil {
		return []*types.Service{}, err
	}

	return g.servicesFromMarathon(apps, tasks), nil
}