-
Notifications
You must be signed in to change notification settings - Fork 6
/
repl.go
250 lines (225 loc) · 6.22 KB
/
repl.go
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package main
import (
"fmt"
"github.com/spf13/cobra"
"github.com/wttech/aemc/pkg"
"github.com/wttech/aemc/pkg/replication"
)
func (c *CLI) replCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "replication",
Short: "Replicate nodes and manage agents",
Aliases: []string{"repl"},
}
cmd.AddCommand(c.replAgentCmd())
cmd.AddCommand(c.replActivateCmd())
cmd.AddCommand(c.replDeactivateCmd())
cmd.AddCommand(c.replActivateTreeCmd())
return cmd
}
func (c *CLI) replActivateCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "activate",
Short: "Activate single node",
Aliases: []string{"act", "replicate", "repl"},
Run: func(cmd *cobra.Command, args []string) {
instance, err := c.aem.InstanceManager().One()
if err != nil {
c.Error(err)
return
}
path := replActivationPathByFlags(cmd)
if err := instance.Replication().Activate(path); err != nil {
c.Error(err)
return
}
c.SetOutput("path", path)
c.Ok("path activated")
},
}
replActivationFlags(cmd)
return cmd
}
func (c *CLI) replDeactivateCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "deactivate",
Short: "Deactivate single node",
Aliases: []string{"deact", "unreplicate", "unrepl"},
Run: func(cmd *cobra.Command, args []string) {
instance, err := c.aem.InstanceManager().One()
if err != nil {
c.Error(err)
return
}
path := replActivationPathByFlags(cmd)
if err := instance.Replication().Deactivate(path); err != nil {
c.Error(err)
return
}
c.SetOutput("path", path)
c.Ok("path deactivated")
},
}
replActivationFlags(cmd)
return cmd
}
func (c *CLI) replActivateTreeCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "tree",
Short: "Activate node and all its children",
Aliases: []string{"activate-tree", "tree-activate"},
Run: func(cmd *cobra.Command, args []string) {
instance, err := c.aem.InstanceManager().One()
if err != nil {
c.Error(err)
return
}
startPath, _ := cmd.Flags().GetString("path")
onlyModified, _ := cmd.Flags().GetBool("only-modified")
dryRun, _ := cmd.Flags().GetBool("dry-run")
onlyActivated, _ := cmd.Flags().GetBool("only-activated")
ignoreDeactivated, _ := cmd.Flags().GetBool("ignore-deactivated")
opts := replication.ActivateTreeOpts{
StartPath: startPath,
OnlyModified: onlyModified,
DryRun: dryRun,
OnlyActivated: onlyActivated,
IgnoreDeactivated: ignoreDeactivated,
}
if err := instance.Replication().ActivateTree(opts); err != nil {
c.Error(err)
return
}
c.SetOutput("path", startPath)
c.Ok("tree activated")
},
}
cmd.Flags().StringP("path", "p", "", "Path to node")
_ = cmd.MarkFlagRequired("path")
cmd.Flags().Bool("dry-run", false, "Only simulate activation")
cmd.Flags().Bool("only-modified", false, "Only activate modified nodes")
cmd.Flags().Bool("only-activated", false, "Only activate nodes that are not activated")
cmd.Flags().Bool("ignore-deactivated", false, "Ignore deactivated nodes")
return cmd
}
func replActivationFlags(cmd *cobra.Command) {
cmd.Flags().StringP("path", "p", "", "Path to node")
_ = cmd.MarkFlagRequired("path")
}
func replActivationPathByFlags(cmd *cobra.Command) string {
path, _ := cmd.Flags().GetString("path")
return path
}
func (c *CLI) replAgentCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "agent",
Short: "Managing replication agents",
Aliases: []string{"ag"},
}
cmd.AddCommand(c.replAgentReadCmd())
cmd.AddCommand(c.replAgentSetupCmd())
cmd.AddCommand(c.replAgentDeleteCmd())
return cmd
}
func (c *CLI) replAgentReadCmd() *cobra.Command {
result := &cobra.Command{
Use: "read",
Short: "Read replication agent details",
Aliases: []string{"get"},
Run: func(cmd *cobra.Command, args []string) {
i, err := c.aem.InstanceManager().One()
if err != nil {
c.Error(err)
return
}
replAgent := replAgentByFlags(cmd, i)
c.SetOutput("replAgent", replAgent)
c.Ok("replication agent details read")
},
}
replAgentDefineFlags(result)
return result
}
func (c *CLI) replAgentDeleteCmd() *cobra.Command {
result := &cobra.Command{
Use: "delete",
Short: "Delete replication agent",
Aliases: []string{"remove", "rm"},
Run: func(cmd *cobra.Command, args []string) {
i, err := c.aem.InstanceManager().One()
if err != nil {
c.Error(err)
return
}
replAgent := replAgentByFlags(cmd, i)
changed, err := replAgent.Delete()
if err != nil {
c.Error(err)
return
}
c.SetOutput("replAgent", replAgent)
if changed {
c.Changed("replication agent deleted")
} else {
c.Ok("replication agent already deleted (does not exist)")
}
},
}
replAgentDefineFlags(result)
return result
}
func (c *CLI) replAgentSetupCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "setup",
Short: "Setup replication agent",
Aliases: []string{"configure"},
Run: func(cmd *cobra.Command, args []string) {
instance, err := c.aem.InstanceManager().One()
if err != nil {
c.Error(err)
return
}
var props map[string]any
if err = c.ReadInput(&props); err != nil {
c.Fail(fmt.Sprintf("cannot setup replication agent as input props cannot be parsed: %s", err))
return
}
replAgent := replAgentByFlags(cmd, instance)
changed, err := replAgent.Setup(props)
if err != nil {
c.Error(err)
return
}
if changed {
if err = instance.Replication().Bundle().Restart(); err != nil {
c.Error(err)
return
}
if err := c.aem.InstanceManager().AwaitStartedOne(*instance); err != nil {
c.Error(err)
return
}
}
c.SetOutput("replAgent", replAgent)
if changed {
c.Changed("replication agent set up")
} else {
c.Ok("replication agent already set up (up-to-date)")
}
},
}
replAgentDefineFlags(cmd)
return cmd
}
func replAgentDefineFlags(cmd *cobra.Command) {
cmd.Flags().String("location", "", "Location")
_ = cmd.MarkFlagRequired("location")
cmd.Flags().String("name", "", "Name")
_ = cmd.MarkFlagRequired("name")
}
func replAgentByFlags(cmd *cobra.Command, i *pkg.Instance) *pkg.ReplAgent {
location, _ := cmd.Flags().GetString("location")
name, _ := cmd.Flags().GetString("name")
replAgent := i.Repo().ReplAgent(location, name)
return &replAgent
}