libconfd

package
v0.5.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 21, 2021 License: Apache-2.0, Apache-2.0 Imports: 28 Imported by: 2

README

libconfd

Build Status Go Report Card GoDoc License

mini confd lib, based on confd/memkv/secconf/logger.

Example

package main

import (
	"openpitrix.io/libconfd"
)

func main() {
	cfg := libconfd.MustLoadConfig("./confd.toml")

	backendConfig := libconfd.MustLoadBackendConfig("./confd-backend.toml")
	backendClient := libconfd.MustNewBackendClient(backendConfig)

	libconfd.NewProcessor().Run(cfg, backendClient)
}

miniconfd (only support toml/etcd backend)

$ go run miniconfd.go -h

See miniconfd.go

Documentation

Overview

Package libconfd provides mini confd lib.

Index

Constants

View Source
const TomlBackendType = "libconfd-backend-toml"

Variables

This section is empty.

Functions

func RegisterBackendClient

func RegisterBackendClient(
	typeName string,
	newClient func(cfg *BackendConfig) (BackendClient, error),
)

Types

type Application

type Application struct {
	// contains filtered or unexported fields
}

func NewApplication

func NewApplication(cfg *Config, client BackendClient) *Application

func (*Application) GetValues

func (p *Application) GetValues(keys ...string)

func (*Application) Info

func (p *Application) Info(names ...string)

func (*Application) List

func (p *Application) List(re string)

func (*Application) Make

func (p *Application) Make(names ...string)

func (*Application) Run

func (p *Application) Run(opts ...Options)

type BackendClient

type BackendClient interface {
	Type() string
	GetValues(keys []string) (map[string]string, error)
	WatchPrefix(prefix string, keys []string, waitIndex uint64, stopChan chan bool) (uint64, error)
	WatchEnabled() bool
	Close() error
}

func MustNewBackendClient

func MustNewBackendClient(cfg *BackendConfig, opts ...func(*BackendConfig)) BackendClient

func NewBackendClient

func NewBackendClient(cfg *BackendConfig, opts ...func(*BackendConfig)) (BackendClient, error)

type BackendConfig

type BackendConfig struct {
	Type string   `toml:"type" json:"type"`
	Host []string `toml:"host" json:"host"`

	UserName string `toml:"user" json:"user"`
	Password string `toml:"password" json:"password"`

	ClientCAKeys string `toml:"client_ca_keys" json:"client_ca_keys"`
	ClientCert   string `toml:"client_cert" json:"client_cert"`
	ClientKey    string `toml:"client_key" json:"client_key"`

	HookKeyAdjuster func(key string) (realKey string) `toml:"-" json:"-"`
}

func LoadBackendConfig

func LoadBackendConfig(path string) (p *BackendConfig, err error)

func LoadBackendConfigFromJsonString

func LoadBackendConfigFromJsonString(s string) (p *BackendConfig, err error)

func MustLoadBackendConfig

func MustLoadBackendConfig(path string) *BackendConfig

func (*BackendConfig) Clone

func (p *BackendConfig) Clone() *BackendConfig

type Call

type Call struct {
	Config *Config
	Client BackendClient
	Error  error
	Done   chan *Call
}

type Config

type Config struct {
	// The path to confd configs.
	// If the confdir is rel path, must convert to abs path.
	//
	// abspath = filepath.Join(ConfigPath, Config.ConfDir)
	//
	ConfDir string `toml:"confdir" json:"confdir"`

	// The backend polling interval in seconds. (10)
	Interval int `toml:"interval" json:"interval"`

	// Enable noop mode. Process all template resources; skip target update.
	Noop bool `toml:"noop" json:"noop"`

	// The string to prefix to keys. ("/")
	Prefix string `toml:"prefix" json:"prefix"`

	// sync without check_cmd and reload_cmd.
	SyncOnly bool `toml:"sync_only" json:"sync_only"`

	// level which confd should log messages
	// DEBUG/INFO/WARN/ERROR/PANIC
	LogLevel string `toml:"log_level" json:"log_level"`

	// run once and exit
	Onetime bool `toml:"onetime" json:"onetime"`

	// enable watch support
	Watch bool `toml:"watch" json:"watch"`

	// keep staged files
	KeepStageFile bool `toml:"keep_stage_file" json:"keep_stage_file"`

	// PGP secret keyring (for use with crypt functions)
	PGPPrivateKey string `toml:"pgp_private_key" json:"pgp_private_key"`

	FuncMap        template.FuncMap                               `toml:"-" json:"-"`
	FuncMapUpdater func(m template.FuncMap, basefn *TemplateFunc) `toml:"-" json:"-"`

	HookAbsKeyAdjuster  func(absKey string) (realKey string) `toml:"-" json:"-"`
	HookOnCheckCmdDone  func(trName, cmd string, err error)  `toml:"-" json:"-"`
	HookOnReloadCmdDone func(trName, cmd string, err error)  `toml:"-" json:"-"`
	HookOnUpdateDone    func(trName string, err error)       `toml:"-" json:"-"`
}

