Example #1
0
func main() {
	brokerConfigPath := configPath()

	brokerLogger := lager.NewLogger("redis-broker")
	brokerLogger.RegisterSink(lager.NewWriterSink(os.Stdout, lager.DEBUG))
	brokerLogger.RegisterSink(lager.NewWriterSink(os.Stderr, lager.ERROR))

	brokerLogger.Info("Starting CF Redis broker")

	brokerLogger.Info("Config File: " + brokerConfigPath)

	config, err := brokerconfig.ParseConfig(brokerConfigPath)
	if err != nil {
		brokerLogger.Fatal("Loading config file", err, lager.Data{
			"broker-config-path": brokerConfigPath,
		})
	}

	commandRunner := system.OSCommandRunner{
		Logger: brokerLogger,
	}

	localRepo := redis.NewLocalRepository(config.RedisConfiguration, brokerLogger)

	localRepo.AllInstancesVerbose()

	processController := &redis.OSProcessController{
		CommandRunner:            commandRunner,
		InstanceInformer:         localRepo,
		Logger:                   brokerLogger,
		ProcessChecker:           &process.ProcessChecker{},
		ProcessKiller:            &process.ProcessKiller{},
		PingFunc:                 redis.PingServer,
		WaitUntilConnectableFunc: availability.Check,
	}

	localCreator := &redis.LocalInstanceCreator{
		FindFreePort:            system.FindFreePort,
		RedisConfiguration:      config.RedisConfiguration,
		ProcessController:       processController,
		LocalInstanceRepository: localRepo,
	}

	agentClient := &redis.RemoteAgentClient{
		HttpAuth: config.AuthConfiguration,
	}
	remoteRepo, err := redis.NewRemoteRepository(agentClient, config, brokerLogger)
	if err != nil {
		brokerLogger.Fatal("Error initializing remote repository", err)
	}

	sigChannel := make(chan os.Signal, 1)
	signal.Notify(sigChannel, syscall.SIGTERM)
	go func() {
		<-sigChannel
		brokerLogger.Info("Starting Redis Broker shutdown")
		localRepo.AllInstancesVerbose()
		remoteRepo.StateFromFile()
		os.Exit(0)
	}()

	serviceBroker := &broker.RedisServiceBroker{
		InstanceCreators: map[string]broker.InstanceCreator{
			"shared":    localCreator,
			"dedicated": remoteRepo,
		},
		InstanceBinders: map[string]broker.InstanceBinder{
			"shared":    localRepo,
			"dedicated": remoteRepo,
		},
		Config: config,
	}

	brokerCredentials := brokerapi.BrokerCredentials{
		Username: config.AuthConfiguration.Username,
		Password: config.AuthConfiguration.Password,
	}

	brokerAPI := brokerapi.New(serviceBroker, brokerLogger, brokerCredentials)

	authWrapper := auth.NewWrapper(brokerCredentials.Username, brokerCredentials.Password)
	debugHandler := authWrapper.WrapFunc(debug.NewHandler(remoteRepo))
	instanceHandler := authWrapper.WrapFunc(redisinstance.NewHandler(remoteRepo))

	http.HandleFunc("/instance", instanceHandler)
	http.HandleFunc("/debug", debugHandler)
	http.Handle("/", brokerAPI)

	brokerLogger.Fatal("http-listen", http.ListenAndServe(config.Host+":"+config.Port, nil))
}
Example #2
0
func main() {
	logger := lager.NewLogger("process-monitor")
	logger.RegisterSink(lager.NewWriterSink(os.Stdout, lager.DEBUG))
	logger.RegisterSink(lager.NewWriterSink(os.Stderr, lager.ERROR))

	sigChannel := make(chan os.Signal, 1)
	signal.Notify(sigChannel, syscall.SIGUSR1)
	skipProcessCheck := false
	go func() {
		<-sigChannel
		logger.Info("Trapped USR1, disabling process monitor")
		skipProcessCheck = true
	}()

	config, err := brokerconfig.ParseConfig(configPath())
	if err != nil {
		logger.Fatal("could not parse config file", err, lager.Data{
			"config-path": configPath(),
		})
	}

	logger.Info("Starting process monitor")

	repo := redis.NewLocalRepository(config.RedisConfiguration, logger)

	commandRunner := system.OSCommandRunner{
		Logger: logger,
	}

	processController := &redis.OSProcessController{
		Logger:                   logger,
		InstanceInformer:         repo,
		CommandRunner:            commandRunner,
		ProcessChecker:           &process.ProcessChecker{},
		ProcessKiller:            &process.ProcessKiller{},
		PingFunc:                 redis.PingServer,
		WaitUntilConnectableFunc: availability.Check,
	}

	checkInterval := config.RedisConfiguration.ProcessCheckIntervalSeconds

	instances, _ := repo.AllInstancesVerbose()

	for _, instance := range instances {
		copyConfigFile(instance, repo, logger)
	}

	for {
		if skipProcessCheck {
			logger.Info("Skipping instance check")
		} else {
			instances, _ := repo.AllInstances()

			for _, instance := range instances {
				ensureRunningIfNotLocked(instance, repo, processController, logger)
			}
		}

		time.Sleep(time.Second * time.Duration(checkInterval))
	}
}
		// set up default conf file
		file, createFileErr := os.Create(defaultConfigFilePath)
		Ω(createFileErr).ToNot(HaveOccurred())

		_, fileWriteErr := file.WriteString(defaultConfigFileContents)
		Ω(fileWriteErr).ToNot(HaveOccurred())

		redisConf := brokerconfig.ServiceConfiguration{
			Host:                  "127.0.0.1",
			DefaultConfigPath:     "/tmp/default_config_path",
			InstanceDataDirectory: tmpInstanceDataDir,
			PidfileDirectory:      tmpPidFileDir,
			InstanceLogDirectory:  tmpInstanceLogDir,
		}

		repo = redis.NewLocalRepository(redisConf, logger)

		err := os.MkdirAll(tmpInstanceDataDir, 0755)
		Ω(err).ToNot(HaveOccurred())

		err = os.MkdirAll(tmpPidFileDir, 0755)
		Ω(err).ToNot(HaveOccurred())

		err = os.MkdirAll(tmpInstanceLogDir, 0755)
		Ω(err).ToNot(HaveOccurred())
	})

	AfterEach(func() {
		err := os.RemoveAll(tmpInstanceDataDir)
		Ω(err).ToNot(HaveOccurred())