Ejemplo n.º 1
0
func (j *Job) Build(path, platform string) {
	j.status("starting")
	result := batt.Message{"result", url.Values{"h": []string{j.h}}}
	build := func() error {
		// create virgin environment
		gopath, err := ioutil.TempDir("", "battc")
		if err != nil {
			return err
		}
		defer os.RemoveAll(gopath)

		j.status("fetching and building")
		cmd := exec.Command("go", "get", path)
		cmd.Env = env(gopath, platform)
		if b, err := cmd.CombinedOutput(); err != nil {
			return fmt.Errorf("go get: %v\n%s", err, b)
		}

		j.status("finding binary")
		bindir := filepath.Join(gopath, "bin")
		bin, size, err := findFile(bindir)
		if err != nil {
			return fmt.Errorf("finding: %v", err)
		}
		j.filename = filepath.Base(bin)
		result.Set("filename", j.filename)
		result.Set("size", fmt.Sprint(size))

		j.status("hashing")
		h, err := batt.ReadFileSHA1(bin)
		if err != nil {
			return fmt.Errorf("hashing: %v", err)
		}
		result.Set("sha1", h)

		j.status("storing file")
		tmpfile, err := cpToTempFile(bin, h)
		if err != nil {
			return fmt.Errorf("storing: %v", err)
		}
		j.tmpfile = tmpfile

		return nil
	}
	if err := build(); err != nil {
		j.logf("build: %v", err)
		result.Set("err", err.Error())
	}
	if j.tmpfile != "" {
		j.logf("tmpfile: %s", j.tmpfile)
	}
	j.logf("result: %v", result)
	out <- result

	j.status("waiting for accept")
}
Ejemplo n.º 2
0
func handler() {
	jobs := make(map[string]*Job)
	for {
		var m batt.Message
		select {
		case <-time.After(nopDelay):
			out <- batt.Message{Verb: "nop"}
			continue
		case m = <-in:
		}
		if m.Verb == "nop" {
			continue // ignore
		}
		log.Println("received:", m)

		switch m.Verb {
		case "build":
			h := m.Get("h")
			j := NewJob(h)
			jobs[h] = j
			go j.Build(m.Get("path"), m.Get("platform"))
		case "accept":
			h := m.Get("h")
			j, ok := jobs[h]
			if !ok {
				log.Printf("unknown job %q", h)
				break
			}
			delete(jobs, h)
			go j.Accept(m.Get("url"))
		default:
			log.Printf("unknown verb %q", m.Verb)
		}
	}
}