-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.go
637 lines (512 loc) · 16.4 KB
/
main.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
package main
import (
"fmt"
"log"
"net/http"
"os"
"os/exec"
"path/filepath"
"runtime"
"slices"
"strconv"
"strings"
"time"
"github.com/hashicorp/go-retryablehttp"
"github.com/carlescere/scheduler"
"github.com/jonhadfield/githosts-utils"
"github.com/pkg/errors"
)
const (
appName = "soba"
workingDIRName = ".working"
workingDIRMode = 0o755
defaultBackupsToRetain = 2
defaultGitLabMinimumProjectAccessLevel = 20
defaultEarlyErrorBackOffSeconds = 5
defaultHTTPClientRequestTimeout = 300 * time.Second
pathSep = string(os.PathSeparator)
// http
maxIdleConns = 10
idleConnTimeout = 30 * time.Second
// env vars
envPath = "PATH"
envSobaLogLevel = "SOBA_LOG"
envSobaWebHookURL = "SOBA_WEBHOOK_URL"
envSobaWebHookFormat = "SOBA_WEBHOOK_FORMAT"
envGitBackupInterval = "GIT_BACKUP_INTERVAL"
envGitBackupDir = "GIT_BACKUP_DIR"
envGitRequestTimeout = "GIT_REQUEST_TIMEOUT"
envGitHubAPIURL = "GITHUB_APIURL"
envGitHubBackups = "GITHUB_BACKUPS"
envAzureDevOpsOrgs = "AZURE_DEVOPS_ORGS"
envAzureDevOpsUserName = "AZURE_DEVOPS_USERNAME"
envAzureDevOpsPAT = "AZURE_DEVOPS_PAT"
envAzureDevOpsCompare = "AZURE_DEVOPS_COMPARE"
envAzureDevOpsBackups = "AZURE_DEVOPS_BACKUPS"
// nolint:gosec
envGitHubToken = "GITHUB_TOKEN"
envGitHubOrgs = "GITHUB_ORGS"
envGitHubSkipUserRepos = "GITHUB_SKIP_USER_REPOS"
envGitHubLimitUserOwned = "GITHUB_LIMIT_USER_OWNED"
envGitHubCompare = "GITHUB_COMPARE"
envGitLabBackups = "GITLAB_BACKUPS"
envGitLabMinAccessLevel = "GITLAB_PROJECT_MIN_ACCESS_LEVEL"
envGitLabToken = "GITLAB_TOKEN"
envGitLabAPIURL = "GITLAB_APIURL"
envGitLabCompare = "GITLAB_COMPARE"
envBitBucketUser = "BITBUCKET_USER"
envBitBucketKey = "BITBUCKET_KEY"
envBitBucketSecret = "BITBUCKET_SECRET"
envBitBucketAPIURL = "BITBUCKET_APIURL"
envBitBucketCompare = "BITBUCKET_COMPARE"
envBitBucketBackups = "BITBUCKET_BACKUPS"
envGiteaToken = "GITEA_TOKEN"
envGiteaAPIURL = "GITEA_APIURL"
envGiteaBackups = "GITEA_BACKUPS"
envGiteaCompare = "GITEA_COMPARE"
envGiteaOrgs = "GITEA_ORGS"
// provider names
providerNameAzureDevOps = "AzureDevOps"
providerNameBitBucket = "BitBucket"
providerNameGitHub = "GitHub"
providerNameGitLab = "GitLab"
providerNameGitea = "Gitea"
// compare types
compareTypeRefs = "refs"
compareTypeClone = "clone"
)
var (
logger *log.Logger
// overwritten at build time.
version, tag, sha, buildDate string
httpClient *retryablehttp.Client
enabledProviderAuth = map[string][]string{
providerNameAzureDevOps: {
envAzureDevOpsUserName,
envAzureDevOpsPAT,
},
providerNameGitHub: {
envGitHubToken,
},
providerNameGitLab: {
envGitLabToken,
},
providerNameBitBucket: {
envBitBucketUser,
envBitBucketKey,
envBitBucketSecret,
},
providerNameGitea: {
envGiteaAPIURL,
envGiteaToken,
},
}
justTokenProviders = []string{
providerNameGitHub,
providerNameGitLab,
providerNameGitea,
}
userAndPasswordProviders = []string{
providerNameBitBucket,
providerNameAzureDevOps,
}
numUserDefinedProviders int64
)
func init() {
logger = log.New(os.Stdout, fmt.Sprintf("%s: ", appName), log.Lshortfile|log.LstdFlags)
}
func getBackupInterval() int {
backupIntervalEnv := os.Getenv(envGitBackupInterval)
hours, isHour := isInt(backupIntervalEnv)
switch {
case isHour:
// an int represents hours
return hours * 60
case strings.HasSuffix(backupIntervalEnv, "h"):
// a string ending in h represents hours
hours, isHour = isInt(backupIntervalEnv[:len(backupIntervalEnv)-1])
if isHour {
return hours * 60
}
case strings.HasSuffix(backupIntervalEnv, "m"):
// a string ending in m represents minutes
minutes, isMinute := isInt(backupIntervalEnv[:len(backupIntervalEnv)-1])
if isMinute {
return minutes
}
}
return 0
}
func getLogLevel() int {
sobaLogLevelEnv := os.Getenv(envSobaLogLevel)
if sobaLogLevelEnv != "" {
sobaLogLevel, err := strconv.Atoi(sobaLogLevelEnv)
if err != nil {
logger.Fatalf("%s must be a number.", envSobaLogLevel)
}
return sobaLogLevel
}
return 0
}
func checkProviderFactory(provider string) func() {
retFunc := func() {
var outputErrs strings.Builder
// tokenOnlyProviders
if slices.Contains(justTokenProviders, provider) {
for _, param := range enabledProviderAuth[provider] {
val, exists := os.LookupEnv(param)
if exists {
if strings.Trim(val, " ") == "" {
_, _ = fmt.Fprintf(&outputErrs, "%s parameter '%s' is not defined.\n", provider, param)
} else {
numUserDefinedProviders++
}
}
}
}
// userAndPasswordProviders
if slices.Contains(userAndPasswordProviders, provider) {
var firstParamFound bool
for _, param := range enabledProviderAuth[provider] {
val, exists := os.LookupEnv(param)
if firstParamFound && !exists {
_, _ = fmt.Fprintf(&outputErrs, "all parameters for '%s' are required.\n", provider)
}
if exists {
firstParamFound = true
if val == "" {
_, _ = fmt.Fprintf(&outputErrs, "%s parameter '%s' is not defined.\n", provider, param)
} else {
numUserDefinedProviders++
}
}
}
}
if outputErrs.Len() > 0 {
logger.Fatalln(outputErrs.String())
}
}
return retFunc
}
func checkProvidersDefined() error {
for provider := range enabledProviderAuth {
checkProviderFactory(provider)()
}
if numUserDefinedProviders == 0 {
return errors.New("no providers defined")
}
return nil
}
func main() {
if tag != "" && buildDate != "" {
logger.Printf("[%s-%s] %s UTC", tag, sha, buildDate)
} else if version != "" {
logger.Println("version", version)
}
if err := run(); err != nil {
logger.Fatal(err)
}
}
func envTrue(envVar string) bool {
val := os.Getenv(envVar)
if val == "" {
return false
}
if strings.EqualFold(val, "yes") || strings.EqualFold(val, "y") {
return true
}
res, err := strconv.ParseBool(os.Getenv(envVar))
if err != nil {
return false
}
return res
}
func displayStartupConfig() {
if backupDIR := os.Getenv(envGitBackupDir); backupDIR != "" {
logger.Printf("root backup directory: %s", backupDIR)
}
// output github config
if ghToken := os.Getenv(envGitHubToken); ghToken != "" {
if ghOrgs := strings.ToLower(os.Getenv(envGitHubOrgs)); ghOrgs != "" {
logger.Printf("GitHub Organistations: %s", ghOrgs)
}
if envTrue(envGitHubSkipUserRepos) {
logger.Printf("GitHub skipping user repos: true")
}
if strings.EqualFold(os.Getenv(envGitHubCompare), compareTypeRefs) {
logger.Print("GitHub compare method: refs")
} else {
logger.Print("GitHub compare method: clone")
}
}
// output gitea config
if giteaToken := os.Getenv(envGiteaToken); giteaToken != "" {
if giteaOrgs := strings.ToLower(os.Getenv(envGiteaOrgs)); giteaOrgs != "" {
logger.Printf("Gitea Organistations: %s", giteaOrgs)
}
if giteaBackups := os.Getenv(envGiteaBackups); giteaBackups != "" {
logger.Printf("Gitea backups to keep: %s", giteaBackups)
}
if strings.EqualFold(os.Getenv(envGiteaCompare), compareTypeRefs) {
logger.Print("Gitea compare method: refs")
} else {
logger.Print("Gitea compare method: clone")
}
}
// output gitlab config
if glToken := os.Getenv(envGitLabToken); glToken != "" {
if glProjectMinAccessLevel := os.Getenv(envGitLabMinAccessLevel); glProjectMinAccessLevel != "" {
logger.Printf("GitLab project minimum access level: %s", glProjectMinAccessLevel)
} else {
logger.Printf("GitLab project minimum access level: %d", githosts.GitLabDefaultMinimumProjectAccessLevel)
}
if glBackups := os.Getenv(envGitLabBackups); glBackups != "" {
logger.Printf("GitLab backups to keep: %s", glBackups)
}
if strings.EqualFold(os.Getenv(envGitLabCompare), compareTypeRefs) {
logger.Print("GitLab compare method: refs")
} else {
logger.Print("GitLab compare method: clone")
}
}
// output bitbucket config
if bbUser := os.Getenv(envBitBucketUser); bbUser != "" {
if bbBackups := os.Getenv(envBitBucketBackups); bbBackups != "" {
logger.Printf("BitBucket backups to keep: %s", bbBackups)
}
if strings.ToLower(os.Getenv(envBitBucketCompare)) == compareTypeRefs {
logger.Printf("BitBucket compare method: %s", compareTypeRefs)
} else {
logger.Printf("BitBucket compare method: %s", compareTypeClone)
}
}
// output azure devops config
if azureDevOpsUserName := os.Getenv(envAzureDevOpsUserName); azureDevOpsUserName != "" {
if ghOrgs := strings.ToLower(os.Getenv(envAzureDevOpsOrgs)); ghOrgs != "" {
logger.Printf("Azure DevOps Organistations: %s", ghOrgs)
}
if strings.EqualFold(os.Getenv(envAzureDevOpsCompare), compareTypeRefs) {
logger.Print("Azure DevOps compare method: refs")
} else {
logger.Print("Azure DevOps compare method: clone")
}
}
}
func run() error {
gitExecPath := gitInstallPath()
if gitExecPath == "" {
return errors.New("git not found in PATH")
}
displayStartupConfig()
logger.Println("using git executable:", gitExecPath)
ok, reqTimeout, err := getRequestTimeout()
if err != nil {
return err
}
if ok {
logger.Printf("using defined request timeout: %s", reqTimeout.String())
} else {
logger.Printf("using default request timeout: %s", reqTimeout.String())
}
backupDIR, backupDIRKeyExists := os.LookupEnv(envGitBackupDir)
if !backupDIRKeyExists || backupDIR == "" {
return fmt.Errorf("environment variable %s must be set", envGitBackupDir)
}
if _, githubOrgsKeyExists := os.LookupEnv(envGitHubOrgs); githubOrgsKeyExists {
if _, githubTokenExists := os.LookupEnv(envGitHubToken); !githubTokenExists {
return fmt.Errorf("environment variable %s must be set if %s is set", envGitHubToken, envGitHubOrgs)
}
}
backupDIR = strings.TrimSuffix(backupDIR, "\n")
_, err = os.Stat(backupDIR)
if os.IsNotExist(err) {
return errors.Wrap(err, fmt.Sprintf("specified backup directory \"%s\" does not exist", backupDIR))
}
if err = checkProvidersDefined(); err != nil {
logger.Fatal("no providers defined")
}
workingDIR := filepath.Join(backupDIR, workingDIRName)
logger.Println("creating working directory:", workingDIR)
createWorkingDIRErr := os.MkdirAll(workingDIR, workingDIRMode)
if createWorkingDIRErr != nil {
logger.Fatal(createWorkingDIRErr)
}
backupInterval := getBackupInterval()
if backupInterval != 0 {
logger.Printf("scheduling to run every %s", formatIntervalDuration(backupInterval))
_, err = scheduler.Every(int(time.Duration(backupInterval))).Minutes().Run(execProviderBackups)
if err != nil {
return errors.Wrapf(err, "scheduler failed")
}
runtime.Goexit()
} else {
execProviderBackups()
}
return nil
}
func formatIntervalDuration(m int) string {
if m == 0 {
return ""
}
if m%60 == 0 {
return fmt.Sprintf("%dh", m/60)
}
return time.Duration(int64(m) * int64(time.Minute)).String()
}
func getRequestTimeout() (bool, time.Duration, error) {
eReqTimeout := os.Getenv(envGitRequestTimeout)
if eReqTimeout != "" {
reqTimeoutInt, err := strconv.Atoi(eReqTimeout)
if err != nil {
return false, defaultHTTPClientRequestTimeout, fmt.Errorf("%s value \"%s\" should be the maximum seconds to wait for a response, defined as an integer", envGitRequestTimeout, eReqTimeout)
}
return true, time.Duration(reqTimeoutInt) * time.Second, nil
}
return false, defaultHTTPClientRequestTimeout, nil
}
func getOrgsListFromEnvVar(envVar string) []string {
orgsList := os.Getenv(envVar)
if orgsList == "" {
return []string{}
}
return strings.Split(orgsList, ",")
}
type ProviderBackupResults struct {
Provider string `json:"provider"`
Results githosts.ProviderBackupResult `json:"results"`
}
type BackupResults struct {
StartedAt sobaTime `json:"started_at"`
FinishedAt sobaTime `json:"finished_at"`
Results *[]ProviderBackupResults `json:"results,omitempty"`
}
func getHTTPClient(logLevel string) *retryablehttp.Client {
tr := &http.Transport{
DisableKeepAlives: false,
DisableCompression: true,
MaxIdleConns: maxIdleConns,
IdleConnTimeout: idleConnTimeout,
ForceAttemptHTTP2: false,
}
rc := retryablehttp.NewClient()
_, reqTimeout, _ := getRequestTimeout()
rc.HTTPClient = &http.Client{
Transport: tr,
Timeout: reqTimeout,
}
if !strings.EqualFold(logLevel, "debug") {
rc.Logger = nil
}
rc.RetryWaitMax = 120 * time.Second
rc.RetryWaitMin = 60 * time.Second
rc.RetryMax = 2
return rc
}
func execProviderBackups() {
startTime := time.Now()
backupDir := os.Getenv(envGitBackupDir)
if httpClient == nil {
httpClient = getHTTPClient(os.Getenv(envSobaLogLevel))
}
backupResults := BackupResults{
StartedAt: sobaTime{
Time: time.Now(),
f: time.RFC3339,
},
}
var providerBackupResults []ProviderBackupResults
if os.Getenv(envBitBucketUser) != "" {
providerBackupResults = append(providerBackupResults, *Bitbucket(backupDir))
}
if os.Getenv(envGiteaToken) != "" {
providerBackupResults = append(providerBackupResults, *Gitea(backupDir))
}
if os.Getenv(envGitHubToken) != "" {
providerBackupResults = append(providerBackupResults, *GitHub(backupDir))
}
if os.Getenv(envGitLabToken) != "" {
providerBackupResults = append(providerBackupResults, *Gitlab(backupDir))
}
if os.Getenv(envAzureDevOpsUserName) != "" {
providerBackupResults = append(providerBackupResults, *AzureDevOps(backupDir))
}
logger.Println("cleaning up")
// startFileRemovals := time.Now()
delErr := os.RemoveAll(backupDir + pathSep + workingDIRName + pathSep)
if delErr != nil {
logger.Printf("failed to delete working directory: %s",
backupDir+pathSep+workingDIRName)
}
// logger.Printf("file removals took %s", time.Since(startFileRemovals).String())
backupResults.Results = &providerBackupResults
backupResults.FinishedAt = sobaTime{
Time: time.Now(),
f: time.RFC3339,
}
succeeded, failed := getBackupsStats(backupResults)
switch {
case succeeded == 0 && failed >= 0:
logger.Println("all backups failed")
case succeeded > 0 && failed > 0:
logger.Println("backups completed with errors")
default:
logger.Println("backups complete")
}
notify(backupResults, succeeded, failed)
var backupInterval int
if backupInterval = getBackupInterval(); backupInterval > 0 {
// help avoid thrashing provider apis if job auto-restartsrestarts
// after an early failure by adding delay if backup took less than 10 seconds
if time.Since(startTime) < time.Second*defaultEarlyErrorBackOffSeconds {
logger.Printf("backup took less than 10 seconds, "+
"waiting %d seconds before next run to avoid thrashing"+
" provider apis", defaultEarlyErrorBackOffSeconds)
time.Sleep(time.Second * defaultEarlyErrorBackOffSeconds)
}
nextBackupTime := startTime.Add(time.Duration(backupInterval) * time.Minute)
if nextBackupTime.Before(time.Now()) {
logger.Fatal("error: backup took longer than scheduled interval")
}
logger.Printf("next run scheduled for: %s", nextBackupTime.Format("2006-01-02 15:04:05 -0700 MST"))
} else {
// if no interval is set then exit
if failed > 0 {
os.Exit(1)
}
}
}
func getProjectMinimumAccessLevel() int {
if os.Getenv(envGitLabMinAccessLevel) == "" {
logger.Printf("environment variable %s not set, using default of %d", envGitLabMinAccessLevel, defaultGitLabMinimumProjectAccessLevel)
return defaultGitLabMinimumProjectAccessLevel
}
minimumProjectAccessLevel, err := strconv.Atoi(os.Getenv(envGitLabMinAccessLevel))
if err != nil {
logger.Printf("error converting environment variable %s to int so defaulting to: %d", envGitLabMinAccessLevel, defaultGitLabMinimumProjectAccessLevel)
return defaultGitLabMinimumProjectAccessLevel
}
return minimumProjectAccessLevel
}
func getBackupsToRetain(envVar string) int {
if os.Getenv(envVar) == "" {
logger.Printf("environment variable %s not set, using default of %d", envVar, defaultBackupsToRetain)
return defaultBackupsToRetain
}
backupsToKeep, err := strconv.Atoi(os.Getenv(envVar))
if err != nil {
logger.Printf("error converting environment variable %s to int so defaulting to: %d", envVar, defaultBackupsToRetain)
return defaultBackupsToRetain
}
return backupsToKeep
}
func isInt(i string) (int, bool) {
if val, err := strconv.Atoi(i); err == nil {
return val, true
}
return 0, false
}
var lookPath = exec.LookPath
func gitInstallPath() string {
path, _ := lookPath("git")
return path
}