Exemplo n.º 1
0
func TestStarts(t *testing.T) {
	td, err := ioutil.TempDir("", "camlistored-test")
	if err != nil {
		t.Fatal(err)
	}
	defer os.RemoveAll(td)

	fakeHome := filepath.Join(td, "fakeHome")
	confDir := filepath.Join(fakeHome, "conf")

	defer pushEnv("HOME", fakeHome)()
	defer pushEnv("HOMEPATH", fakeHome)()
	defer pushEnv("APPDATA", filepath.Join(fakeHome, "appdata"))()
	defer pushEnv("CAMLI_CONFIG_DIR", confDir)()

	if _, err := os.Stat(osutil.CamliConfigDir()); !os.IsNotExist(err) {
		t.Fatalf("expected conf dir %q to not exist", osutil.CamliConfigDir())
	}
	if !strings.Contains(osutil.CamliBlobRoot(), td) {
		t.Fatalf("blob root %q should contain the temp dir %q", osutil.CamliBlobRoot(), td)
	}
	if _, err := os.Stat(osutil.CamliBlobRoot()); !os.IsNotExist(err) {
		t.Fatalf("expected blobroot dir %q to not exist", osutil.CamliBlobRoot())
	}
	if fi, err := os.Stat(osutil.UserServerConfigPath()); !os.IsNotExist(err) {
		t.Errorf("expected no server config file; got %v, %v", fi, err)
	}

	mkdir(t, confDir)
	*flagOpenBrowser = false
	defaultListenAddr = ":0"

	up := make(chan struct{})
	down := make(chan struct{})
	dead := make(chan int, 1)
	osExit = func(status int) {
		dead <- status
		close(dead)
		runtime.Goexit()
	}
	go Main(up, down)
	select {
	case status := <-dead:
		t.Errorf("os.Exit(%d) before server came up", status)
		return
	case <-up:
		t.Logf("server is up")
	case <-time.After(10 * time.Second):
		t.Fatal("timeout starting server")
	}

	if _, err := os.Stat(osutil.UserServerConfigPath()); err != nil {
		t.Errorf("expected a server config file; got %v", err)
	}

	down <- struct{}{}
	<-dead
}
Exemplo n.º 2
0
// WriteDefaultConfigFile generates a new default high-level server configuration
// file at filePath. If useSQLite, the default indexer will use SQLite, otherwise
// leveldb. If filePath already exists, it is overwritten.
func WriteDefaultConfigFile(filePath string, useSQLite bool) error {
	conf := defaultBaseConfig
	blobDir := osutil.CamliBlobRoot()
	if err := wkfs.MkdirAll(blobDir, 0700); err != nil {
		return fmt.Errorf("Could not create default blobs directory: %v", err)
	}
	conf.BlobPath = blobDir
	conf.PackRelated = true
	if useSQLite {
		conf.SQLite = filepath.Join(osutil.CamliVarDir(), "index.sqlite")
	} else {
		conf.LevelDB = filepath.Join(osutil.CamliVarDir(), "index.leveldb")
	}

	keyID, secretRing, err := getOrMakeKeyring()
	if err != nil {
		return err
	}
	conf.Identity = keyID
	conf.IdentitySecretRing = secretRing

	confData, err := json.MarshalIndent(conf, "", "    ")
	if err != nil {
		return fmt.Errorf("Could not json encode config file : %v", err)
	}

	if err := wkfs.WriteFile(filePath, confData, 0600); err != nil {
		return fmt.Errorf("Could not create or write default server config: %v", err)
	}

	return nil
}
Exemplo n.º 3
0
func newDefaultConfigFile(path string) error {
	conf := defaultConfigFile{
		Listen:      defaultListenAddr,
		HTTPS:       false,
		Auth:        "localhost",
		ReplicateTo: make([]interface{}, 0),
	}
	blobDir := osutil.CamliBlobRoot()
	if err := os.MkdirAll(blobDir, 0700); err != nil {
		return fmt.Errorf("Could not create default blobs directory: %v", err)
	}
	conf.BlobPath = blobDir
	if sqlite.CompiledIn() {
		conf.SQLite = filepath.Join(osutil.CamliVarDir(), "camli-index.db")
		if fi, err := os.Stat(conf.SQLite); os.IsNotExist(err) || (fi != nil && fi.Size() == 0) {
			if err := initSQLiteDB(conf.SQLite); err != nil {
				log.Printf("Error initializing DB %s: %v", conf.SQLite, err)
			}
		}
	} else {
		conf.KVFile = filepath.Join(osutil.CamliVarDir(), "camli-index.kvdb")
	}

	var keyId string
	secRing := osutil.IdentitySecretRing()
	_, err := os.Stat(secRing)
	switch {
	case err == nil:
		keyId, err = jsonsign.KeyIdFromRing(secRing)
		if err != nil {
			return fmt.Errorf("Could not find any keyId in file %q: %v", secRing, err)
		}
		log.Printf("Re-using identity with keyId %q found in file %s", keyId, secRing)
	case os.IsNotExist(err):
		keyId, err = jsonsign.GenerateNewSecRing(secRing)
		if err != nil {
			return fmt.Errorf("Could not generate new secRing at file %q: %v", secRing, err)
		}
		log.Printf("Generated new identity with keyId %q in file %s", keyId, secRing)
	}
	if err != nil {
		return fmt.Errorf("Could not stat secret ring %q: %v", secRing, err)
	}
	conf.Identity = keyId
	conf.IdentitySecretRing = secRing

	confData, err := json.MarshalIndent(conf, "", "    ")
	if err != nil {
		return fmt.Errorf("Could not json encode config file : %v", err)
	}

	if err := ioutil.WriteFile(path, confData, 0600); err != nil {
		return fmt.Errorf("Could not create or write default server config: %v", err)
	}

	return nil
}
Exemplo n.º 4
0
func newDefaultConfigFile(path string) error {
	conf := defaultConfigFile{
		Listen:      ":3179",
		HTTPS:       false,
		Auth:        "localhost",
		ReplicateTo: make([]interface{}, 0),
	}
	blobDir := osutil.CamliBlobRoot()
	if err := os.MkdirAll(blobDir, 0700); err != nil {
		return fmt.Errorf("Could not create default blobs directory: %v", err)
	}
	conf.BlobPath = blobDir
	conf.SQLite = filepath.Join(osutil.CamliVarDir(), "camli-index.db")

	var keyId string
	secRing := osutil.IdentitySecretRing()
	_, err := os.Stat(secRing)
	switch {
	case err == nil:
		keyId, err = jsonsign.KeyIdFromRing(secRing)
		log.Printf("Re-using identity with keyId %q found in file %s", keyId, secRing)
	case os.IsNotExist(err):
		keyId, err = jsonsign.GenerateNewSecRing(secRing)
		log.Printf("Generated new identity with keyId %q in file %s", keyId, secRing)
	}
	if err != nil {
		return fmt.Errorf("Secret ring: %v", err)
	}
	conf.Identity = keyId
	conf.IdentitySecretRing = secRing

	confData, err := json.MarshalIndent(conf, "", "    ")
	if err != nil {
		return fmt.Errorf("Could not json encode config file : %v", err)
	}

	if err := ioutil.WriteFile(path, confData, 0600); err != nil {
		return fmt.Errorf("Could not create or write default server config: %v", err)
	}

	if sqlite.CompiledIn() {
		if fi, err := os.Stat(conf.SQLite); os.IsNotExist(err) || (fi != nil && fi.Size() == 0) {
			if err := initSQLiteDB(conf.SQLite); err != nil {
				log.Printf("Error initializing DB %s: %v", conf.SQLite, err)
			}
		}
	} else {
		log.Printf("Wrote config file assuming SQLite, but SQLite is not available. Recompile with SQLite or modify %s and pick an index type. Please see http://camlistore.org/docs/server-config#windows", path)
		return errors.New("Newly written configuration not usable.")
	}
	return nil
}
Exemplo n.º 5
0
func newDefaultConfigFile(path string) error {
	conf :=
		`{
	"listen": "localhost:3179",
	"TLS": false,
	"auth": "userpass:camlistore:pass3179:+localhost",
        "identity": "%KEYID%",
	"identitySecretRing": "%SECRING%",
	"blobPath": "%BLOBPATH%",
	"mysql": "",
	"mongo": "",
	"s3": "",
	"replicateTo": [],
	"publish": {}
}
`
	blobDir := osutil.CamliBlobRoot()
	if err := os.MkdirAll(blobDir, 0700); err != nil {
		return fmt.Errorf("Could not create default blobs directory: %v", err)
	}
	conf = strings.Replace(conf, "%BLOBPATH%", blobDir, 1)

	var keyId string
	secRing := osutil.IdentitySecretRing()
	_, err := os.Stat(secRing)
	switch {
	case err == nil:
		keyId, err = keyIdFromRing(secRing)
		log.Printf("Re-using identity with keyId %q found in file %s", keyId, secRing)
	case os.IsNotExist(err):
		keyId, err = generateNewSecRing(secRing)
		log.Printf("Generated new identity with keyId %q in file %s", keyId, secRing)
	}
	if err != nil {
		return fmt.Errorf("Secret ring: %v", err)
	}

	conf = strings.Replace(conf, "%SECRING%", secRing, 1)
	conf = strings.Replace(conf, "%KEYID%", keyId, 1)
	if err := ioutil.WriteFile(path, []byte(conf), 0600); err != nil {
		return fmt.Errorf("Could not create or write default server config: %v", err)
	}
	return nil
}
Exemplo n.º 6
0
func newDefaultConfigFile(path string) error {
	conf := defaultConfigFile{
		Listen:      ":3179",
		HTTPS:       false,
		Auth:        "localhost",
		ReplicateTo: make([]interface{}, 0),
	}

	blobDir := osutil.CamliBlobRoot()
	if err := os.MkdirAll(blobDir, 0700); err != nil {
		return fmt.Errorf("Could not create default blobs directory: %v", err)
	}
	conf.BlobPath = blobDir

	var keyId string
	secRing := osutil.IdentitySecretRing()
	_, err := os.Stat(secRing)
	switch {
	case err == nil:
		keyId, err = keyIdFromRing(secRing)
		log.Printf("Re-using identity with keyId %q found in file %s", keyId, secRing)
	case os.IsNotExist(err):
		keyId, err = generateNewSecRing(secRing)
		log.Printf("Generated new identity with keyId %q in file %s", keyId, secRing)
	}
	if err != nil {
		return fmt.Errorf("Secret ring: %v", err)
	}
	conf.Identity = keyId
	conf.IdentitySecretRing = secRing

	confData, err := json.MarshalIndent(conf, "", "    ")
	if err != nil {
		return fmt.Errorf("Could not json encode config file : %v", err)
	}

	if err := ioutil.WriteFile(path, confData, 0600); err != nil {
		return fmt.Errorf("Could not create or write default server config: %v", err)
	}
	return nil
}