Example #1
0
func start(ctx *cli.Context) {
	if ctx.Bool("profile") {
		defer profile.Start(profile.CPUProfile).Stop()
		defer profile.Start(profile.MemProfile).Stop()
		defer profile.Start(profile.BlockProfile).Stop()
		defer profile.Start(profile.ProfilePath("."))
	}
	quit := make(chan bool)
	initLogrus(ctx)
	log.Info("Starting fullerite...")

	c, err := config.ReadConfig(ctx.String("config"))
	if err != nil {
		return
	}
	handlers := createHandlers(c)
	hook := NewLogErrorHook(handlers)
	log.Logger.Hooks.Add(hook)

	startHandlers(handlers)
	collectors := startCollectors(c)

	collectorStatChan := make(chan metric.CollectorEmission)

	internalServer := internalserver.New(c,
		handlerStatFunc(handlers),
		readCollectorStat(collectorStatChan))

	go internalServer.Run()

	readFromCollectors(collectors, handlers, collectorStatChan)

	<-quit
}
Example #2
0
// Main starts mc application
func Main() {
	// Enable profiling supported modes are [cpu, mem, block].
	// ``MC_PROFILER`` supported options are [cpu, mem, block].
	switch os.Getenv("MC_PROFILER") {
	case "cpu":
		defer profile.Start(profile.CPUProfile, profile.ProfilePath(mustGetProfileDir())).Stop()
	case "mem":
		defer profile.Start(profile.MemProfile, profile.ProfilePath(mustGetProfileDir())).Stop()
	case "block":
		defer profile.Start(profile.BlockProfile, profile.ProfilePath(mustGetProfileDir())).Stop()
	}

	probe.Init() // Set project's root source path.
	probe.SetAppInfo("Release-Tag", ReleaseTag)
	probe.SetAppInfo("Commit", ShortCommitID)

	app := registerApp()
	app.Before = registerBefore
	app.ExtraInfo = func() map[string]string {
		if _, e := pb.GetTerminalWidth(); e != nil {
			globalQuiet = true
		}
		if globalDebug {
			return getSystemData()
		}
		return make(map[string]string)
	}
	app.RunAndExitOnError()
}
Example #3
0
func (p PkgProfile) Start() ProfilerStart {
	if *FLAGS.PROFILE_MEM {
		PROFILE = profile.Start(profile.MemProfile, profile.ProfilePath("."))
	} else {
		PROFILE = profile.Start(profile.CPUProfile, profile.ProfilePath("."))
	}
	return PROFILE
}
Example #4
0
func main() {
	args.Bind = ":53"
	argParser := arg.MustParse(&args)

	// Enable profiling
	switch args.Profile {
	case "cpu":
		defer profile.Start(profile.CPUProfile).Stop()
	case "mem":
		defer profile.Start(profile.MemProfile).Stop()
	case "block":
		defer profile.Start(profile.BlockProfile).Stop()
	}

	// Control number of workers via GOMAXPROCS
	if args.Workers > 0 {
		runtime.GOMAXPROCS(args.Workers)
	}

	if len(args.Zones) == 0 {
		log.Println("must supply at least 1 zone file to serve")
		argParser.WriteUsage(os.Stderr)
		os.Exit(1)
	}

	var registry *realm.Registry
	registry = realm.NewRegistry()

	for _, filename := range args.Zones {
		// Load and parse the zone file
		var zone *realm.Zone
		var err error
		log.Printf("parsing zone file \"%s\"\n", filename)
		zone, err = realm.ParseZone(filename)
		if err != nil {
			log.Fatal(err)
		}
		registry.AddZone(zone)
	}

	// Create and start the server
	log.Printf("starting the server on \"%s\"\n", args.Bind)
	var server *realm.Server
	var err error
	server, err = realm.NewServer(args.Bind, registry, args.StatsD)
	if err != nil {
		log.Fatal(err)
	}

	log.Fatal(server.ListenAndServe())
}
Example #5
0
func Profile(mode string) Stop {
	var stop Stop
	switch mode {
	case "mem":
		stop = profileOnExit(profile.Start(profile.MemProfile, profile.ProfilePath("."), profile.NoShutdownHook))
	case "cpu":
		stop = profileOnExit(profile.Start(profile.CPUProfile, profile.ProfilePath("."), profile.NoShutdownHook))
	case "block":
		stop = profileOnExit(profile.Start(profile.BlockProfile, profile.ProfilePath("."), profile.NoShutdownHook))
	default:
		stop = stopper{}
	}
	return stop
}
Example #6
0
File: utils.go Project: tixu/mmjira
func activateProfiline(profileType string, dir string) {
	// activating
	switch profileType {
	case "cpu":
		defer profile.Start(profile.ProfilePath(dir), profile.CPUProfile).Stop()
	case "mem":
		defer profile.Start(profile.ProfilePath(dir), profile.MemProfile).Stop()
	case "block":
		defer profile.Start(profile.ProfilePath(dir), profile.BlockProfile).Stop()
	default:
		// do nothing
	}

}
Example #7
0
// Starts a profiler returns nil if profiler is not enabled, caller needs to handle this.
func startProfiler(profiler string) interface {
	Stop()
} {
	// Enable profiler if ``_MINIO_PROFILER`` is set. Supported options are [cpu, mem, block].
	switch profiler {
	case "cpu":
		return profile.Start(profile.CPUProfile, profile.NoShutdownHook)
	case "mem":
		return profile.Start(profile.MemProfile, profile.NoShutdownHook)
	case "block":
		return profile.Start(profile.BlockProfile, profile.NoShutdownHook)
	default:
		return nil
	}
}
Example #8
0
func ExampleStart_withFlags() {
	// use the flags package to selectively enable profiling.
	mode := flag.String("profile.mode", "", "enable profiling mode, one of [cpu, mem, block]")
	flag.Parse()
	switch *mode {
	case "cpu":
		defer profile.Start(profile.CPUProfile).Stop()
	case "mem":
		defer profile.Start(profile.MemProfile).Stop()
	case "block":
		defer profile.Start(profile.BlockProfile).Stop()
	default:
		// do nothing
	}
}
Example #9
0
func main() {
	flag.Parse()
	if *clients > *num {
		log.Println("# of clients can't be greater than # of keys written")
		return
	}
	runtime.GOMAXPROCS(*procs)
	if *profileEnable {
		defer profile.Start().Stop()
	}
	s := NewScrambled()
	value = make([]byte, *vsize)
	s.Read(value)
	perClient = *num / *clients
	perGroup := perClient / *groups
	if perGroup == 0 {
		log.Printf("Can't split %d writes across %d groups", perClient, *groups)
		log.Println("Need -num to be at least:", *clients**groups)
		return
	}

	log.Println("Using streaming api:", *streamTest)

	if *vsWriteTest || *vsReadTest {
		VSTests()
	}
	if *gsWriteTest || *gsReadTest {
		GSTests()
	}
	return
}
Example #10
0
func main() {
	r := rand.New(rand.NewSource(4))

	players := make([]*player, numPlayers)
	m := make(map[int]map[*player]bool)
	m[0] = make(map[*player]bool)
	for i := range players {
		players[i] = &player{
			skill: r.NormFloat64() * skillStdDev,
		}
		m[0][players[i]] = true
	}

	a := arena{
		r:              r,
		allPlayers:     players,
		playersByStars: m,
	}
	defer profile.Start().Stop()
	for i := 0; i < numPlayers*avgNumGamesPerPlayer; i++ {

		a.fight()
	}
	a.print()
}
func main() {

	thrust.InitLogger()
	// Set any Custom Provisioners before Start
	thrust.SetProvisioner(provisioner.NewSingleBinaryThrustProvisioner())
	// thrust.Start() must always come before any bindings are created.
	thrust.Start()

	thrustWindow := thrust.NewWindow(thrust.WindowOptions{
		RootUrl:  "http://breach.cc/",
		HasFrame: true,
	})
	thrustWindow.Show()
	thrustWindow.Focus()

	// Lets do a window timeout
	go func() {
		// <-time.After(time.Second * 5)
		// thrustWindow.Close()
		// thrust.Exit()
	}()

	// In lieu of something like an http server, we need to lock this thread
	// in order to keep it open, and keep the process running.
	// Dont worry we use runtime.Gosched :)
	defer profile.Start().Stop()
	thrust.LockThread()
}
Example #12
0
func main() {
	p := profile.Start(profile.MemProfile, profile.ProfilePath("."), profile.NoShutdownHook)
	// initialize representation
	index, _ = iindex.IndexDirectory("/home/rhibbitts/Dropbox/Notes/WorkNotes")
	// run  user interface
	ui()
	p.Stop()
}
Example #13
0
// Main is the entry-point of the guble server.
func Main() {
	defer func() {
		if p := recover(); p != nil {
			logger.Fatal("Fatal error in gubled after recover")
		}
	}()

	parseConfig()

	log.SetFormatter(&logformatter.LogstashFormatter{Env: *Config.EnvName})
	level, err := log.ParseLevel(*Config.Log)
	if err != nil {
		logger.WithError(err).Fatal("Invalid log level")
	}
	log.SetLevel(level)

	switch *Config.Profile {
	case cpuProfile:
		logger.Info("starting to profile cpu")
		defer profile.Start(profile.CPUProfile).Stop()
	case memProfile:
		logger.Info("starting to profile memory")
		defer profile.Start(profile.MemProfile).Stop()
	case blockProfile:
		logger.Info("starting to profile blocking/contention")
		defer profile.Start(profile.BlockProfile).Stop()
	default:
		logger.Debug("no profiling was started")
	}

	if err := ValidateStoragePath(); err != nil {
		logger.Fatal("Fatal error in gubled in validation of storage path")
	}

	srv := StartService()
	if srv == nil {
		logger.Fatal("exiting because of unrecoverable error(s) when starting the service")
	}

	waitForTermination(func() {
		err := srv.Stop()
		if err != nil {
			logger.WithField("error", err.Error()).Error("errors occurred while stopping service")
		}
	})
}
Example #14
0
// Starts a profiler returns nil if profiler is not enabled, caller needs to handle this.
func startProfiler(profiler string) interface {
	Stop()
} {
	// Set ``MINIO_PROFILE_DIR`` to the directory where profiling information should be persisted
	profileDir := os.Getenv("MINIO_PROFILE_DIR")
	// Enable profiler if ``MINIO_PROFILER`` is set. Supported options are [cpu, mem, block].
	switch profiler {
	case "cpu":
		return profile.Start(profile.CPUProfile, profile.NoShutdownHook, profile.ProfilePath(profileDir))
	case "mem":
		return profile.Start(profile.MemProfile, profile.NoShutdownHook, profile.ProfilePath(profileDir))
	case "block":
		return profile.Start(profile.BlockProfile, profile.NoShutdownHook, profile.ProfilePath(profileDir))
	default:
		return nil
	}
}
Example #15
0
func main() {
	p := profile.Start(
		profile.MemProfile,
		profile.ProfilePath("."))
	defer p.Stop()

	q := queue.NewDeque()
	fill(q)
	clear(q)
}
Example #16
0
func main() {
	//prof := profile.CPUProfile
	prof := profile.MemProfile
	defer profile.Start(prof, profile.ProfilePath(".")).Stop()

	t := bpt.NewTree()
	for i := 0; i < 1000000; i++ {
		x := []byte(fmt.Sprintf("%x", i))
		t.Set(x, x)
	}
	t.Close()
}
Example #17
0
func main() {
	prof := profile.CPUProfile
	//prof := profile.MemProfile
	defer profile.Start(prof, profile.ProfilePath(".")).Stop()

	t := bptx.NewTree()
	for i := 0; i < 10000; i++ {
		x := bptx.UUID()
		t.Set(x, x)
	}
	t.Close()
}
Example #18
0
func main() {
	flag.Parse()

	input := flag.Arg(0)
	output := flag.Arg(1)
	cwd, _ := os.Getwd()
	defer profile.Start(profile.ProfilePath(cwd)).Stop()

	m, _ := mapping.NewMap(input)

	r := rendering.NewRenderer(m, 5000, 5000)

	r.ClipToMap()
	r.DrawToFile(output)
}
Example #19
0
func main() {
	p := profile.Start(
		profile.MemProfile,
		profile.ProfilePath("."),
		profile.NoShutdownHook)
	defer p.Stop()

	u := &command.Unit{"Test-01", 0, 0}
	s := command.NewCommandStack(10)
	s.Do(command.NewMoveUnitCommand(u, 10, 10))

	for i := 0; i < 10000000; i++ {
		s.Do(command.NewMoveUnitCommand(u, i*10, i*10))
	}
}
Example #20
0
func main() {
	config.InitConfig()

	if config.EnableProfile {
		defer profile.Start(profile.CPUProfile).Stop()
	}

	runtime.GOMAXPROCS(runtime.NumCPU() * 3)
	utils.InitLogger()
	if config.RC.MySQLEnabled {
		query.RC_MySQLConf = config.RC.MySQLConf
		query.InitMySQL(query.RC_MySQLConf)
	}
	server.NewServer()

}
Example #21
0
File: apec.go Project: pmezard/apec
func dispatch() error {
	cmd := kingpin.MustParse(app.Parse(os.Args[1:]))
	if *prof {
		defer profile.Start(profile.CPUProfile).Stop()
	}
	cfg := NewConfig(*dataDir)
	switch cmd {
	case crawlCmd.FullCommand():
		return crawlFn(cfg)
	case indexCmd.FullCommand():
		return indexOffers(cfg)
	case searchCmd.FullCommand():
		return search(cfg)
	case webCmd.FullCommand():
		return web(cfg)
	case geocodeCmd.FullCommand():
		return geocode(cfg)
	case upgradeCmd.FullCommand():
		return upgrade(cfg)
	case dumpDeletedCmd.FullCommand():
		return dumpDeletedOffersFn(cfg)
	case changesCmd.FullCommand():
		return changesFn(cfg)
	case spatialCmd.FullCommand():
		return spatialFn(cfg)
	case debugQueryCmd.FullCommand():
		return debugQueryFn(cfg)
	case analyzeCmd.FullCommand():
		return analyzeFn(cfg)
	case geocodedCmd.FullCommand():
		return geocodedFn(cfg)
	case densityCmd.FullCommand():
		return densityFn(cfg)
	case histogramCmd.FullCommand():
		return histogramFn(cfg)
	case indexStatsCmd.FullCommand():
		return indexStatsFn(cfg)
	case listDeletedCmd.FullCommand():
		return listDeletedFn(cfg)
	case duplicatesCmd.FullCommand():
		return duplicatesFn(cfg)
	case dumpOfferCmd.FullCommand():
		return dumpOfferFn(cfg)
	}
	return fmt.Errorf("unknown command: %s", cmd)
}
Example #22
0
File: dock.go Project: sqp/godock
// runDock starts dock routines and locks the main thread with gtk.
//
func runDock(cmd *Command, args []string) {
	if *pprofFile {
		defer profile.Start().Stop()
	}

	switch {
	case *showVersionGldi:
		fmt.Println(globals.Version()) // -v option only prints gldi version.

	case *showVersionAll:
		versions.Print() // -vv option only prints all versions.

	case dock.Run(logger, dockSettings): // Start dock success => lock gtk.
		dockSettings = nil // free
		gldi.LockGTK()
		maindock.Clean() // may be better with defer, but cause confused panic messages.
	}
}
Example #23
0
func main() {
	p := profile.Start(
		profile.MemProfile,
		profile.ProfilePath("."))
	defer p.Stop()

	q := &queue.Queue{}

	for i := 0; i < 20000000; i++ {
		n := string(i)
		q.Put(n)
	}

	for i := 0; i < 20000000; i++ {
		if q.Get() == nil {
			panic("value is nil, expected *node\n")
		}
	}
}
Example #24
0
func main() {
	defer profile.Start(profile.CPUProfile, profile.MemProfile, profile.BlockProfile).Stop()

	h := HiveConfig("192.168.0.223:10000", "default", "hdfs", "", "")
	err := h.Conn.Open()
	fatalCheck("Populate", err)

	var student Students

	totalWorker := 10
	retVal, err := h.LoadFileWithWorker("/home/developer/contoh.txt", "students", "csv", "dd/MM/yyyy", &student, totalWorker)

	if err != nil {
		fatalCheck("Populate", err)
	}

	h.Conn.Close()
	log.Printf("retVal: \n%v\n", retVal)
}
func main() {
	defer profile.Start(profile.CPUProfile).Stop()

	var start time.Time
	var zero Int

	var iConcrete *Int = &zero
	start = time.Now()
	for i := 0; i < nbOps; i++ {
		iConcrete = iConcrete.Sum(Int(10))
	}
	_ = iConcrete
	fmt.Printf("[concrete]  computed %d sums in %v\n", nbOps, time.Now().Sub(start))

	var iInterface Summable = &zero
	start = time.Now()
	for i := 0; i < nbOps; i++ {
		iInterface = iInterface.Sum(Int(10))
	}
	_ = iInterface
	fmt.Printf("[interface] computed %d sums in %v\n", nbOps, time.Now().Sub(start))
}
Example #26
0
func main() {
	if os.Getenv("HYDRA_PROFILING") == "1" {
		defer profile.Start().Stop()
	}
	cmd.Execute()
}
Example #27
0
func main() {
	defer func() {
		if r := recover(); r != nil {
			fmt.Fprintf(os.Stderr, string(sf.FormatError(fmt.Errorf("%s : %s", r, debug.Stack()))))
		}
	}()

	if version.Show() {
		fmt.Fprintf(os.Stdout, "%s\n", version.String())
		return
	}

	// Enable profiling if mode is set
	switch options.profiling {
	case "cpu":
		defer profile.Start(profile.CPUProfile, profile.ProfilePath("."), profile.Quiet).Stop()
	case "mem":
		defer profile.Start(profile.MemProfile, profile.ProfilePath("."), profile.Quiet).Stop()
	case "block":
		defer profile.Start(profile.BlockProfile, profile.ProfilePath("."), profile.Quiet).Stop()
	default:
		// do nothing
	}

	// Register our custom Error hook
	log.AddHook(NewErrorHook(os.Stderr))

	// Enable runtime tracing if tracing is true
	if options.tracing {
		tracing, err := os.Create(time.Now().Format("2006-01-02T150405.pprof"))
		if err != nil {
			log.Fatalf("Failed to create tracing logfile: %s", err)
		}
		defer tracing.Close()

		if err := trace.Start(tracing); err != nil {
			log.Fatalf("Failed to start tracing: %s", err)
		}
		defer trace.Stop()
	}

	// Open the log file
	f, err := os.OpenFile(options.logfile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644)
	if err != nil {
		log.Fatalf("Failed to open the logfile %s: %s", options.logfile, err)
	}
	defer f.Close()

	// Initiliaze logger with default TextFormatter
	log.SetFormatter(&log.TextFormatter{DisableColors: true, FullTimestamp: true})

	// Set the log level
	if options.debug {
		log.SetLevel(log.DebugLevel)
	}

	// SetOutput to log file and/or stdout
	log.SetOutput(f)
	if options.stdout {
		log.SetOutput(io.MultiWriter(os.Stdout, f))
	}

	// Parse the -reference parameter
	if err = ParseReference(); err != nil {
		log.Fatalf(err.Error())
	}

	// Host is either the host's UUID (if run on vsphere) or the hostname of
	// the system (if run standalone)
	host, err := sys.UUID()
	if host != "" {
		log.Infof("Using UUID (%s) for imagestore name", host)
	} else if options.standalone {
		host, err = os.Hostname()
		log.Infof("Using host (%s) for imagestore name", host)
	}

	if err != nil {
		log.Fatalf("Failed to return the UUID or host name: %s", err)
	}

	if !options.standalone {
		log.Debugf("Running with portlayer")

		// Ping the server to ensure it's at least running
		ok, err := PingPortLayer()
		if err != nil || !ok {
			log.Fatalf("Failed to ping portlayer: %s", err)
		}
	} else {
		log.Debugf("Running standalone")
	}

	// Calculate (and overwrite) the registry URL and make sure that it responds to requests
	options.registry, err = LearnRegistryURL(options)
	if err != nil {
		log.Fatalf("Error while pulling image: %s", err)
	}

	// Get the URL of the OAuth endpoint
	url, err := LearnAuthURL(options)
	if err != nil {
		log.Fatalf("Failed to obtain OAuth endpoint: %s", err)
	}

	// Get the OAuth token - if only we have a URL
	if url != nil {
		token, err := FetchToken(url)
		if err != nil {
			log.Fatalf("Failed to fetch OAuth token: %s", err)
		}
		options.token = token
	}

	// HACK: Required to learn the name of the vmdk from given reference
	// Used by docker personality until metadata support lands
	if !options.resolv {
		progress.Message(po, "", "Pulling from "+options.image)
	}

	// Get the manifest
	manifest, err := FetchImageManifest(options)
	if err != nil {
		if strings.Contains(err.Error(), "image not found") {
			log.Fatalf("Error: image %s not found", options.image)
		} else {
			log.Fatalf("Error while pulling image manifest: %s", err)
		}
	}

	// Create the ImageWithMeta slice to hold Image structs
	images, imageLayer, err := ImagesToDownload(manifest, host)
	if err != nil {
		log.Fatalf(err.Error())
	}

	// HACK: Required to learn the name of the vmdk from given reference
	// Used by docker personality until metadata support lands
	if options.resolv {
		if len(images) > 0 {
			fmt.Printf("%s", images[0].meta)
			os.Exit(0)
		}
		os.Exit(1)
	}

	// Fetch the blobs from registry
	if err := DownloadImageBlobs(images); err != nil {
		log.Fatalf(err.Error())
	}

	if err := CreateImageConfig(images, manifest); err != nil {
		log.Fatalf(err.Error())
	}

	// Write blobs to the storage layer
	if err := WriteImageBlobs(images); err != nil {
		log.Fatalf(err.Error())
	}

	if err := updateImageMetadata(imageLayer, manifest); err != nil {
		log.Fatalf(err.Error())
	}

	progress.Message(po, "", "Digest: "+manifest.Digest)
	if len(images) > 0 {
		progress.Message(po, "", "Status: Downloaded newer image for "+options.image+":"+options.tag)
	} else {
		progress.Message(po, "", "Status: Image is up to date for "+options.image+":"+options.tag)
	}
}
Example #28
0
File: main.go Project: n054/weave
func main() {
	procs := runtime.NumCPU()
	// packet sniffing can block an OS thread, so we need one thread
	// for that plus at least one more.
	if procs < 2 {
		procs = 2
	}
	runtime.GOMAXPROCS(procs)

	var (
		justVersion        bool
		config             mesh.Config
		networkConfig      weave.NetworkConfig
		protocolMinVersion int
		resume             bool
		ifaceName          string
		routerName         string
		nickName           string
		password           string
		pktdebug           bool
		logLevel           string
		prof               string
		bufSzMB            int
		noDiscovery        bool
		httpAddr           string
		ipamConfig         ipamConfig
		dockerAPI          string
		peers              []string
		noDNS              bool
		dnsConfig          dnsConfig
		datapathName       string
		trustedSubnetStr   string
		dbPrefix           string
		isAWSVPC           bool

		defaultDockerHost = "unix:///var/run/docker.sock"
	)

	if val := os.Getenv("DOCKER_HOST"); val != "" {
		defaultDockerHost = val
	}

	mflag.BoolVar(&justVersion, []string{"#version", "-version"}, false, "print version and exit")
	mflag.StringVar(&config.Host, []string{"-host"}, "", "router host")
	mflag.IntVar(&config.Port, []string{"#port", "-port"}, mesh.Port, "router port")
	mflag.IntVar(&protocolMinVersion, []string{"-min-protocol-version"}, mesh.ProtocolMinVersion, "minimum weave protocol version")
	mflag.BoolVar(&resume, []string{"-resume"}, false, "resume connections to previous peers")
	mflag.StringVar(&ifaceName, []string{"#iface", "-iface"}, "", "name of interface to capture/inject from (disabled if blank)")
	mflag.StringVar(&routerName, []string{"#name", "-name"}, "", "name of router (defaults to MAC of interface)")
	mflag.StringVar(&nickName, []string{"#nickname", "-nickname"}, "", "nickname of peer (defaults to hostname)")
	mflag.StringVar(&password, []string{"#password", "-password"}, "", "network password")
	mflag.StringVar(&logLevel, []string{"-log-level"}, "info", "logging level (debug, info, warning, error)")
	mflag.BoolVar(&pktdebug, []string{"#pktdebug", "#-pktdebug", "-pkt-debug"}, false, "enable per-packet debug logging")
	mflag.StringVar(&prof, []string{"#profile", "-profile"}, "", "enable profiling and write profiles to given path")
	mflag.IntVar(&config.ConnLimit, []string{"#connlimit", "#-connlimit", "-conn-limit"}, 30, "connection limit (0 for unlimited)")
	mflag.BoolVar(&noDiscovery, []string{"#nodiscovery", "#-nodiscovery", "-no-discovery"}, false, "disable peer discovery")
	mflag.IntVar(&bufSzMB, []string{"#bufsz", "-bufsz"}, 8, "capture buffer size in MB")
	mflag.StringVar(&httpAddr, []string{"#httpaddr", "#-httpaddr", "-http-addr"}, "", "address to bind HTTP interface to (disabled if blank, absolute path indicates unix domain socket)")
	mflag.StringVar(&ipamConfig.Mode, []string{"-ipalloc-init"}, "", "allocator initialisation strategy (consensus, seed or observer)")
	mflag.StringVar(&ipamConfig.IPRangeCIDR, []string{"#iprange", "#-iprange", "-ipalloc-range"}, "", "IP address range reserved for automatic allocation, in CIDR notation")
	mflag.StringVar(&ipamConfig.IPSubnetCIDR, []string{"#ipsubnet", "#-ipsubnet", "-ipalloc-default-subnet"}, "", "subnet to allocate within by default, in CIDR notation")
	mflag.IntVar(&ipamConfig.PeerCount, []string{"#initpeercount", "#-initpeercount", "-init-peer-count"}, 0, "number of peers in network (for IP address allocation)")
	mflag.StringVar(&dockerAPI, []string{"#api", "#-api", "-docker-api"}, defaultDockerHost, "Docker API endpoint")
	mflag.BoolVar(&noDNS, []string{"-no-dns"}, false, "disable DNS server")
	mflag.StringVar(&dnsConfig.Domain, []string{"-dns-domain"}, nameserver.DefaultDomain, "local domain to server requests for")
	mflag.StringVar(&dnsConfig.ListenAddress, []string{"-dns-listen-address"}, nameserver.DefaultListenAddress, "address to listen on for DNS requests")
	mflag.IntVar(&dnsConfig.TTL, []string{"-dns-ttl"}, nameserver.DefaultTTL, "TTL for DNS request from our domain")
	mflag.DurationVar(&dnsConfig.ClientTimeout, []string{"-dns-fallback-timeout"}, nameserver.DefaultClientTimeout, "timeout for fallback DNS requests")
	mflag.StringVar(&dnsConfig.EffectiveListenAddress, []string{"-dns-effective-listen-address"}, "", "address DNS will actually be listening, after Docker port mapping")
	mflag.StringVar(&dnsConfig.ResolvConf, []string{"-resolv-conf"}, "", "path to resolver configuration for fallback DNS lookups")
	mflag.StringVar(&datapathName, []string{"-datapath"}, "", "ODP datapath name")
	mflag.StringVar(&trustedSubnetStr, []string{"-trusted-subnets"}, "", "comma-separated list of trusted subnets in CIDR notation")
	mflag.StringVar(&dbPrefix, []string{"-db-prefix"}, "/weavedb/weave", "pathname/prefix of filename to store data")
	mflag.BoolVar(&isAWSVPC, []string{"-awsvpc"}, false, "use AWS VPC for routing")

	// crude way of detecting that we probably have been started in a
	// container, with `weave launch` --> suppress misleading paths in
	// mflags error messages.
	if os.Args[0] == "/home/weave/weaver" { // matches the Dockerfile ENTRYPOINT
		os.Args[0] = "weave"
		mflag.CommandLine.Init("weave", mflag.ExitOnError)
	}

	mflag.Parse()

	peers = mflag.Args()
	if resume && len(peers) > 0 {
		Log.Fatalf("You must not specify an initial peer list in conjunction with --resume")
	}

	common.SetLogLevel(logLevel)

	if justVersion {
		fmt.Printf("weave router %s\n", version)
		os.Exit(0)
	}

	Log.Println("Command line options:", options())

	if prof != "" {
		defer profile.Start(profile.CPUProfile, profile.ProfilePath(prof), profile.NoShutdownHook).Stop()
	}

	if protocolMinVersion < mesh.ProtocolMinVersion || protocolMinVersion > mesh.ProtocolMaxVersion {
		Log.Fatalf("--min-protocol-version must be in range [%d,%d]", mesh.ProtocolMinVersion, mesh.ProtocolMaxVersion)
	}
	config.ProtocolMinVersion = byte(protocolMinVersion)

	if pktdebug {
		networkConfig.PacketLogging = packetLogging{}
	} else {
		networkConfig.PacketLogging = nopPacketLogging{}
	}

	overlay, bridge := createOverlay(datapathName, ifaceName, isAWSVPC, config.Host, config.Port, bufSzMB)
	networkConfig.Bridge = bridge

	name := peerName(routerName, bridge.Interface())

	if nickName == "" {
		var err error
		nickName, err = os.Hostname()
		checkFatal(err)
	}

	config.Password = determinePassword(password)
	config.TrustedSubnets = parseTrustedSubnets(trustedSubnetStr)
	config.PeerDiscovery = !noDiscovery

	if isAWSVPC && len(config.Password) > 0 {
		Log.Fatalf("--awsvpc mode is not compatible with the --password option")
	}

	db, err := db.NewBoltDB(dbPrefix + "data.db")
	checkFatal(err)
	defer db.Close()

	router := weave.NewNetworkRouter(config, networkConfig, name, nickName, overlay, db)
	Log.Println("Our name is", router.Ourself)

	if peers, err = router.InitialPeers(resume, peers); err != nil {
		Log.Fatal("Unable to get initial peer set: ", err)
	}

	var dockerCli *docker.Client
	dockerVersion := "none"
	if dockerAPI != "" {
		dc, err := docker.NewClient(dockerAPI)
		if err != nil {
			Log.Fatal("Unable to start docker client: ", err)
		} else {
			Log.Info(dc.Info())
		}
		dockerCli = dc
		dockerVersion = dockerCli.DockerVersion()
	}

	network := ""
	if isAWSVPC {
		network = "awsvpc"
	}
	checkForUpdates(dockerVersion, network)

	observeContainers := func(o docker.ContainerObserver) {
		if dockerCli != nil {
			if err := dockerCli.AddObserver(o); err != nil {
				Log.Fatal("Unable to start watcher", err)
			}
		}
	}
	isKnownPeer := func(name mesh.PeerName) bool {
		return router.Peers.Fetch(name) != nil
	}

	var (
		allocator     *ipam.Allocator
		defaultSubnet address.CIDR
		trackerName   string
	)
	if ipamConfig.Enabled() {
		var t tracker.LocalRangeTracker
		if isAWSVPC {
			Log.Infoln("Creating AWSVPC LocalRangeTracker")
			t, err = tracker.NewAWSVPCTracker()
			if err != nil {
				Log.Fatalf("Cannot create AWSVPC LocalRangeTracker: %s", err)
			}
			trackerName = "awsvpc"
		}
		allocator, defaultSubnet = createAllocator(router, ipamConfig, db, t, isKnownPeer)
		observeContainers(allocator)
		if dockerCli != nil {
			ids, err := dockerCli.AllContainerIDs()
			checkFatal(err)
			allocator.PruneOwned(ids)
		}
	}

	var (
		ns        *nameserver.Nameserver
		dnsserver *nameserver.DNSServer
	)
	if !noDNS {
		ns, dnsserver = createDNSServer(dnsConfig, router.Router, isKnownPeer)
		observeContainers(ns)
		ns.Start()
		defer ns.Stop()
		dnsserver.ActivateAndServe()
		defer dnsserver.Stop()
	}

	router.Start()
	if errors := router.InitiateConnections(peers, false); len(errors) > 0 {
		Log.Fatal(common.ErrorMessages(errors))
	}

	// The weave script always waits for a status call to succeed,
	// so there is no point in doing "weave launch --http-addr ''".
	// This is here to support stand-alone use of weaver.
	if httpAddr != "" {
		muxRouter := mux.NewRouter()
		if allocator != nil {
			allocator.HandleHTTP(muxRouter, defaultSubnet, trackerName, dockerCli)
		}
		if ns != nil {
			ns.HandleHTTP(muxRouter, dockerCli)
		}
		router.HandleHTTP(muxRouter)
		HandleHTTP(muxRouter, version, router, allocator, defaultSubnet, ns, dnsserver)
		http.Handle("/", common.LoggingHTTPHandler(muxRouter))
		Log.Println("Listening for HTTP control messages on", httpAddr)
		go listenAndServeHTTP(httpAddr)
	}

	common.SignalHandlerLoop(router)
}
Example #29
0
func main() {
	kingpin.Version(version)
	kingpin.Parse()
	logging.LogStd(fmt.Sprintf("Starting firehose-to-syslog %s ", version), true)

	logging.SetupLogging(*syslogServer, *debug)

	c := cfclient.Config{
		ApiAddress:        *apiEndpoint,
		Username:          *user,
		Password:          *password,
		SkipSslValidation: *skipSSLValidation,
	}
	cfClient := cfclient.NewClient(&c)

	if len(*dopplerEndpoint) > 0 {
		cfClient.Endpoint.DopplerEndpoint = *dopplerEndpoint
	}
	logging.LogStd(fmt.Sprintf("Using %s as doppler endpoint", cfClient.Endpoint.DopplerEndpoint), true)

	logging.LogStd("Setting up event routing!", true)
	err := events.SetupEventRouting(*wantedEvents)
	if err != nil {
		log.Fatal("Error setting up event routing: ", err)
		os.Exit(1)

	}

	//Use bolt for in-memory  - file caching
	db, err := bolt.Open(*boltDatabasePath, 0600, &bolt.Options{Timeout: 1 * time.Second})
	if err != nil {
		log.Fatal("Error opening bolt db: ", err)
		os.Exit(1)

	}
	defer db.Close()

	if *modeProf != "" {
		switch *modeProf {
		case "cpu":
			defer profile.Start(profile.CPUProfile, profile.ProfilePath(*pathProf)).Stop()
		case "mem":
			defer profile.Start(profile.MemProfile, profile.ProfilePath(*pathProf)).Stop()
		case "block":
			defer profile.Start(profile.BlockProfile, profile.ProfilePath(*pathProf)).Stop()
		default:
			// do nothing
		}
	}

	caching.SetCfClient(cfClient)
	caching.SetAppDb(db)
	caching.CreateBucket()

	//Let's Update the database the first time
	logging.LogStd("Start filling app/space/org cache.", true)
	apps := caching.GetAllApp()
	logging.LogStd(fmt.Sprintf("Done filling cache! Found [%d] Apps", len(apps)), true)

	// Ticker Pooling the CC every X sec
	ccPooling := time.NewTicker(*tickerTime)

	go func() {
		for range ccPooling.C {
			apps = caching.GetAllApp()
		}
	}()

	// Parse extra fields from cmd call
	extraFields, err := extrafields.ParseExtraFields(*extraFields)
	if err != nil {
		log.Fatal("Error parsing extra fields: ", err)
		os.Exit(1)
	}

	if *logEventTotals == true {
		events.LogEventTotals(*logEventTotalsTime, *dopplerEndpoint)
	}

	if logging.Connect() || *debug {

		logging.LogStd("Connected to Syslog Server! Connecting to Firehose...", true)

		firehose := firehose.CreateFirehoseChan(cfClient.Endpoint.DopplerEndpoint, cfClient.GetToken(), *subscriptionId, *skipSSLValidation)
		if firehose != nil {
			logging.LogStd("Firehose Subscription Succesfull! Routing events...", true)
			events.RouteEvents(firehose, extraFields)
		} else {
			logging.LogError("Failed connecting to Firehose...Please check settings and try again!", "")
		}

	} else {
		logging.LogError("Failed connecting to the Syslog Server...Please check settings and try again!", "")
	}
}
Example #30
0
func main() {

	if gogoprofile := os.Getenv("CFS_PROFILE"); gogoprofile == "true" {
		defer profile.Start(profile.MemProfile).Stop()
	}

	// Process command line arguments
	var token string
	var fsNum string
	var serverAddr string

	app := cli.NewApp()
	app.Name = "cfs"
	app.Usage = "Client used to test filesysd"
	app.Flags = []cli.Flag{
		cli.StringFlag{
			Name:        "token, T",
			Value:       "",
			Usage:       "Access token",
			EnvVar:      "OOHHC_TOKEN_KEY",
			Destination: &token,
		},
	}
	app.Commands = []cli.Command{
		{
			Name:      "show",
			Usage:     "Show a File Systems",
			ArgsUsage: "<region>://<file system uuid>",
			Action: func(c *cli.Context) error {
				if !c.Args().Present() {
					fmt.Println("Invalid syntax for show.")
					os.Exit(1)
				}
				if token == "" {
					fmt.Println("Token is required")
					os.Exit(1)
				}
				serverAddr, fsNum = parseurl(c.Args().Get(0), "8445")
				if fsNum == "" {
					fmt.Println("Missing file system id")
					os.Exit(1)
				}
				conn := setupWS(serverAddr)
				ws := pb.NewFileSystemAPIClient(conn)
				result, err := ws.ShowFS(context.Background(), &pb.ShowFSRequest{Token: token, FSid: fsNum})
				if err != nil {
					log.Fatalf("Bad Request: %v", err)
					conn.Close()
					os.Exit(1)
				}
				conn.Close()
				log.Printf("SHOW Results: %s", result.Data)
				return nil
			},
		},
		{
			Name:      "create",
			Usage:     "Create a File Systems",
			ArgsUsage: "<region>:// -N <file system name>",
			Flags: []cli.Flag{
				cli.StringFlag{
					Name:  "name, N",
					Value: "",
					Usage: "Name of the file system",
				},
			},
			Action: func(c *cli.Context) error {
				if !c.Args().Present() {
					fmt.Println("Invalid syntax for show.")
					os.Exit(1)
				}
				if token == "" {
					fmt.Println("Token is required")
				}
				// For create serverAddr and acctnum are required
				serverAddr, _ = parseurl(c.Args().Get(0), "8445")
				if c.String("name") == "" {
					fmt.Println("File system name is a required field.")
					os.Exit(1)
				}
				conn := setupWS(serverAddr)
				ws := pb.NewFileSystemAPIClient(conn)
				result, err := ws.CreateFS(context.Background(), &pb.CreateFSRequest{Token: token, FSName: c.String("name")})
				if err != nil {
					log.Fatalf("Bad Request: %v", err)
					conn.Close()
					os.Exit(1)
				}
				conn.Close()
				log.Printf("Create Results: %s", result.Data)
				return nil
			},
		},
		{
			Name:      "list",
			Usage:     "List File Systems for an account",
			ArgsUsage: "<region>://",
			Action: func(c *cli.Context) error {
				if !c.Args().Present() {
					fmt.Println("Invalid syntax for list.")
					os.Exit(1)
				}
				if token == "" {
					fmt.Println("Token is required")
					os.Exit(1)
				}
				serverAddr, _ = parseurl(c.Args().Get(0), "8445")
				conn := setupWS(serverAddr)
				ws := pb.NewFileSystemAPIClient(conn)
				result, err := ws.ListFS(context.Background(), &pb.ListFSRequest{Token: token})
				if err != nil {
					log.Fatalf("Bad Request: %v", err)
					conn.Close()
					os.Exit(1)
				}
				conn.Close()
				log.Printf("LIST Results: %s", result.Data)
				return nil
			},
		},
		{
			Name:      "delete",
			Usage:     "Delete a File Systems",
			ArgsUsage: "<region>://<file system uuid>",
			Action: func(c *cli.Context) error {
				if !c.Args().Present() {
					fmt.Println("Invalid syntax for delete.")
					os.Exit(1)
				}
				if token == "" {
					fmt.Println("Token is required")
				}
				serverAddr, fsNum = parseurl(c.Args().Get(0), "8445")
				if fsNum == "" {
					fmt.Println("Missing file system id")
					os.Exit(1)
				}
				conn := setupWS(serverAddr)
				ws := pb.NewFileSystemAPIClient(conn)
				result, err := ws.DeleteFS(context.Background(), &pb.DeleteFSRequest{Token: token, FSid: fsNum})
				if err != nil {
					log.Fatalf("Bad Request: %v", err)
					conn.Close()
					os.Exit(1)
				}
				conn.Close()
				log.Printf("Delete Results: %s", result.Data)
				return nil
			},
		},
		{
			Name:      "update",
			Usage:     "Update a File Systems",
			ArgsUsage: "<region>://<file system uuid> -o [OPTIONS]",
			Flags: []cli.Flag{
				cli.StringFlag{
					Name:  "name, N",
					Value: "",
					Usage: "Name of the file system",
				},
				cli.StringFlag{
					Name:  "S, status",
					Value: "",
					Usage: "Status of the file system",
				},
			},
			Action: func(c *cli.Context) error {
				if !c.Args().Present() {
					fmt.Println("Invalid syntax for update.")
					os.Exit(1)
				}
				if token == "" {
					fmt.Println("Token is required")
					os.Exit(1)
				}
				serverAddr, fsNum = parseurl(c.Args().Get(0), "8445")
				if fsNum == "" {
					fmt.Println("Missing file system id")
					os.Exit(1)
				}
				if c.String("name") != "" {
					fmt.Printf("Invalid File System String: %q\n", c.String("name"))
					os.Exit(1)
				}
				fsMod := &pb.ModFS{
					Name:   c.String("name"),
					Status: c.String("status"),
				}
				conn := setupWS(serverAddr)
				ws := pb.NewFileSystemAPIClient(conn)
				result, err := ws.UpdateFS(context.Background(), &pb.UpdateFSRequest{Token: token, FSid: fsNum, Filesys: fsMod})
				if err != nil {
					log.Fatalf("Bad Request: %v", err)
					conn.Close()
					os.Exit(1)
				}
				conn.Close()
				log.Printf("Update Results: %s", result.Data)
				return nil
			},
		},
		{
			Name:      "grant",
			Usage:     "Grant an Addr access to a File Systems",
			ArgsUsage: "<region>://<file system uuid> -addr <IP Address>",
			Flags: []cli.Flag{
				cli.StringFlag{
					Name:  "addr",
					Value: "",
					Usage: "Address to Grant",
				},
			},
			Action: func(c *cli.Context) error {
				if !c.Args().Present() {
					fmt.Println("Invalid syntax for delete.")
					os.Exit(1)
				}
				if token == "" {
					fmt.Println("Token is required")
					os.Exit(1)
				}
				if c.String("addr") == "" {
					fmt.Println("addr is required")
					os.Exit(1)
				}
				serverAddr, fsNum = parseurl(c.Args().Get(0), "8445")
				if fsNum == "" {
					fmt.Println("Missing file system id")
					os.Exit(1)
				}
				conn := setupWS(serverAddr)
				ws := pb.NewFileSystemAPIClient(conn)
				result, err := ws.GrantAddrFS(context.Background(), &pb.GrantAddrFSRequest{Token: token, FSid: fsNum, Addr: c.String("addr")})
				if err != nil {
					log.Fatalf("Bad Request: %v", err)
					conn.Close()
					os.Exit(1)
				}
				conn.Close()
				log.Printf("Result: %s\n", result.Data)
				return nil
			},
		},
		{
			Name:      "revoke",
			Usage:     "Revoke an Addr's access to a File Systems",
			ArgsUsage: "<region>://<file system uuid> -addr <IP Address>",
			Flags: []cli.Flag{
				cli.StringFlag{
					Name:  "addr",
					Value: "",
					Usage: "Address to Revoke",
				},
			},
			Action: func(c *cli.Context) error {
				if !c.Args().Present() {
					fmt.Println("Invalid syntax for revoke.")
					os.Exit(1)
				}
				if token == "" {
					fmt.Println("Token is required")
					os.Exit(1)
				}
				if c.String("addr") == "" {
					fmt.Println("addr is required")
					os.Exit(1)
				}
				serverAddr, fsNum = parseurl(c.Args().Get(0), "8445")
				if fsNum == "" {
					fmt.Println("Missing file system id")
					os.Exit(1)
				}
				conn := setupWS(serverAddr)
				ws := pb.NewFileSystemAPIClient(conn)
				result, err := ws.RevokeAddrFS(context.Background(), &pb.RevokeAddrFSRequest{Token: token, FSid: fsNum, Addr: c.String("addr")})
				if err != nil {
					log.Fatalf("Bad Request: %v", err)
					conn.Close()
					os.Exit(1)
				}
				conn.Close()
				log.Printf("Result: %s\n", result.Data)
				return nil
			},
		},
		{
			Name:      "mount",
			Usage:     "mount a file system",
			ArgsUsage: "<region>://<file system uuid> <mount point> -o [OPTIONS]",
			Flags: []cli.Flag{
				cli.StringFlag{
					Name:  "o",
					Value: "",
					Usage: "mount options",
				},
			},
			Action: func(c *cli.Context) error {
				if !c.Args().Present() {
					fmt.Println("Invalid syntax for revoke.")
					os.Exit(1)
				}
				serverAddr, fsNum = parseurl(c.Args().Get(0), "8445")
				fsnum, err := uuid.FromString(fsNum)
				if err != nil {
					fmt.Print("File System id is not valid: ", err)
				}
				mountpoint := c.Args().Get(1)
				// check mountpoint exists
				if _, ferr := os.Stat(mountpoint); os.IsNotExist(ferr) {
					log.Printf("Mount point %s does not exist\n\n", mountpoint)
					os.Exit(1)
				}
				fusermountPath()
				// process file system options
				allowOther := false
				if c.String("o") != "" {
					clargs := getArgs(c.String("o"))
					// crapy debug log handling :)
					if debug, ok := clargs["debug"]; ok {
						if debug == "false" {
							log.SetFlags(0)
							log.SetOutput(ioutil.Discard)
						}
					} else {
						log.SetFlags(0)
						log.SetOutput(ioutil.Discard)
					}
					_, allowOther = clargs["allow_other"]
				}
				// Setup grpc
				var opts []grpc.DialOption
				creds := credentials.NewTLS(&tls.Config{
					InsecureSkipVerify: true,
				})
				opts = append(opts, grpc.WithTransportCredentials(creds))
				conn, err := grpc.Dial(serverAddr, opts...)
				if err != nil {
					log.Fatalf("failed to dial: %v", err)
				}
				defer conn.Close()
				// Work with fuse
				var cfs *fuse.Conn
				// TODO: Make setting the fuse config cleaner
				if allowOther {
					cfs, err = fuse.Mount(
						mountpoint,
						fuse.FSName("cfs"),
						fuse.Subtype("cfs"),
						fuse.LocalVolume(),
						fuse.VolumeName("CFS"),
						fuse.AllowOther(),
						fuse.DefaultPermissions(),
						fuse.MaxReadahead(128*1024),
						fuse.AsyncRead(),
					)
				} else {
					cfs, err = fuse.Mount(
						mountpoint,
						fuse.FSName("cfs"),
						fuse.Subtype("cfs"),
						fuse.LocalVolume(),
						fuse.VolumeName("CFS"),
						fuse.DefaultPermissions(),
						fuse.MaxReadahead(128*1024),
						fuse.AsyncRead(),
						//fuse.WritebackCache(), // Waiting on concurrent chunk update fix
						//fuse.AutoInvalData(),  // requires https://github.com/bazil/fuse/pull/137
					)
				}
				if err != nil {
					log.Fatal(err)
				}
				defer cfs.Close()

				rpc := newrpc(conn)
				fs := newfs(cfs, rpc, fsnum.String())
				err = fs.InitFs()
				if err != nil {
					log.Fatal(err)
				}
				srv := newserver(fs)

				if err := srv.serve(); err != nil {
					log.Fatal(err)
				}

				<-cfs.Ready
				if err := cfs.MountError; err != nil {
					log.Fatal(err)
				}
				return nil
			},
		},
	}
	app.Run(os.Args)
}