func LoadConfig

func LoadConfig(path string) (p *Config, err error)

func LoadConfigFromJsonString

func LoadConfigFromJsonString(s string) (p *Config, err error)

func MustLoadConfig

func MustLoadConfig(path string) *Config

func (*Config) Clone

func (p *Config) Clone() *Config

func (*Config) GetConfigDir

func (p *Config) GetConfigDir() string

func (*Config) GetDefaultTemplateOutputDir

func (p *Config) GetDefaultTemplateOutputDir() string

func (*Config) GetTemplateDir

func (p *Config) GetTemplateDir() string

func (*Config) Save

func (p *Config) Save(name string) error

func (*Config) Valid

func (p *Config) Valid() error

type KVPair

type KVPair struct {
	Key   string
	Value string
}

func (KVPair) String

func (p KVPair) String() string

type KVStore

type KVStore struct {
	// contains filtered or unexported fields
}

A KVStore represents an in-memory key-value store safe for concurrent access.

func NewKVStore

func NewKVStore() *KVStore

New creates and initializes a new KVStore.

func (*KVStore) Del

func (p *KVStore) Del(key string)

Delete deletes the KVPair associated with key.

func (*KVStore) Exists

func (p *KVStore) Exists(key string) bool

Exists checks for the existence of key in the store.

func (*KVStore) Get

func (p *KVStore) Get(key string) (kv KVPair, ok bool)

Get gets the KVPair associated with key. If there is no KVPair associated with key, Get returns KVPair{}, false.

func (*KVStore) GetAll

func (p *KVStore) GetAll(pattern string) ([]KVPair, error)

GetAll returns a KVPair for all nodes with keys matching pattern. The syntax of patterns is the same as in path.Match.

func (*KVStore) GetAllValues

func (p *KVStore) GetAllValues(pattern string) ([]string, error)

func (*KVStore) GetValue

func (p *KVStore) GetValue(key string, v ...string) (s string, ok bool)

GetValue gets the value associated with key. If there are no values associated with key, GetValue returns "", false.

func (*KVStore) List

func (p *KVStore) List(filePath string) []string

func (*KVStore) ListDir

func (p *KVStore) ListDir(filePath string) []string

func (*KVStore) Purge

func (s *KVStore) Purge()

func (*KVStore) Set

func (s *KVStore) Set(key string, value string)

Set sets the KVPair entry associated with key to value.

type Logger

type Logger interface {
	Assert(condition bool, v ...interface{})
	Assertln(condition bool, v ...interface{})
	Assertf(condition bool, format string, v ...interface{})
	Debug(v ...interface{})
	Debugln(v ...interface{})
	Debugf(format string, v ...interface{})
	Info(v ...interface{})
	Infoln(v ...interface{})
	Infof(format string, v ...interface{})
	Warning(v ...interface{})
	Warningln(v ...interface{})
	Warningf(format string, v ...interface{})
	Error(v ...interface{})
	Errorln(v ...interface{})
	Errorf(format string, v ...interface{})
	Panic(v ...interface{})
	Panicln(v ...interface{})
	Panicf(format string, v ...interface{})
	Fatal(v ...interface{})
	Fatalln(v ...interface{})
	Fatalf(format string, v ...interface{})

	// Level: DEBUG < INFO < WARN < ERROR < PANIC < FATAL
	GetLevel() string
	SetLevel(new string) (old string)
}

Logger interface

See https://github.com/chai2010/logger

func GetLogger

func GetLogger() Logger

func NewStdLogger

func NewStdLogger(out io.Writer, prefix, level string, flag int) Logger

NewStdLogger create new logger based on std log. If level is empty string, use WARN as the default level. If flag is zore, use 'log.LstdFlags|log.Lshortfile' as the default flag. Level: DEBUG < INFO < WARN < ERROR < PANIC < FATAL

func SetLogger

func SetLogger(new Logger) (old Logger)

type Options

type Options func(*Config)

func WithAbsKeyAdjuster

func WithAbsKeyAdjuster(fn func(absKey string) (realKey string)) Options

func WithFuncMap

func WithFuncMap(maps ...template.FuncMap) Options

