-
-
Save nmreadelf/2f37c6bd8d71ff0a7a80442585b6103b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
etcdv2 "./etdv2" | |
"context" | |
"flag" | |
"fmt" | |
log "github.com/golang/glog" | |
"os" | |
"os/signal" | |
"strings" | |
"sync" | |
"syscall" | |
) | |
var Version = "0.5.3+git" | |
type CmdLineOpts struct { | |
etcdEndpoints string | |
etcdPrefix string | |
etcdKeyfile string | |
etcdCertfile string | |
etcdCAFile string | |
etcdUsername string | |
etcdPassword string | |
action string | |
filepath string | |
version bool | |
} | |
var ( | |
opts CmdLineOpts | |
fs = flag.NewFlagSet("", flag.ExitOnError) | |
) | |
func init() { | |
fs.StringVar(&opts.etcdEndpoints, "etcd-endpoints", "http://127.0.0.1:2379", "a comma-delimited list of etcd endpoints") | |
fs.StringVar(&opts.etcdPrefix, "etcd-prefix", "/kubernetes/network", "etcd prefix") | |
fs.StringVar(&opts.etcdKeyfile, "etcd-keyfile", "", "SSL key file used to secure etcd communication") | |
fs.StringVar(&opts.etcdCertfile, "etcd-certfile", "", "SSL certification file used to secure etcd communication") | |
fs.StringVar(&opts.etcdCAFile, "etcd-cafile", "", "SSL Certificate Authority file used to secure etcd communication") | |
fs.StringVar(&opts.etcdUsername, "etcd-username", "", "username for BasicAuth to etcd") | |
fs.StringVar(&opts.etcdPassword, "etcd-password", "", "password for BasicAuth to etcd") | |
fs.StringVar(&opts.filepath, "file", "", "save and load file") | |
fs.StringVar(&opts.action, "action", "","program action, dump or restore") | |
fs.BoolVar(&opts.version, "version", false, "print version and exit") | |
fs.Set("logtostderr", "true") | |
copyFlag("v") | |
copyFlag("vmodule") | |
copyFlag("log_backtrace_at") | |
fs.Usage = usage | |
fs.Parse(os.Args[1:]) | |
} | |
func copyFlag(name string) { | |
fs.Var(flag.Lookup(name).Value, flag.Lookup(name).Name, flag.Lookup(name).Usage) | |
} | |
func usage() { | |
fmt.Fprintf(os.Stderr, "Usage: %s [OPTION]...\n", os.Args[0]) | |
fs.PrintDefaults() | |
os.Exit(0) | |
} | |
func shutdownHandler(ctx context.Context, sigs chan os.Signal, cancel context.CancelFunc) { | |
// Wait for the context do be Done or for the signal to come in to shutdown. | |
select { | |
case <-ctx.Done(): | |
log.Info("Stopping shutdownHandler...") | |
case <-sigs: | |
// Call cancel on the context to close everything down. | |
cancel() | |
log.Info("shutdownHandler sent cancel signal...") | |
} | |
// Unregister to get default OS nuke behaviour in case we don't exit cleanly | |
signal.Stop(sigs) | |
} | |
func main() { | |
if opts.version { | |
fmt.Fprintln(os.Stderr, Version) | |
os.Exit(0) | |
} | |
if len(opts.filepath) < 1 { | |
log.Fatalf("file need specify") | |
} | |
if len(opts.action) < 1 { | |
log.Fatalf("maybe you need specify program action") | |
} | |
sigs := make(chan os.Signal, 1) | |
signal.Notify(sigs, os.Interrupt, syscall.SIGTERM) | |
cfg := &etcdv2.EtcdConfig{ | |
Endpoints: strings.Split(opts.etcdEndpoints, ","), | |
Keyfile: opts.etcdKeyfile, | |
Certfile: opts.etcdCertfile, | |
CAFile: opts.etcdCAFile, | |
Prefix: opts.etcdPrefix, | |
Username: opts.etcdUsername, | |
Password: opts.etcdPassword, | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment