Exemple #1
0
// setupClient is used to setup the client if enabled
func (a *Agent) setupClient() error {
	if !a.config.Client.Enabled {
		return nil
	}

	// Setup the configuration
	conf, err := a.clientConfig()
	if err != nil {
		return fmt.Errorf("client setup failed: %v", err)
	}

	// Reserve some ports for the plugins if we are on Windows
	if runtime.GOOS == "windows" {
		if err := a.reservePortsForClient(conf); err != nil {
			return err
		}
	}

	// Create the client
	client, err := client.NewClient(conf, a.consulSyncer, a.logger)
	if err != nil {
		return fmt.Errorf("client setup failed: %v", err)
	}
	a.client = client

	// Resolve the http check address
	httpCheckAddr := a.config.normalizedAddrs.HTTP
	if a.config.Consul.ChecksUseAdvertise {
		httpCheckAddr = a.config.AdvertiseAddrs.HTTP
	}

	// Create the Nomad Client  services for Consul
	// TODO think how we can re-introduce HTTP/S checks when Consul 0.7.1 comes
	// out
	if a.config.Consul.AutoAdvertise {
		httpServ := &structs.Service{
			Name:      a.config.Consul.ClientServiceName,
			PortLabel: a.config.AdvertiseAddrs.HTTP,
			Tags:      []string{consul.ServiceTagHTTP},
			Checks: []*structs.ServiceCheck{
				&structs.ServiceCheck{
					Name:      "Nomad Client HTTP Check",
					Type:      "http",
					Path:      "/v1/agent/servers",
					Protocol:  "http",
					Interval:  clientHttpCheckInterval,
					Timeout:   clientHttpCheckTimeout,
					PortLabel: httpCheckAddr,
				},
			},
		}
		if !conf.TLSConfig.EnableHTTP {
			a.consulSyncer.SetServices(consul.ClientDomain, map[consul.ServiceKey]*structs.Service{
				consul.GenerateServiceKey(httpServ): httpServ,
			})
		}
	}

	return nil
}
Exemple #2
0
// generateServiceKeys takes a list of interpolated Nomad Services and returns a map
// of ServiceKeys to Nomad Services.
func generateServiceKeys(allocID string, services []*structs.Service) map[consul.ServiceKey]*structs.Service {
	keys := make(map[consul.ServiceKey]*structs.Service, len(services))
	for _, service := range services {
		key := consul.GenerateServiceKey(service)
		keys[key] = service
	}
	return keys
}
Exemple #3
0
// setupClient is used to setup the client if enabled
func (a *Agent) setupClient() error {
	if !a.config.Client.Enabled {
		return nil
	}

	// Setup the configuration
	conf, err := a.clientConfig()
	if err != nil {
		return fmt.Errorf("client setup failed: %v", err)
	}

	// Reserve some ports for the plugins if we are on Windows
	if runtime.GOOS == "windows" {
		if err := a.reservePortsForClient(conf); err != nil {
			return err
		}
	}

	// Create the client
	client, err := client.NewClient(conf, a.consulSyncer)
	if err != nil {
		return fmt.Errorf("client setup failed: %v", err)
	}
	a.client = client

	// Create the Nomad Client  services for Consul
	if a.config.Consul.AutoAdvertise {
		httpServ := &structs.Service{
			Name:      a.config.Consul.ClientServiceName,
			PortLabel: a.clientHTTPAddr,
			Tags:      []string{consul.ServiceTagHTTP},
		}
		rpcServ := &structs.Service{
			Name:      a.config.Consul.ClientServiceName,
			PortLabel: a.clientRPCAddr,
			Tags:      []string{consul.ServiceTagRPC},
		}
		a.consulSyncer.SetServices(consul.ClientDomain, map[consul.ServiceKey]*structs.Service{
			consul.GenerateServiceKey(httpServ): httpServ,
			consul.GenerateServiceKey(rpcServ):  rpcServ,
		})
	}

	return nil
}
Exemple #4
0
// setupServer is used to setup the server if enabled
func (a *Agent) setupServer() error {
	if !a.config.Server.Enabled {
		return nil
	}

	// Setup the configuration
	conf, err := a.serverConfig()
	if err != nil {
		return fmt.Errorf("server config setup failed: %s", err)
	}

	// Create the server
	server, err := nomad.NewServer(conf)
	if err != nil {
		return fmt.Errorf("server setup failed: %v", err)
	}
	a.server = server

	// Create the Nomad Server services for Consul
	if a.config.Consul.AutoAdvertise {
		httpServ := &structs.Service{
			Name:      a.config.Consul.ServerServiceName,
			PortLabel: a.serverHTTPAddr,
			Tags:      []string{consul.ServiceTagHTTP},
		}
		rpcServ := &structs.Service{
			Name:      a.config.Consul.ServerServiceName,
			PortLabel: a.serverRPCAddr,
			Tags:      []string{consul.ServiceTagRPC},
		}
		serfServ := &structs.Service{
			PortLabel: a.serverSerfAddr,
			Name:      a.config.Consul.ServerServiceName,
			Tags:      []string{consul.ServiceTagSerf},
		}
		a.consulSyncer.SetServices(consul.ServerDomain, map[consul.ServiceKey]*structs.Service{
			consul.GenerateServiceKey(httpServ): httpServ,
			consul.GenerateServiceKey(rpcServ):  rpcServ,
			consul.GenerateServiceKey(serfServ): serfServ,
		})
	}

	return nil
}
Exemple #5
0
// setupClient is used to setup the client if enabled
func (a *Agent) setupClient() error {
	if !a.config.Client.Enabled {
		return nil
	}

	// Setup the configuration
	conf, err := a.clientConfig()
	if err != nil {
		return fmt.Errorf("client setup failed: %v", err)
	}

	// Reserve some ports for the plugins if we are on Windows
	if runtime.GOOS == "windows" {
		if err := a.reservePortsForClient(conf); err != nil {
			return err
		}
	}

	// Create the client
	client, err := client.NewClient(conf, a.consulSyncer, a.logger)
	if err != nil {
		return fmt.Errorf("client setup failed: %v", err)
	}
	a.client = client

	// Create the Nomad Client  services for Consul
	if a.config.Consul.AutoAdvertise {
		httpServ := &structs.Service{
			Name:      a.config.Consul.ClientServiceName,
			PortLabel: a.clientHTTPAddr,
			Tags:      []string{consul.ServiceTagHTTP},
			Checks: []*structs.ServiceCheck{
				&structs.ServiceCheck{
					Name:     "Nomad Client HTTP Check",
					Type:     "http",
					Path:     "/v1/agent/servers",
					Protocol: "http", // TODO TLS
					Interval: clientHttpCheckInterval,
					Timeout:  clientHttpCheckTimeout,
				},
			},
		}
		a.consulSyncer.SetServices(consul.ClientDomain, map[consul.ServiceKey]*structs.Service{
			consul.GenerateServiceKey(httpServ): httpServ,
		})
	}

	return nil
}
Exemple #6
0
// setupServer is used to setup the server if enabled
func (a *Agent) setupServer() error {
	if !a.config.Server.Enabled {
		return nil
	}

	// Setup the configuration
	conf, err := a.serverConfig()
	if err != nil {
		return fmt.Errorf("server config setup failed: %s", err)
	}

	// Sets up the keyring for gossip encryption
	if err := a.setupKeyrings(conf); err != nil {
		return fmt.Errorf("failed to configure keyring: %v", err)
	}

	// Create the server
	server, err := nomad.NewServer(conf, a.consulSyncer, a.logger)
	if err != nil {
		return fmt.Errorf("server setup failed: %v", err)
	}
	a.server = server

	// Create the Nomad Server services for Consul
	if a.config.Consul.AutoAdvertise {
		httpServ := &structs.Service{
			Name:      a.config.Consul.ServerServiceName,
			PortLabel: a.serverHTTPAddr,
			Tags:      []string{consul.ServiceTagHTTP},
			Checks: []*structs.ServiceCheck{
				&structs.ServiceCheck{
					Name:     "Nomad Server HTTP Check",
					Type:     "http",
					Path:     "/v1/status/peers",
					Protocol: "http", // TODO TLS
					Interval: serverHttpCheckInterval,
					Timeout:  serverHttpCheckTimeout,
				},
			},
		}
		rpcServ := &structs.Service{
			Name:      a.config.Consul.ServerServiceName,
			PortLabel: a.serverRPCAddr,
			Tags:      []string{consul.ServiceTagRPC},
			Checks: []*structs.ServiceCheck{
				&structs.ServiceCheck{
					Name:     "Nomad Server RPC Check",
					Type:     "tcp",
					Interval: serverRpcCheckInterval,
					Timeout:  serverRpcCheckTimeout,
				},
			},
		}
		serfServ := &structs.Service{
			PortLabel: a.serverSerfAddr,
			Name:      a.config.Consul.ServerServiceName,
			Tags:      []string{consul.ServiceTagSerf},
			Checks: []*structs.ServiceCheck{
				&structs.ServiceCheck{
					Name:     "Nomad Server Serf Check",
					Type:     "tcp",
					Interval: serverSerfCheckInterval,
					Timeout:  serverSerfCheckTimeout,
				},
			},
		}
		a.consulSyncer.SetServices(consul.ServerDomain, map[consul.ServiceKey]*structs.Service{
			consul.GenerateServiceKey(httpServ): httpServ,
			consul.GenerateServiceKey(rpcServ):  rpcServ,
			consul.GenerateServiceKey(serfServ): serfServ,
		})
	}

	return nil
}
Exemple #7
0
// setupServer is used to setup the server if enabled
func (a *Agent) setupServer() error {
	if !a.config.Server.Enabled {
		return nil
	}

	// Setup the configuration
	conf, err := a.serverConfig()
	if err != nil {
		return fmt.Errorf("server config setup failed: %s", err)
	}

	// Sets up the keyring for gossip encryption
	if err := a.setupKeyrings(conf); err != nil {
		return fmt.Errorf("failed to configure keyring: %v", err)
	}

	// Create the server
	server, err := nomad.NewServer(conf, a.consulSyncer, a.logger)
	if err != nil {
		return fmt.Errorf("server setup failed: %v", err)
	}
	a.server = server

	// Consul check addresses default to bind but can be toggled to use advertise
	httpCheckAddr := a.config.normalizedAddrs.HTTP
	rpcCheckAddr := a.config.normalizedAddrs.RPC
	serfCheckAddr := a.config.normalizedAddrs.Serf
	if a.config.Consul.ChecksUseAdvertise {
		httpCheckAddr = a.config.AdvertiseAddrs.HTTP
		rpcCheckAddr = a.config.AdvertiseAddrs.RPC
		serfCheckAddr = a.config.AdvertiseAddrs.Serf
	}

	// Create the Nomad Server services for Consul
	// TODO re-introduce HTTP/S checks when Consul 0.7.1 comes out
	if a.config.Consul.AutoAdvertise {
		httpServ := &structs.Service{
			Name:      a.config.Consul.ServerServiceName,
			PortLabel: a.config.AdvertiseAddrs.HTTP,
			Tags:      []string{consul.ServiceTagHTTP},
			Checks: []*structs.ServiceCheck{
				&structs.ServiceCheck{
					Name:      "Nomad Server HTTP Check",
					Type:      "http",
					Path:      "/v1/status/peers",
					Protocol:  "http",
					Interval:  serverHttpCheckInterval,
					Timeout:   serverHttpCheckTimeout,
					PortLabel: httpCheckAddr,
				},
			},
		}
		rpcServ := &structs.Service{
			Name:      a.config.Consul.ServerServiceName,
			PortLabel: a.config.AdvertiseAddrs.RPC,
			Tags:      []string{consul.ServiceTagRPC},
			Checks: []*structs.ServiceCheck{
				&structs.ServiceCheck{
					Name:      "Nomad Server RPC Check",
					Type:      "tcp",
					Interval:  serverRpcCheckInterval,
					Timeout:   serverRpcCheckTimeout,
					PortLabel: rpcCheckAddr,
				},
			},
		}
		serfServ := &structs.Service{
			Name:      a.config.Consul.ServerServiceName,
			PortLabel: a.config.AdvertiseAddrs.Serf,
			Tags:      []string{consul.ServiceTagSerf},
			Checks: []*structs.ServiceCheck{
				&structs.ServiceCheck{
					Name:      "Nomad Server Serf Check",
					Type:      "tcp",
					Interval:  serverSerfCheckInterval,
					Timeout:   serverSerfCheckTimeout,
					PortLabel: serfCheckAddr,
				},
			},
		}

		// Add the http port check if TLS isn't enabled
		// TODO Add TLS check when Consul 0.7.1 comes out.
		consulServices := map[consul.ServiceKey]*structs.Service{
			consul.GenerateServiceKey(rpcServ):  rpcServ,
			consul.GenerateServiceKey(serfServ): serfServ,
		}
		if !conf.TLSConfig.EnableHTTP {
			consulServices[consul.GenerateServiceKey(httpServ)] = httpServ
		}
		a.consulSyncer.SetServices(consul.ServerDomain, consulServices)
	}

	return nil
}