func WithFuncMapUpdater

func WithFuncMapUpdater(fn func(m template.FuncMap, basefn *TemplateFunc)) Options

func WithHookOnCheckCmdDone

func WithHookOnCheckCmdDone(fn func(trName, cmd string, err error)) Options

func WithHookOnReloadCmdDone

func WithHookOnReloadCmdDone(fn func(trName, cmd string, err error)) Options

func WithHookOnUpdateDone

func WithHookOnUpdateDone(fn func(trName string, err error)) Options

func WithInterval

func WithInterval(interval int) Options

func WithIntervalMode

func WithIntervalMode() Options

func WithOnetimeMode

func WithOnetimeMode() Options

func WithWatchMode

func WithWatchMode() Options

type Processor

type Processor struct {
	// contains filtered or unexported fields
}

func NewProcessor

func NewProcessor() *Processor

func (*Processor) Close

func (p *Processor) Close() error

func (*Processor) Go

func (p *Processor) Go(cfg *Config, client BackendClient, opts ...Options) *Call

func (*Processor) Run

func (p *Processor) Run(cfg *Config, client BackendClient, opts ...Options) error

type TemplateFunc

type TemplateFunc struct {
	FuncMap       map[string]interface{}
	Store         *KVStore
	PGPPrivateKey []byte

	HookKeyAdjuster func(key string) (realKey string)
}

func NewTemplateFunc

func NewTemplateFunc(
	store *KVStore, pgpPrivateKey []byte,
	hookKeyAdjuster func(key string) (realKey string),
) *TemplateFunc

func (TemplateFunc) Add

func (_ TemplateFunc) Add(a, b int) int

func (TemplateFunc) Atoi

func (_ TemplateFunc) Atoi(s string) int

func (TemplateFunc) Base

func (_ TemplateFunc) Base(path string) string

func (TemplateFunc) Base64Decode

func (_ TemplateFunc) Base64Decode(data string) string

func (TemplateFunc) Base64Encode

func (_ TemplateFunc) Base64Encode(data string) string

func (TemplateFunc) Cget

func (p TemplateFunc) Cget(key string) KVPair

func (TemplateFunc) Cgets

func (p TemplateFunc) Cgets(pattern string) []KVPair

func (TemplateFunc) Cgetv

func (p TemplateFunc) Cgetv(key string) string

func (TemplateFunc) Cgetvs

func (p TemplateFunc) Cgetvs(pattern string) []string

func (TemplateFunc) Contains

func (_ TemplateFunc) Contains(s, substr string) bool

func (TemplateFunc) Datetime

func (_ TemplateFunc) Datetime() time.Time

func (TemplateFunc) Dir

func (_ TemplateFunc) Dir(path string) string

func (TemplateFunc) Div

func (_ TemplateFunc) Div(a, b int) int

func (TemplateFunc) Exists

func (p TemplateFunc) Exists(key string) bool

func (TemplateFunc) FileExists

func (_ TemplateFunc) FileExists(filepath string) bool

func (TemplateFunc) Get

func (p TemplateFunc) Get(key string) KVPair

func (TemplateFunc) Getenv

func (_ TemplateFunc) Getenv(key string, defaultValue ...string) string

getenv retrieves the value of the environment variable named by the key. It returns the value, which will the default value if the variable is not present. If no default value was given - returns "".

func (TemplateFunc) Gets

func (p TemplateFunc) Gets(pattern string) []KVPair

func (TemplateFunc) Getv

func (p TemplateFunc) Getv(key string, v ...string) string

func (TemplateFunc) Getvs

func (p TemplateFunc) Getvs(pattern string) []string

func (TemplateFunc) Join

func (_ TemplateFunc) Join(a []string, sep string) string

func (TemplateFunc) Json

func (_ TemplateFunc) Json(data string) map[string]interface{}

func (TemplateFunc) JsonArray

func (_ TemplateFunc) JsonArray(data string) []interface{}

func (TemplateFunc) LookupIP

func (_ TemplateFunc) LookupIP(data string) []string

func (TemplateFunc) LookupIPV4

func (_ TemplateFunc) LookupIPV4(data string) []string

func (TemplateFunc) LookupIPV6

func (_ TemplateFunc) LookupIPV6(data string) []string

func (TemplateFunc) LookupSRV

func (_ TemplateFunc) LookupSRV(service, proto, name string) []*net.SRV

func (TemplateFunc) Ls

func (p TemplateFunc) Ls(filepath string) []string

func (TemplateFunc) Lsdir

func (p TemplateFunc) Lsdir(filepath string) []string

func (TemplateFunc) Map

