Beispiel #1
0
func setupServer(t *testing.T, rest_port, rep_port uint16, cl cluster.Cluster) (replication.Replication, api.Server) {
	var rep replication.Replication
	var server api.Server
	networkAvailable := network.WaitForPrivateNetwork()
	assert.NotNil(t, networkAvailable)

	// Configure and create the cluster module
	if cl == nil {
		return nil, nil
	}
	var err error

	// Configure and create the replication module
	self := cluster.NewMember(network.GetPrivateIP(), rep_port)
	repConfig := &replication.Config{
		Membership:  cl.Membership(),
		Registrator: cl.Registrator(self),
	}
	rep, err = replication.New(repConfig)
	assert.NoError(t, err)
	assert.NotNil(t, rep)

	regConfig := &store.Config{
		DefaultTTL:        time.Duration(30) * time.Second,
		MinimumTTL:        time.Duration(10) * time.Second,
		MaximumTTL:        time.Duration(600) * time.Second,
		SyncWaitTime:      time.Duration(defaultSyncWaitTime) * time.Second,
		NamespaceCapacity: 50,
	}

	reg := store.New(regConfig, rep)
	server, err = api.NewServer(
		&api.Config{
			HTTPAddressSpec: fmt.Sprintf(":%d", rest_port),
			Registry:        reg,
		},
	)

	assert.NoError(t, err)
	assert.NotNil(t, server)

	// Start the API server, and "wait" for it to bind
	go server.Start()
	time.Sleep(100 * time.Millisecond)

	return rep, server
}
Beispiel #2
0
// registryMain is the logical entry point for the service registry.
func registryMain(conf *config.Values) error {

	// Configure logging
	parsedLogLevel, err := logrus.ParseLevel(conf.LogLevel)
	if err != nil {
		return err
	}
	logrus.SetLevel(parsedLogLevel)

	formatter, err := logging.GetLogFormatter(conf.LogFormat)
	if err != nil {
		return err
	}
	logrus.SetFormatter(formatter)

	// Configure locales and translations
	err = i18n.LoadLocales("./locales")
	if err != nil {
		return err
	}

	var rep replication.Replication
	if conf.Replication {

		// Wait for private network to become available
		// In some cloud environments, that may take several seconds
		networkAvailable := network.WaitForPrivateNetwork()
		if !networkAvailable {
			return fmt.Errorf("No private network is available within defined timeout")
		}

		// Configure and create the cluster module
		clConfig := &cluster.Config{
			BackendType: cluster.FilesystemBackend,
			Directory:   conf.ClusterDirectory,
			Size:        conf.ClusterSize,
		}
		cl, err := cluster.New(clConfig)
		if err != nil {
			return fmt.Errorf("Failed to create the cluster module: %s", err)
		}

		// Configure and create the replication module
		self := cluster.NewMember(network.GetPrivateIP(), conf.ReplicationPort)
		repConfig := &replication.Config{
			Membership:  cl.Membership(),
			Registrator: cl.Registrator(self),
		}
		rep, err = replication.New(repConfig)
		if err != nil {
			return fmt.Errorf("Failed to create the replication module: %s", err)
		}
	}

	var authenticator auth.Authenticator
	if len(conf.AuthModes) > 0 {
		auths := make([]auth.Authenticator, len(conf.AuthModes))
		for i, mode := range conf.AuthModes {
			switch mode {
			case "trusted":
				auths[i] = auth.NewTrustedAuthenticator()
			case "jwt":
				jwtAuth, err := auth.NewJWTAuthenticator([]byte(conf.JWTSecret))
				if err != nil {
					return fmt.Errorf("Failed to create the authentication module: %s", err)
				}
				auths[i] = jwtAuth
			default:
				return fmt.Errorf("Failed to create the authentication module: unrecognized authentication mode '%s'", err)
			}
		}
		authenticator, err = auth.NewChainAuthenticator(auths)
		if err != nil {
			return err
		}
	} else {
		authenticator = auth.DefaultAuthenticator()
	}

	regConfig := &store.Config{
		DefaultTTL:        conf.DefaultTTL,
		MinimumTTL:        conf.MinTTL,
		MaximumTTL:        conf.MaxTTL,
		SyncWaitTime:      conf.SyncTimeout,
		NamespaceCapacity: conf.NamespaceCapacity,
	}
	reg := store.New(regConfig, rep)

	serverConfig := &api.Config{
		HTTPAddressSpec: fmt.Sprintf(":%d", conf.APIPort),
		Registry:        reg,
		Authenticator:   authenticator,
		RequireHTTPS:    conf.RequireHTTPS,
	}
	server, err := api.NewServer(serverConfig)
	if err != nil {
		return err
	}

	go metrics.DumpPeriodically()

	return server.Start()
}