Esempio n. 1
0
func main() {
	hosted := env("HOSTED", "")
	if hosted == "" {
		panic("don't know where the builder lives. Please set the HOSTED env var.")
	}

	bu := web.New(
		builder.New(env("GOOS", ""), env("GOARCH", ""), ""),
		env("TRACKER", "http://goci.me/rpc/tracker"),
		hosted,
	)

	l, err := net.Listen("tcp", "0.0.0.0:"+env("PORT", "9080"))
	if err != nil {
		panic(err)
	}
	defer l.Close()
	go http.Serve(l, bu)

	if err := bu.Announce(); err != nil {
		panic(err)
	}
	defer bu.Remove()

	//wait for a signal
	signals := []os.Signal{
		syscall.SIGQUIT,
		syscall.SIGKILL,
		syscall.SIGINT,
		syscall.SIGTERM,
	}
	ch := make(chan os.Signal, len(signals))
	signal.Notify(ch, signals...)
	sig := <-ch
	log.Printf("Captured a %v\n", sig)
}
Esempio n. 2
0
func main() {
	//load up the environment if its specified
	if config.env != "" {
		if err := loader.Load(config.env); err != nil {
			panic(err)
		}
	}

	//configure the frontend
	frontend.Config.Templates = env("TEMPLATES", "./templates")
	frontend.Config.Static = env("STATIC", "./static")
	frontend.Config.Debug = env("DEBUG", "") != ""

	//configure the notifications
	notifications.Config.Username = mustEnv("XMPPUSER")
	notifications.Config.Password = mustEnv("XMPPPASS")
	notifications.Config.Domain = mustEnv("XMPPDOMAIN")

	//connect to the mongo database.
	sess, err := mgo.Dial(env("DATABASE", "mongodb://localhost/gocitest"))
	if err != nil {
		panic(err)
	}
	//empty implies whatever was specified in dial.
	httputil.Config.DB = sess.DB("")

	//set up the httputil domain so we can build absolute urls
	httputil.Config.Domain = mustEnv("DOMAIN")

	//start the server.
	//we can't use listenandserve because the scheduler might not give it the
	//opportunity to set up the listen socket before we attempt to announce.
	l, err := net.Listen("tcp", "0.0.0.0:"+mustEnv("PORT"))
	if err != nil {
		panic(err)
	}
	defer l.Close()
	go http.Serve(l, nil)

	//set up some vars for our target os and arch and the runner
	var GOOS, GOARCH string
	var runner Service

	//check if we're running direct or not
	if env("DIRECTRUN", "") == "" {
		//we're running on heroku so build for that target
		GOOS, GOARCH = "linux", "amd64"
		runner = newWebRunner()
	} else {
		//we're running things directly
		GOOS, GOARCH = env("GOOS", runtime.GOOS), env("GOARCH", runtime.GOARCH)
		runner = newDirectRunner()
	}

	//add the runner to our system
	http.Handle("/runner/", http.StripPrefix("/runner", runner))

	//announce the runner
	if err := runner.Announce(); err != nil {
		panic(err)
	}
	defer runner.Remove()

	var goroot string
	if err := checkTools(); err != nil {
		//create a temporary directory to house the go tool and hg+bzr.
		tmpdir, err := ioutil.TempDir("", "tools")
		if err != nil {
			panic(err)
		}
		defer os.RemoveAll(tmpdir)

		//execute our setup script
		cmd := exec.Command("bash", "heroku_setup.sh", "heroku/dist", tmpdir)
		if err := cmd.Run(); err != nil {
			panic(err)
		}

		//store our goroot
		goroot = filepath.Join(tmpdir, "go1.0.2.linux-amd64", "go")

		//add goroot/bin and venv/bin to path
		path := os.Getenv("PATH")
		path += string(filepath.ListSeparator) + filepath.Join(goroot, "bin")
		path += string(filepath.ListSeparator) + filepath.Join(tmpdir, "venv", "bin")
		os.Setenv("PATH", path)

		//check for the tools again as they should installed
		if err := checkTools(); err != nil {
			panic("couldn't find tools after installing: " + err.Error())
		}
	}

	//if we don't have goroot set yet set it to where we find the go command
	if goroot == "" {
		path, err := exec.LookPath("go")
		if err != nil {
			panic("unable to find go tool")
		}
		goroot = filepath.Dir(filepath.Dir(path))
	}

	//create the builder and announce it
	bu := buweb.New(
		builder.New(GOOS, GOARCH, goroot),
		httputil.Absolute(router.Lookup("Tracker")),
		httputil.Absolute("/builder/"),
	)
	http.Handle("/builder/", http.StripPrefix("/builder", bu))

	if err := bu.Announce(); err != nil {
		panic(err)
	}
	defer bu.Remove()

	//wait for a signal
	signals := []os.Signal{
		syscall.SIGQUIT,
		syscall.SIGKILL,
		syscall.SIGINT,
		syscall.SIGTERM,
	}
	ch := make(chan os.Signal, 1)
	signal.Notify(ch, signals...)
	sig := <-ch
	log.Printf("Captured a %v\n", sig)
}