Example #1
0
func testServer(t *testing.T, cb func(*nomad.Config)) (*nomad.Server, string) {
	f := false

	// Setup the default settings
	config := nomad.DefaultConfig()
	config.VaultConfig.Enabled = &f
	config.Build = "unittest"
	config.DevMode = true
	config.RPCAddr = &net.TCPAddr{
		IP:   []byte{127, 0, 0, 1},
		Port: getPort(),
	}
	config.NodeName = fmt.Sprintf("Node %d", config.RPCAddr.Port)

	// Tighten the Serf timing
	config.SerfConfig.MemberlistConfig.BindAddr = "127.0.0.1"
	config.SerfConfig.MemberlistConfig.BindPort = getPort()
	config.SerfConfig.MemberlistConfig.SuspicionMult = 2
	config.SerfConfig.MemberlistConfig.RetransmitMult = 2
	config.SerfConfig.MemberlistConfig.ProbeTimeout = 50 * time.Millisecond
	config.SerfConfig.MemberlistConfig.ProbeInterval = 100 * time.Millisecond
	config.SerfConfig.MemberlistConfig.GossipInterval = 100 * time.Millisecond

	// Tighten the Raft timing
	config.RaftConfig.LeaderLeaseTimeout = 20 * time.Millisecond
	config.RaftConfig.HeartbeatTimeout = 40 * time.Millisecond
	config.RaftConfig.ElectionTimeout = 40 * time.Millisecond
	config.RaftConfig.StartAsLeader = true
	config.RaftTimeout = 500 * time.Millisecond

	// Invoke the callback if any
	if cb != nil {
		cb(config)
	}

	shutdownCh := make(chan struct{})
	logger := log.New(config.LogOutput, "", log.LstdFlags)
	consulSyncer, err := consul.NewSyncer(config.ConsulConfig, shutdownCh, logger)
	if err != nil {
		t.Fatalf("err: %v", err)
	}

	// Create server
	server, err := nomad.NewServer(config, consulSyncer, logger)
	if err != nil {
		t.Fatalf("err: %v", err)
	}
	return server, config.RPCAddr.String()
}
Example #2
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
}
Example #3
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
	return nil
}
Example #4
0
File: agent.go Project: nak3/nomad
// 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
}
Example #5
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
}