Ejemplo n.º 1
0
func Setup(name string) error {
	chroot := filepath.Join(RepoRoot(), name)
	u, err := user.Current()
	if err != nil {
		return err
	}

	var sc bytes.Buffer
	if err := scriptTemplate.Execute(&sc, u); err != nil {
		return err
	}

	plog.Info("Configuring SDK chroot")
	sh := exec.Command(
		"sudo", sudoPrompt,
		"chroot", chroot,
		"/usr/bin/env", "-i",
		"/bin/bash", "--login")
	sh.Stdin = &sc
	sh.Stderr = os.Stderr
	if plog.LevelAt(capnslog.INFO) {
		out, err := sh.StdoutPipe()
		if err != nil {
			return err
		}
		go util.LogFrom(capnslog.INFO, out)
	}
	if plog.LevelAt(capnslog.DEBUG) {
		sh.Args = append(sh.Args, "-x")
	}
	return sh.Run()
}
Ejemplo n.º 2
0
func NewDnsmasq() (*Dnsmasq, error) {
	dm := &Dnsmasq{}
	for s := byte(0); s < numSegments; s++ {
		seg, err := newSegment(s)
		if err != nil {
			return nil, fmt.Errorf("Network setup failed: %v", err)
		}
		dm.Segments = append(dm.Segments, seg)
	}

	// setup lo
	lo, err := netlink.LinkByName("lo")
	if err != nil {
		return nil, fmt.Errorf("Network loopback setup failed: %v", err)
	}
	err = netlink.LinkSetUp(lo)
	if err != nil {
		return nil, fmt.Errorf("Network loopback setup failed: %v", err)
	}

	dm.dnsmasq = exec.Command("dnsmasq", "--conf-file=-")
	cfg, err := dm.dnsmasq.StdinPipe()
	if err != nil {
		return nil, err
	}
	out, err := dm.dnsmasq.StdoutPipe()
	if err != nil {
		return nil, err
	}
	dm.dnsmasq.Stderr = dm.dnsmasq.Stdout
	go util.LogFrom(capnslog.INFO, out)

	if err = dm.dnsmasq.Start(); err != nil {
		cfg.Close()
		return nil, err
	}

	if err = configTemplate.Execute(cfg, dm); err != nil {
		cfg.Close()
		dm.Destroy()
		return nil, err
	}
	cfg.Close()

	return dm, nil
}
Ejemplo n.º 3
0
func (o *OmahaHandler) ServeHTTP(w http.ResponseWriter, httpReq *http.Request) {
	if httpReq.Method != "POST" {
		plog.Errorf("Unexpected HTTP method: %s", httpReq.Method)
		http.Error(w, "Expected a POST", http.StatusBadRequest)
		return
	}

	// A request over 1M in size is certainly bogus.
	var reader io.Reader
	reader = http.MaxBytesReader(w, httpReq.Body, 1024*1024)

	// Optionally log request bodies.
	if plog.LevelAt(capnslog.DEBUG) {
		pr, pw := io.Pipe()
		go util.LogFrom(capnslog.DEBUG, pr)
		reader = io.TeeReader(reader, pw)
	}

	decoder := xml.NewDecoder(reader)
	var omahaReq Request
	if err := decoder.Decode(&omahaReq); err != nil {
		plog.Errorf("Failed decoding XML: %v", err)
		http.Error(w, "Invalid XML", http.StatusBadRequest)
		return
	}

	if omahaReq.Protocol != "3.0" {
		plog.Errorf("Unexpected protocol: %q", omahaReq.Protocol)
		http.Error(w, "Omaha 3.0 Required", http.StatusBadRequest)
		return
	}

	httpStatus := 0
	omahaResp := NewResponse()
	for _, appReq := range omahaReq.Apps {
		appResp := o.serveApp(omahaResp, httpReq, &omahaReq, appReq)
		if appResp.Status == AppOK {
			// HTTP is ok if any app is ok.
			httpStatus = http.StatusOK
		} else if httpStatus == 0 {
			// If no app is ok HTTP will use the first error.
			if appResp.Status == AppInternalError {
				httpStatus = http.StatusInternalServerError
			} else {
				httpStatus = http.StatusBadRequest
			}
		}
	}

	if httpStatus == 0 {
		httpStatus = http.StatusBadRequest
	}

	w.Header().Set("Content-Type", "text/xml; charset=utf-8")
	w.WriteHeader(httpStatus)

	// Optionally log response body.
	var writer io.Writer = w
	if plog.LevelAt(capnslog.DEBUG) {
		pr, pw := io.Pipe()
		go util.LogFrom(capnslog.DEBUG, pr)
		writer = io.MultiWriter(w, pw)
	}

	if _, err := writer.Write([]byte(xml.Header)); err != nil {
		plog.Errorf("Failed writing response: %v", err)
		return
	}

	encoder := xml.NewEncoder(writer)
	encoder.Indent("", "\t")
	if err := encoder.Encode(omahaResp); err != nil {
		plog.Errorf("Failed encoding response: %v", err)
	}
}