Ejemplo n.º 1
0
// call this fuction to add nodes
func (c *Config) AddConsulNodes() (err error) {

	// iterate over all nodes and take appropriate action
	for _, node := range c.Nodes {
		var (
			request *http.Request
			client  *http.Client
			resp    *http.Response
			url     string
			putdata []byte
		)
		client = &http.Client{
			CheckRedirect: nil,
		}
		url = fmt.Sprintf("http://%s/v1/catalog/register?token=%s", c.Consul, c.Token)
		log.Debugf("Attempting to register the service using URL: %s", url)
		node.Datacenter = c.Datacenter
		//putdata, err := json.MarshalIndent(node, "", "	")
		putdata, err := json.Marshal(node)
		if err != nil {
			log.Fatalf("Unable to create JSON from our structure. This could only be caused by a bug in your yaml: %s", err)
			return err
		}

		log.Debugf("The token data to PUT: %s", string(putdata))
		request, err = http.NewRequest("PUT", url, nil)
		if err != nil {
			log.Warnf("Unable to register the node. Err: %s", err)
			return err
		}
		reqdata := strings.NewReader(string(putdata))
		request, err = http.NewRequest("PUT", url, reqdata)

		resp, err = client.Do(request)

		if err != nil {
			log.Fatalf("There was a problem calling the Consul server: %s", err)
			return err
		}
		if resp.StatusCode != 200 {
			log.Fatalf("There was a problem calling the Consul server. Response Code %i", resp.StatusCode)
			return err
		}

	}
	return err
}
Ejemplo n.º 2
0
// call this fuction to add nodes
func (c *Config) AddConsulKvPairs() (err error) {
	// iterate over all nodes and take appropriate action
	for _, pair := range c.KeyValues {
		var (
			request *http.Request
			client  *http.Client
			resp    *http.Response
			url     string
		)
		client = &http.Client{
			CheckRedirect: nil,
		}
		url = fmt.Sprintf("http://%s/v1/kv/%s?token=%s", c.Consul, pair.Name, c.Token)
		log.Debugf("Attempting to register the service using URL: %s", url)
		log.Debugf("The token data to PUT: %s", pair.Value)
		request, err = http.NewRequest("PUT", url, nil)
		if err != nil {
			log.Warnf("Unable to register the node. Err: %s", err)
			return err
		}
		reqdata := strings.NewReader(pair.Value)
		request, err = http.NewRequest("PUT", url, reqdata)

		resp, err = client.Do(request)

		if err != nil {
			log.Fatalf("There was a problem calling the Consul server: %s", err)
			return err
		}
		if resp.StatusCode != 200 {
			log.Fatalf("There was a problem calling the Consul server. Response Code %i", resp.StatusCode)
			return err
		}

	}
	return err
}
Ejemplo n.º 3
0
// does the logic to set the tokens on the Consul cluster
// if a token is set to be removed, it does the logic to delete
// the token.
func (c *Config) SetConsulACL() (err error) {

	// iterate over all tokens and take appropriate action
	for _, token := range c.Tokens {

		var (
			request *http.Request
			client  *http.Client
			resp    *http.Response
			url     string
			tok     ConsulACL
		)
		tok = ConsulACL{}
		client = &http.Client{
			CheckRedirect: nil,
		}
		tok.ID = token.Token

		if token.Remove {
			url = fmt.Sprintf("http://%s/v1/acl/destroy/%s?token=%s", c.Consul, tok.ID, c.Token)
			log.Debugf("Attempting to destroy an ACL. Using URL: %s", url)
			request, err = http.NewRequest("PUT", url, nil)
			if err != nil {
				log.Warnf("Unable to destroy the ACL, it's possible it was already deleted. %s", err)
			}

		} else {
			url = fmt.Sprintf("http://%s/v1/acl/destroy/%s?token=%s", c.Consul, tok.ID, c.Token)
			log.Debugf("Cancel URL: %s", url)
			request, err = http.NewRequest("PUT", url, nil)

			if err != nil {
				log.Fatalf("Unable to issue PUT request to desroy an existing token: %s", url)
				return err
			}

			url = fmt.Sprintf("http://%s/v1/acl/create?token=%s", c.Consul, c.Token)
			log.Debugf("Create URL: %s", url)

			tok.Name = token.Name
			tok.Type = token.Type
			tok.Rules, err = c.RulesString(token)

			if err != nil {
				log.Fatalf("Unable to create JSON string of the rules: %s", err)
				return err
			}

			tokdata, err := json.MarshalIndent(tok, "", "  ")

			if err != nil {
				log.Fatalf("Unable to create JSON from our structure. This could only be caused by a bug in your code: %s", err)
				return err
			}

			log.Debugf("The token data to PUT: %s", string(tokdata))
			reqdata := strings.NewReader(string(tokdata))
			request, err = http.NewRequest("PUT", url, reqdata)
		}

		resp, err = client.Do(request)

		if err != nil {
			log.Fatalf("There was a problem calling the Consul server: %s", err)
			return err
		}
		if resp.StatusCode != 200 {
			log.Fatalf("There was a problem calling the Consul server. Response Code %i", resp.StatusCode)
			return
		}
	}
	log.Info("Ran all updates against Consul at: ", c.Consul)
	return
}