Ejemplo n.º 1
0
// NewServerKite creates a server kite for the given server.
func NewServerKite(s *Server, name, version string) (*kite.Kite, error) {
	k := kite.New(name, version)

	if s.opts.Config == nil {
		cfg, err := config.Get()
		if err != nil {
			return nil, err
		}
		s.opts.Config = cfg
	}

	s.opts.Config.KontrolURL = s.opts.kontrolURL()
	s.opts.Config.Transport = config.XHRPolling

	if s.opts.Port != 0 {
		s.opts.Config.Port = s.opts.Port
	}
	if s.opts.Region != "" {
		s.opts.Config.Region = s.opts.Region
	}
	if s.opts.Environment != "" {
		s.opts.Config.Environment = s.opts.Environment
	}
	if s.opts.Test {
		s.opts.Config.DisableAuthentication = true
	}
	if s.opts.Debug {
		k.SetLogLevel(kite.DEBUG)
	}

	k.Log = s.opts.Log
	k.Config = s.opts.Config
	k.ClientFunc = httputil.ClientFunc(s.opts.Debug)

	if fn := s.metricsFunc(); fn != nil {
		k.PreHandleFunc(fn)
	}

	k.HandleFunc("register", s.Register)
	k.HandleFunc("registerServices", s.RegisterServices)
	k.HandleHTTPFunc("/healthCheck", artifact.HealthCheckHandler(name))
	k.HandleHTTPFunc("/version", artifact.VersionHandler())

	// Tunnel helper methods, like ports, stats etc.
	k.HandleHTTPFunc("/-/discover/{service}", s.discoverHandler())

	// Route all the rest requests (match all paths that does not begin with /-/).
	k.HandleHTTP(`/{rest:.?$|[^\/].+|\/[^-].+|\/-[^\/].*}`, s.serverHandler())

	u, err := url.Parse(s.opts.registerURL())
	if err != nil {
		return nil, fmt.Errorf("error parsing registerURL: %s", err)
	}

	if err := k.RegisterForever(u); err != nil {
		return nil, fmt.Errorf("error registering to Kontrol: %s", err)
	}

	return k, nil
}
Ejemplo n.º 2
0
func newConfig() error {
	var err error
	config, err = kiteconfig.Get()
	if err != nil {
		return err
	}

	config.Environment = Environment
	config.Region = Region

	return nil
}
Ejemplo n.º 3
0
// NewKite creates a new kite for serving terraformer
func NewKite(t *Terraformer, conf *Config) (*kite.Kite, error) {
	var err error
	k := kite.New(Name, Version)
	k.Config, err = kiteconfig.Get()
	if err != nil {
		return nil, err
	}

	k = setupKite(k, conf)

	// handle current status of terraformer
	k.PostHandleFunc(t.handleState)

	// track every kind of call
	k.PreHandleFunc(createTracker(t.Metrics))

	// Terraformer handling methods
	k.HandleFunc("apply", t.Apply)
	k.HandleFunc("destroy", t.Destroy)
	k.HandleFunc("plan", t.Plan)

	// artifact handling
	k.HandleHTTPFunc("/healthCheck", artifact.HealthCheckHandler(Name))
	k.HandleHTTPFunc("/version", artifact.VersionHandler())

	secretKey := conf.SecretKey

	// allow kloud to make calls to us
	k.Authenticators["kloud"] = func(r *kite.Request) error {
		if r.Auth.Key != secretKey {
			return errors.New("wrong secret key passed, you are not authenticated")
		}
		return nil
	}

	return k, nil
}
Ejemplo n.º 4
0
func (k *Konfig) buildKiteConfig() *konfig.Config {
	if k.KiteKey != "" {
		tok, err := jwt.ParseWithClaims(k.KiteKey, &kitekey.KiteClaims{}, kitekey.GetKontrolKey)
		if err == nil {
			cfg := &konfig.Config{}

			if err = cfg.ReadToken(tok); err == nil {
				return cfg
			}
		}
	}

	if k.KiteKeyFile != "" {
		if cfg, err := konfig.NewFromKiteKey(k.KiteKeyFile); err == nil {
			return cfg
		}
	}

	if cfg, err := konfig.Get(); err == nil {
		return cfg
	}

	return konfig.New()
}
Ejemplo n.º 5
0
func (bk *balancedKlients) init() error {
	bk.mu.Lock()
	defer bk.mu.Unlock()

	k := kite.New("kloudctl", "0.0.1")
	c, err := config.Get()
	if err != nil {
		return err
	}

	k.Config = c
	k.Config.KontrolURL = flagKontrolURL
	k.Config.Transport = config.XHRPolling
	if flagDebug {
		k.SetLogLevel(kite.DEBUG)
	} else {
		k.SetLogLevel(kite.WARNING)
	}

	// use production environment by default
	if c.Environment == "" || c.Environment == "unknown" {
		c.Environment = "production"
	}

	query := &protocol.KontrolQuery{
		Name:        "kloud",
		Environment: c.Environment,
	}

	// Try up to three times.
	//
	// TODO(rjeczalik): make the GetKites timeout configurable.
	var klients []*kite.Client
	for i := 0; i < 3; i++ {
		klients, err = k.GetKites(query)
		if err == nil {
			break
		}

		k.Log.Debug("GetKites(%+v) failed: %s", query, err)
		time.Sleep(2 * time.Second)
	}
	if err != nil {
		return err
	}

	uniq := make(map[string]struct{})

	// in production environment klouds are balanced behind
	// https://kontrol.com/kloud/kite, however get the uniq URLs
	// in case we're running in different env
	for i, klient := range klients {
		k.Log.Debug("kloud[%d] = %s", i, klient.Kite.Hostname)
		uniq[klient.URL] = struct{}{}
	}

	delete(uniq, "")

	klouds := make([]string, 0, len(uniq))
	for url := range uniq {
		klouds = append(klouds, url)
	}

	bk.kite = k
	bk.klouds = klouds
	bk.failCnt = make([]int32, len(klouds))

	return nil
}