Example #1
0
File: config.go Project: mc0/okq
func init() {
	l := lever.New("okq", nil)
	l.Add(lever.Param{
		Name:        "--listen-addr",
		Description: "Address to listen for client connections on",
		Default:     ":4777",
	})
	l.Add(lever.Param{
		Name:        "--redis-addr",
		Description: "Address redis is listening on",
		Default:     "127.0.0.1:6379",
	})
	l.Add(lever.Param{
		Name:        "--redis-sentinel-addr",
		Description: "A sentinel address to connect to through to the client - overrides other options",
	})
	l.Add(lever.Param{
		Name:        "--redis-cluster",
		Description: "Whether or not to treat the redis address as a node in a larger cluster",
		Flag:        true,
	})
	l.Add(lever.Param{
		Name:        "--redis-sentinel-group",
		Description: "A redis sentinel group name for selecting which redis masters to connect",
		Default:     "master",
	})
	l.Add(lever.Param{
		Name:        "--redis-pool-size",
		Description: "Number of connections to make to redis. If cluster or sentinel is being used this many connections will be made to *each* instance involved",
		Default:     "50",
	})
	l.Add(lever.Param{
		Name:        "--debug",
		Aliases:     []string{"-d"},
		Description: "Turn on debug logging",
		Flag:        true,
	})
	l.Add(lever.Param{
		Name:        "--bg-push-pool-size",
		Description: "Number of goroutines to have processing NOBLOCK Q*PUSH commands",
		Default:     "128",
	})
	l.Add(lever.Param{
		Name:        "--cpu-profile",
		Description: "Name of a file to write a cpu profile out to. If set the cpu profile will be written until okq is closed",
	})
	l.Parse()

	ListenAddr, _ = l.ParamStr("--listen-addr")
	RedisAddr, _ = l.ParamStr("--redis-addr")
	RedisCluster = l.ParamFlag("--redis-cluster")
	RedisSentinels, RedisSentinel = l.ParamStrs("--redis-sentinel-addr")
	RedisSentinelGroup, _ = l.ParamStr("--redis-sentinel-group")
	RedisPoolSize, _ = l.ParamInt("--redis-pool-size")
	Debug = l.ParamFlag("--debug")
	BGPushPoolSize, _ = l.ParamInt("--bg-push-pool-size")
	CPUProfile, _ = l.ParamStr("--cpu-profile")
}
Example #2
0
func init() {
	l := lever.New("locksmith", nil)
	l.Add(lever.Param{
		Name:        "--internal-addr",
		Description: "Address to listen on for the internal api",
		Default:     ":8889",
	})
	l.Add(lever.Param{
		Name:        "--key",
		Description: "HMAC key for incoming requests",
		Default:     "",
	})
	l.Add(lever.Param{
		Name:        "--ovpn-template",
		Description: "Template file for ovpn output",
		Default:     "./ovpn.template",
	})
	l.Add(lever.Param{
		Name:        "--ca-file",
		Description: "CA file (if you have a bundle, this should be the bundle)",
		Default:     "",
	})
	l.Add(lever.Param{
		Name:        "--cfssl-addr",
		Description: "Address to cfssl server",
		Default:     "127.0.0.1:8888",
	})
	l.Add(lever.Param{
		Name:        "--cfssl-auth",
		Description: "Auth Key to create certificates in cfssl",
		Default:     "",
	})
	l.Add(lever.Param{
		Name:        "--default-name-file",
		Description: "Default name params in a json file",
		Default:     "",
	})
	l.Add(lever.Param{
		Name:        "--timestamp-drift",
		Description: "Maximum allowed timestamp drift in seconds (0 to disable checking)",
		Default:     "10",
	})
	l.Parse()

	InternalAPIAddr, _ = l.ParamStr("--internal-addr")
	k, _ := l.ParamStr("--key")
	HMACKey = []byte(k)
	OVPNTemplateFile, _ = l.ParamStr("--ovpn-template")
	CAFile, _ = l.ParamStr("--ca-file")
	CFSSLAddr, _ = l.ParamStr("--cfssl-addr")
	CFSSLKey, _ = l.ParamStr("--cfssl-auth")
	DefaultNameFile, _ = l.ParamStr("--default-name-file")
	td, _ := l.ParamInt("--timestamp-drift")
	TimestampDrift = float64(td)
}
Example #3
0
func init() {
	l := lever.New("thumper", nil)
	l.Add(lever.Param{
		Name:        "--alerts",
		Aliases:     []string{"-a"},
		Description: "Required. A yaml file, or directory with yaml files, containing alert definitions",
	})
	l.Add(lever.Param{
		Name:        "--elasticsearch-addr",
		Description: "Address to find an elasticsearch instance on",
		Default:     "127.0.0.1:9200",
	})
	l.Add(lever.Param{
		Name:        "--lua-init",
		Description: "If set the given lua script will be executed at the initialization of every lua vm",
	})
	l.Add(lever.Param{
		Name:        "--lua-vms",
		Description: "How many lua vms should be used. Each vm is completely independent of the other, and requests are executed on whatever vm is available at that moment. Allows lua scripts to not all be blocked on the same os thread",
		Default:     "1",
	})
	l.Add(lever.Param{
		Name:        "--pagerduty-key",
		Description: "PagerDuty api key, required if using any pagerduty actions",
	})
	l.Add(lever.Param{
		Name:        "--force-run",
		Description: "If set with the name of an alert, will immediately run that alert and exit. Useful for testing changes to alert definitions",
	})
	l.Add(lever.Param{
		Name:        "--log-level",
		Description: "Adjust the log level. Valid options are: error, warn, info, debug",
		Default:     "info",
	})
	l.Parse()

	AlertFileDir, _ = l.ParamStr("--alerts")
	ElasticSearchAddr, _ = l.ParamStr("--elasticsearch-addr")
	LuaInit, _ = l.ParamStr("--lua-init")
	LuaVMs, _ = l.ParamInt("--lua-vms")
	LogLevel, _ = l.ParamStr("--log-level")
	PagerDutyKey, _ = l.ParamStr("--pagerduty-key")
	ForceRun, _ = l.ParamStr("--force-run")
	llog.SetLevelFromString(LogLevel)
}
Example #4
0
func main() {
	l := lever.New("holdingpattern", nil)
	l.Add(lever.Param{
		Name:        "--api",
		Description: "Address skyapi is listening on",
		Default:     "127.0.0.1:8053",
	})
	l.Add(lever.Param{
		Name:        "--hostname",
		Description: "Hostname to advertise",
	})
	l.Add(lever.Param{
		Name:        "--category",
		Description: "Category to advertise under. If unset the skyapi instance's global default will be used",
	})
	l.Add(lever.Param{
		Name:        "--addr",
		Description: "Address and port to advertise",
		Default:     "",
	})
	l.Add(lever.Param{
		Name:        "--weight",
		Description: "Weight to advertise",
		Default:     "100",
	})
	l.Add(lever.Param{
		Name:        "--priority",
		Description: "Priority to advertise",
		Default:     "1",
	})
	l.Add(lever.Param{
		Name:        "--prefix",
		Description: "Prefix to pass to skyapi. This will be prefixed to the unique id that is actually stored by skyapi",
	})
	l.Parse()

	apiAddr, _ := l.ParamStr("--api")
	hostname, _ := l.ParamStr("--hostname")
	category, _ := l.ParamStr("--category")
	addr, _ := l.ParamStr("--addr")
	weight, _ := l.ParamInt("--weight")
	priority, _ := l.ParamInt("--priority")
	prefix, _ := l.ParamStr("--prefix")

	argsFound := 0
	var argHost string
	var argAddr string
	for _, v := range l.ParamRest() {
		if strings.HasPrefix(v, "-") {
			continue
		}
		switch argsFound {
		case 0:
			argHost = v
		case 1:
			argAddr = v
		}
		argsFound++
	}

	if hostname == "" {
		if argHost != "" {
			hostname = argHost
			argHost = "" //reset so addr doesn't pick it up
		} else {
			llog.Fatal("no hostname sent")
		}
	}

	if addr == "" {
		// allow them to run ./holdingpattern --host=test 127.0.0.1:8000
		if argHost != "" {
			addr = argHost
		} else if argAddr != "" {
			addr = argAddr
		} else {
			llog.Fatal("no address sent")
		}
	}

	hostcat := hostname
	if category != "" {
		hostcat = hostname + "." + category
	}

	kv := llog.KV{
		"apiAddr":  apiAddr,
		"host":     hostcat,
		"thisAddr": addr,
		"priority": priority,
		"weight":   weight,
		"prefix":   prefix,
	}
	for {
		llog.Info("advertising", kv)
		err := client.ProvideOpts(client.Opts{
			SkyAPIAddr:        apiAddr,
			Service:           hostname,
			ThisAddr:          addr,
			Category:          category,
			Priority:          priority,
			Weight:            weight,
			Prefix:            prefix,
			ReconnectAttempts: 0, // do not attempt to reconnect, we'll do that here
		})
		if err != nil {
			llog.Warn("skyapi error", kv, llog.ErrKV(err))
		}
		time.Sleep(1 * time.Second)
	}
}
Example #5
0
func main() {
	l := lever.New("struggledns", nil)
	l.Add(lever.Param{
		Name:        "--listen-addr",
		Description: "Address to listen on for dns requests. Will bind to both tcp and udp",
		Default:     ":53",
	})
	l.Add(lever.Param{
		Name:        "--fwd-to",
		Description: "Address (ip:port) of a dns server to attempt forward requests to. Specify multiple times to make multiple request attempts. Order specified dictates precedence should more than one server respond for a request",
	})
	l.Add(lever.Param{
		Name:        "--parallel",
		Description: "If sent the query will be sent to all addresses in parallel",
		Flag:        true,
	})
	l.Add(lever.Param{
		Name:        "--timeout",
		Description: "Timeout in milliseconds for each request",
		Default:     "300",
	})
	l.Add(lever.Param{
		Name:        "--log-level",
		Description: "Minimum log level to show, either debug, info, warn, error, or fatal",
		Default:     "warn",
	})
	l.Add(lever.Param{
		Name:        "--allow-truncated",
		Description: "If we should allow truncated responses to be proxied",
		Flag:        true,
	})
	if version != "" {
		l.Add(lever.Param{
			Name:        "--version",
			Aliases:     []string{"-v"},
			Description: "Print version info",
			Flag:        true,
		})
	}
	l.Parse()

	if l.ParamFlag("--version") {
		fmt.Println(version)
		return
	}

	addr, _ := l.ParamStr("--listen-addr")
	dnsServers, _ := l.ParamStrs("--fwd-to")
	combineGroups := l.ParamFlag("--parallel")
	timeout, _ := l.ParamInt("--timeout")

	logLevel, _ := l.ParamStr("--log-level")
	llog.SetLevelFromString(logLevel)

	allowTruncated = l.ParamFlag("--allow-truncated")

	if combineGroups {
		//combine all the servers sent into one group
		dnsServerGroups = make([][]string, 1)
		var groupServers []string
		for i := range dnsServers {
			groupServers = strings.Split(dnsServers[i], ",")
			dnsServerGroups[0] = append(dnsServerGroups[0], groupServers...)
		}
	} else {
		dnsServerGroups = make([][]string, len(dnsServers))
		for i := range dnsServers {
			dnsServerGroups[i] = strings.Split(dnsServers[i], ",")
		}
	}

	client = dns.Client{
		//since this is UDP, the Dial/Write timeouts don't mean much
		//we really only care about setting the read
		DialTimeout:  time.Millisecond * 100,
		WriteTimeout: time.Millisecond * 100,
		ReadTimeout:  time.Millisecond * time.Duration(timeout),
		UDPSize:      4096,
	}

	handler := dns.HandlerFunc(handleRequest)
	go func() {
		llog.Info("listening on udp", llog.KV{"addr": addr})
		err := dns.ListenAndServe(addr, "udp", handler)
		llog.Fatal("error listening on udp", llog.KV{"err": err})
	}()
	go func() {
		llog.Info("listening on tcp", llog.KV{"addr": addr})
		err := dns.ListenAndServe(addr, "tcp", handler)
		llog.Fatal("error listening on tcp", llog.KV{"err": err})
	}()

	select {}
}
Example #6
0
func main() {
	l := lever.New("teamcity-latest", nil)
	l.Add(lever.Param{
		Name:        "--rest-user",
		Description: "Username to authenticate to the rest api as",
	})
	l.Add(lever.Param{
		Name:        "--rest-pass",
		Description: "Password to authenticate to the rest api with",
	})
	l.Add(lever.Param{
		Name:        "--rest-addr",
		Description: "Address the rest api is listening on",
		Default:     "http://localhost:8111",
	})
	l.Add(lever.Param{
		Name:        "--listen-addr",
		Description: "Address to listen for requests on",
		Default:     ":8112",
	})
	l.Add(lever.Param{
		Name:        "--skyapi-addr",
		Description: "Hostname of skyapi, to be looked up via a SRV request. Unset means don't register with skyapi",
	})
	l.Add(lever.Param{
		Name:        "--log-level",
		Description: "Minimum log level to show, either debug, info, warn, error, or fatal",
		Default:     "info",
	})
	l.Parse()

	restUser, _ = l.ParamStr("--rest-user")
	restPass, _ = l.ParamStr("--rest-pass")
	restAddr, _ = l.ParamStr("--rest-addr")
	listenAddr, _ = l.ParamStr("--listen-addr")
	skyapiAddr, _ = l.ParamStr("--skyapi-addr")

	logLevel, _ := l.ParamStr("--log-level")
	llog.SetLevelFromString(logLevel)

	if skyapiAddr != "" {
		actualSkyapiAddr, err := srvclient.SRV(skyapiAddr)
		if err != nil {
			llog.Fatal("couldn't look up skyapi address", llog.KV{"skyapiAddr": skyapiAddr, "err": err})
		}

		go func() {
			err := client.Provide(
				actualSkyapiAddr, "teamcity-latest", listenAddr, 1, 100,
				3, 15*time.Second,
			)
			llog.Fatal("skapi client failed", llog.KV{"skyapiAddr": actualSkyapiAddr, "err": err})
		}()
	}

	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		var req requestParams
		req.ip = r.RemoteAddr

		parts := strings.Split(r.URL.Path[1:], "/")
		if len(parts) < 2 {
			req.log(llog.Warn, "invalid url, not enough parts", llog.KV{"url": r.URL.Path})
			http.Error(w, "invalid url, must be /buildTypeID/[tag]/artifactName", 400)
			return
		}
		req.buildTypeID = parts[0]
		if len(parts) == 3 {
			req.tag = parts[1]
			req.artifactName = parts[2]
		} else {
			req.artifactName = parts[1]
		}

		if req.buildTypeID == "" || req.artifactName == "" {
			req.log(llog.Warn, "invalid url, empty parts", llog.KV{"url": r.URL.Path})
			http.Error(w, "invalid url, must be /buildTypeID/[tag]/artifactName", 400)
			return
		}

		req.log(llog.Info, "request")

		var err error
		req.latestBuildID, err = latestBuildID(req.buildTypeID, req.tag)
		if err != nil {
			req.log(llog.Error, "couldn't get last build id", llog.KV{"err": err})
			http.Error(w, err.Error(), 500)
			return
		}

		if remoteHash := r.Header.Get("If-None-Match"); remoteHash != "" {
			tcHash, err := artifactHash(req.latestBuildID, req.artifactName)
			if err != nil {
				req.log(llog.Error, "couldn't check hash", llog.KV{"err": err})
				http.Error(w, fmt.Sprintf("Could not check hash: %s", err), 500)
				return
			}
			if tcHash == remoteHash {
				req.log(llog.Info, "hashes match, not retrieving")
				w.WriteHeader(304)
				return
			}
		}

		rc, contentLen, err := buildDownload(req.latestBuildID, req.artifactName)
		if err != nil {
			req.log(llog.Info, "couldn't get build download", llog.KV{"err": err})
			http.Error(w, err.Error(), 500)
			return
		}
		defer rc.Close()

		w.Header().Set("Content-Length", strconv.FormatInt(contentLen, 10))
		io.Copy(w, rc)
	})

	llog.Info("listening", llog.KV{"addr": listenAddr})
	err := http.ListenAndServe(listenAddr, nil)
	llog.Fatal("error listening", llog.KV{"err": err})
}