-
Notifications
You must be signed in to change notification settings - Fork 97
/
Copy pathcommands.go
1042 lines (903 loc) · 25.1 KB
/
commands.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
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2014-2019 Ludovic Fauvet
// Licensed under the MIT license
package cli
import (
"bufio"
"bytes"
"context"
"flag"
"fmt"
"io/ioutil"
"net/url"
"os"
"os/exec"
"reflect"
"sort"
"strings"
"sync"
"text/tabwriter"
"time"
"github.com/etix/mirrorbits/core"
"github.com/etix/mirrorbits/filesystem"
"github.com/etix/mirrorbits/mirrors"
"github.com/etix/mirrorbits/rpc"
"github.com/etix/mirrorbits/utils"
"github.com/golang/protobuf/ptypes"
"github.com/golang/protobuf/ptypes/empty"
"github.com/howeyc/gopass"
"github.com/op/go-logging"
"github.com/pkg/errors"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"gopkg.in/yaml.v2"
)
const (
commentSeparator = "##### Comments go below this line #####"
defaultRPCTimeout = time.Second * 10
)
var (
log = logging.MustGetLogger("main")
)
type cli struct {
sync.Mutex
rpcconn *grpc.ClientConn
creds *loginCreds
}
// ParseCommands parses the command line and call the appropriate functions
func ParseCommands(args ...string) error {
c := &cli{
creds: &loginCreds{
Password: core.RPCPassword,
},
}
if len(args) > 0 && args[0] != "help" {
method, exists := c.getMethod(args[0])
if !exists {
fmt.Println("Error: Command not found:", args[0])
return c.CmdHelp()
}
if len(c.creds.Password) == 0 && core.RPCAskPass {
fmt.Print("Password: ")
passwd, err := gopass.GetPasswdMasked()
if err != nil {
return err
}
c.creds.Password = string(passwd)
}
ret := method.Func.CallSlice([]reflect.Value{
reflect.ValueOf(c),
reflect.ValueOf(args[1:]),
})[0].Interface()
if c.rpcconn != nil {
c.rpcconn.Close()
}
if ret == nil {
return nil
}
return ret.(error)
}
return c.CmdHelp()
}
func (c *cli) getMethod(name string) (reflect.Method, bool) {
methodName := "Cmd" + strings.ToUpper(name[:1]) + strings.ToLower(name[1:])
return reflect.TypeOf(c).MethodByName(methodName)
}
func (c *cli) CmdHelp() error {
help := fmt.Sprintf("Usage: mirrorbits [OPTIONS] COMMAND [arg...]\n\nA smart download redirector.\n\n")
help += fmt.Sprintf("Server commands:\n %-10.10s%s\n\n", "daemon", "Start the server")
help += fmt.Sprintf("CLI commands:\n")
for _, command := range [][]string{
{"add", "Add a new mirror"},
{"disable", "Disable a mirror"},
{"edit", "Edit a mirror"},
{"enable", "Enable a mirror"},
{"export", "Export the mirror database"},
{"list", "List all mirrors"},
{"logs", "Print logs of a mirror"},
{"refresh", "Refresh the local repository"},
{"reload", "Reload configuration"},
{"remove", "Remove a mirror"},
{"scan", "(Re-)Scan a mirror"},
{"show", "Print a mirror configuration"},
{"stats", "Show download stats"},
{"upgrade", "Seamless binary upgrade"},
{"version", "Print version information"},
} {
help += fmt.Sprintf(" %-10.10s%s\n", command[0], command[1])
}
fmt.Fprintf(os.Stderr, "%s\n", help)
return nil
}
// SubCmd prints the usage of a subcommand
func SubCmd(name, signature, description string) *flag.FlagSet {
flags := flag.NewFlagSet(name, flag.ContinueOnError)
flags.Usage = func() {
fmt.Fprintf(os.Stderr, "\nUsage: mirrorbits %s %s\n\n%s\n\n", name, signature, description)
flags.PrintDefaults()
}
return flags
}
type ByDate []*rpc.Mirror
func (d ByDate) Len() int { return len(d) }
func (d ByDate) Swap(i, j int) { d[i], d[j] = d[j], d[i] }
func (d ByDate) Less(i, j int) bool { return d[i].StateSince.Seconds > d[j].StateSince.Seconds }
func (c *cli) CmdList(args ...string) error {
cmd := SubCmd("list", "", "Get the list of mirrors")
http := cmd.Bool("http", false, "Print HTTP addresses")
rsync := cmd.Bool("rsync", false, "Print rsync addresses")
ftp := cmd.Bool("ftp", false, "Print FTP addresses")
location := cmd.Bool("location", false, "Print the country and continent code")
state := cmd.Bool("state", true, "Print the state of the mirror")
score := cmd.Bool("score", false, "Print the score of the mirror")
disabled := cmd.Bool("disabled", false, "List disabled mirrors only")
enabled := cmd.Bool("enabled", false, "List enabled mirrors only")
down := cmd.Bool("down", false, "List only mirrors currently down")
if err := cmd.Parse(args); err != nil {
return nil
}
if cmd.NArg() != 0 {
cmd.Usage()
return nil
}
client := c.GetRPC()
ctx, cancel := context.WithTimeout(context.Background(), defaultRPCTimeout)
defer cancel()
list, err := client.List(ctx, &empty.Empty{})
if err != nil {
log.Fatal("list error:", err)
}
sort.Sort(ByDate(list.Mirrors))
w := new(tabwriter.Writer)
w.Init(os.Stdout, 0, 8, 0, '\t', 0)
fmt.Fprint(w, "Identifier ")
if *score == true {
fmt.Fprint(w, "\tSCORE")
}
if *http == true {
fmt.Fprint(w, "\tHTTP ")
}
if *rsync == true {
fmt.Fprint(w, "\tRSYNC ")
}
if *ftp == true {
fmt.Fprint(w, "\tFTP ")
}
if *location == true {
fmt.Fprint(w, "\tLOCATION ")
}
if *state == true {
fmt.Fprint(w, "\tSTATE\tSINCE")
}
fmt.Fprint(w, "\n")
for _, mirror := range list.Mirrors {
if *disabled == true {
if mirror.Enabled == true {
continue
}
}
if *enabled == true {
if mirror.Enabled == false {
continue
}
}
if *down == true {
if mirror.Up == true {
continue
}
}
stateSince, err := ptypes.Timestamp(mirror.StateSince)
if err != nil {
log.Fatal("list error:", err)
}
fmt.Fprintf(w, "%s ", mirror.Name)
if *score == true {
fmt.Fprintf(w, "\t%d ", mirror.Score)
}
if *http == true {
fmt.Fprintf(w, "\t%s ", mirror.HttpURL)
}
if *rsync == true {
fmt.Fprintf(w, "\t%s ", mirror.RsyncURL)
}
if *ftp == true {
fmt.Fprintf(w, "\t%s ", mirror.FtpURL)
}
if *location == true {
countries := strings.Split(mirror.CountryCodes, " ")
countryCode := "/"
if len(countries) >= 1 {
countryCode = countries[0]
}
fmt.Fprintf(w, "\t%s (%s) ", countryCode, mirror.ContinentCode)
}
if *state == true {
if mirror.Enabled == false {
fmt.Fprintf(w, "\tdisabled")
} else if mirror.Up == true {
fmt.Fprintf(w, "\tup")
} else {
fmt.Fprintf(w, "\tdown")
}
fmt.Fprintf(w, " \t(%s)", stateSince.Format(time.RFC1123))
}
fmt.Fprint(w, "\n")
}
w.Flush()
return nil
}
func (c *cli) CmdAdd(args ...string) error {
cmd := SubCmd("add", "[OPTIONS] IDENTIFIER", "Add a new mirror")
http := cmd.String("http", "", "HTTP base URL")
rsync := cmd.String("rsync", "", "RSYNC base URL (for scanning only)")
ftp := cmd.String("ftp", "", "FTP base URL (for scanning only)")
sponsorName := cmd.String("sponsor-name", "", "Name of the sponsor")
sponsorURL := cmd.String("sponsor-url", "", "URL of the sponsor")
sponsorLogo := cmd.String("sponsor-logo", "", "URL of a logo to display for this mirror")
adminName := cmd.String("admin-name", "", "Admin's name")
adminEmail := cmd.String("admin-email", "", "Admin's email")
customData := cmd.String("custom-data", "", "Associated data to return when the mirror is selected (i.e. json document)")
continentOnly := cmd.Bool("continent-only", false, "The mirror should only handle its continent")
countryOnly := cmd.Bool("country-only", false, "The mirror should only handle its country")
asOnly := cmd.Bool("as-only", false, "The mirror should only handle clients in the same AS number")
score := cmd.Int("score", 0, "Weight to give to the mirror during selection")
comment := cmd.String("comment", "", "Comment")
if err := cmd.Parse(args); err != nil {
return nil
}
if cmd.NArg() < 1 {
cmd.Usage()
return nil
}
if strings.Contains(cmd.Arg(0), " ") {
fmt.Fprintf(os.Stderr, "The identifier cannot contain a space\n")
os.Exit(-1)
}
if *http == "" {
fmt.Fprintf(os.Stderr, "You *must* pass at least an HTTP URL\n")
os.Exit(-1)
}
if !strings.HasPrefix(*http, "http://") && !strings.HasPrefix(*http, "https://") {
*http = "http://" + *http
}
_, err := url.Parse(*http)
if err != nil {
fmt.Fprintf(os.Stderr, "Can't parse url\n")
os.Exit(-1)
}
mirror := &mirrors.Mirror{
Name: cmd.Arg(0),
HttpURL: *http,
RsyncURL: *rsync,
FtpURL: *ftp,
SponsorName: *sponsorName,
SponsorURL: *sponsorURL,
SponsorLogoURL: *sponsorLogo,
AdminName: *adminName,
AdminEmail: *adminEmail,
CustomData: *customData,
ContinentOnly: *continentOnly,
CountryOnly: *countryOnly,
ASOnly: *asOnly,
Score: *score,
Comment: *comment,
}
client := c.GetRPC()
ctx, cancel := context.WithTimeout(context.Background(), defaultRPCTimeout)
defer cancel()
m, err := rpc.MirrorToRPC(mirror)
if err != nil {
log.Fatal("edit error:", err)
}
reply, err := client.AddMirror(ctx, m)
if err != nil {
if err.Error() == rpc.ErrNameAlreadyTaken.Error() {
log.Fatalf("Mirror %s already exists!\n", mirror.Name)
}
log.Fatal("edit error:", err)
}
for i := 0; i < len(reply.Warnings); i++ {
fmt.Println(reply.Warnings[i])
if i == len(reply.Warnings)-1 {
fmt.Println("")
}
}
if reply.Country != "" {
fmt.Println("Mirror location:")
fmt.Printf("Latitude: %.4f\n", reply.Latitude)
fmt.Printf("Longitude: %.4f\n", reply.Longitude)
fmt.Printf("Continent: %s\n", reply.Continent)
fmt.Printf("Country: %s\n", reply.Country)
fmt.Printf("ASN: %s\n", reply.ASN)
fmt.Println("")
}
fmt.Printf("Mirror '%s' added successfully\n", mirror.Name)
fmt.Printf("Enable this mirror using\n $ mirrorbits enable %s\n", mirror.Name)
return nil
}
func (c *cli) CmdRemove(args ...string) error {
cmd := SubCmd("remove", "IDENTIFIER", "Remove an existing mirror")
force := cmd.Bool("f", false, "Never prompt for confirmation")
if err := cmd.Parse(args); err != nil {
return nil
}
if cmd.NArg() != 1 {
cmd.Usage()
return nil
}
id, name := c.matchMirror(cmd.Arg(0))
if *force == false {
fmt.Printf("Removing %s, are you sure? [y/N]", name)
reader := bufio.NewReader(os.Stdin)
s, _ := reader.ReadString('\n')
switch s[0] {
case 'y', 'Y':
break
default:
return nil
}
}
client := c.GetRPC()
ctx, cancel := context.WithTimeout(context.Background(), defaultRPCTimeout)
defer cancel()
_, err := client.RemoveMirror(ctx, &rpc.MirrorIDRequest{
ID: int32(id),
})
if err != nil {
log.Fatal("remove error:", err)
}
fmt.Printf("Mirror '%s' removed successfully\n", name)
return nil
}
func (c *cli) CmdScan(args ...string) error {
cmd := SubCmd("scan", "[IDENTIFIER]", "(Re-)Scan a mirror")
enable := cmd.Bool("enable", false, "Enable the mirror automatically if the scan is successful")
all := cmd.Bool("all", false, "Scan all mirrors at once")
ftp := cmd.Bool("ftp", false, "Force a scan using FTP")
rsync := cmd.Bool("rsync", false, "Force a scan using rsync")
timeout := cmd.Uint("timeout", 0, "Timeout in seconds")
if err := cmd.Parse(args); err != nil {
return nil
}
if !*all && cmd.NArg() != 1 || *all && cmd.NArg() != 0 {
cmd.Usage()
return nil
}
client := c.GetRPC()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
list := make(map[int]string)
// Get the list of mirrors to scan
if *all == true {
reply, err := client.MatchMirror(ctx, &rpc.MatchRequest{
Pattern: "", // Match all of them
})
if err != nil {
return errors.New("Cannot fetch the list of mirrors")
}
for _, m := range reply.Mirrors {
list[int(m.ID)] = m.Name
}
} else {
// Single mirror
id, name := c.matchMirror(cmd.Arg(0))
list[id] = name
}
// Set the method of the scan (if not default)
var method rpc.ScanMirrorRequest_Method
if *ftp == false && *rsync == false {
method = rpc.ScanMirrorRequest_ALL
} else if *rsync == true {
method = rpc.ScanMirrorRequest_RSYNC
} else if *ftp == true {
method = rpc.ScanMirrorRequest_FTP
}
for id, name := range list {
if *timeout > 0 {
ctx, cancel = context.WithTimeout(context.Background(), time.Duration(*timeout)*time.Second)
defer cancel()
}
fmt.Printf("Scanning %s... ", name)
reply, err := client.ScanMirror(ctx, &rpc.ScanMirrorRequest{
ID: int32(id),
AutoEnable: *enable,
Protocol: method,
})
if err != nil {
s := status.Convert(err)
if s.Code() == codes.FailedPrecondition || len(list) == 1 {
return errors.New("\nscan error: " + grpc.ErrorDesc(err))
}
fmt.Println("scan error:", grpc.ErrorDesc(err))
continue
} else {
fmt.Printf("%d files indexed, %d known and %d removed\n", reply.FilesIndexed, reply.KnownIndexed, reply.Removed)
if reply.GetTZOffsetMs() != 0 {
fmt.Printf(" ∟ Timezone offset detected and corrected: %d milliseconds\n", reply.TZOffsetMs)
}
if reply.Enabled {
fmt.Println(" ∟ Enabled")
}
}
}
return nil
}
func (c *cli) CmdRefresh(args ...string) error {
cmd := SubCmd("refresh", "", "Scan the local repository")
rehash := cmd.Bool("rehash", false, "Force a rehash of the files")
if err := cmd.Parse(args); err != nil {
return nil
}
if cmd.NArg() != 0 {
cmd.Usage()
return nil
}
fmt.Print("Refreshing the local repository... ")
client := c.GetRPC()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
_, err := client.RefreshRepository(ctx, &rpc.RefreshRepositoryRequest{
Rehash: *rehash,
})
if err != nil {
fmt.Println("")
log.Fatal(err)
}
fmt.Println("done")
return nil
}
func (c *cli) matchMirror(pattern string) (id int, name string) {
if len(pattern) == 0 {
return -1, ""
}
client := c.GetRPC()
ctx, cancel := context.WithTimeout(context.Background(), defaultRPCTimeout)
defer cancel()
reply, err := client.MatchMirror(ctx, &rpc.MatchRequest{
Pattern: pattern,
})
if err != nil {
fmt.Fprintf(os.Stderr, "mirror matching: %s\n", err)
os.Exit(1)
}
switch len(reply.Mirrors) {
case 0:
fmt.Fprintf(os.Stderr, "No match for '%s'\n", pattern)
os.Exit(1)
case 1:
id, name, err := GetSingle(reply.Mirrors)
if err != nil {
log.Fatal("unexpected error:", err)
}
return id, name
default:
fmt.Fprintln(os.Stderr, "Multiple match:")
for _, mirror := range reply.Mirrors {
fmt.Fprintf(os.Stderr, " %s\n", mirror.Name)
}
os.Exit(1)
}
return
}
func GetSingle(list []*rpc.MirrorID) (int, string, error) {
if len(list) == 0 {
return -1, "", errors.New("list is empty")
} else if len(list) > 1 {
return -1, "", errors.New("too many results")
}
return int(list[0].ID), list[0].Name, nil
}
func (c *cli) CmdEdit(args ...string) error {
cmd := SubCmd("edit", "[IDENTIFIER]", "Edit a mirror")
if err := cmd.Parse(args); err != nil {
return nil
}
if cmd.NArg() != 1 {
cmd.Usage()
return nil
}
// Find the editor to use
editor := os.Getenv("EDITOR")
if editor == "" {
log.Fatal("Environment variable $EDITOR not set")
}
id, _ := c.matchMirror(cmd.Arg(0))
client := c.GetRPC()
ctx, cancel := context.WithTimeout(context.Background(), defaultRPCTimeout)
defer cancel()
rpcm, err := client.MirrorInfo(ctx, &rpc.MirrorIDRequest{
ID: int32(id),
})
if err != nil {
log.Fatal("edit error:", err)
}
mirror, err := rpc.MirrorFromRPC(rpcm)
if err != nil {
log.Fatal("edit error:", err)
}
// Generate a yaml configuration string from the struct
out, err := yaml.Marshal(mirror)
// Open a temporary file
f, err := ioutil.TempFile(os.TempDir(), "edit")
if err != nil {
log.Fatal("Cannot create temporary file:", err)
}
defer os.Remove(f.Name())
f.WriteString("# You can now edit this mirror configuration.\n" +
"# Just save and quit when you're done.\n\n")
f.WriteString(string(out))
f.WriteString(fmt.Sprintf("\n%s\n\n%s\n", commentSeparator, mirror.Comment))
f.Close()
// Checksum the original file
chk, _ := filesystem.Sha256sum(f.Name())
reopen:
// Launch the editor with the filename as first parameter
exe := exec.Command(editor, f.Name())
exe.Stdin = os.Stdin
exe.Stdout = os.Stdout
exe.Stderr = os.Stderr
err = exe.Run()
if err != nil {
log.Fatal(err)
}
// Read the file back
out, err = ioutil.ReadFile(f.Name())
if err != nil {
log.Fatal("Cannot read file", f.Name())
}
// Checksum the file back and compare
chk2, _ := filesystem.Sha256sum(f.Name())
if bytes.Compare(chk, chk2) == 0 {
fmt.Println("Aborted - settings are unmodified, so there is nothing to change.")
return nil
}
var comment string
yamlstr := string(out)
commentIndex := strings.Index(yamlstr, commentSeparator)
if commentIndex > 0 {
comment = strings.TrimSpace(yamlstr[commentIndex+len(commentSeparator):])
yamlstr = yamlstr[:commentIndex]
}
reopen := func(err error) bool {
eagain:
fmt.Printf("%s\nRetry? [Y/n]", err.Error())
reader := bufio.NewReader(os.Stdin)
s, _ := reader.ReadString('\n')
switch s[0] {
case 'y', 'Y', 10:
return true
case 'n', 'N':
fmt.Println("Aborted")
return false
default:
goto eagain
}
}
// Fill the struct from the yaml
err = yaml.Unmarshal([]byte(yamlstr), &mirror)
if err != nil {
switch reopen(err) {
case true:
goto reopen
case false:
return nil
}
}
mirror.Comment = comment
ctx, cancel = context.WithTimeout(context.Background(), defaultRPCTimeout)
defer cancel()
m, err := rpc.MirrorToRPC(mirror)
if err != nil {
log.Fatal("edit error:", err)
}
reply, err := client.UpdateMirror(ctx, m)
if err != nil {
if err.Error() == rpc.ErrNameAlreadyTaken.Error() {
switch reopen(errors.New("Name already taken")) {
case true:
goto reopen
case false:
return nil
}
}
log.Fatal("edit error:", err)
}
if len(reply.Diff) > 0 {
fmt.Println(reply.Diff)
}
fmt.Printf("Mirror '%s' edited successfully\n", mirror.Name)
return nil
}
func (c *cli) CmdShow(args ...string) error {
cmd := SubCmd("show", "[IDENTIFIER]", "Print a mirror configuration")
if err := cmd.Parse(args); err != nil {
return nil
}
if cmd.NArg() != 1 {
cmd.Usage()
return nil
}
id, _ := c.matchMirror(cmd.Arg(0))
client := c.GetRPC()
ctx, cancel := context.WithTimeout(context.Background(), defaultRPCTimeout)
defer cancel()
rpcm, err := client.MirrorInfo(ctx, &rpc.MirrorIDRequest{
ID: int32(id),
})
if err != nil {
log.Fatal("edit error:", err)
}
mirror, err := rpc.MirrorFromRPC(rpcm)
if err != nil {
log.Fatal("edit error:", err)
}
// Generate a yaml configuration string from the struct
out, err := yaml.Marshal(mirror)
if err != nil {
log.Fatal("show error:", err)
}
fmt.Printf("%s\nComment:\n%s\n", out, mirror.Comment)
return nil
}
func (c *cli) CmdExport(args ...string) error {
cmd := SubCmd("export", "[format]", "Export the mirror database.\n\nAvailable formats: mirmon")
rsync := cmd.Bool("rsync", true, "Export rsync URLs")
http := cmd.Bool("http", true, "Export http URLs")
ftp := cmd.Bool("ftp", true, "Export ftp URLs")
disabled := cmd.Bool("disabled", true, "Export disabled mirrors")
if err := cmd.Parse(args); err != nil {
return nil
}
if cmd.NArg() != 1 {
cmd.Usage()
return nil
}
if cmd.Arg(0) != "mirmon" {
fmt.Fprintf(os.Stderr, "Unsupported format\n")
cmd.Usage()
return nil
}
client := c.GetRPC()
ctx, cancel := context.WithTimeout(context.Background(), defaultRPCTimeout)
defer cancel()
list, err := client.List(ctx, &empty.Empty{})
if err != nil {
log.Fatal("export error:", err)
}
w := new(tabwriter.Writer)
w.Init(os.Stdout, 0, 8, 0, '\t', 0)
for _, m := range list.Mirrors {
if *disabled == false {
if m.Enabled == false {
continue
}
}
ccodes := strings.Fields(m.CountryCodes)
urls := make([]string, 0, 3)
if *rsync == true && m.RsyncURL != "" {
urls = append(urls, m.RsyncURL)
}
if *http == true && m.HttpURL != "" {
urls = append(urls, m.HttpURL)
}
if *ftp == true && m.FtpURL != "" {
urls = append(urls, m.FtpURL)
}
for _, u := range urls {
fmt.Fprintf(w, "%s\t%s\t%s\n", ccodes[0], u, m.AdminEmail)
}
}
w.Flush()
return nil
}
func (c *cli) CmdEnable(args ...string) error {
cmd := SubCmd("enable", "[IDENTIFIER]", "Enable a mirror")
if err := cmd.Parse(args); err != nil {
return nil
}
if cmd.NArg() != 1 {
cmd.Usage()
return nil
}
c.changeStatus(cmd.Arg(0), true)
return nil
}
func (c *cli) CmdDisable(args ...string) error {
cmd := SubCmd("disable", "[IDENTIFIER]", "Disable a mirror")
if err := cmd.Parse(args); err != nil {
return nil
}
if cmd.NArg() != 1 {
cmd.Usage()
return nil
}
c.changeStatus(cmd.Arg(0), false)
return nil
}
func (c *cli) changeStatus(pattern string, enabled bool) {
id, name := c.matchMirror(pattern)
client := c.GetRPC()
ctx, cancel := context.WithTimeout(context.Background(), defaultRPCTimeout)
defer cancel()
_, err := client.ChangeStatus(ctx, &rpc.ChangeStatusRequest{
ID: int32(id),
Enabled: enabled,
})
if err != nil {
if enabled {
log.Fatalf("Couldn't enable mirror '%s': %s\n", name, err)
} else {
log.Fatalf("Couldn't disable mirror '%s': %s\n", name, err)
}
}
if enabled {
fmt.Printf("Mirror '%s' enabled successfully\n", name)
} else {
fmt.Printf("Mirror '%s' disabled successfully\n", name)
}
return
}
func (c *cli) CmdStats(args ...string) error {
cmd := SubCmd("stats", "[OPTIONS] [mirror|file] [IDENTIFIER|PATTERN]", "Show download stats for a particular mirror or a file pattern")
dateStart := cmd.String("start-date", "", "Starting date (format YYYY-MM-DD)")
dateEnd := cmd.String("end-date", "", "Ending date (format YYYY-MM-DD)")
human := cmd.Bool("h", true, "Human readable version")
if err := cmd.Parse(args); err != nil {
return nil
}
if cmd.NArg() != 2 || (cmd.Arg(0) != "mirror" && cmd.Arg(0) != "file") {
cmd.Usage()
return nil
}
start, err := time.Parse("2006-1-2", *dateStart)
if err != nil {
start = time.Now()
}
startproto, _ := ptypes.TimestampProto(start)
end, err := time.Parse("2006-1-2", *dateEnd)
if err != nil {
end = time.Now()
}
endproto, _ := ptypes.TimestampProto(end)
client := c.GetRPC()
ctx, cancel := context.WithTimeout(context.Background(), defaultRPCTimeout)
defer cancel()
if cmd.Arg(0) == "file" {
// File stats
reply, err := client.StatsFile(ctx, &rpc.StatsFileRequest{
Pattern: cmd.Arg(1),
DateStart: startproto,
DateEnd: endproto,
})
if err != nil {
log.Fatal("file stats error:", err)
}
// Format the results
w := new(tabwriter.Writer)
w.Init(os.Stdout, 0, 8, 0, '\t', 0)
// Sort keys and count requests
var keys []string
var requests int64
for k, req := range reply.Files {
requests += req
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
fmt.Fprintf(w, "%s:\t%d\n", k, reply.Files[k])
}
if len(keys) > 0 {
// Add a line separator
fmt.Fprintf(w, "\t\n")
}
fmt.Fprintf(w, "Total download requests: \t%d\n", requests)
w.Flush()
} else if cmd.Arg(0) == "mirror" {
// Mirror stats
id, name := c.matchMirror(cmd.Arg(1))
reply, err := client.StatsMirror(ctx, &rpc.StatsMirrorRequest{
ID: int32(id),
DateStart: startproto,
DateEnd: endproto,
})
if err != nil {
log.Fatal("mirror stats error:", err)
}
// Format the results
w := new(tabwriter.Writer)
w.Init(os.Stdout, 0, 8, 0, '\t', 0)
fmt.Fprintf(w, "Identifier:\t%s\n", name)
if !reply.Mirror.Enabled {
fmt.Fprintf(w, "Status:\tdisabled\n")
} else if reply.Mirror.Up {
fmt.Fprintf(w, "Status:\tup\n")
} else {
fmt.Fprintf(w, "Status:\tdown\n")
}
fmt.Fprintf(w, "Download requests:\t%d\n", reply.Requests)
fmt.Fprint(w, "Bytes transferred:\t")
if *human {
fmt.Fprintln(w, utils.ReadableSize(reply.Bytes))
} else {
fmt.Fprintln(w, reply.Bytes)
}
w.Flush()
}
return nil
}
func (c *cli) CmdLogs(args ...string) error {
cmd := SubCmd("logs", "[IDENTIFIER]", "Print logs of a mirror")
maxResults := cmd.Uint("l", 500, "Maximum number of logs to return")
if err := cmd.Parse(args); err != nil {
return nil
}
if cmd.NArg() != 1 {
cmd.Usage()
return nil
}
id, name := c.matchMirror(cmd.Arg(0))
client := c.GetRPC()
ctx, cancel := context.WithTimeout(context.Background(), defaultRPCTimeout)
defer cancel()
resp, err := client.GetMirrorLogs(ctx, &rpc.GetMirrorLogsRequest{
ID: int32(id),
MaxResults: int32(*maxResults),
})
if err != nil {
log.Fatal("logs error:", err)
}
if len(resp.Line) == 0 {
fmt.Printf("No logs for %s\n", name)
return nil
}
fmt.Printf("Printing logs for %s:\n", name)
for _, l := range resp.Line {
fmt.Println(l)
}
return nil
}
func (c *cli) CmdReload(args ...string) error {
client := c.GetRPC()
ctx, cancel := context.WithTimeout(context.Background(), defaultRPCTimeout)
defer cancel()
_, err := client.Reload(ctx, &empty.Empty{})
if err != nil {
log.Fatal("upgrade error:", err)
}