func (_ TemplateFunc) Map(values ...interface{}) map[string]interface{}

Map creates a key-value map of string -> interface{} The i'th is the key and the i+1 is the value

func (TemplateFunc) Mod

func (_ TemplateFunc) Mod(a, b int) int

func (TemplateFunc) Mul

func (_ TemplateFunc) Mul(a, b int) int

func (TemplateFunc) ParseBool

func (_ TemplateFunc) ParseBool(s string) bool

func (TemplateFunc) Replace

func (_ TemplateFunc) Replace(s, old, new string, n int) string

func (TemplateFunc) Reverse

func (_ TemplateFunc) Reverse(values interface{}) interface{}

reverse returns the array in reversed order works with []string and []KVPair

func (TemplateFunc) Seq

func (_ TemplateFunc) Seq(first, last int) []int

seq creates a sequence of integers. It's named and used as GNU's seq. seq takes the first and the last element as arguments. So Seq(3, 5) will generate [3,4,5]

func (TemplateFunc) SortByLength

func (_ TemplateFunc) SortByLength(values []string) []string

func (TemplateFunc) SortKVByLength

func (_ TemplateFunc) SortKVByLength(values []KVPair) []KVPair

func (TemplateFunc) Split

func (_ TemplateFunc) Split(s, sep string) []string

func (TemplateFunc) Sub

func (_ TemplateFunc) Sub(a, b int) int

func (TemplateFunc) ToLower

func (_ TemplateFunc) ToLower(s string) string

func (TemplateFunc) ToUpper

func (_ TemplateFunc) ToUpper(s string) string

func (TemplateFunc) TrimSuffix

func (_ TemplateFunc) TrimSuffix(s, suffix string) string

type TemplateResource

type TemplateResource struct {
	Src           string      `toml:"src" json:"src"`
	Dest          string      `toml:"dest" json:"dest"`
	Prefix        string      `toml:"prefix" json:"prefix"`
	Keys          []string    `toml:"keys" json:"keys"`
	Mode          string      `toml:"mode" json:"mode"`
	Gid           int         `toml:"gid" json:"gid"`
	Uid           int         `toml:"uid" json:"uid"`
	CheckCmd      string      `toml:"check_cmd" json:"check_cmd"`
	ReloadCmd     string      `toml:"reload_cmd" json:"reload_cmd"`
	FileMode      os.FileMode `toml:"file_mode" json:"file_mode"`
	PGPPrivateKey []byte      `toml:"pgp_private_key" json:"pgp_private_key"`
}

TemplateResource is the representation of a parsed template resource.

func ListTemplateResource

func ListTemplateResource(confdir string) ([]*TemplateResource, []string, error)

func LoadTemplateResourceFile

func LoadTemplateResourceFile(confdir, name string) (*TemplateResource, error)

func (*TemplateResource) SaveFile

func (p *TemplateResource) SaveFile(path string) error

func (*TemplateResource) TomlString

func (p *TemplateResource) TomlString() string

type TemplateResourceProcessor

type TemplateResourceProcessor struct {
	TemplateResource
	// contains filtered or unexported fields
}

func MakeAllTemplateResourceProcessor

func MakeAllTemplateResourceProcessor(
	config *Config, client BackendClient,
) (
	[]*TemplateResourceProcessor,
	error,
)

func NewTemplateResourceProcessor

func NewTemplateResourceProcessor(
	path string, config *Config, client BackendClient, res *TemplateResource,
) *TemplateResourceProcessor

NewTemplateResourceProcessor creates a NewTemplateResourceProcessor.

func (*TemplateResourceProcessor) Process

func (p *TemplateResourceProcessor) Process(call *Call) (err error)

process is a convenience function that wraps calls to the three main tasks required to keep local configuration files in sync. First we gather vars from the store, then we stage a candidate configuration file, and finally sync things up. It returns an error if any.

type TomlBackend

type TomlBackend struct {
	TOMLFile string
}

func NewTomlBackendClient

func NewTomlBackendClient(cfg *BackendConfig) *TomlBackend

func (*TomlBackend) Close

func (_ *TomlBackend) Close() error

func (*TomlBackend) GetValues

func (p *TomlBackend) GetValues(keys []string) (m map[string]string, err error)

func (*TomlBackend) Type

func (_ *TomlBackend) Type() string

func (*TomlBackend) WatchEnabled

func (_ *TomlBackend) WatchEnabled() bool

func (*TomlBackend) WatchPrefix

func (_ *TomlBackend) WatchPrefix(prefix string, keys []string, waitIndex uint64, stopChan chan bool) (uint64, error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL