2012-03-15 12:31:06 +00:00
|
|
|
/*
|
|
|
|
Copyright 2012 Google Inc.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2014-01-23 16:18:22 +00:00
|
|
|
package serverinit
|
2012-03-15 12:31:06 +00:00
|
|
|
|
|
|
|
import (
|
2012-08-04 01:12:39 +00:00
|
|
|
"errors"
|
2012-03-15 12:31:06 +00:00
|
|
|
"fmt"
|
2013-08-09 00:50:50 +00:00
|
|
|
"net/url"
|
2012-03-15 12:31:06 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2014-03-14 19:08:43 +00:00
|
|
|
"sort"
|
2012-03-15 12:31:06 +00:00
|
|
|
"strings"
|
|
|
|
|
2013-08-04 02:54:30 +00:00
|
|
|
"camlistore.org/pkg/blob"
|
2012-03-15 12:31:06 +00:00
|
|
|
"camlistore.org/pkg/jsonconfig"
|
2012-04-12 18:39:53 +00:00
|
|
|
"camlistore.org/pkg/jsonsign"
|
2013-09-01 16:50:35 +00:00
|
|
|
"camlistore.org/pkg/osutil"
|
2014-01-23 22:40:12 +00:00
|
|
|
"camlistore.org/pkg/types/serverconfig"
|
2012-03-15 12:31:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// various parameters derived from the high-level user config
|
|
|
|
// and needed to set up the low-level config.
|
|
|
|
type configPrefixesParams struct {
|
2013-07-16 15:55:20 +00:00
|
|
|
secretRing string
|
|
|
|
keyId string
|
|
|
|
indexerPath string
|
|
|
|
blobPath string
|
2013-11-25 10:23:50 +00:00
|
|
|
packBlobs bool
|
2013-08-04 02:54:30 +00:00
|
|
|
searchOwner blob.Ref
|
2013-07-16 15:55:20 +00:00
|
|
|
shareHandlerPath string
|
2013-11-19 04:53:46 +00:00
|
|
|
flickr string
|
2013-11-28 19:09:16 +00:00
|
|
|
memoryIndex bool
|
2012-03-15 12:31:06 +00:00
|
|
|
}
|
|
|
|
|
2013-08-25 17:25:30 +00:00
|
|
|
var (
|
|
|
|
tempDir = os.TempDir
|
|
|
|
noMkdir bool // for tests to not call os.Mkdir
|
|
|
|
)
|
2013-01-11 18:52:22 +00:00
|
|
|
|
2013-06-28 15:15:48 +00:00
|
|
|
func addPublishedConfig(prefixes jsonconfig.Obj,
|
2014-01-23 22:40:12 +00:00
|
|
|
published map[string]*serverconfig.Publish,
|
2014-03-14 19:08:43 +00:00
|
|
|
sourceRoot string) ([]string, error) {
|
|
|
|
var pubPrefixes []string
|
2012-04-22 15:33:22 +00:00
|
|
|
for k, v := range published {
|
2014-02-28 17:05:13 +00:00
|
|
|
name := strings.Replace(k, "/", "", -1)
|
|
|
|
rootName := name + "Root"
|
2014-01-23 22:40:12 +00:00
|
|
|
if !v.Root.Valid() {
|
|
|
|
return nil, fmt.Errorf("Invalid or missing \"rootPermanode\" key in configuration for %s.", k)
|
2012-04-22 15:33:22 +00:00
|
|
|
}
|
2014-01-23 22:40:12 +00:00
|
|
|
if v.GoTemplate == "" {
|
|
|
|
return nil, fmt.Errorf("Missing \"goTemplate\" key in configuration for %s.", k)
|
2012-04-22 15:33:22 +00:00
|
|
|
}
|
|
|
|
ob := map[string]interface{}{}
|
|
|
|
ob["handler"] = "publish"
|
|
|
|
handlerArgs := map[string]interface{}{
|
2012-05-01 23:02:07 +00:00
|
|
|
"rootName": rootName,
|
|
|
|
"blobRoot": "/bs-and-maybe-also-index/",
|
|
|
|
"searchRoot": "/my-search/",
|
|
|
|
"cache": "/cache/",
|
2014-01-23 22:40:12 +00:00
|
|
|
"rootPermanode": []interface{}{"/sighelper/", v.Root.String()},
|
2012-04-22 15:33:22 +00:00
|
|
|
}
|
2013-06-28 15:15:48 +00:00
|
|
|
if sourceRoot != "" {
|
|
|
|
handlerArgs["sourceRoot"] = sourceRoot
|
|
|
|
}
|
2014-01-23 22:40:12 +00:00
|
|
|
handlerArgs["goTemplate"] = v.GoTemplate
|
|
|
|
if v.Style != "" {
|
|
|
|
handlerArgs["css"] = []interface{}{v.Style}
|
2013-11-22 17:12:46 +00:00
|
|
|
}
|
2014-01-23 22:40:12 +00:00
|
|
|
if v.Javascript != "" {
|
|
|
|
handlerArgs["js"] = []interface{}{v.Javascript}
|
2013-11-22 17:12:46 +00:00
|
|
|
}
|
2013-12-19 16:33:24 +00:00
|
|
|
// TODO(mpl): we'll probably want to use osutil.CacheDir() if thumbnails.kv
|
|
|
|
// contains private info? same for some of the other "camli-cache" ones?
|
|
|
|
thumbsCacheDir := filepath.Join(tempDir(), "camli-cache")
|
|
|
|
handlerArgs["scaledImage"] = map[string]interface{}{
|
|
|
|
"type": "kv",
|
2014-02-28 17:05:13 +00:00
|
|
|
"file": filepath.Join(thumbsCacheDir, name+"-thumbnails.kv"),
|
2013-12-19 16:33:24 +00:00
|
|
|
}
|
|
|
|
if err := os.MkdirAll(thumbsCacheDir, 0700); err != nil {
|
|
|
|
return nil, fmt.Errorf("Could not create cache dir %s: %v", thumbsCacheDir, err)
|
|
|
|
}
|
2012-04-22 15:33:22 +00:00
|
|
|
ob["handlerArgs"] = handlerArgs
|
2012-12-22 00:13:36 +00:00
|
|
|
prefixes[k] = ob
|
2012-04-22 15:33:22 +00:00
|
|
|
pubPrefixes = append(pubPrefixes, k)
|
|
|
|
}
|
2014-03-14 19:08:43 +00:00
|
|
|
sort.Strings(pubPrefixes)
|
2012-04-22 15:33:22 +00:00
|
|
|
return pubPrefixes, nil
|
|
|
|
}
|
|
|
|
|
2013-12-14 17:37:56 +00:00
|
|
|
func addUIConfig(params *configPrefixesParams,
|
|
|
|
prefixes jsonconfig.Obj,
|
2013-06-20 21:33:00 +00:00
|
|
|
uiPrefix string,
|
2014-03-14 19:08:43 +00:00
|
|
|
published []string,
|
2013-06-20 21:33:00 +00:00
|
|
|
sourceRoot string) {
|
2013-12-14 17:37:56 +00:00
|
|
|
|
|
|
|
args := map[string]interface{}{
|
2012-03-15 12:31:06 +00:00
|
|
|
"jsonSignRoot": "/sighelper/",
|
|
|
|
"cache": "/cache/",
|
|
|
|
}
|
|
|
|
if len(published) > 0 {
|
2013-12-14 17:37:56 +00:00
|
|
|
args["publishRoots"] = published
|
2012-03-15 12:31:06 +00:00
|
|
|
}
|
2013-06-20 21:33:00 +00:00
|
|
|
if sourceRoot != "" {
|
2013-12-14 17:37:56 +00:00
|
|
|
args["sourceRoot"] = sourceRoot
|
|
|
|
}
|
|
|
|
if params.blobPath != "" {
|
|
|
|
args["scaledImage"] = map[string]interface{}{
|
|
|
|
"type": "kv",
|
|
|
|
"file": filepath.Join(params.blobPath, "thumbmeta.kv"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
prefixes[uiPrefix] = map[string]interface{}{
|
|
|
|
"handler": "ui",
|
|
|
|
"handlerArgs": args,
|
2013-06-20 21:33:00 +00:00
|
|
|
}
|
2012-03-15 12:31:06 +00:00
|
|
|
}
|
|
|
|
|
2012-12-22 00:13:36 +00:00
|
|
|
func addMongoConfig(prefixes jsonconfig.Obj, dbname string, dbinfo string) {
|
2012-10-23 19:45:46 +00:00
|
|
|
fields := strings.Split(dbinfo, "@")
|
|
|
|
if len(fields) != 2 {
|
|
|
|
exitFailure("Malformed mongo config string. Got \"%v\", want: \"user:password@host\"", dbinfo)
|
|
|
|
}
|
|
|
|
host := fields[1]
|
|
|
|
fields = strings.Split(fields[0], ":")
|
|
|
|
if len(fields) != 2 {
|
|
|
|
exitFailure("Malformed mongo config string. Got \"%v\", want: \"user:password\"", fields[0])
|
|
|
|
}
|
2012-03-15 12:31:06 +00:00
|
|
|
ob := map[string]interface{}{}
|
|
|
|
ob["enabled"] = true
|
|
|
|
ob["handler"] = "storage-mongodbindexer"
|
|
|
|
ob["handlerArgs"] = map[string]interface{}{
|
2012-10-23 19:45:46 +00:00
|
|
|
"host": host,
|
|
|
|
"user": fields[0],
|
|
|
|
"password": fields[1],
|
2012-03-15 12:31:06 +00:00
|
|
|
"database": dbname,
|
|
|
|
"blobSource": "/bs/",
|
|
|
|
}
|
2012-12-22 00:13:36 +00:00
|
|
|
prefixes["/index-mongo/"] = ob
|
2012-03-15 12:31:06 +00:00
|
|
|
}
|
|
|
|
|
2012-12-22 00:13:36 +00:00
|
|
|
func addSQLConfig(rdbms string, prefixes jsonconfig.Obj, dbname string, dbinfo string) {
|
2012-03-15 12:31:06 +00:00
|
|
|
fields := strings.Split(dbinfo, "@")
|
|
|
|
if len(fields) != 2 {
|
2012-11-03 18:58:50 +00:00
|
|
|
exitFailure("Malformed " + rdbms + " config string. Want: \"user@host:password\"")
|
2012-03-15 12:31:06 +00:00
|
|
|
}
|
|
|
|
user := fields[0]
|
|
|
|
fields = strings.Split(fields[1], ":")
|
|
|
|
if len(fields) != 2 {
|
2012-11-03 18:58:50 +00:00
|
|
|
exitFailure("Malformed " + rdbms + " config string. Want: \"user@host:password\"")
|
2012-03-15 12:31:06 +00:00
|
|
|
}
|
|
|
|
ob := map[string]interface{}{}
|
|
|
|
ob["enabled"] = true
|
2012-11-03 18:58:50 +00:00
|
|
|
ob["handler"] = "storage-" + rdbms + "indexer"
|
2012-03-15 12:31:06 +00:00
|
|
|
ob["handlerArgs"] = map[string]interface{}{
|
|
|
|
"host": fields[0],
|
|
|
|
"user": user,
|
|
|
|
"password": fields[1],
|
|
|
|
"database": dbname,
|
|
|
|
"blobSource": "/bs/",
|
|
|
|
}
|
2012-12-22 00:13:36 +00:00
|
|
|
prefixes["/index-"+rdbms+"/"] = ob
|
2012-11-03 18:58:50 +00:00
|
|
|
}
|
|
|
|
|
2012-12-22 00:13:36 +00:00
|
|
|
func addPostgresConfig(prefixes jsonconfig.Obj, dbname string, dbinfo string) {
|
2012-11-03 18:58:50 +00:00
|
|
|
addSQLConfig("postgres", prefixes, dbname, dbinfo)
|
|
|
|
}
|
|
|
|
|
2012-12-22 00:13:36 +00:00
|
|
|
func addMySQLConfig(prefixes jsonconfig.Obj, dbname string, dbinfo string) {
|
2012-11-03 18:58:50 +00:00
|
|
|
addSQLConfig("mysql", prefixes, dbname, dbinfo)
|
2012-03-15 12:31:06 +00:00
|
|
|
}
|
|
|
|
|
2013-01-10 23:29:08 +00:00
|
|
|
func addSQLiteConfig(prefixes jsonconfig.Obj, file string) {
|
|
|
|
ob := map[string]interface{}{}
|
2013-01-11 00:16:10 +00:00
|
|
|
ob["handler"] = "storage-sqliteindexer"
|
2013-01-10 23:29:08 +00:00
|
|
|
ob["handlerArgs"] = map[string]interface{}{
|
|
|
|
"blobSource": "/bs/",
|
|
|
|
"file": file,
|
|
|
|
}
|
|
|
|
prefixes["/index-sqlite/"] = ob
|
|
|
|
}
|
|
|
|
|
2013-08-25 17:25:30 +00:00
|
|
|
func addKVConfig(prefixes jsonconfig.Obj, file string) {
|
|
|
|
prefixes["/index-kv/"] = map[string]interface{}{
|
|
|
|
"handler": "storage-kvfileindexer",
|
|
|
|
"handlerArgs": map[string]interface{}{
|
|
|
|
"blobSource": "/bs/",
|
|
|
|
"file": file,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-24 23:12:11 +00:00
|
|
|
func addS3Config(params *configPrefixesParams, prefixes jsonconfig.Obj, s3 string) error {
|
2013-09-01 16:50:35 +00:00
|
|
|
f := strings.SplitN(s3, ":", 4)
|
|
|
|
if len(f) < 3 {
|
2012-12-22 00:13:36 +00:00
|
|
|
return errors.New(`genconfig: expected "s3" field to be of form "access_key_id:secret_access_key:bucket"`)
|
|
|
|
}
|
|
|
|
accessKey, secret, bucket := f[0], f[1], f[2]
|
2013-09-01 16:50:35 +00:00
|
|
|
var hostname string
|
|
|
|
if len(f) == 4 {
|
|
|
|
hostname = f[3]
|
|
|
|
}
|
2013-01-08 18:44:59 +00:00
|
|
|
isPrimary := false
|
|
|
|
if _, ok := prefixes["/bs/"]; !ok {
|
|
|
|
isPrimary = true
|
|
|
|
}
|
|
|
|
s3Prefix := ""
|
|
|
|
if isPrimary {
|
|
|
|
s3Prefix = "/bs/"
|
|
|
|
} else {
|
|
|
|
s3Prefix = "/sto-s3/"
|
|
|
|
}
|
2013-09-01 16:50:35 +00:00
|
|
|
args := map[string]interface{}{
|
|
|
|
"aws_access_key": accessKey,
|
|
|
|
"aws_secret_access_key": secret,
|
|
|
|
"bucket": bucket,
|
|
|
|
}
|
|
|
|
if hostname != "" {
|
|
|
|
args["hostname"] = hostname
|
|
|
|
}
|
2012-12-22 00:13:36 +00:00
|
|
|
prefixes[s3Prefix] = map[string]interface{}{
|
2013-09-01 16:50:35 +00:00
|
|
|
"handler": "storage-s3",
|
|
|
|
"handlerArgs": args,
|
2012-12-22 00:13:36 +00:00
|
|
|
}
|
2013-01-08 18:44:59 +00:00
|
|
|
if isPrimary {
|
|
|
|
// TODO(mpl): s3CacheBucket
|
|
|
|
// See http://code.google.com/p/camlistore/issues/detail?id=85
|
|
|
|
prefixes["/cache/"] = map[string]interface{}{
|
|
|
|
"handler": "storage-filesystem",
|
|
|
|
"handlerArgs": map[string]interface{}{
|
2013-01-11 18:52:22 +00:00
|
|
|
"path": filepath.Join(tempDir(), "camli-cache"),
|
2013-01-08 18:44:59 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
} else {
|
2013-11-24 23:12:11 +00:00
|
|
|
if params.blobPath == "" {
|
|
|
|
panic("unexpected empty blobpath with sync-to-s3")
|
|
|
|
}
|
2013-01-08 18:44:59 +00:00
|
|
|
prefixes["/sync-to-s3/"] = map[string]interface{}{
|
|
|
|
"handler": "sync",
|
|
|
|
"handlerArgs": map[string]interface{}{
|
|
|
|
"from": "/bs/",
|
|
|
|
"to": s3Prefix,
|
2013-11-24 23:12:11 +00:00
|
|
|
"queue": map[string]interface{}{
|
|
|
|
"type": "kv",
|
|
|
|
"file": filepath.Join(params.blobPath, "sync-to-s3-queue.kv"),
|
|
|
|
},
|
2013-01-08 18:44:59 +00:00
|
|
|
},
|
|
|
|
}
|
2012-12-22 00:13:36 +00:00
|
|
|
}
|
|
|
|
return nil
|
2012-03-15 12:31:06 +00:00
|
|
|
}
|
|
|
|
|
2013-08-11 15:07:18 +00:00
|
|
|
func addGoogleDriveConfig(prefixes jsonconfig.Obj, highCfg string) error {
|
2013-07-06 20:29:17 +00:00
|
|
|
f := strings.SplitN(highCfg, ":", 4)
|
|
|
|
if len(f) != 4 {
|
2013-08-11 15:07:18 +00:00
|
|
|
return errors.New(`genconfig: expected "googledrive" field to be of form "client_id:client_secret:refresh_token:parent_id"`)
|
|
|
|
}
|
|
|
|
clientId, secret, refreshToken, parentId := f[0], f[1], f[2], f[3]
|
|
|
|
|
|
|
|
isPrimary := false
|
|
|
|
if _, ok := prefixes["/bs/"]; !ok {
|
|
|
|
isPrimary = true
|
|
|
|
}
|
|
|
|
|
|
|
|
prefix := ""
|
|
|
|
if isPrimary {
|
|
|
|
prefix = "/bs/"
|
|
|
|
} else {
|
|
|
|
prefix = "/sto-googledrive/"
|
|
|
|
}
|
|
|
|
prefixes[prefix] = map[string]interface{}{
|
|
|
|
"handler": "storage-googledrive",
|
|
|
|
"handlerArgs": map[string]interface{}{
|
|
|
|
"parent_id": parentId,
|
|
|
|
"auth": map[string]interface{}{
|
|
|
|
"client_id": clientId,
|
|
|
|
"client_secret": secret,
|
|
|
|
"refresh_token": refreshToken,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if isPrimary {
|
|
|
|
prefixes["/cache/"] = map[string]interface{}{
|
|
|
|
"handler": "storage-filesystem",
|
|
|
|
"handlerArgs": map[string]interface{}{
|
|
|
|
"path": filepath.Join(tempDir(), "camli-cache"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
prefixes["/sync-to-googledrive/"] = map[string]interface{}{
|
|
|
|
"handler": "sync",
|
|
|
|
"handlerArgs": map[string]interface{}{
|
|
|
|
"from": "/bs/",
|
|
|
|
"to": prefix,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func addGoogleCloudStorageConfig(prefixes jsonconfig.Obj, highCfg string) error {
|
|
|
|
f := strings.SplitN(highCfg, ":", 4)
|
|
|
|
if len(f) != 4 {
|
|
|
|
return errors.New(`genconfig: expected "googlecloudstorage" field to be of form "client_id:client_secret:refresh_token:bucket"`)
|
2013-07-06 20:29:17 +00:00
|
|
|
}
|
|
|
|
clientId, secret, refreshToken, bucket := f[0], f[1], f[2], f[3]
|
|
|
|
|
|
|
|
isPrimary := false
|
|
|
|
if _, ok := prefixes["/bs/"]; !ok {
|
|
|
|
isPrimary = true
|
|
|
|
}
|
|
|
|
|
|
|
|
gsPrefix := ""
|
|
|
|
if isPrimary {
|
|
|
|
gsPrefix = "/bs/"
|
|
|
|
} else {
|
2013-08-11 15:07:18 +00:00
|
|
|
gsPrefix = "/sto-googlecloudstorage/"
|
2013-07-06 20:29:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
prefixes[gsPrefix] = map[string]interface{}{
|
2013-08-11 15:07:18 +00:00
|
|
|
"handler": "storage-googlecloudstorage",
|
2013-07-06 20:29:17 +00:00
|
|
|
"handlerArgs": map[string]interface{}{
|
|
|
|
"bucket": bucket,
|
|
|
|
"auth": map[string]interface{}{
|
|
|
|
"client_id": clientId,
|
|
|
|
"client_secret": secret,
|
|
|
|
"refresh_token": refreshToken,
|
|
|
|
// If high-level config is for the common user then fullSyncOnStart = true
|
|
|
|
// Then the default just works.
|
|
|
|
//"fullSyncOnStart": true,
|
|
|
|
//"blockingFullSyncOnStart": false
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if isPrimary {
|
|
|
|
// TODO: cacheBucket like s3CacheBucket?
|
|
|
|
prefixes["/cache/"] = map[string]interface{}{
|
|
|
|
"handler": "storage-filesystem",
|
|
|
|
"handlerArgs": map[string]interface{}{
|
|
|
|
"path": filepath.Join(tempDir(), "camli-cache"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
} else {
|
2013-08-11 15:07:18 +00:00
|
|
|
prefixes["/sync-to-googlecloudstorage/"] = map[string]interface{}{
|
2013-07-06 20:29:17 +00:00
|
|
|
"handler": "sync",
|
|
|
|
"handlerArgs": map[string]interface{}{
|
|
|
|
"from": "/bs/",
|
|
|
|
"to": gsPrefix,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-06-26 19:55:47 +00:00
|
|
|
func genLowLevelPrefixes(params *configPrefixesParams, ownerName string) (m jsonconfig.Obj) {
|
2012-12-21 23:47:08 +00:00
|
|
|
m = make(jsonconfig.Obj)
|
2012-03-15 12:31:06 +00:00
|
|
|
|
2013-01-10 23:29:08 +00:00
|
|
|
haveIndex := params.indexerPath != ""
|
|
|
|
root := "/bs/"
|
|
|
|
pubKeyDest := root
|
|
|
|
if haveIndex {
|
|
|
|
root = "/bs-and-maybe-also-index/"
|
|
|
|
pubKeyDest = "/bs-and-index/"
|
|
|
|
}
|
|
|
|
|
2013-06-26 19:55:47 +00:00
|
|
|
rootArgs := map[string]interface{}{
|
|
|
|
"stealth": false,
|
|
|
|
"blobRoot": root,
|
|
|
|
"statusRoot": "/status/",
|
|
|
|
}
|
|
|
|
if ownerName != "" {
|
|
|
|
rootArgs["ownerName"] = ownerName
|
|
|
|
}
|
2012-12-21 23:47:08 +00:00
|
|
|
m["/"] = map[string]interface{}{
|
2013-06-26 19:55:47 +00:00
|
|
|
"handler": "root",
|
|
|
|
"handlerArgs": rootArgs,
|
2012-11-08 14:27:17 +00:00
|
|
|
}
|
2013-01-10 23:29:08 +00:00
|
|
|
if haveIndex {
|
|
|
|
setMap(m, "/", "handlerArgs", "searchRoot", "/my-search/")
|
|
|
|
}
|
2012-03-15 12:31:06 +00:00
|
|
|
|
2012-12-21 23:47:08 +00:00
|
|
|
m["/setup/"] = map[string]interface{}{
|
|
|
|
"handler": "setup",
|
|
|
|
}
|
2012-04-03 21:39:49 +00:00
|
|
|
|
2013-06-05 17:18:27 +00:00
|
|
|
m["/status/"] = map[string]interface{}{
|
|
|
|
"handler": "status",
|
|
|
|
}
|
|
|
|
|
2013-07-16 15:55:20 +00:00
|
|
|
if params.shareHandlerPath != "" {
|
|
|
|
m[params.shareHandlerPath] = map[string]interface{}{
|
2013-02-28 23:30:16 +00:00
|
|
|
"handler": "share",
|
|
|
|
"handlerArgs": map[string]interface{}{
|
|
|
|
"blobRoot": "/bs/",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-21 23:47:08 +00:00
|
|
|
m["/sighelper/"] = map[string]interface{}{
|
|
|
|
"handler": "jsonsign",
|
|
|
|
"handlerArgs": map[string]interface{}{
|
|
|
|
"secretRing": params.secretRing,
|
|
|
|
"keyId": params.keyId,
|
2013-01-10 23:29:08 +00:00
|
|
|
"publicKeyDest": pubKeyDest,
|
2012-03-15 12:31:06 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2013-11-25 10:23:50 +00:00
|
|
|
storageType := "filesystem"
|
|
|
|
if params.packBlobs {
|
|
|
|
storageType = "diskpacked"
|
|
|
|
}
|
2013-01-08 18:44:59 +00:00
|
|
|
if params.blobPath != "" {
|
|
|
|
m["/bs/"] = map[string]interface{}{
|
2013-11-25 10:23:50 +00:00
|
|
|
"handler": "storage-" + storageType,
|
2013-01-08 18:44:59 +00:00
|
|
|
"handlerArgs": map[string]interface{}{
|
|
|
|
"path": params.blobPath,
|
|
|
|
},
|
|
|
|
}
|
2012-03-15 12:31:06 +00:00
|
|
|
|
2013-01-08 18:44:59 +00:00
|
|
|
m["/cache/"] = map[string]interface{}{
|
2013-11-28 04:50:16 +00:00
|
|
|
"handler": "storage-" + storageType,
|
2013-01-08 18:44:59 +00:00
|
|
|
"handlerArgs": map[string]interface{}{
|
|
|
|
"path": filepath.Join(params.blobPath, "/cache"),
|
|
|
|
},
|
|
|
|
}
|
2012-03-15 12:31:06 +00:00
|
|
|
}
|
|
|
|
|
2013-11-19 04:53:46 +00:00
|
|
|
if params.flickr != "" {
|
2013-11-17 21:52:45 +00:00
|
|
|
m["/importer-flickr/"] = map[string]interface{}{
|
2013-12-28 09:43:19 +00:00
|
|
|
"handler": "importer-flickr",
|
|
|
|
"handlerArgs": map[string]interface{}{
|
|
|
|
"apiKey": params.flickr,
|
|
|
|
},
|
2013-11-17 21:52:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-10 23:29:08 +00:00
|
|
|
if haveIndex {
|
2013-08-20 18:11:37 +00:00
|
|
|
syncArgs := map[string]interface{}{
|
|
|
|
"from": "/bs/",
|
|
|
|
"to": params.indexerPath,
|
|
|
|
}
|
|
|
|
// TODO(mpl): Brad says the cond should be dest == /index-*.
|
|
|
|
// But what about when dest is index-mem and we have a local disk;
|
|
|
|
// don't we want to have an active synchandler to do the fullSyncOnStart?
|
|
|
|
// Anyway, that condition works for now.
|
|
|
|
if params.blobPath == "" {
|
|
|
|
// When our primary blob store is remote (s3 or google cloud),
|
|
|
|
// i.e not an efficient replication source, we do not want the
|
|
|
|
// synchandler to mirror to the indexer. But we still want a
|
|
|
|
// synchandler to provide the discovery for e.g tools like
|
|
|
|
// camtool sync. See http://camlistore.org/issue/201
|
|
|
|
syncArgs["idle"] = true
|
2013-11-24 23:12:11 +00:00
|
|
|
} else {
|
|
|
|
syncArgs["queue"] = map[string]interface{}{
|
|
|
|
"type": "kv",
|
|
|
|
"file": filepath.Join(params.blobPath, "sync-to-index-queue.kv"),
|
|
|
|
}
|
2013-08-20 18:11:37 +00:00
|
|
|
}
|
2013-01-10 23:29:08 +00:00
|
|
|
m["/sync/"] = map[string]interface{}{
|
2013-08-20 18:11:37 +00:00
|
|
|
"handler": "sync",
|
|
|
|
"handlerArgs": syncArgs,
|
2013-01-10 23:29:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
m["/bs-and-index/"] = map[string]interface{}{
|
|
|
|
"handler": "storage-replica",
|
|
|
|
"handlerArgs": map[string]interface{}{
|
|
|
|
"backends": []interface{}{"/bs/", params.indexerPath},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
m["/bs-and-maybe-also-index/"] = map[string]interface{}{
|
|
|
|
"handler": "storage-cond",
|
|
|
|
"handlerArgs": map[string]interface{}{
|
|
|
|
"write": map[string]interface{}{
|
|
|
|
"if": "isSchema",
|
|
|
|
"then": "/bs-and-index/",
|
|
|
|
"else": "/bs/",
|
|
|
|
},
|
|
|
|
"read": "/bs/",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2013-11-28 19:09:16 +00:00
|
|
|
searchArgs := map[string]interface{}{
|
2013-12-11 08:20:22 +00:00
|
|
|
"index": params.indexerPath,
|
|
|
|
"owner": params.searchOwner.String(),
|
2013-11-28 19:09:16 +00:00
|
|
|
}
|
|
|
|
if params.memoryIndex {
|
|
|
|
searchArgs["slurpToMemory"] = true
|
|
|
|
}
|
|
|
|
m["/my-search/"] = map[string]interface{}{
|
2013-12-11 08:20:22 +00:00
|
|
|
"handler": "search",
|
2013-11-28 19:09:16 +00:00
|
|
|
"handlerArgs": searchArgs,
|
2013-01-10 23:29:08 +00:00
|
|
|
}
|
2012-03-15 12:31:06 +00:00
|
|
|
}
|
|
|
|
|
2012-12-21 23:47:08 +00:00
|
|
|
return
|
2012-03-15 12:31:06 +00:00
|
|
|
}
|
|
|
|
|
2012-11-07 16:51:42 +00:00
|
|
|
// genLowLevelConfig returns a low-level config from a high-level config.
|
2014-01-23 22:40:12 +00:00
|
|
|
func genLowLevelConfig(conf *serverconfig.Config) (lowLevelConf *Config, err error) {
|
2012-03-19 20:09:00 +00:00
|
|
|
obj := jsonconfig.Obj{}
|
2014-01-23 22:40:12 +00:00
|
|
|
if conf.HTTPS {
|
|
|
|
if (conf.HTTPSCert != "") != (conf.HTTPSKey != "") {
|
|
|
|
return nil, errors.New("Must set both httpsCert and httpsKey (or neither to generate a self-signed cert)")
|
2012-08-04 01:12:39 +00:00
|
|
|
}
|
2014-01-23 22:40:12 +00:00
|
|
|
if conf.HTTPSCert != "" {
|
|
|
|
obj["httpsCert"] = conf.HTTPSCert
|
|
|
|
obj["httpsKey"] = conf.HTTPSKey
|
2012-08-04 01:12:39 +00:00
|
|
|
} else {
|
2014-01-23 22:40:12 +00:00
|
|
|
obj["httpsCert"] = osutil.DefaultTLSCert()
|
|
|
|
obj["httpsKey"] = osutil.DefaultTLSKey()
|
2012-08-04 01:12:39 +00:00
|
|
|
}
|
2012-03-15 12:31:06 +00:00
|
|
|
}
|
|
|
|
|
2014-01-23 22:40:12 +00:00
|
|
|
if conf.BaseURL != "" {
|
|
|
|
u, err := url.Parse(conf.BaseURL)
|
2013-08-09 00:50:50 +00:00
|
|
|
if err != nil {
|
2014-01-23 22:40:12 +00:00
|
|
|
return nil, fmt.Errorf("Error parsing baseURL %q as a URL: %v", conf.BaseURL, err)
|
2013-08-09 00:50:50 +00:00
|
|
|
}
|
2013-09-08 05:32:39 +00:00
|
|
|
if u.Path != "" && u.Path != "/" {
|
|
|
|
return nil, fmt.Errorf("baseURL can't have a path, only a scheme, host, and optional port.")
|
2012-09-16 22:20:49 +00:00
|
|
|
}
|
2013-09-08 05:32:39 +00:00
|
|
|
u.Path = ""
|
|
|
|
obj["baseURL"] = u.String()
|
2012-09-16 22:20:49 +00:00
|
|
|
}
|
2014-01-23 22:40:12 +00:00
|
|
|
if conf.Listen != "" {
|
|
|
|
obj["listen"] = conf.Listen
|
2012-08-04 11:42:10 +00:00
|
|
|
}
|
2014-01-23 22:40:12 +00:00
|
|
|
obj["https"] = conf.HTTPS
|
|
|
|
obj["auth"] = conf.Auth
|
2012-08-04 11:42:10 +00:00
|
|
|
|
2013-08-28 13:53:58 +00:00
|
|
|
username := ""
|
2014-01-23 22:40:12 +00:00
|
|
|
if conf.DBName == "" {
|
2013-08-28 13:53:58 +00:00
|
|
|
username = osutil.Username()
|
2012-03-15 12:31:06 +00:00
|
|
|
if username == "" {
|
2013-08-28 13:53:58 +00:00
|
|
|
return nil, fmt.Errorf("USER (USERNAME on windows) env var not set; needed to define dbname")
|
2012-03-15 12:31:06 +00:00
|
|
|
}
|
2014-01-23 22:40:12 +00:00
|
|
|
conf.DBName = "camli" + username
|
2012-03-15 12:31:06 +00:00
|
|
|
}
|
|
|
|
|
2012-04-12 02:22:02 +00:00
|
|
|
var indexerPath string
|
2014-01-23 22:40:12 +00:00
|
|
|
numIndexers := numSet(conf.Mongo, conf.MySQL, conf.PostgreSQL, conf.SQLite, conf.KVFile)
|
|
|
|
runIndex := conf.RunIndex.Get()
|
2012-04-12 02:22:02 +00:00
|
|
|
switch {
|
2013-01-10 23:29:08 +00:00
|
|
|
case runIndex && numIndexers == 0:
|
2013-12-11 08:20:22 +00:00
|
|
|
return nil, fmt.Errorf("Unless runIndex is set to false, you must specify an index option (kvIndexFile, mongo, mysql, postgres, sqlite).")
|
2013-01-10 23:29:08 +00:00
|
|
|
case runIndex && numIndexers != 1:
|
2013-12-11 08:20:22 +00:00
|
|
|
return nil, fmt.Errorf("With runIndex set true, you can only pick exactly one indexer (mongo, mysql, postgres, sqlite).")
|
2013-01-10 23:29:08 +00:00
|
|
|
case !runIndex && numIndexers != 0:
|
2013-12-11 08:20:22 +00:00
|
|
|
return nil, fmt.Errorf("With runIndex disabled, you can't specify any of mongo, mysql, postgres, sqlite.")
|
2014-01-23 22:40:12 +00:00
|
|
|
case conf.MySQL != "":
|
2012-04-12 02:22:02 +00:00
|
|
|
indexerPath = "/index-mysql/"
|
2014-01-23 22:40:12 +00:00
|
|
|
case conf.PostgreSQL != "":
|
2012-11-03 18:58:50 +00:00
|
|
|
indexerPath = "/index-postgres/"
|
2014-01-23 22:40:12 +00:00
|
|
|
case conf.Mongo != "":
|
2012-04-12 02:22:02 +00:00
|
|
|
indexerPath = "/index-mongo/"
|
2014-01-23 22:40:12 +00:00
|
|
|
case conf.SQLite != "":
|
2013-01-10 23:29:08 +00:00
|
|
|
indexerPath = "/index-sqlite/"
|
2014-01-23 22:40:12 +00:00
|
|
|
case conf.KVFile != "":
|
2013-08-25 17:25:30 +00:00
|
|
|
indexerPath = "/index-kv/"
|
2012-04-12 02:22:02 +00:00
|
|
|
}
|
2012-03-15 12:31:06 +00:00
|
|
|
|
2014-01-23 22:40:12 +00:00
|
|
|
entity, err := jsonsign.EntityFromSecring(conf.Identity, conf.IdentitySecretRing)
|
2012-04-12 18:39:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
armoredPublicKey, err := jsonsign.ArmoredPublicKey(entity)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-01-23 22:40:12 +00:00
|
|
|
nolocaldisk := conf.BlobPath == ""
|
2013-08-20 18:11:37 +00:00
|
|
|
if nolocaldisk {
|
2014-01-23 22:40:12 +00:00
|
|
|
if conf.S3 == "" && conf.GoogleCloudStorage == "" {
|
2013-08-20 18:11:37 +00:00
|
|
|
return nil, errors.New("You need at least one of blobPath (for localdisk) or s3 or googlecloudstorage configured for a blobserver.")
|
|
|
|
}
|
2014-01-23 22:40:12 +00:00
|
|
|
if conf.S3 != "" && conf.GoogleCloudStorage != "" {
|
2013-08-20 18:11:37 +00:00
|
|
|
return nil, errors.New("Using S3 as a primary storage and Google Cloud Storage as a mirror is not supported for now.")
|
|
|
|
}
|
2013-01-08 18:44:59 +00:00
|
|
|
}
|
|
|
|
|
2014-01-23 22:40:12 +00:00
|
|
|
if conf.ShareHandler && conf.ShareHandlerPath == "" {
|
|
|
|
conf.ShareHandlerPath = "/share/"
|
2013-07-16 15:55:20 +00:00
|
|
|
}
|
|
|
|
|
2012-03-15 12:31:06 +00:00
|
|
|
prefixesParams := &configPrefixesParams{
|
2014-01-23 22:40:12 +00:00
|
|
|
secretRing: conf.IdentitySecretRing,
|
|
|
|
keyId: conf.Identity,
|
2013-07-16 15:55:20 +00:00
|
|
|
indexerPath: indexerPath,
|
2014-01-23 22:40:12 +00:00
|
|
|
blobPath: conf.BlobPath,
|
|
|
|
packBlobs: conf.PackBlobs,
|
2013-08-04 02:54:30 +00:00
|
|
|
searchOwner: blob.SHA1FromString(armoredPublicKey),
|
2014-01-23 22:40:12 +00:00
|
|
|
shareHandlerPath: conf.ShareHandlerPath,
|
|
|
|
flickr: conf.Flickr,
|
|
|
|
memoryIndex: conf.MemoryIndex.Get(),
|
2012-03-15 12:31:06 +00:00
|
|
|
}
|
|
|
|
|
2014-01-23 22:40:12 +00:00
|
|
|
prefixes := genLowLevelPrefixes(prefixesParams, conf.OwnerName)
|
2013-01-08 18:44:59 +00:00
|
|
|
var cacheDir string
|
|
|
|
if nolocaldisk {
|
|
|
|
// Whether camlistored is run from EC2 or not, we use
|
|
|
|
// a temp dir as the cache when primary storage is S3.
|
|
|
|
// TODO(mpl): s3CacheBucket
|
|
|
|
// See http://code.google.com/p/camlistore/issues/detail?id=85
|
2013-01-11 18:52:22 +00:00
|
|
|
cacheDir = filepath.Join(tempDir(), "camli-cache")
|
2013-01-08 18:44:59 +00:00
|
|
|
} else {
|
2014-01-23 22:40:12 +00:00
|
|
|
cacheDir = filepath.Join(conf.BlobPath, "cache")
|
2013-01-08 18:44:59 +00:00
|
|
|
}
|
2013-08-25 17:25:30 +00:00
|
|
|
if !noMkdir {
|
|
|
|
if err := os.MkdirAll(cacheDir, 0700); err != nil {
|
|
|
|
return nil, fmt.Errorf("Could not create blobs cache dir %s: %v", cacheDir, err)
|
|
|
|
}
|
2012-03-15 12:31:06 +00:00
|
|
|
}
|
|
|
|
|
2014-03-14 19:08:43 +00:00
|
|
|
var published []string
|
2014-01-23 22:40:12 +00:00
|
|
|
if len(conf.Publish) > 0 {
|
2013-01-10 23:29:08 +00:00
|
|
|
if !runIndex {
|
|
|
|
return nil, fmt.Errorf("publishing requires an index")
|
|
|
|
}
|
2014-01-23 22:40:12 +00:00
|
|
|
published, err = addPublishedConfig(prefixes, conf.Publish, conf.SourceRoot)
|
2012-04-22 15:33:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Could not generate config for published: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-10 23:29:08 +00:00
|
|
|
if runIndex {
|
2014-01-23 22:40:12 +00:00
|
|
|
addUIConfig(prefixesParams, prefixes, "/ui/", published, conf.SourceRoot)
|
2013-01-10 23:29:08 +00:00
|
|
|
}
|
2012-03-15 12:31:06 +00:00
|
|
|
|
2014-01-23 22:40:12 +00:00
|
|
|
if conf.MySQL != "" {
|
|
|
|
addMySQLConfig(prefixes, conf.DBName, conf.MySQL)
|
2012-04-12 02:22:02 +00:00
|
|
|
}
|
2014-01-23 22:40:12 +00:00
|
|
|
if conf.PostgreSQL != "" {
|
|
|
|
addPostgresConfig(prefixes, conf.DBName, conf.PostgreSQL)
|
2012-11-03 18:58:50 +00:00
|
|
|
}
|
2014-01-23 22:40:12 +00:00
|
|
|
if conf.Mongo != "" {
|
|
|
|
addMongoConfig(prefixes, conf.DBName, conf.Mongo)
|
2012-12-22 00:13:36 +00:00
|
|
|
}
|
2014-01-23 22:40:12 +00:00
|
|
|
if conf.SQLite != "" {
|
|
|
|
addSQLiteConfig(prefixes, conf.SQLite)
|
2013-01-10 23:29:08 +00:00
|
|
|
}
|
2014-01-23 22:40:12 +00:00
|
|
|
if conf.KVFile != "" {
|
|
|
|
addKVConfig(prefixes, conf.KVFile)
|
2013-08-25 17:25:30 +00:00
|
|
|
}
|
2014-01-23 22:40:12 +00:00
|
|
|
if conf.S3 != "" {
|
|
|
|
if err := addS3Config(prefixesParams, prefixes, conf.S3); err != nil {
|
2012-12-22 00:13:36 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2012-04-12 02:22:02 +00:00
|
|
|
}
|
2014-01-23 22:40:12 +00:00
|
|
|
if conf.GoogleDrive != "" {
|
|
|
|
if err := addGoogleDriveConfig(prefixes, conf.GoogleDrive); err != nil {
|
2013-08-11 15:07:18 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2014-01-23 22:40:12 +00:00
|
|
|
if conf.GoogleCloudStorage != "" {
|
|
|
|
if err := addGoogleCloudStorageConfig(prefixes, conf.GoogleCloudStorage); err != nil {
|
2013-07-06 20:29:17 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2012-03-15 12:31:06 +00:00
|
|
|
|
|
|
|
obj["prefixes"] = (map[string]interface{})(prefixes)
|
|
|
|
|
|
|
|
lowLevelConf = &Config{
|
2014-01-23 22:40:12 +00:00
|
|
|
Obj: obj,
|
2012-03-15 12:31:06 +00:00
|
|
|
}
|
|
|
|
return lowLevelConf, nil
|
|
|
|
}
|
2013-01-10 23:29:08 +00:00
|
|
|
|
|
|
|
func numSet(vv ...interface{}) (num int) {
|
|
|
|
for _, vi := range vv {
|
|
|
|
switch v := vi.(type) {
|
|
|
|
case string:
|
|
|
|
if v != "" {
|
|
|
|
num++
|
|
|
|
}
|
|
|
|
case bool:
|
|
|
|
if v {
|
|
|
|
num++
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
panic("unknown type")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func setMap(m map[string]interface{}, v ...interface{}) {
|
|
|
|
if len(v) < 2 {
|
|
|
|
panic("too few args")
|
|
|
|
}
|
|
|
|
if len(v) == 2 {
|
|
|
|
m[v[0].(string)] = v[1]
|
|
|
|
return
|
|
|
|
}
|
|
|
|
setMap(m[v[0].(string)].(map[string]interface{}), v[1:]...)
|
|
|
|
}
|