Commit cb36cb32 authored by ale's avatar ale
Browse files

Add commands for the metadb server

parent 27485287
package main
import (
"context"
"flag"
"log"
"os"
"os/signal"
"syscall"
"git.autistici.org/ale/tabacco"
mdbc "git.autistici.org/ale/tabacco/metadb/client"
"github.com/google/subcommands"
)
type daemonCommand struct {
configPath string
httpAddr string
}
func (c *daemonCommand) Name() string { return "daemon" }
func (c *daemonCommand) Synopsis() string { return "start the backup daemon" }
func (c *daemonCommand) Usage() string {
return `daemon [<flags>]:
Start the backup daemon.
`
}
func (c *daemonCommand) SetFlags(f *flag.FlagSet) {
f.StringVar(&c.configPath, "config", "/etc/tabacco/config.yml", "configuration `file`")
f.StringVar(&c.httpAddr, "http-addr", ":5331", "listen `address` for the HTTP server exporting metrics and debugging")
}
func (c *daemonCommand) Execute(ctx context.Context, f *flag.FlagSet, args ...interface{}) subcommands.ExitStatus {
if f.NArg() > 0 {
log.Printf("error: too many arguments")
return subcommands.ExitUsageError
}
// Build a ConfigManager that can be reloaded with SIGHUP.
config, err := tabacco.ReadConfig(c.configPath)
if err != nil {
log.Printf("configuration error: %v", err)
return subcommands.ExitFailure
}
configMgr, err := tabacco.NewConfigManager(config)
if err != nil {
log.Printf("configuration error: %v", err)
return subcommands.ExitFailure
}
hupCh := make(chan os.Signal, 1)
go func() {
for range hupCh {
log.Printf("SIGHUP received, reloading configuration")
newConfig, err := tabacco.ReadConfig(c.configPath) // nolint: vetshadow
if err != nil {
log.Printf("configuration error: %v", err)
continue
}
if err := configMgr.Reload(newConfig); err != nil {
log.Printf("error reloading configuration: %v", err)
}
}
}()
signal.Notify(hupCh, syscall.SIGHUP)
store, err := mdbc.New(config.MetadataStoreBackend)
if err != nil {
log.Printf("error: %v", err)
return subcommands.ExitFailure
}
mgr, err := tabacco.NewManager(ctx, configMgr, store)
if err != nil {
log.Printf("error: %v", err)
return subcommands.ExitFailure
}
defer mgr.Close() // nolint
// Wait for the outmost Context to terminate (presumably due to SIGTERM).
log.Printf("backup manager started")
<-ctx.Done()
log.Printf("backup manager stopped")
return subcommands.ExitSuccess
}
func init() {
subcommands.Register(&withSignalHandlers{&daemonCommand{}}, "")
}
......@@ -11,33 +11,6 @@ import (
"github.com/google/subcommands"
)
type daemonCommand struct {
configPath string
httpAddr string
}
func (c *daemonCommand) Name() string { return "daemon" }
func (c *daemonCommand) Synopsis() string { return "start the backup daemon" }
func (c *daemonCommand) Usage() string {
return `daemon [<flags>]:
Start the backup daemon.
`
}
func (c *daemonCommand) SetFlags(f *flag.FlagSet) {
f.StringVar(&c.configPath, "config", "/etc/tabacco/config.yml", "configuration `file`")
f.StringVar(&c.httpAddr, "http-addr", ":5331", "listen `address` for the HTTP server exporting metrics and debugging")
}
func (c *daemonCommand) Execute(ctx context.Context, f *flag.FlagSet, args ...interface{}) subcommands.ExitStatus {
if f.NArg() > 0 {
log.Printf("error: too many arguments")
return subcommands.ExitUsageError
}
return subcommands.ExitSuccess
}
// Wrapper that installs a SIGTERM handler that will cancel the
// Context passed to the Execute() method.
type withSignalHandlers struct {
......@@ -62,8 +35,6 @@ func (c *withSignalHandlers) Execute(ctx context.Context, f *flag.FlagSet, args
}
func init() {
subcommands.Register(&withSignalHandlers{&daemonCommand{}}, "")
subcommands.Register(subcommands.HelpCommand(), "help")
subcommands.Register(subcommands.CommandsCommand(), "help")
subcommands.Register(subcommands.FlagsCommand(), "help")
......
package main
import (
"context"
"flag"
"io/ioutil"
"log"
"git.autistici.org/ai3/go-common/serverutil"
mdbs "git.autistici.org/ale/tabacco/metadb/server"
"github.com/google/subcommands"
"gopkg.in/yaml.v1"
)
type metadbCommand struct {
configPath string
addr string
}
func (c *metadbCommand) Name() string { return "metadb" }
func (c *metadbCommand) Synopsis() string { return "start the metadata database server" }
func (c *metadbCommand) Usage() string {
return `metadb [<flags>]:
Start the metadata database server.
`
}
func (c *metadbCommand) SetFlags(f *flag.FlagSet) {
f.StringVar(&c.configPath, "config", "/etc/tabacco/metadb.yml", "configuration `file`")
f.StringVar(&c.addr, "addr", ":5332", "listen `address` for the HTTP server")
}
type metadbConfig struct {
Driver string `yaml:"db_driver"`
DBURI string `yaml:"db_uri"`
ServerConfig *serverutil.ServerConfig `yaml:"http_server"`
}
func loadMetadbConfig(path string) (*metadbConfig, error) {
data, err := ioutil.ReadFile(path) // #nosec
if err != nil {
return nil, err
}
var config metadbConfig
err = yaml.Unmarshal(data, &config)
return &config, err
}
func (c *metadbCommand) Execute(ctx context.Context, f *flag.FlagSet, args ...interface{}) subcommands.ExitStatus {
if f.NArg() > 0 {
log.Printf("error: too many arguments")
return subcommands.ExitUsageError
}
config, err := loadMetadbConfig(c.configPath)
if err != nil {
log.Printf("configuration error: %v", err)
return subcommands.ExitFailure
}
svc, err := mdbs.New(config.Driver, config.DBURI)
if err != nil {
log.Printf("error: %v", err)
return subcommands.ExitFailure
}
if err := mdbs.Serve(svc, config.ServerConfig, c.addr); err != nil {
log.Printf("error: %v", err)
return subcommands.ExitFailure
}
return subcommands.ExitSuccess
}
func init() {
subcommands.Register(&metadbCommand{}, "")
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment