-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathcommand.go
More file actions
145 lines (122 loc) · 4.26 KB
/
Copy pathcommand.go
File metadata and controls
145 lines (122 loc) · 4.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package restore
import (
"fmt"
stdLog "log"
ccns "github.com/myENA/consul-backinator/common/consul"
ct "github.com/myENA/consul-backinator/common/transformer"
)
// primary configuration
type config struct {
fileName string
cryptKey string
noKV bool
aclFileName string
queryFileName string
pathTransform string
delTree bool
consulPrefix string
consulConfig *ccns.Config
}
// Command is a Command implementation that runs the backup operation
type Command struct {
Self string
Log *stdLog.Logger
config *config
consulClient *ccns.Client
pathTransformer *ct.PathTransformer
}
// Run is a function to run the command
func (c *Command) Run(args []string) int {
var err error // error holder
var count int // key counter
// setup flags
if err = c.setupFlags(args); err != nil {
c.Log.Printf("[Error] Setup failed: %s", err.Error())
return 1
}
// sanity check
if c.config.noKV && (c.config.aclFileName == "" && c.config.queryFileName == "") {
c.Log.Printf("[Error] Passing 'nokv' and an empty 'acls' and/or 'queries' file " +
"doesn't make any sense. You should specify an 'acls' and/or 'queries' file " +
"when using the 'nokv' option.")
return 1
}
// build client
if c.consulClient, err = c.config.consulConfig.New(); err != nil {
c.Log.Printf("[Error] Failed initialize consul client: %s", err.Error())
return 1
}
// build transformer if needed
if c.pathTransformer, err = ct.New(c.config.pathTransform); err != nil {
c.Log.Printf("[Error] Failed to initialize path transformer: %s", err.Error())
return 1
}
// restore keys unless otherwise requested
if !c.config.noKV {
if count, err = c.restoreKeys(); err != nil {
c.Log.Printf("[Error] Failed to restore kv data: %s", err.Error())
return 1
}
// show success
c.Log.Printf("[Success] Restored %d keys from %s to %s/%s",
count,
c.config.fileName,
c.config.consulConfig.Address,
c.config.consulPrefix)
}
// restore acls if requested
if c.config.aclFileName != "" {
if count, err = c.restoreACLs(); err != nil {
c.Log.Printf("[Error] Failed to restore ACL items: %s", err.Error())
return 1
}
// show success
c.Log.Printf("[Success] Restored %d ACL roles, policies, and tokens from %s to %s",
count,
c.config.aclFileName,
c.config.consulConfig.Address)
}
// restore queries if requested
if c.config.queryFileName != "" {
if count, err = c.restoreQueries(); err != nil {
c.Log.Printf("[Error] Failed to restore query definitions: %s", err.Error())
return 1
}
// show success
c.Log.Printf("[Success] Restored %d query definitions from %s to %s",
count,
c.config.queryFileName,
c.config.consulConfig.Address)
}
// exit clean
return 0
}
// Synopsis shows the command summary
func (c *Command) Synopsis() string {
return "Perform a restore operation"
}
// Help shows the detailed command options
func (c *Command) Help() string {
return fmt.Sprintf(`Usage: %s restore [options]
Performs a restore operation against a consul cluster.
Options:
-file Source filename or S3 location (default: "consul.bak")
-key Passphrase for data encryption and signature validation (default: "password")
-nokv Do not attempt to restore kv data
-acls Optional source filename or S3 location for acl tokens
-queries Optional source filename or S3 location for query definitions
-transform Optional path transformation (oldPath,newPath...)
-delete Delete all keys under specified prefix prior to restoration (default: false)
-prefix Path prefix for delete and restore operation
-addr Optional consul address and port (default: "127.0.0.1:8500")
-scheme Optional consul scheme ("http" or "https")
-dc Optional consul datacenter
-token Optional consul access token
-ca-cert Optional path to a PEM encoded CA cert file
-client-cert Optional path to a PEM encoded client certificate
-client-key Optional path to an unencrypted PEM encoded private key
-tls-skip-verify Optional bool for verifying a TLS certificate (not recommended)
Please see documentation on GitHub for a detailed explanation of all options.
https://github.com/myENA/consul-backinator
`, c.Self)
}