Пример #1
0
func WatchStats(cli *etcd.Client, myid string, conf *config.FiddlerConf) {
	goChan, stopChan := lock.Acquire(cli, "fiddler/watcher", 20)

	go func() {
		<-goChan

		run := true

		for run {
			select {
			case <-stopChan:
				run = false // We're going to exit
			default:
				log.Println("I am king")

				// As king, we'll need a spawn pool
				pool, err := spawner.GetSpawnPool(conf.Env)
				if err != nil {
					log.Fatal("Error getting spawn pool:", err)
				}

				// We're going to look at the stats we want to look at
				// and determine the correct count of servers

				err = checkStats(cli, conf, pool)
				if err != nil {
					log.Printf("Encountered error: %s", err)
				}

				time.Sleep(5 * time.Second)
			}
		}
	}()
}
Пример #2
0
func main() {
	// First, let's make sure we have a configuration file
	var c = flag.String("config", "", "location of configuration file (http okay)")
	flag.Parse()

	if len(flag.Args()) < 1 {
		printUsage()
		return
	}

	if *c == "" {
		fmt.Println("Missing config option")
		printUsage()
		return
	}

	// Next, we'll load the config
	var conf *config.FiddlerConf
	conf, err := config.LoadFiddlerConfig(*c)

	if err != nil {
		log.Fatal("Unable to load config file:", err)
	}

	var cmd = flag.Args()[0]

	switch cmd {
	case "i", "install":
		// We should install Fiddler
		err := installer.InstallFiddler(*c, *conf)
		if err != nil {
			log.Fatal("Error installing Fiddler:", err)
		}
	case "s", "spawn":
		if len(conf.Env) == 0 {
			fmt.Println("Must provide env name in config")
			return
		}

		// We'll grab spawn pool and grow it
		pool, err := spawner.GetSpawnPool(conf.Env)
		if err != nil {
			log.Fatal("Error getting spawn pool:", err)
		}

		err = pool.Grow(*c)
		if err != nil {
			log.Fatal("Error growing spawn pool:", err)
		}
	case "l", "launch":
		// We should launch our container
		var cmd *exec.Cmd
		cmd, err = launcher.Launch(conf.Docker)
		if err != nil {
			log.Fatal("Error launching container:", conf.Docker, err)
		}

		err = cmd.Wait() // We'll stay open so long as the docker is open.  We can ensure the docker stays open, etc.
		if err != nil {
			log.Fatal("Failed to run container:", conf.Docker, err)
		}
	case "d", "daemon":
		// We should be like a daemon, tracking stats

		// Now, we're going to make sure we're monitoring our stats
		go tracker.TrackMyStats(cli, myid, []string{"cpu"})
		go tracker.WatchStats(cli, myid, conf)

		ch := make(chan int)
		<-ch // Hold forever
	default:
		printUsage()
		return
	}
}