Ejemplo n.º 1
0
func hitURL(uri string) (err error) {
	tsk := newTask("http.Get", uri)
	defer func() {
		if err != nil {
			tsk.Err = fmt.Sprintf("%v", err)
		}
		biSuitelk.Lock()
		currentTestSuite.addRun(tsk)
		biSuitelk.Unlock()
	}()
	dbg.Println(tsk.String())
	tsk.Start = time.Now()
	u, err := url.Parse(uri)
	if err != nil {
		return fmt.Errorf("%v: could not get host:port to dial from %v: %v\n", tsk.String(), uri, err)
	}
	hostPort := u.Host
	err = netutil.AwaitReachable(hostPort, camlistoredTimeOut)
	if err != nil {
		return fmt.Errorf("%v: camlistored unreachable at %v after %v: %v\n", tsk.String(), hostPort, camlistoredTimeOut, err)
	}
	var resp *http.Response
	resp, err = http.Get(uri)
	if err != nil {
		return fmt.Errorf("%v: %v\n", tsk.String(), err)
	}
	defer resp.Body.Close()
	if resp.StatusCode != 200 {
		return fmt.Errorf("%v, got StatusCode: %d\n", tsk.String(), resp.StatusCode)
	}
	return nil
}
func main() {
	flag.Parse()

	checkArgs()

	if _, err := wkfs.Stat(camliServerConf); err == nil {
		fmt.Printf("Configuration file %v already exists, not overwriting.\n", camliServerConf)
	} else {
		if !os.IsNotExist(err) {
			log.Fatal(err)
		}
		if err := writeDefaultConfigFile(); err != nil {
			log.Fatal(err)
		}
	}

	if !*flagDebug {
		// Because we can't run these in a docker shell
		setupServices()
		setupFUSE()
	}

	if err := netutil.AwaitReachable("localhost:3179", time.Duration(*flagTimeout)*time.Second); err != nil {
		log.Fatalf("Could not ask server for client config: %v", err)
	}
	if err := writeClientConfigFile(); err != nil {
		log.Fatal(err)
	}
}
Ejemplo n.º 3
0
// lookup retrieves the ip address of the container, and tries to reach
// before timeout the tcp address at this ip and given port.
func (c ContainerID) lookup(port int, timeout time.Duration) (ip string, err error) {
	ip, err = c.IP()
	if err != nil {
		err = fmt.Errorf("error getting IP: %v", err)
		return
	}
	addr := fmt.Sprintf("%s:%d", ip, port)
	err = netutil.AwaitReachable(addr, timeout)
	return
}
Ejemplo n.º 4
0
func waitForReachable(timeout time.Duration) error {
	verboseLog("Waiting up to", StartupTimeout, "seconds for", ApplicationName, "to be reachable at", ApplicationHost)

	if err := netutil.AwaitReachable(ApplicationHost, timeout); err != nil { // HL
		return err // HL
	} // HL

	verboseLog("Application Host is reachable, resuming")
	return nil
}
Ejemplo n.º 5
0
// lookup retrieves the ip address of the container, and tries to reach
// before timeout the tcp address at this ip and given port.
func (c ContainerID) lookup(port int, timeout time.Duration) (ip string, err error) {
	ip, err = c.IP()
	if err != nil {
		err = fmt.Errorf("Error getting container IP: %v", err)
		return
	}
	addr := fmt.Sprintf("%s:%d", ip, port)
	if err = netutil.AwaitReachable(addr, timeout); err != nil {
		err = fmt.Errorf("timeout trying to reach %s for container %v: %v", addr, c, err)
	}
	return
}
Ejemplo n.º 6
0
// lookup retrieves the ip address of the container, and tries to reach
// before timeout the tcp address at this ip and given port.
func (c ContainerID) lookup(port int, timeout time.Duration) (ip string, err error) {
	if DockerMachineAvailable {
		var out []byte
		out, err = exec.Command("docker-machine", "ip", DockerMachineName).Output()
		ip = strings.TrimSpace(string(out))
	} else if bindDockerToLocalhost != "" {
		ip = "127.0.0.1"
	} else {
		ip, err = c.IP()
	}
	if err != nil {
		err = fmt.Errorf("error getting IP: %v", err)
		return
	}
	addr := fmt.Sprintf("%s:%d", ip, port)
	err = netutil.AwaitReachable(addr, timeout)
	return
}
Ejemplo n.º 7
0
// TestMongoKV tests against a real MongoDB instance, using a Docker container.
// Currently using https://index.docker.io/u/robinvdvleuten/mongo/
func TestMongoKV(t *testing.T) {
	if testing.Short() {
		t.Skip("skipping in short mode")
	}
	if _, err := exec.LookPath("docker"); err != nil {
		t.Skip("skipping without docker available in path")
	}
	if ok, err := dockerHaveImage(mongoImage); !ok || err != nil {
		if err != nil {
			t.Skipf("Error running docker to check for %s: %v", mongoImage, err)
		}
		log.Printf("Pulling docker image %s ...", mongoImage)
		if err := dockerPull(mongoImage); err != nil {
			t.Skipf("Error pulling %s: %v", mongoImage, err)
		}
	}
	containerID, err := dockerRun("-d", mongoImage, "--smallfiles")
	if err != nil {
		t.Fatalf("docker run: %v", err)
	}
	defer dockerKillContainer(containerID)

	ip, err := dockerIP(containerID)
	if err != nil {
		t.Fatalf("Error getting container IP: %v", err)
	}

	if err := netutil.AwaitReachable(ip+":27017", 10*time.Second); err != nil {
		t.Fatal("timeout waiting for port to become reachable")
	}

	kv, err := NewKeyValue(Config{
		Server:   ip,
		Database: "camlitest",
	})
	if err != nil {
		t.Fatalf("monogo.NewKeyValue = %v", err)
	}
	kvtest.TestSorted(t, kv)
}
Ejemplo n.º 8
0
func (c *serverCmd) makeThings() error {
	const importerPrefix = "/importer/"
	// check that "/importer/" prefix is in config, just in case it ever changes.
	configFile := filepath.Join(camliSrcRoot, "config", "dev-server-config.json")
	config, err := ioutil.ReadFile(configFile)
	if err != nil {
		return fmt.Errorf("could not read config file %v: %v", configFile, err)
	}
	if !bytes.Contains(config, []byte(importerPrefix)) {
		return fmt.Errorf("%s prefix not found in dev config. Did it change?", importerPrefix)
	}

	if err := netutil.AwaitReachable("localhost:"+c.port, time.Minute); err != nil {
		return err
	}

	osutil.AddSecretRingFlag()
	setCamdevVars()

	baseURL := c.env.m["CAMLI_BASEURL"]
	if baseURL == "" {
		return errors.New("CAMLI_BASEURL is not set")
	}

	cl := client.New(baseURL)
	signer, err := cl.Signer()
	if err != nil {
		return err
	}
	ClientId := make(map[string]string)
	ClientSecret := make(map[string]string)
	for name := range importer.All() {
		ClientId[name] = "fakeStaticClientId"
		ClientSecret[name] = "fakeStaticClientSecret"
	}
	hc := importer.HostConfig{
		BaseURL:      baseURL,
		Prefix:       importerPrefix,
		Target:       cl,
		BlobSource:   cl,
		Signer:       signer,
		Search:       cl,
		ClientId:     ClientId,
		ClientSecret: ClientSecret,
	}

	for name, imp := range importer.All() {
		mk, ok := imp.(importer.TestDataMaker)
		if !ok {
			continue
		}

		tr := mk.MakeTestData()

		hc.HTTPClient = &http.Client{Transport: tr}
		host, err := importer.NewHost(hc)
		if err != nil {
			return fmt.Errorf("could not obtain Host: %v", err)
		}

		rc, err := importer.CreateAccount(host, name)
		if err != nil {
			return err
		}

		if err := mk.SetTestAccount(rc.AccountNode()); err != nil {
			return fmt.Errorf("could not set fake account node for importer %v: %v", name, err)
		}

		if err := imp.Run(rc); err != nil {
			return err
		}
	}
	return nil
}
Ejemplo n.º 9
0
// Main sends on up when it's running, and shuts down when it receives from down.
func Main(up chan<- struct{}, down <-chan struct{}) {
	flag.Parse()

	if *flagVersion {
		fmt.Fprintf(os.Stderr, "camlistored version: %s\nGo version: %s (%s/%s)\n",
			buildinfo.Version(), runtime.Version(), runtime.GOOS, runtime.GOARCH)
		return
	}
	if legalprint.MaybePrint(os.Stderr) {
		return
	}
	if env.OnGCE() {
		log.SetOutput(gce.LogWriter())
	}

	if *flagReindex {
		index.SetImpendingReindex()
	}

	log.Printf("Starting camlistored version %s; Go %s (%s/%s)", buildinfo.Version(), runtime.Version(),
		runtime.GOOS, runtime.GOARCH)

	shutdownc := make(chan io.Closer, 1) // receives io.Closer to cleanly shut down
	go handleSignals(shutdownc)

	// In case we're running in a Docker container with no
	// filesytem from which to load the root CAs, this
	// conditionally installs a static set if necessary. We do
	// this before we load the config file, which might come from
	// an https URL.
	httputil.InstallCerts()

	config, isNewConfig, err := loadConfig(*flagConfigFile)
	if err != nil {
		exitf("Error loading config file: %v", err)
	}

	ws := webserver.New()
	listen, baseURL := listenAndBaseURL(config)

	hostname, err := certHostname(listen, baseURL)
	if err != nil {
		exitf("Bad baseURL or listen address: %v", err)
	}
	setupTLS(ws, config, hostname)

	err = ws.Listen(listen)
	if err != nil {
		exitf("Listen: %v", err)
	}

	if baseURL == "" {
		baseURL = ws.ListenURL()
	}

	shutdownCloser, err := config.InstallHandlers(ws, baseURL, *flagReindex, nil)
	if err != nil {
		exitf("Error parsing config: %v", err)
	}
	shutdownc <- shutdownCloser

	urlToOpen := baseURL
	if !isNewConfig {
		// user may like to configure the server at the initial startup,
		// open UI if this is not the first run with a new config file.
		urlToOpen += config.UIPath
	}

	if *flagOpenBrowser {
		go osutil.OpenURL(urlToOpen)
	}

	go ws.Serve()
	if flagPollParent {
		osutil.DieOnParentDeath()
	}

	if err := config.StartApps(); err != nil {
		exitf("StartApps: %v", err)
	}

	for appName, appURL := range config.AppURL() {
		addr, err := netutil.HostPort(appURL)
		if err != nil {
			log.Printf("Could not get app %v address: %v", appName, err)
			continue
		}
		if err := netutil.AwaitReachable(addr, 5*time.Second); err != nil {
			log.Printf("Could not reach app %v: %v", appName, err)
		}
	}
	log.Printf("Available on %s", urlToOpen)

	if env.OnGCE() && strings.HasPrefix(baseURL, "https://") {
		go redirectFromHTTP(baseURL)
	}

	// Block forever, except during tests.
	up <- struct{}{}
	<-down
	osExit(0)
}