|
|
|
@ -3,8 +3,11 @@ package etcdsnapshot
|
|
|
|
|
import ( |
|
|
|
|
"context" |
|
|
|
|
"errors" |
|
|
|
|
"fmt" |
|
|
|
|
"os" |
|
|
|
|
"path/filepath" |
|
|
|
|
"text/tabwriter" |
|
|
|
|
"time" |
|
|
|
|
|
|
|
|
|
"github.com/erikdubbelboer/gspt" |
|
|
|
|
"github.com/rancher/k3s/pkg/cli/cmds" |
|
|
|
@ -18,7 +21,7 @@ import (
|
|
|
|
|
|
|
|
|
|
// commandSetup setups up common things needed
|
|
|
|
|
// for each etcd command.
|
|
|
|
|
func commandSetup(app *cli.Context, cfg *cmds.Server) (string, error) { |
|
|
|
|
func commandSetup(app *cli.Context, cfg *cmds.Server, sc *server.Config) (string, error) { |
|
|
|
|
gspt.SetProcTitle(os.Args[0]) |
|
|
|
|
|
|
|
|
|
nodeName := app.String("node-name") |
|
|
|
@ -32,6 +35,21 @@ func commandSetup(app *cli.Context, cfg *cmds.Server) (string, error) {
|
|
|
|
|
|
|
|
|
|
os.Setenv("NODE_NAME", nodeName) |
|
|
|
|
|
|
|
|
|
sc.DisableAgent = true |
|
|
|
|
sc.ControlConfig.DataDir = cfg.DataDir |
|
|
|
|
sc.ControlConfig.EtcdSnapshotName = cfg.EtcdSnapshotName |
|
|
|
|
sc.ControlConfig.EtcdSnapshotDir = cfg.EtcdSnapshotDir |
|
|
|
|
sc.ControlConfig.EtcdS3 = cfg.EtcdS3 |
|
|
|
|
sc.ControlConfig.EtcdS3Endpoint = cfg.EtcdS3Endpoint |
|
|
|
|
sc.ControlConfig.EtcdS3EndpointCA = cfg.EtcdS3EndpointCA |
|
|
|
|
sc.ControlConfig.EtcdS3SkipSSLVerify = cfg.EtcdS3SkipSSLVerify |
|
|
|
|
sc.ControlConfig.EtcdS3AccessKey = cfg.EtcdS3AccessKey |
|
|
|
|
sc.ControlConfig.EtcdS3SecretKey = cfg.EtcdS3SecretKey |
|
|
|
|
sc.ControlConfig.EtcdS3BucketName = cfg.EtcdS3BucketName |
|
|
|
|
sc.ControlConfig.EtcdS3Region = cfg.EtcdS3Region |
|
|
|
|
sc.ControlConfig.EtcdS3Folder = cfg.EtcdS3Folder |
|
|
|
|
sc.ControlConfig.Runtime = &config.ControlRuntime{} |
|
|
|
|
|
|
|
|
|
return server.ResolveDataDir(cfg.DataDir) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -43,7 +61,9 @@ func Run(app *cli.Context) error {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func run(app *cli.Context, cfg *cmds.Server) error { |
|
|
|
|
dataDir, err := commandSetup(app, cfg) |
|
|
|
|
var serverConfig server.Config |
|
|
|
|
|
|
|
|
|
dataDir, err := commandSetup(app, cfg, &serverConfig) |
|
|
|
|
if err != nil { |
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
@ -52,22 +72,8 @@ func run(app *cli.Context, cfg *cmds.Server) error {
|
|
|
|
|
return cmds.ErrCommandNoArgs |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
var serverConfig server.Config |
|
|
|
|
serverConfig.DisableAgent = true |
|
|
|
|
serverConfig.ControlConfig.DataDir = dataDir |
|
|
|
|
serverConfig.ControlConfig.EtcdSnapshotName = cfg.EtcdSnapshotName |
|
|
|
|
serverConfig.ControlConfig.EtcdSnapshotDir = cfg.EtcdSnapshotDir |
|
|
|
|
serverConfig.ControlConfig.EtcdSnapshotRetention = 0 // disable retention check
|
|
|
|
|
serverConfig.ControlConfig.EtcdS3 = cfg.EtcdS3 |
|
|
|
|
serverConfig.ControlConfig.EtcdS3Endpoint = cfg.EtcdS3Endpoint |
|
|
|
|
serverConfig.ControlConfig.EtcdS3EndpointCA = cfg.EtcdS3EndpointCA |
|
|
|
|
serverConfig.ControlConfig.EtcdS3SkipSSLVerify = cfg.EtcdS3SkipSSLVerify |
|
|
|
|
serverConfig.ControlConfig.EtcdS3AccessKey = cfg.EtcdS3AccessKey |
|
|
|
|
serverConfig.ControlConfig.EtcdS3SecretKey = cfg.EtcdS3SecretKey |
|
|
|
|
serverConfig.ControlConfig.EtcdS3BucketName = cfg.EtcdS3BucketName |
|
|
|
|
serverConfig.ControlConfig.EtcdS3Region = cfg.EtcdS3Region |
|
|
|
|
serverConfig.ControlConfig.EtcdS3Folder = cfg.EtcdS3Folder |
|
|
|
|
serverConfig.ControlConfig.Runtime = &config.ControlRuntime{} |
|
|
|
|
serverConfig.ControlConfig.Runtime.ETCDServerCA = filepath.Join(dataDir, "tls", "etcd", "server-ca.crt") |
|
|
|
|
serverConfig.ControlConfig.Runtime.ClientETCDCert = filepath.Join(dataDir, "tls", "etcd", "client.crt") |
|
|
|
|
serverConfig.ControlConfig.Runtime.ClientETCDKey = filepath.Join(dataDir, "tls", "etcd", "client.key") |
|
|
|
@ -108,7 +114,9 @@ func Delete(app *cli.Context) error {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func delete(app *cli.Context, cfg *cmds.Server) error { |
|
|
|
|
dataDir, err := commandSetup(app, cfg) |
|
|
|
|
var serverConfig server.Config |
|
|
|
|
|
|
|
|
|
dataDir, err := commandSetup(app, cfg, &serverConfig) |
|
|
|
|
if err != nil { |
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
@ -118,21 +126,7 @@ func delete(app *cli.Context, cfg *cmds.Server) error {
|
|
|
|
|
return errors.New("no snapshots given for removal") |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
var serverConfig server.Config |
|
|
|
|
serverConfig.DisableAgent = true |
|
|
|
|
serverConfig.ControlConfig.DataDir = dataDir |
|
|
|
|
serverConfig.ControlConfig.EtcdSnapshotName = cfg.EtcdSnapshotName |
|
|
|
|
serverConfig.ControlConfig.EtcdSnapshotDir = cfg.EtcdSnapshotDir |
|
|
|
|
serverConfig.ControlConfig.EtcdS3 = cfg.EtcdS3 |
|
|
|
|
serverConfig.ControlConfig.EtcdS3Endpoint = cfg.EtcdS3Endpoint |
|
|
|
|
serverConfig.ControlConfig.EtcdS3EndpointCA = cfg.EtcdS3EndpointCA |
|
|
|
|
serverConfig.ControlConfig.EtcdS3SkipSSLVerify = cfg.EtcdS3SkipSSLVerify |
|
|
|
|
serverConfig.ControlConfig.EtcdS3AccessKey = cfg.EtcdS3AccessKey |
|
|
|
|
serverConfig.ControlConfig.EtcdS3SecretKey = cfg.EtcdS3SecretKey |
|
|
|
|
serverConfig.ControlConfig.EtcdS3BucketName = cfg.EtcdS3BucketName |
|
|
|
|
serverConfig.ControlConfig.EtcdS3Region = cfg.EtcdS3Region |
|
|
|
|
serverConfig.ControlConfig.EtcdS3Folder = cfg.EtcdS3Folder |
|
|
|
|
serverConfig.ControlConfig.Runtime = &config.ControlRuntime{} |
|
|
|
|
serverConfig.ControlConfig.Runtime.KubeConfigAdmin = filepath.Join(dataDir, "cred", "admin.kubeconfig") |
|
|
|
|
|
|
|
|
|
ctx := signals.SetupSignalHandler(context.Background()) |
|
|
|
@ -147,3 +141,43 @@ func delete(app *cli.Context, cfg *cmds.Server) error {
|
|
|
|
|
|
|
|
|
|
return e.DeleteSnapshots(ctx, app.Args()) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func List(app *cli.Context) error { |
|
|
|
|
if err := cmds.InitLogging(); err != nil { |
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
|
return list(app, &cmds.ServerConfig) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func list(app *cli.Context, cfg *cmds.Server) error { |
|
|
|
|
var serverConfig server.Config |
|
|
|
|
|
|
|
|
|
dataDir, err := commandSetup(app, cfg, &serverConfig) |
|
|
|
|
if err != nil { |
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
serverConfig.ControlConfig.DataDir = dataDir |
|
|
|
|
|
|
|
|
|
ctx := signals.SetupSignalHandler(context.Background()) |
|
|
|
|
e := etcd.NewETCD() |
|
|
|
|
e.SetControlConfig(&serverConfig.ControlConfig) |
|
|
|
|
|
|
|
|
|
sf, err := e.ListSnapshots(ctx) |
|
|
|
|
if err != nil { |
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', 0) |
|
|
|
|
defer w.Flush() |
|
|
|
|
|
|
|
|
|
for _, s := range sf { |
|
|
|
|
if cfg.EtcdS3 { |
|
|
|
|
fmt.Fprintf(w, "%s\t%d\t%s\n", s.Name, s.Size, s.CreatedAt.Format(time.RFC3339)) |
|
|
|
|
} else { |
|
|
|
|
fmt.Fprintf(w, "%s\t%s\t%d\t%s\n", s.Name, s.Location, s.Size, s.CreatedAt.Format(time.RFC3339)) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return nil |
|
|
|
|
} |
|
|
|
|