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 }
// loadConfig returns the server's parsed config file, locating it using the provided arg. // // The arg may be of the form: // - empty, to mean automatic (will write a default high-level config if // no cloud config is available) // - a filepath absolute or relative to the user's configuration directory, // - a URL func loadConfig(arg string) (conf *serverinit.Config, isNewConfig bool, err error) { if strings.HasPrefix(arg, "http://") || strings.HasPrefix(arg, "https://") { contents, err := slurpURL(arg, 256<<10) if err != nil { return nil, false, err } conf, err = serverinit.Load(contents) return conf, false, err } var absPath string switch { case arg == "": absPath = osutil.UserServerConfigPath() _, err = wkfs.Stat(absPath) if err != nil { if !os.IsNotExist(err) { return } conf, err = serverinit.DefaultEnvConfig() if err != nil || conf != nil { return } err = wkfs.MkdirAll(osutil.CamliConfigDir(), 0700) if err != nil { return } log.Printf("Generating template config file %s", absPath) if err = serverinit.WriteDefaultConfigFile(absPath, sqlite.CompiledIn()); err == nil { isNewConfig = true } } case filepath.IsAbs(arg): absPath = arg default: absPath = filepath.Join(osutil.CamliConfigDir(), arg) } conf, err = serverinit.LoadFile(absPath) return }
// findConfigFile returns the absolute path of the user's // config file. // The provided file may be absolute or relative // to the user's configuration directory. // If file is empty, a default high-level config is written // for the user. func findConfigFile(file string) (absPath string, isNewConfig bool, err error) { switch { case file == "": absPath = osutil.UserServerConfigPath() _, err = os.Stat(absPath) if os.IsNotExist(err) { err = os.MkdirAll(osutil.CamliConfigDir(), 0700) if err != nil { return } log.Printf("Generating template config file %s", absPath) if err = serverinit.WriteDefaultConfigFile(absPath, sqlite.CompiledIn()); err == nil { isNewConfig = true } } return case filepath.IsAbs(file): absPath = file default: absPath = filepath.Join(osutil.CamliConfigDir(), file) } _, err = os.Stat(absPath) return }