Ejemplo n.º 1
0
func (r *HipacheAdapter) doConsulTagging(service *bridge.Service) error {
	var svc *consulapi.AgentService
	mytag := service.Name
	ConsulName := strings.Replace(r.domain, ".service", "", -1)
	agent := r.consul.Agent()
	consulsvcs, err := agent.Services()
	if err != nil {
		return err
	}
	svc, found := consulsvcs[ConsulName]
	if !found {
		svc, err = doConsulAddService(agent, ConsulName, service.Origin.HostIP)
		if err != nil {
			return err
		}
	}

	_, found = locate(svc.Tags, mytag)
	if !found {
		svc.Tags = append(svc.Tags, mytag)
		log.Print("hipache: adding tag to consul:", ConsulName, "/", mytag, "/", svc.Tags)
		return updateConsulTags(agent, svc)
	}
	return nil
}
Ejemplo n.º 2
0
// createService creates a Consul AgentService from a Nomad Service
func (c *ConsulService) createService(service *structs.Service) (*consul.AgentService, error) {
	srv := consul.AgentService{
		ID:      service.ID(c.allocID, c.task.Name),
		Service: service.Name,
		Tags:    service.Tags,
	}
	host, port := c.task.FindHostAndPortFor(service.PortLabel)
	if host != "" {
		srv.Address = host
	}

	if port != 0 {
		srv.Port = port
	}

	return &srv, nil
}
Ejemplo n.º 3
0
// createService creates a Consul AgentService from a Nomad Service
func (c *ConsulService) createService(service *structs.Service) (*consul.AgentService, error) {
	srv := consul.AgentService{
		ID:      service.ID(c.serviceIdentifier),
		Service: service.Name,
		Tags:    service.Tags,
	}
	host, port := c.addrFinder(service.PortLabel)
	if host != "" {
		srv.Address = host
	}

	if port != 0 {
		srv.Port = port
	}

	return &srv, nil
}
Ejemplo n.º 4
0
func (r *HipacheAdapter) doConsulUnTagging(service *bridge.Service) error {
	var svc *consulapi.AgentService
	var found bool
	mytag := service.Name
	ConsulName := strings.Replace(r.domain, ".service", "", -1)
	agent := r.consul.Agent()
	consulsvcs, err := agent.Services()
	if err != nil {
		return err
	}
	svc, found = consulsvcs[ConsulName]
	if !found {
		log.Print("hipache: how strange -- the service you want me to detag does not exist: ", ConsulName)
		return nil
	}
	svc.Tags = excise(svc.Tags, mytag)
	err = updateConsulTags(agent, svc)
	return nil
}
Ejemplo n.º 5
0
func (r *ConsulAdapter) Register(service *bridge.Service) error {
	agentService := new(consulapi.AgentService)
	agentService.ID = service.ID
	agentService.Service = service.Name
	agentService.Port = service.Port
	agentService.Tags = service.Tags
	agentService.Address = service.IP

	registration := new(consulapi.CatalogRegistration)
	registration.Node = Hostname
	registration.Address = service.Origin.HostIP
	registration.Datacenter = service.Attrs["region"]
	registration.Service = agentService
	registration.Check = nil

	writeOptions := new(consulapi.WriteOptions)
	writeOptions.Datacenter = service.Attrs["region"]

	out, _ := json.Marshal(registration)

	log.Println("REGISTERING :", string(out))
	_, res := r.client.Catalog().Register(registration, writeOptions)

	return res
}