/
mongoPurge.go
209 lines (158 loc) · 6.06 KB
/
mongoPurge.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
package jobs
import (
"gitlab.4medica.net/gke/kube-backup/config"
"fmt"
"k8s.io/client-go/kubernetes"
"gopkg.in/yaml.v2"
"github.com/pkg/errors"
"github.com/codeskyblue/go-sh"
"strconv"
"strings"
"time"
"github.com/Sirupsen/logrus"
)
type mongoPurgeCollection [] struct {
Name string `yaml:"name"`
TimestampField string `yaml:"timestampField"`
ThresholdSize float64 `yaml:"thresholdSize"`
PurgeOlderThan int `yaml:"purgeOlderThan"`
}
func RunMongoPurge(plan config.Plan, k8Client *kubernetes.Clientset, tmpPath string, filePostFix string) (string, string, error) {
var collectionsToBePurged mongoPurgeCollection
backupLocation := fmt.Sprintf("%v/%v-%v", tmpPath, plan.Name, filePostFix)
var archive string
log := fmt.Sprintf("%v/%v-%v.log", tmpPath, plan.Name, filePostFix)
k8coreV1Client := k8Client.CoreV1();
username, password, err := retrieveMongoCredentialsFromSecret(k8coreV1Client, plan.Target["secret"])
timeout := plan.Scheduler.Timeout
if err != nil {
return "", log, errors.Wrap(err, "Failed to retrieve credentials")
}
collections, err := parseCollectionsFromTarget(plan.Target["collections"])
if err != nil {
return "", log, errors.Wrap(err, "Failed to parse collections")
}
mongoBaseCommand := formMongoBaseCommand(plan.Target, username, password)
for _, collection := range collections {
sizeInGB, err := retrieveCollectionSize(mongoBaseCommand, collection.Name, log)
if err != nil {
return "", log, err
}
if sizeInGB > collection.ThresholdSize {
collectionsToBePurged = append(collectionsToBePurged, collection)
}
}
mongoBackupCommand := formMongoBackupCommand(plan.Target, username, password, backupLocation)
for _, collection := range collectionsToBePurged {
if (plan.Target["backupBeforePurge"].(bool)) {
err = backupCollection(mongoBackupCommand, collection.Name, log, timeout)
if err != nil {
return "", log, err
}
}
dateString := time.Now().AddDate(0, 0, -collection.PurgeOlderThan).Format("2006-01-02")
err = purgeCollection(mongoBaseCommand, collection.Name, collection.TimestampField, dateString, log, timeout)
if err != nil {
return "", log, err
}
}
if len(collectionsToBePurged) > 0 && plan.Target["backupBeforePurge"].(bool) {
err = createArchiveAndCleanup(backupLocation, plan.Name, log)
archive = backupLocation + ".tgz"
}
return archive, log, err
}
func retrieveCollectionSize(baseCmd string, collectionName string, logFile string) (float64, error) {
collectionSizeCommand := fmt.Sprintf("%v --quiet --eval 'db[`'%v'`].stats().size'", baseCmd, collectionName)
var emptyFloat float64
var size int
output, err := sh.Command("sh", "-c", collectionSizeCommand).CombinedOutput()
outputString := strings.TrimSuffix(string(output), "\n")
if err != nil {
logToFile(logFile, output)
return emptyFloat, errors.Wrap(err, "Unable to find collection size")
} else if outputString == "" {
return emptyFloat, errors.New("Unable to find collection size")
}
// https://jira.mongodb.org/browse/SERVER-27159
outputStringSlice := strings.Split(outputString, "\n")
size, err = strconv.Atoi(outputStringSlice[len(outputStringSlice)-1])
if err != nil {
return emptyFloat, errors.Wrap(err, "Unable to find collection size")
}
sizeInGB := float64(size) / 1024 / 1024 / 1024
logrus.WithFields(logrus.Fields{
"collection": collectionName,
"size": sizeInGB,
}).Debug("Successfully retrieved collection size")
return sizeInGB, nil
}
func formMongoBaseCommand (target map[string]interface{}, username string, password string) string {
mongoBaseCommand := fmt.Sprintf("mongo --host %v %v ",
target["connectionString"].(string),
target["database"].(string),
)
if username != "" && password != "" {
mongoBaseCommand += fmt.Sprintf("-u %v -p %v ", username, password)
}
if target["params"].(string) != "" {
mongoBaseCommand += fmt.Sprintf("%v", target["params"].(string))
}
return mongoBaseCommand
}
func formMongoBackupCommand (target map[string]interface{}, username string, password string, backupLocation string) string {
mongoBaseCommand := fmt.Sprintf("mongodump --host %v --db %v --out %v ",
target["connectionString"].(string),
target["database"].(string),
backupLocation,
)
if username != "" && password != "" {
mongoBaseCommand += fmt.Sprintf("-u %v -p %v ", username, password)
}
if target["params"].(string) != "" {
mongoBaseCommand += fmt.Sprintf("%v", target["params"].(string))
}
return mongoBaseCommand
}
func parseCollectionsFromTarget (collections interface{}) (mongoPurgeCollection, error) {
parsedCollections := make(mongoPurgeCollection, 0)
collectionsString, err := yaml.Marshal(collections)
if err != nil {
return parsedCollections, err
}
err = yaml.Unmarshal(collectionsString, &parsedCollections)
return parsedCollections, err;
}
func backupCollection (baseCmd, collection, logFile string, timeout int) error {
collectionBackupCommand := fmt.Sprintf("%v --collection %v ", baseCmd, collection)
output, err := sh.Command("/bin/sh", "-c", collectionBackupCommand).SetTimeout(time.Duration(timeout) * time.Minute).CombinedOutput()
if err != nil {
ex := ""
if len(output) > 0 {
ex = strings.Replace(string(output), "\n", " ", -1)
}
return errors.Wrapf(err, "mongodump log %v", ex)
}
logToFile(logFile, output)
logrus.WithFields(logrus.Fields{
"collection": collection,
}).Debug("Collection backup successful")
return nil
}
func purgeCollection (baseCmd, collection, timestampField, dateString, logFile string, timeout int) error {
collectionPurgeCommand := fmt.Sprintf("%v --quiet --eval " +
"'db.%v.remove({\"%v\" : { $lte : ISODate(\"%v\") }})'", baseCmd, collection, timestampField, dateString)
output, err := sh.Command("/bin/sh", "-c", collectionPurgeCommand).SetTimeout(time.Duration(timeout) * time.Minute).CombinedOutput()
if err != nil {
ex := ""
if len(output) > 0 {
ex = strings.Replace(string(output), "\n", " ", -1)
}
return errors.Wrapf(err, "mongopurge log %v", ex)
}
logToFile(logFile, output)
logrus.WithFields(logrus.Fields{
"collection": collection,
}).Debug("Successfully purged collection")
return nil
}