<commit_before>package kontrol

import (
	"encoding/json"
	"errors"
	"fmt"
	"github.com/coreos/go-etcd/etcd"
	logging "github.com/op/go-logging"
	"koding/db/mongodb/modelhelper"
	"koding/newkite/dnode"
	"koding/newkite/kite"
	"koding/newkite/kodingkey"
	"koding/newkite/protocol"
	"koding/newkite/token"
	"koding/tools/config"
	stdlog "log"
	"net"
	"os"
	"strconv"
	"strings"
	"time"
)

const (
	HeartbeatInterval = 1 * time.Minute
	HeartbeatDelay    = 2 * time.Minute
	KitesPrefix       = "/kites"
)

var log = logging.MustGetLogger("Kontrol")

type Kontrol struct {
	kite       *kite.Kite
	etcd       *etcd.Client
	watcherHub *watcherHub
}

func New() *Kontrol {
	kiteOptions := &kite.Options{
		Kitename:    "kontrol",
		Version:     "1",
		Port:        strconv.Itoa(config.Current.NewKontrol.Port),
		Region:      "localhost",
		Environment: "development",
	}

	// Read list of etcd servers from config.
	machines := make([]string, len(config.Current.Etcd))
	for i, s := range config.Current.Etcd {
		machines[i] = "http://" + s.Host + ":" + strconv.FormatUint(uint64(s.Port), 10)
	}

	kontrol := &Kontrol{
		kite:       kite.New(kiteOptions),
		etcd:       etcd.NewClient(machines),
		watcherHub: newWatcherHub(),
	}

	kontrol.kite.KontrolEnabled = false // Because we are Kontrol!

	kontrol.kite.Authenticators["kodingKey"] = kontrol.AuthenticateFromKodingKey
	kontrol.kite.Authenticators["sessionID"] = kontrol.AuthenticateFromSessionID

	kontrol.kite.HandleFunc("register", kontrol.handleRegister)
	kontrol.kite.HandleFunc("getKites", kontrol.handleGetKites)

	return kontrol
}

func (k *Kontrol) Run() {
	k.init()
	k.kite.Run()
}

func (k *Kontrol) Start() {
	k.init()
	k.kite.Start()
}

// init does common operations of Run() and Start().
func (k *Kontrol) init() {
	setupLogging()
	go k.WatchEtcd()
}

func setupLogging() {
	log.Module = "Kontrol"
	logging.SetFormatter(logging.MustStringFormatter("%{level:-8s} ▶ %{message}"))
	stderrBackend := logging.NewLogBackend(os.Stderr, "", stdlog.LstdFlags|stdlog.Lshortfile)
	stderrBackend.Color = true
	syslogBackend, _ := logging.NewSyslogBackend(log.Module)
	logging.SetBackend(stderrBackend, syslogBackend)
}

// registerValue is the type of the value that is saved to etcd.
type registerValue struct {
	PublicIP  string
	Port      string
	KodingKey string
}

func (k *Kontrol) handleRegister(r *kite.Request) (interface{}, error) {
	log.Info("Register request from: %#v", r.RemoteKite.Kite)

	// Only accept requests with kodingKey because we need this info
	// for generating tokens for this kite.
	if r.Authentication.Type != "kodingKey" {
		return nil, fmt.Errorf("Unexpected authentication type: %s", r.Authentication.Type)
	}

	// Set PublicIP address if it's empty.
	if r.RemoteKite.PublicIP == "" {
		r.RemoteKite.PublicIP, _, _ = net.SplitHostPort(r.RemoteAddr)
	}

	return k.register(r.RemoteKite, r.Authentication.Key)
}

func (k *Kontrol) register(r *kite.RemoteKite, kodingkey string) (*protocol.RegisterResult, error) {
	kite := &r.Kite

	key, err := getKiteKey(kite)
	if err != nil {<commit_msg>kite: update kontrol to use the logger from kite package<commit_after>package kontrol

import (
	"encoding/json"
	"errors"
	"fmt"
	"github.com/coreos/go-etcd/etcd"
	"github.com/op/go-logging"
	"koding/db/mongodb/modelhelper"
	"koding/newkite/dnode"
	"koding/newkite/kite"
	"koding/newkite/kodingkey"
	"koding/newkite/protocol"
	"koding/newkite/token"
	"koding/tools/config"
	"net"
	"strconv"
	"strings"
	"time"
)

const (
	HeartbeatInterval = 1 * time.Minute
	HeartbeatDelay    = 2 * time.Minute
	KitesPrefix       = "/kites"
)

var log *logging.Logger

type Kontrol struct {
	kite       *kite.Kite
	etcd       *etcd.Client
	watcherHub *watcherHub
}

func New() *Kontrol {
	kiteOptions := &kite.Options{
		Kitename:    "kontrol",
		Version:     "1",
		Port:        strconv.Itoa(config.Current.NewKontrol.Port),
		Region:      "localhost",
		Environment: "development",
	}

	// Read list of etcd servers from config.
	machines := make([]string, len(config.Current.Etcd))
	for i, s := range config.Current.Etcd {
		machines[i] = "http://" + s.Host + ":" + strconv.FormatUint(uint64(s.Port), 10)
	}

	kontrol := &Kontrol{
		kite:       kite.New(kiteOptions),
		etcd:       etcd.NewClient(machines),
		watcherHub: newWatcherHub(),
	}

	log = kontrol.kite.Log

	kontrol.kite.KontrolEnabled = false // Because we are Kontrol!

	kontrol.kite.Authenticators["kodingKey"] = kontrol.AuthenticateFromKodingKey
	kontrol.kite.Authenticators["sessionID"] = kontrol.AuthenticateFromSessionID

	kontrol.kite.HandleFunc("register", kontrol.handleRegister)
	kontrol.kite.HandleFunc("getKites", kontrol.handleGetKites)

	return kontrol
}

func (k *Kontrol) Run() {
	k.init()
	k.kite.Run()
}

func (k *Kontrol) Start() {
	k.init()
	k.kite.Start()
}

// init does common operations of Run() and Start().
func (k *Kontrol) init() {
	go k.WatchEtcd()
}

// registerValue is the type of the value that is saved to etcd.
type registerValue struct {
	PublicIP  string
	Port      string
	KodingKey string
}

func (k *Kontrol) handleRegister(r *kite.Request) (interface{}, error) {
	log.Info("Register request from: %#v", r.RemoteKite.Kite)

	// Only accept requests with kodingKey because we need this info
	// for generating tokens for this kite.
	if r.Authentication.Type != "kodingKey" {
		return nil, fmt.Errorf("Unexpected authentication type: %s", r.Authentication.Type)
	}

	// Set PublicIP address if it's empty.
	if r.RemoteKite.PublicIP == "" {
		r.RemoteKite.PublicIP, _, _ = net.SplitHostPort(r.RemoteAddr)
	}

	return k.register(r.RemoteKite, r.Authentication.Key)
}

func (k *Kontrol) register(r *kite.RemoteKite, kodingkey string) (*protocol.RegisterResult, error) {
	kite := &r.Kite

	key, err := getKiteKey(kite)
	if err != nil {
{'fraction_non_alphanumeric': 0.13940231473484194, 'fraction_numerical': 0.0025911210917256866, 'mean_word_length': 4.039164490861618, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2596241', 'n_tokens_mistral': 2255, 'n_tokens_neox': 2152, 'n_words': 460}