forked from pydio/cells
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vars.go
245 lines (219 loc) · 6.78 KB
/
vars.go
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*
* Copyright (c) 2018. Abstrium SAS <team (at) pydio.com>
* This file is part of Pydio Cells.
*
* Pydio Cells is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Pydio Cells is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Pydio Cells. If not, see <http://www.gnu.org/licenses/>.
*
* The latest code can be found at <https://pydio.com>.
*/
// Package config provides tools for managing configurations
package config
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"sync"
"time"
"github.com/micro/go-config/reader"
"github.com/pydio/go-os/config"
"github.com/pydio/go-os/config/source/file"
"github.com/spf13/viper"
"github.com/pydio/cells/common"
"github.com/pydio/cells/common/config/envvar"
file2 "github.com/pydio/cells/common/config/file"
"github.com/pydio/cells/common/config/memory"
"github.com/pydio/cells/common/config/remote"
)
var (
PydioConfigDir = ApplicationDataDir()
PydioConfigFile = "pydio.json"
VersionsStore file2.VersionsStore
defaultConfig *Config
once sync.Once
remoteSourceOnce sync.Once
remoteSource bool
)
// Config wrapper around micro Config
type Config struct {
config.Config
}
func initVersionStore() {
var e error
VersionsStore, e = file2.NewStore(PydioConfigDir)
if e != nil {
//log.Println("Could not open versions store", e)
}
written, err := file2.WriteIfNotExists(filepath.Join(PydioConfigDir, PydioConfigFile), SampleConfig)
if err != nil {
fmt.Println("Error while trying to create default config file")
os.Exit(1)
}
if written && VersionsStore != nil {
var data interface{}
if e := json.Unmarshal([]byte(SampleConfig), data); e == nil {
VersionsStore.Put(&file2.Version{
User: "cli",
Date: time.Now(),
Log: "Initialize with sample config",
Data: data,
})
}
}
}
// Default Config with initialisation
func Default() config.Config {
once.Do(func() {
if GetRemoteSource() {
// Warning, loading remoteSource will trigger a call to defaults.NewClient()
defaultConfig = &Config{config.NewConfig(
config.WithSource(newRemoteSource(config.SourceName("config"))),
config.PollInterval(10*time.Second),
)}
} else {
initVersionStore()
defaultConfig = &Config{config.NewConfig(
config.WithSource(newLocalSource()),
config.PollInterval(10*time.Second),
)}
if save, e := UpgradeConfigsIfRequired(defaultConfig); e == nil && save {
e2 := saveConfig(defaultConfig, common.PYDIO_SYSTEM_USERNAME, "Configs upgrades applied")
if e2 != nil {
fmt.Println("[Configs] Error while saving upgraded configs")
} else {
fmt.Println("[Configs] successfully saved config after upgrade - Reloading from source")
}
// Reload fully from source to make sure it's in sync with JSON
defaultConfig = &Config{config.NewConfig(
config.WithSource(newLocalSource()),
config.PollInterval(10*time.Second),
)}
} else if e != nil {
fmt.Printf("[Configs] something went wrong while upgrading configs: %s\n", e.Error())
}
}
})
return defaultConfig
}
func newEnvSource() config.Source {
return envvar.NewSource(
config.SourceName("PYDIO"),
)
}
func newLocalSource() config.Source {
// If file exists and is not empty, check it has valid JSON content
fName := filepath.Join(PydioConfigDir, PydioConfigFile)
if data, err := ioutil.ReadFile(fName); err == nil && len(data) > 0 {
var whatever map[string]interface{}
if e := json.Unmarshal(data, &whatever); e != nil {
errColor := "\033[1;31m%s\033[0m"
fmt.Println("**************************************************************************************")
fmt.Println("It seems that your configuration file contains invalid JSON. Did you edit it manually?")
fmt.Println("File is located at " + fName)
fmt.Println("Error was: ", fmt.Sprintf(errColor, e.Error()))
fmt.Println("")
fmt.Printf(errColor, "FATAL ERROR : Aborting now\n")
fmt.Println("**************************************************************************************")
log.Fatal(e)
}
}
return file.NewSource(
config.SourceName(filepath.Join(PydioConfigDir, PydioConfigFile)),
)
}
func newRemoteSource(opts ...config.SourceOption) config.Source {
return remote.NewSource(opts...)
}
func Get(path ...string) reader.Value {
return Default().Get(path...)
}
func Set(val interface{}, path ...string) {
if GetRemoteSource() {
remote.UpdateRemote("config", val, path...)
return
}
tmpConfig := Config{Config: config.NewConfig(config.WithSource(memory.NewSource(Default().Bytes())))}
tmpConfig.Set(val, path...)
// Make sure to init vault
Vault()
// Filter values
hasFilter := false
for _, p := range registeredVaultKeys {
savedUuid := Default().Get(p...).String("")
newConfig := tmpConfig.Get(p...).String("")
if newConfig != savedUuid {
hasFilter = true
if savedUuid != "" {
vaultSource.Delete(savedUuid, true)
}
if newConfig != "" {
// Replace value with a secret Uuid
fmt.Println("Replacing config value with a secret UUID", strings.Join(p, "/"))
newUuid := NewKeyForSecret()
e := vaultSource.Set(newUuid, newConfig, true)
if e != nil {
fmt.Println("Something went wrong when saving file!", e.Error())
}
tmpConfig.Set(newUuid, p...)
}
}
}
if hasFilter {
// Replace fully from tmp
// Does not work probably due to a bug in the underlying TP library
// Default().Set(tmpConfig.Get())
// Rather explicitly replace all values.
var all map[string]interface{}
json.Unmarshal(tmpConfig.Bytes(), &all)
for k, v := range all {
Default().Set(v, k)
}
} else {
// Just update default config
Default().Set(val, path...)
}
}
func Del(path ...string) {
if GetRemoteSource() {
remote.DeleteRemote("config", path...)
return
}
Default().Del(path...)
}
func Watch(path ...string) (config.Watcher, error) {
return Default().Watch(path...)
}
func (c *Config) Unmarshal(val interface{}) error {
return c.Config.Get().Scan(&val)
}
func (c *Config) UnmarshalKey(key string, val interface{}) error {
return c.Config.Get(key).Scan(&val)
}
func watchConfig() {
}
// GetJsonPath build path for json that contain the local config
func GetJsonPath() string {
return filepath.Join(PydioConfigDir, PydioConfigFile)
}
func GetRemoteSource() bool {
remoteSourceOnce.Do(func() {
if viper.GetString("registry_cluster_routes") != "" {
remoteSource = true
}
})
return remoteSource
}