Exemplo n.º 1
0
func (n NitroApi) executeRequest(method string, url string, contentType string, jsonContent string) (interface{}, error) {
	log.Debugf("HTTP %s to %s, contents=%s", method, url, jsonContent)
	client := &http.Client{}
	req, err := http.NewRequest(method, "http://"+n.nitroBaseUrl+url, strings.NewReader(jsonContent))
	if err != nil {
		return nil, err
	}
	req.Header.Add("X-NITRO-USER", "root")
	req.Header.Add("X-NITRO-PASS", "linux")
	req.Header.Add("Content-Type", contentType)
	var attempts float64 = 1
	for {
		resp, err := client.Do(req)
		if err != nil {
			log.Errorf("%v", err)
		} else {
			defer resp.Body.Close()
			log.Debugf("response StatusCode: %v", resp.StatusCode)
			body, _ := ioutil.ReadAll(resp.Body)
			log.Debugf("response Body: %v", string(body))
			if resp.StatusCode == 200 || resp.StatusCode == 201 || resp.StatusCode == 409 {
				return body, nil
			}
		}
		millis := math.Min(60000, math.Pow(2, attempts)*100)
		log.Debugf("attempt %v, waiting %v millis", attempts, millis)
		time.Sleep(time.Duration(millis) * time.Millisecond)
		attempts++
	}

	return nil, err
}
Exemplo n.º 2
0
// Make HTTP GET from metadata, parse JSON results, and map to LB mappings
func (m *MetadataHandler) GetLbConfig() (map[string]nme.Lbvserver, error) {
	client := &http.Client{}
	serviceReq, err := http.NewRequest("GET", "http://"+m.metadataUrl+"/latest/services", nil)
	if err != nil {
		return nil, err
	}
	serviceReq.Header.Add("Accept", "application/json")
	serviceResp, err := client.Do(serviceReq)
	defer serviceResp.Body.Close()
	if err != nil {
		return nil, err
	}
	body, err := ioutil.ReadAll(serviceResp.Body)
	if err != nil {
		return nil, err
	}
	log.Debugf("resp body=%s", string(body[:]))
	var serviceMappings interface{}
	err = json.Unmarshal(body, &serviceMappings)
	if err != nil {
		return nil, err
	}

	services, ok := serviceMappings.([]interface{})
	if !ok {
		return nil, fmt.Errorf("Unexpected JSON results from metadata service: %v", serviceMappings)
	}

	containerReq, err := http.NewRequest("GET", "http://"+m.metadataUrl+"/latest/containers", nil)
	if err != nil {
		return nil, err
	}
	containerReq.Header.Add("Accept", "application/json")
	containerResp, err := client.Do(containerReq)
	defer containerResp.Body.Close()
	if err != nil {
		return nil, err
	}
	body, err = ioutil.ReadAll(containerResp.Body)
	if err != nil {
		return nil, err
	}
	log.Debugf("resp body=%s", string(body[:]))
	var containerMappings interface{}
	err = json.Unmarshal(body, &containerMappings)
	if err != nil {
		return nil, err
	}

	containers, ok := containerMappings.([]interface{})
	if !ok {
		return nil, fmt.Errorf("Unexpected JSON results from metadata service: %v", containerMappings)
	}

	lbConfig, err := m.getLbConfigFromJSONResults(services, containers)
	return lbConfig, err
}
Exemplo n.º 3
0
func (m *MetadataHandler) GetHash() (string, error) {
	client := &http.Client{}
	req, err := http.NewRequest("GET", "http://"+m.metadataUrl+"/latest/version", nil)
	resp, err := client.Do(req)
	if err != nil {
		return "", err
	}
	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return "", err
	}
	log.Debugf("resp body=%s", string(body[:]))
	return string(body[:]), nil
}
Exemplo n.º 4
0
func (m *MetadataHandler) getServiceToPortConfigsFromVipService(services []interface{}) (map[string]ExtLbConfig, error) {
	for i := range services {
		service, ok := services[i].(map[string]interface{})
		if !ok {
			return nil, fmt.Errorf("Error parsing service data: %v", services[i])
		}
		labels, ok := service["labels"].(map[string]interface{})
		if !ok {
			log.Debugf("skipping %v", service)
			continue
		}
		networkServices, ok := labels["io.rancher.network.services"]
		if !ok || !strings.Contains(networkServices.(string), "vipService") {
			log.Debugf("skipping %v", service)
			continue
		}
		extLbConfigs, ok := labels["io.rancher.netscaler.lb"].(string)
		if ok {
			log.Debugf("found netscaler external lb configs: %v", extLbConfigs)
			serviceToPortConfigs := make(map[string]ExtLbConfig)
			configs := strings.Split(extLbConfigs, ",")
			for i := range configs {
				log.Debugf("configs %v", configs)
				extLbConfigPieces := strings.Split(configs[i], ":")
				switch len(extLbConfigPieces) {
				case 2, 3:
					stackServiceName := strings.Split(extLbConfigPieces[0], "/")
					if len(stackServiceName) != 2 {
						continue
					}
					var portProtocol []string
					var publicPort string
					if len(extLbConfigPieces) == 2 {
						portProtocol = strings.Split(extLbConfigPieces[1], "/")
					} else if len(extLbConfigPieces) == 3 {
						publicPort = extLbConfigPieces[1]
						portProtocol = strings.Split(extLbConfigPieces[2], "/")
					}
					privatePort := portProtocol[0]
					serviceType := "ANY"
					if len(portProtocol) > 1 {
						serviceType = strings.ToUpper(portProtocol[1])
					}
					if publicPort == "" {
						publicPort = privatePort
					}
					extLbConfig := ExtLbConfig{
						stackName:   stackServiceName[0],
						serviceName: stackServiceName[1],
						publicPort:  publicPort,
						privatePort: privatePort,
						serviceType: serviceType,
					}
					log.Debugf("extLbConfig: %v", extLbConfig)
					serviceToPortConfigs[extLbConfig.stackName+"/"+extLbConfig.serviceName] = extLbConfig
				}
			}
			return serviceToPortConfigs, nil
		}
		return nil, nil
	}
	return nil, nil
}