Esempio n. 1
0
func checkIn(controller clients.Controller, conf *config.Config) error {

	confNotValidErr := conf.Validate(true)

	creds, err := getCredentials(controller)
	if err != nil {
		// if id not found error
		if _, ok := err.(*clients.TenantNotFoundError); ok {
			logrus.Info("ID not found, registering with controller")
			err = registerWithProxy(controller, confNotValidErr)
			if err != nil {
				// tenant already exists, possible race condition in container group
				if _, ok = err.(*clients.ConflictError); ok {
					logrus.Warn("Possible race condition occurred during register")
					return nil
				}
				// unrecoverable error occurred registering with controller
				logrus.WithError(err).Error("Could not register with Controller")
				return err
			}

			// register succeeded
			return nil
		}
		// unrecoverable error occurred getting credentials from controller
		logrus.WithError(err).Error("Could not retrieve credentials")
		return err
	}

	if conf.ForceUpdate {
		// TODO
	}

	// if sidecar already has valid config do not need to set anything
	if confNotValidErr != nil {
		logrus.Info("Updating credentials with those from controller")
		conf.Kafka.APIKey = creds.Kafka.APIKey
		conf.Kafka.Brokers = creds.Kafka.Brokers
		conf.Kafka.Password = creds.Kafka.Password
		conf.Kafka.RestURL = creds.Kafka.RestURL
		conf.Kafka.SASL = creds.Kafka.SASL
		conf.Kafka.Username = creds.Kafka.User

		conf.Registry.Token = creds.Registry.Token
		conf.Registry.URL = creds.Registry.URL
	}
	return nil
}
Esempio n. 2
0
func sidecarMain(conf config.Config) error {
	var err error

	logrus.SetLevel(conf.LogLevel)

	if err = conf.Validate(false); err != nil {
		logrus.WithError(err).Error("Validation of config failed")
		return err
	}

	if conf.Log {
		//Replace the LOGSTASH_REPLACEME string in filebeat.yml with
		//the value provided by the user

		//TODO: Make this configurable
		filebeatConf := "/etc/filebeat/filebeat.yml"
		filebeat, err := ioutil.ReadFile(filebeatConf)
		if err != nil {
			logrus.WithError(err).Error("Could not read filebeat conf")
			return err
		}

		fileContents := strings.Replace(string(filebeat), "LOGSTASH_REPLACEME", conf.LogstashServer, -1)

		err = ioutil.WriteFile("/tmp/filebeat.yml", []byte(fileContents), 0)
		if err != nil {
			logrus.WithError(err).Error("Could not write filebeat conf")
			return err
		}

		// TODO: Log failure?
		go supervisor.DoLogManagement("/tmp/filebeat.yml")
	}

	if conf.Proxy {
		if err = startProxy(&conf); err != nil {
			logrus.WithError(err).Error("Could not start proxy")
		}
	}

	if conf.Register {
		if err = conf.Validate(true); err != nil {
			logrus.WithError(err).Error("Validation of config failed")
			return err
		}
		logrus.Info("Registering")

		registryClient, err := client.New(client.Config{
			URL:       conf.Registry.URL,
			AuthToken: conf.Registry.Token,
		})
		if err != nil {
			logrus.WithError(err).Error("Could not create registry client")
			return err
		}

		address := fmt.Sprintf("%v:%v", conf.EndpointHost, conf.EndpointPort)
		serviceInstance := &client.ServiceInstance{
			ServiceName: conf.ServiceName,
			Endpoint: client.ServiceEndpoint{
				Type:  conf.EndpointType,
				Value: address,
			},
			TTL: 60,
		}

		if conf.ServiceVersion != "" {
			data, err := json.Marshal(map[string]string{"version": conf.ServiceVersion})
			if err == nil {
				serviceInstance.Metadata = data
			} else {
				logrus.WithError(err).Warn("Could not marshal service version metadata")
			}
		}

		agent, err := register.NewRegistrationAgent(register.RegistrationConfig{
			Client:          registryClient,
			ServiceInstance: serviceInstance,
		})
		if err != nil {
			logrus.WithError(err).Error("Could not create registry agent")
			return err
		}

		agent.Start()
	}

	if conf.Supervise {
		supervisor.DoAppSupervision(conf.AppArgs)
	} else {
		select {}
	}

	return nil
}