aboutsummaryrefslogtreecommitdiff
path: root/weed/filer/configuration.go
blob: 9ef2f3e0f9ed8d294a91664bf69206de610e7523 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package filer

import (
	"github.com/chrislusf/seaweedfs/weed/glog"
	"github.com/chrislusf/seaweedfs/weed/util"
	"os"
	"reflect"
	"strings"
)

var (
	Stores []FilerStore
)

func (f *Filer) LoadConfiguration(config *util.ViperProxy) {

	validateOneEnabledStore(config)

	// load configuration for default filer store
	hasDefaultStoreConfigured := false
	for _, store := range Stores {
		if config.GetBool(store.GetName() + ".enabled") {
			store = reflect.New(reflect.ValueOf(store).Elem().Type()).Interface().(FilerStore)
			if err := store.Initialize(config, store.GetName()+"."); err != nil {
				glog.Fatalf("failed to initialize store for %s: %+v", store.GetName(), err)
			}
			f.SetStore(store)
			glog.V(0).Infof("configured filer store to %s", store.GetName())
			hasDefaultStoreConfigured = true
			break
		}
	}

	if !hasDefaultStoreConfigured {
		println()
		println("Supported filer stores are:")
		for _, store := range Stores {
			println("    " + store.GetName())
		}
		os.Exit(-1)
	}

	// load path-specific filer store here
	// f.Store.AddPathSpecificStore(path, store)
	storeNames := make(map[string]FilerStore)
	for _, store := range Stores {
		storeNames[store.GetName()] = store
	}
	allKeys := config.AllKeys()
	for _, key := range allKeys {
		if !strings.HasSuffix(key, ".enabled") {
			continue
		}
		key = key[:len(key)-len(".enabled")]
		if !strings.Contains(key, ".") {
			continue
		}

		parts := strings.Split(key, ".")
		storeName, storeId := parts[0], parts[1]

		store, found := storeNames[storeName]
		if !found {
			continue
		}
		store = reflect.New(reflect.ValueOf(store).Elem().Type()).Interface().(FilerStore)
		if err := store.Initialize(config, key+"."); err != nil {
			glog.Fatalf("Failed to initialize store for %s: %+v", key, err)
		}
		location := config.GetString(key + ".location")
		if location == "" {
			glog.Errorf("path-specific filer store needs %s", key+".location")
			os.Exit(-1)
		}
		f.Store.AddPathSpecificStore(location, storeId, store)

		glog.V(0).Infof("configure filer %s for %s", store.GetName(), location)
	}

}

func validateOneEnabledStore(config *util.ViperProxy) {
	enabledStore := ""
	for _, store := range Stores {
		if config.GetBool(store.GetName() + ".enabled") {
			if enabledStore == "" {
				enabledStore = store.GetName()
			} else {
				glog.Fatalf("Filer store is enabled for both %s and %s", enabledStore, store.GetName())
			}
		}
	}
}