2015-06-26 04:56:48 +02:00
|
|
|
|
/*
|
|
|
|
|
* Quick - Quick key value store for config files and persistent state files
|
|
|
|
|
*
|
2019-04-09 20:39:42 +02:00
|
|
|
|
* Quick (C) 2015, 2016, 2017 MinIO, Inc.
|
2015-06-26 04:56:48 +02:00
|
|
|
|
*
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package quick
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"fmt"
|
|
|
|
|
"io/ioutil"
|
|
|
|
|
"os"
|
|
|
|
|
"reflect"
|
|
|
|
|
"sync"
|
|
|
|
|
|
2015-08-23 03:34:00 +02:00
|
|
|
|
"github.com/fatih/structs"
|
2016-04-07 01:05:30 +02:00
|
|
|
|
"github.com/minio/minio/pkg/safe"
|
2020-12-03 20:35:18 +01:00
|
|
|
|
etcd "go.etcd.io/etcd/clientv3"
|
2015-06-26 04:56:48 +02:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Config - generic config interface functions
|
|
|
|
|
type Config interface {
|
|
|
|
|
String() string
|
|
|
|
|
Version() string
|
2016-04-29 23:24:10 +02:00
|
|
|
|
Save(string) error
|
|
|
|
|
Load(string) error
|
2015-06-26 04:56:48 +02:00
|
|
|
|
Data() interface{}
|
2016-04-29 23:24:10 +02:00
|
|
|
|
Diff(Config) ([]structs.Field, error)
|
|
|
|
|
DeepDiff(Config) ([]structs.Field, error)
|
2015-06-26 04:56:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-23 20:05:56 +02:00
|
|
|
|
// config - implements quick.Config interface
|
|
|
|
|
type config struct {
|
2015-08-14 01:24:48 +02:00
|
|
|
|
data interface{}
|
2018-07-12 23:12:40 +02:00
|
|
|
|
clnt *etcd.Client
|
2015-06-26 04:56:48 +02:00
|
|
|
|
lock *sync.RWMutex
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Version returns the current config file format version
|
2018-04-23 20:05:56 +02:00
|
|
|
|
func (d config) Version() string {
|
2015-08-14 01:24:48 +02:00
|
|
|
|
st := structs.New(d.data)
|
2017-03-22 18:23:25 +01:00
|
|
|
|
f := st.Field("Version")
|
|
|
|
|
return f.Value().(string)
|
2016-01-15 03:17:24 +01:00
|
|
|
|
}
|
|
|
|
|
|
2015-06-26 04:56:48 +02:00
|
|
|
|
// String converts JSON config to printable string
|
2018-04-23 20:05:56 +02:00
|
|
|
|
func (d config) String() string {
|
2015-08-14 01:24:48 +02:00
|
|
|
|
configBytes, _ := json.MarshalIndent(d.data, "", "\t")
|
2015-06-26 04:56:48 +02:00
|
|
|
|
return string(configBytes)
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-03 19:22:09 +01:00
|
|
|
|
// Save writes config data to a file. Data format
|
|
|
|
|
// is selected based on file extension or JSON if
|
|
|
|
|
// not provided.
|
2018-04-23 20:05:56 +02:00
|
|
|
|
func (d config) Save(filename string) error {
|
2015-06-26 04:56:48 +02:00
|
|
|
|
d.lock.Lock()
|
|
|
|
|
defer d.lock.Unlock()
|
|
|
|
|
|
2018-04-23 20:05:56 +02:00
|
|
|
|
if d.clnt != nil {
|
|
|
|
|
return saveFileConfigEtcd(filename, d.clnt, d.data)
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-22 18:23:25 +01:00
|
|
|
|
// Backup if given file exists
|
|
|
|
|
oldData, err := ioutil.ReadFile(filename)
|
|
|
|
|
if err != nil {
|
|
|
|
|
// Ignore if file does not exist.
|
|
|
|
|
if !os.IsNotExist(err) {
|
2016-04-29 23:24:10 +02:00
|
|
|
|
return err
|
2016-01-15 03:17:24 +01:00
|
|
|
|
}
|
2017-03-22 18:23:25 +01:00
|
|
|
|
} else {
|
2016-01-15 03:17:24 +01:00
|
|
|
|
// Save read data to the backup file.
|
2017-03-22 18:23:25 +01:00
|
|
|
|
backupFilename := filename + ".old"
|
|
|
|
|
if err = writeFile(backupFilename, oldData); err != nil {
|
2016-04-29 23:24:10 +02:00
|
|
|
|
return err
|
2016-01-15 03:17:24 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2015-08-04 01:17:21 +02:00
|
|
|
|
|
2016-01-15 03:17:24 +01:00
|
|
|
|
// Save data.
|
2017-03-03 19:22:09 +01:00
|
|
|
|
return saveFileConfig(filename, d.data)
|
2015-06-26 04:56:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-03-03 19:22:09 +01:00
|
|
|
|
// Load - loads config from file and merge with currently set values
|
|
|
|
|
// File content format is guessed from the file name extension, if not
|
|
|
|
|
// available, consider that we have JSON.
|
2018-04-23 20:05:56 +02:00
|
|
|
|
func (d config) Load(filename string) error {
|
2015-08-14 01:24:48 +02:00
|
|
|
|
d.lock.Lock()
|
|
|
|
|
defer d.lock.Unlock()
|
2018-04-23 20:05:56 +02:00
|
|
|
|
if d.clnt != nil {
|
|
|
|
|
return loadFileConfigEtcd(filename, d.clnt, d.data)
|
|
|
|
|
}
|
2017-09-19 21:37:56 +02:00
|
|
|
|
return loadFileConfig(filename, d.data)
|
2015-06-26 04:56:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Data - grab internal data map for reading
|
2018-04-23 20:05:56 +02:00
|
|
|
|
func (d config) Data() interface{} {
|
2015-08-14 01:24:48 +02:00
|
|
|
|
return d.data
|
2015-06-26 04:56:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-02-03 03:18:52 +01:00
|
|
|
|
// Diff - list fields that are in A but not in B
|
2018-04-23 20:05:56 +02:00
|
|
|
|
func (d config) Diff(c Config) ([]structs.Field, error) {
|
2015-08-04 01:17:21 +02:00
|
|
|
|
var fields []structs.Field
|
2015-06-26 04:56:48 +02:00
|
|
|
|
|
|
|
|
|
currFields := structs.Fields(d.Data())
|
|
|
|
|
newFields := structs.Fields(c.Data())
|
|
|
|
|
|
2017-01-17 02:05:00 +01:00
|
|
|
|
var found bool
|
2015-06-26 04:56:48 +02:00
|
|
|
|
for _, currField := range currFields {
|
|
|
|
|
found = false
|
|
|
|
|
for _, newField := range newFields {
|
|
|
|
|
if reflect.DeepEqual(currField.Name(), newField.Name()) {
|
|
|
|
|
found = true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if !found {
|
|
|
|
|
fields = append(fields, *currField)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return fields, nil
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-06 19:16:59 +02:00
|
|
|
|
// DeepDiff - list fields in A that are missing or not equal to fields in B
|
2018-04-23 20:05:56 +02:00
|
|
|
|
func (d config) DeepDiff(c Config) ([]structs.Field, error) {
|
2015-08-04 01:17:21 +02:00
|
|
|
|
var fields []structs.Field
|
2015-06-26 04:56:48 +02:00
|
|
|
|
|
|
|
|
|
currFields := structs.Fields(d.Data())
|
|
|
|
|
newFields := structs.Fields(c.Data())
|
|
|
|
|
|
2017-01-17 02:05:00 +01:00
|
|
|
|
var found bool
|
2015-06-26 04:56:48 +02:00
|
|
|
|
for _, currField := range currFields {
|
|
|
|
|
found = false
|
|
|
|
|
for _, newField := range newFields {
|
|
|
|
|
if reflect.DeepEqual(currField.Value(), newField.Value()) {
|
|
|
|
|
found = true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if !found {
|
|
|
|
|
fields = append(fields, *currField)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return fields, nil
|
|
|
|
|
}
|
2017-03-22 18:23:25 +01:00
|
|
|
|
|
2018-08-15 06:41:47 +02:00
|
|
|
|
// CheckData - checks the validity of config data. Data should be of
|
2017-03-22 18:23:25 +01:00
|
|
|
|
// type struct and contain a string type field called "Version".
|
2018-08-15 06:41:47 +02:00
|
|
|
|
func CheckData(data interface{}) error {
|
2017-03-22 18:23:25 +01:00
|
|
|
|
if !structs.IsStruct(data) {
|
|
|
|
|
return fmt.Errorf("interface must be struct type")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
st := structs.New(data)
|
|
|
|
|
f, ok := st.FieldOk("Version")
|
|
|
|
|
if !ok {
|
|
|
|
|
return fmt.Errorf("struct ‘%s’ must have field ‘Version’", st.Name())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if f.Kind() != reflect.String {
|
|
|
|
|
return fmt.Errorf("‘Version’ field in struct ‘%s’ must be a string type", st.Name())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// writeFile writes data to a file named by filename.
|
|
|
|
|
// If the file does not exist, writeFile creates it;
|
|
|
|
|
// otherwise writeFile truncates it before writing.
|
|
|
|
|
func writeFile(filename string, data []byte) error {
|
|
|
|
|
safeFile, err := safe.CreateFile(filename)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
_, err = safeFile.Write(data)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
return safeFile.Close()
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-23 20:05:56 +02:00
|
|
|
|
// GetVersion - extracts the version information.
|
2018-07-12 23:12:40 +02:00
|
|
|
|
func GetVersion(filename string, clnt *etcd.Client) (version string, err error) {
|
2017-05-06 19:16:59 +02:00
|
|
|
|
var qc Config
|
2018-04-23 20:05:56 +02:00
|
|
|
|
qc, err = LoadConfig(filename, clnt, &struct {
|
2017-05-06 19:16:59 +02:00
|
|
|
|
Version string
|
2018-04-23 20:05:56 +02:00
|
|
|
|
}{})
|
|
|
|
|
if err != nil {
|
2017-05-06 19:16:59 +02:00
|
|
|
|
return "", err
|
|
|
|
|
}
|
2018-04-23 20:05:56 +02:00
|
|
|
|
return qc.Version(), nil
|
2017-05-06 19:16:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-23 20:05:56 +02:00
|
|
|
|
// LoadConfig - loads json config from filename for the a given struct data
|
2018-07-12 23:12:40 +02:00
|
|
|
|
func LoadConfig(filename string, clnt *etcd.Client, data interface{}) (qc Config, err error) {
|
2018-04-23 20:05:56 +02:00
|
|
|
|
qc, err = NewConfig(data, clnt)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
2017-03-22 18:23:25 +01:00
|
|
|
|
}
|
2018-04-23 20:05:56 +02:00
|
|
|
|
return qc, qc.Load(filename)
|
2017-03-22 18:23:25 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-23 20:05:56 +02:00
|
|
|
|
// SaveConfig - saves given configuration data into given file as JSON.
|
2018-07-12 23:12:40 +02:00
|
|
|
|
func SaveConfig(data interface{}, filename string, clnt *etcd.Client) (err error) {
|
2018-08-15 06:41:47 +02:00
|
|
|
|
if err = CheckData(data); err != nil {
|
2018-04-23 20:05:56 +02:00
|
|
|
|
return err
|
|
|
|
|
}
|
2017-03-22 18:23:25 +01:00
|
|
|
|
var qc Config
|
2018-04-23 20:05:56 +02:00
|
|
|
|
qc, err = NewConfig(data, clnt)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
2017-03-22 18:23:25 +01:00
|
|
|
|
}
|
2018-04-23 20:05:56 +02:00
|
|
|
|
return qc.Save(filename)
|
|
|
|
|
}
|
2017-03-22 18:23:25 +01:00
|
|
|
|
|
2018-04-23 20:05:56 +02:00
|
|
|
|
// NewConfig loads config from etcd client if provided, otherwise loads from a local filename.
|
|
|
|
|
// fails when all else fails.
|
2018-07-12 23:12:40 +02:00
|
|
|
|
func NewConfig(data interface{}, clnt *etcd.Client) (cfg Config, err error) {
|
2018-08-15 06:41:47 +02:00
|
|
|
|
if err := CheckData(data); err != nil {
|
2018-04-23 20:05:56 +02:00
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
d := new(config)
|
|
|
|
|
d.data = data
|
|
|
|
|
d.clnt = clnt
|
|
|
|
|
d.lock = new(sync.RWMutex)
|
|
|
|
|
return d, nil
|
2017-03-22 18:23:25 +01:00
|
|
|
|
}
|