| 1 |
// Copyright (c) 2014-2019 Ludovic Fauvet |
| 2 |
// Licensed under the MIT license |
| 3 |
|
| 4 |
package cli |
| 5 |
|
| 6 |
import ( |
| 7 |
"bufio" |
| 8 |
"bytes" |
| 9 |
"context" |
| 10 |
"errors" |
| 11 |
"flag" |
| 12 |
"fmt" |
| 13 |
"net/url" |
| 14 |
"os" |
| 15 |
"os/exec" |
| 16 |
"reflect" |
| 17 |
"sort" |
| 18 |
"strings" |
| 19 |
"sync" |
| 20 |
"text/tabwriter" |
| 21 |
"time" |
| 22 |
|
| 23 |
"github.com/etix/mirrorbits/core" |
| 24 |
"github.com/etix/mirrorbits/filesystem" |
| 25 |
"github.com/etix/mirrorbits/mirrors" |
| 26 |
"github.com/etix/mirrorbits/rpc" |
| 27 |
"github.com/etix/mirrorbits/utils" |
| 28 |
"github.com/golang/protobuf/ptypes" |
| 29 |
"github.com/golang/protobuf/ptypes/empty" |
| 30 |
"github.com/op/go-logging" |
| 31 |
"golang.org/x/term" |
| 32 |
"google.golang.org/grpc" |
| 33 |
"google.golang.org/grpc/codes" |
| 34 |
"google.golang.org/grpc/status" |
| 35 |
"gopkg.in/yaml.v3" |
| 36 |
) |
| 37 |
|
| 38 |
const ( |
| 39 |
commentSeparator = "##### Comments go below this line #####" |
| 40 |
defaultRPCTimeout = time.Second * 10 |
| 41 |
) |
| 42 |
|
| 43 |
var ( |
| 44 |
log = logging.MustGetLogger("main") |
| 45 |
) |
| 46 |
|
| 47 |
type cli struct { |
| 48 |
sync.Mutex |
| 49 |
rpcconn *grpc.ClientConn |
| 50 |
creds *loginCreds |
| 51 |
} |
| 52 |
|
| 53 |
// ParseCommands parses the command line and call the appropriate functions |
| 54 |
func ParseCommands(args ...string) error { |
| 55 |
c := &cli{ |
| 56 |
creds: &loginCreds{ |
| 57 |
Password: core.RPCPassword, |
| 58 |
}, |
| 59 |
} |
| 60 |
|
| 61 |
if len(args) > 0 && args[0] != "help" { |
| 62 |
method, exists := c.getMethod(args[0]) |
| 63 |
if !exists { |
| 64 |
fmt.Println("Error: Command not found:", args[0]) |
| 65 |
return c.CmdHelp() |
| 66 |
} |
| 67 |
if len(c.creds.Password) == 0 && core.RPCAskPass { |
| 68 |
fmt.Print("Password: ") |
| 69 |
passwd, err := term.ReadPassword(int(os.Stdin.Fd())) |
| 70 |
fmt.Println("") |
| 71 |
if err != nil { |
| 72 |
return err |
| 73 |
} |
| 74 |
c.creds.Password = string(passwd) |
| 75 |
} |
| 76 |
ret := method.Func.CallSlice([]reflect.Value{ |
| 77 |
reflect.ValueOf(c), |
| 78 |
reflect.ValueOf(args[1:]), |
| 79 |
})[0].Interface() |
| 80 |
if c.rpcconn != nil { |
| 81 |
c.rpcconn.Close() |
| 82 |
} |
| 83 |
if ret == nil { |
| 84 |
return nil |
| 85 |
} |
| 86 |
return ret.(error) |
| 87 |
} |
| 88 |
return c.CmdHelp() |
| 89 |
} |
| 90 |
|
| 91 |
func (c *cli) getMethod(name string) (reflect.Method, bool) { |
| 92 |
methodName := "Cmd" + strings.ToUpper(name[:1]) + strings.ToLower(name[1:]) |
| 93 |
return reflect.TypeOf(c).MethodByName(methodName) |
| 94 |
} |
| 95 |
|
| 96 |
func (c *cli) CmdHelp() error { |
| 97 |
help := fmt.Sprintf("Usage: mirrorbits [OPTIONS] COMMAND [arg...]\n\nA smart download redirector.\n\n") |
| 98 |
help += fmt.Sprintf("Server commands:\n %-10.10s%s\n\n", "daemon", "Start the server") |
| 99 |
help += fmt.Sprintf("CLI commands:\n") |
| 100 |
for _, command := range [][]string{ |
| 101 |
{"add", "Add a new mirror"}, |
| 102 |
{"disable", "Disable a mirror"}, |
| 103 |
{"edit", "Edit a mirror"}, |
| 104 |
{"enable", "Enable a mirror"}, |
| 105 |
{"export", "Export the mirror database"}, |
| 106 |
{"geoupdate", "Update geolocation of a mirror"}, |
| 107 |
{"list", "List all mirrors"}, |
| 108 |
{"logs", "Print logs of a mirror"}, |
| 109 |
{"refresh", "Refresh the local repository"}, |
| 110 |
{"reload", "Reload configuration"}, |
| 111 |
{"remove", "Remove a mirror"}, |
| 112 |
{"scan", "(Re-)Scan a mirror"}, |
| 113 |
{"show", "Print a mirror configuration"}, |
| 114 |
{"stats", "Show download stats"}, |
| 115 |
{"upgrade", "Seamless binary upgrade"}, |
| 116 |
{"version", "Print version information"}, |
| 117 |
} { |
| 118 |
help += fmt.Sprintf(" %-10.10s%s\n", command[0], command[1]) |
| 119 |
} |
| 120 |
fmt.Fprintf(os.Stderr, "%s\n", help) |
| 121 |
return nil |
| 122 |
} |
| 123 |
|
| 124 |
// SubCmd prints the usage of a subcommand |
| 125 |
func SubCmd(name, signature, description string) *flag.FlagSet { |
| 126 |
flags := flag.NewFlagSet(name, flag.ContinueOnError) |
| 127 |
flags.Usage = func() { |
| 128 |
fmt.Fprintf(os.Stderr, "\nUsage: mirrorbits %s %s\n\n%s\n\n", name, signature, description) |
| 129 |
flags.PrintDefaults() |
| 130 |
} |
| 131 |
return flags |
| 132 |
} |
| 133 |
|
| 134 |
type ByDate []*rpc.Mirror |
| 135 |
|
| 136 |
func (d ByDate) Len() int { return len(d) } |
| 137 |
func (d ByDate) Swap(i, j int) { d[i], d[j] = d[j], d[i] } |
| 138 |
func (d ByDate) Less(i, j int) bool { return d[i].StateSince.Seconds > d[j].StateSince.Seconds } |
| 139 |
|
| 140 |
func (c *cli) CmdList(args ...string) error { |
| 141 |
cmd := SubCmd("list", "", "Get the list of mirrors") |
| 142 |
http := cmd.Bool("http", false, "Print HTTP addresses") |
| 143 |
rsync := cmd.Bool("rsync", false, "Print rsync addresses") |
| 144 |
ftp := cmd.Bool("ftp", false, "Print FTP addresses") |
| 145 |
location := cmd.Bool("location", false, "Print the country and continent code") |
| 146 |
state := cmd.Bool("state", true, "Print the state of the mirror") |
| 147 |
score := cmd.Bool("score", false, "Print the score of the mirror") |
| 148 |
disabled := cmd.Bool("disabled", false, "List disabled mirrors only") |
| 149 |
enabled := cmd.Bool("enabled", false, "List enabled mirrors only") |
| 150 |
down := cmd.Bool("down", false, "List only mirrors currently down") |
| 151 |
|
| 152 |
if err := cmd.Parse(args); err != nil { |
| 153 |
return nil |
| 154 |
} |
| 155 |
if cmd.NArg() != 0 { |
| 156 |
cmd.Usage() |
| 157 |
return nil |
| 158 |
} |
| 159 |
|
| 160 |
client := c.GetRPC() |
| 161 |
ctx, cancel := context.WithTimeout(context.Background(), defaultRPCTimeout) |
| 162 |
defer cancel() |
| 163 |
list, err := client.List(ctx, &empty.Empty{}) |
| 164 |
if err != nil { |
| 165 |
log.Fatal("list error:", err) |
| 166 |
} |
| 167 |
|
| 168 |
sort.Sort(ByDate(list.Mirrors)) |
| 169 |
|
| 170 |
w := new(tabwriter.Writer) |
| 171 |
w.Init(os.Stdout, 0, 8, 1, '\t', 0) |
| 172 |
fmt.Fprint(w, "IDENTIFIER") |
| 173 |
if *score == true { |
| 174 |
fmt.Fprint(w, "\tSCORE") |
| 175 |
} |
| 176 |
if *http == true { |
| 177 |
fmt.Fprint(w, "\tHTTP") |
| 178 |
} |
| 179 |
if *rsync == true { |
| 180 |
fmt.Fprint(w, "\tRSYNC") |
| 181 |
} |
| 182 |
if *ftp == true { |
| 183 |
fmt.Fprint(w, "\tFTP") |
| 184 |
} |
| 185 |
if *location == true { |
| 186 |
fmt.Fprint(w, "\tLOCATION") |
| 187 |
} |
| 188 |
if *state == true { |
| 189 |
fmt.Fprint(w, "\tSTATE\tSINCE\tREASON") |
| 190 |
} |
| 191 |
fmt.Fprint(w, "\n") |
| 192 |
|
| 193 |
for _, mirror := range list.Mirrors { |
| 194 |
if *disabled == true { |
| 195 |
if mirror.Enabled == true { |
| 196 |
continue |
| 197 |
} |
| 198 |
} |
| 199 |
if *enabled == true { |
| 200 |
if mirror.Enabled == false { |
| 201 |
continue |
| 202 |
} |
| 203 |
} |
| 204 |
if *down == true { |
| 205 |
if IsUp(mirror) || mirror.Enabled == false { |
| 206 |
continue |
| 207 |
} |
| 208 |
} |
| 209 |
stateSince, err := ptypes.Timestamp(mirror.StateSince) |
| 210 |
if err != nil { |
| 211 |
log.Fatal("list error:", err) |
| 212 |
} |
| 213 |
fmt.Fprintf(w, "%s", mirror.Name) |
| 214 |
if *score == true { |
| 215 |
fmt.Fprintf(w, "\t%d", mirror.Score) |
| 216 |
} |
| 217 |
if *http == true { |
| 218 |
fmt.Fprintf(w, "\t%s", mirror.HttpURL) |
| 219 |
} |
| 220 |
if *rsync == true { |
| 221 |
fmt.Fprintf(w, "\t%s", mirror.RsyncURL) |
| 222 |
} |
| 223 |
if *ftp == true { |
| 224 |
fmt.Fprintf(w, "\t%s", mirror.FtpURL) |
| 225 |
} |
| 226 |
if *location == true { |
| 227 |
countries := strings.Split(mirror.CountryCodes, " ") |
| 228 |
countryCode := "/" |
| 229 |
if len(countries) >= 1 { |
| 230 |
countryCode = countries[0] |
| 231 |
} |
| 232 |
fmt.Fprintf(w, "\t%s (%s)", countryCode, mirror.ContinentCode) |
| 233 |
} |
| 234 |
if *state == true { |
| 235 |
status := "disabled" |
| 236 |
reason := "" |
| 237 |
if mirror.Enabled == true { |
| 238 |
status = StatusString(mirror) |
| 239 |
reason = ReasonString(mirror) |
| 240 |
} |
| 241 |
since := stateSince.Format(time.RFC1123) |
| 242 |
fmt.Fprintf(w, "\t%s\t(%s)\t%s", status, since, reason) |
| 243 |
} |
| 244 |
fmt.Fprint(w, "\n") |
| 245 |
} |
| 246 |
|
| 247 |
w.Flush() |
| 248 |
|
| 249 |
return nil |
| 250 |
} |
| 251 |
|
| 252 |
func IsHTTPOnly(m *rpc.Mirror) bool { |
| 253 |
return strings.HasPrefix(m.HttpURL, "http://") |
| 254 |
} |
| 255 |
|
| 256 |
func IsHTTPSOnly(m *rpc.Mirror) bool { |
| 257 |
return strings.HasPrefix(m.HttpURL, "https://") |
| 258 |
} |
| 259 |
|
| 260 |
func IsUp(m *rpc.Mirror) bool { |
| 261 |
if m.HttpUp == m.HttpsUp { |
| 262 |
return m.HttpUp |
| 263 |
} |
| 264 |
if IsHTTPOnly(m) { |
| 265 |
return m.HttpUp |
| 266 |
} |
| 267 |
if IsHTTPSOnly(m) { |
| 268 |
return m.HttpsUp |
| 269 |
} |
| 270 |
return false |
| 271 |
} |
| 272 |
|
| 273 |
func StatusString(m *rpc.Mirror) string { |
| 274 |
var http string |
| 275 |
var https string |
| 276 |
|
| 277 |
if m.HttpUp { |
| 278 |
http = "up" |
| 279 |
} else { |
| 280 |
http = "down" |
| 281 |
} |
| 282 |
|
| 283 |
if m.HttpsUp { |
| 284 |
https = "up" |
| 285 |
} else { |
| 286 |
https = "down" |
| 287 |
} |
| 288 |
|
| 289 |
if m.HttpUp == m.HttpsUp { |
| 290 |
return http |
| 291 |
} |
| 292 |
if IsHTTPOnly(m) { |
| 293 |
return http |
| 294 |
} |
| 295 |
if IsHTTPSOnly(m) { |
| 296 |
return https |
| 297 |
} |
| 298 |
return fmt.Sprintf("%s/%s", http, https) |
| 299 |
} |
| 300 |
|
| 301 |
func ReasonString(m *rpc.Mirror) string { |
| 302 |
var http string |
| 303 |
var https string |
| 304 |
|
| 305 |
http = m.HttpDownReason |
| 306 |
https = m.HttpsDownReason |
| 307 |
|
| 308 |
if http == https { |
| 309 |
return http |
| 310 |
} |
| 311 |
if IsHTTPOnly(m) { |
| 312 |
return http |
| 313 |
} |
| 314 |
if IsHTTPSOnly(m) { |
| 315 |
return https |
| 316 |
} |
| 317 |
if http == "" { |
| 318 |
return https |
| 319 |
} |
| 320 |
if https == "" { |
| 321 |
return http |
| 322 |
} |
| 323 |
return fmt.Sprintf("%s / %s", http, https) |
| 324 |
} |
| 325 |
|
| 326 |
func (c *cli) CmdAdd(args ...string) error { |
| 327 |
cmd := SubCmd("add", "[OPTIONS] IDENTIFIER", "Add a new mirror") |
| 328 |
http := cmd.String("http", "", "HTTP base URL") |
| 329 |
rsync := cmd.String("rsync", "", "RSYNC base URL (for scanning only)") |
| 330 |
ftp := cmd.String("ftp", "", "FTP base URL (for scanning only)") |
| 331 |
sponsorName := cmd.String("sponsor-name", "", "Name of the sponsor") |
| 332 |
sponsorURL := cmd.String("sponsor-url", "", "URL of the sponsor") |
| 333 |
sponsorLogo := cmd.String("sponsor-logo", "", "URL of a logo to display for this mirror") |
| 334 |
adminName := cmd.String("admin-name", "", "Admin's name") |
| 335 |
adminEmail := cmd.String("admin-email", "", "Admin's email") |
| 336 |
customData := cmd.String("custom-data", "", "Associated data to return when the mirror is selected (i.e. json document)") |
| 337 |
continentOnly := cmd.Bool("continent-only", false, "The mirror should only handle its continent") |
| 338 |
countryOnly := cmd.Bool("country-only", false, "The mirror should only handle its country") |
| 339 |
asOnly := cmd.Bool("as-only", false, "The mirror should only handle clients in the same AS number") |
| 340 |
score := cmd.Int("score", 0, "Weight to give to the mirror during selection") |
| 341 |
comment := cmd.String("comment", "", "Comment") |
| 342 |
|
| 343 |
if err := cmd.Parse(args); err != nil { |
| 344 |
return nil |
| 345 |
} |
| 346 |
if cmd.NArg() < 1 { |
| 347 |
cmd.Usage() |
| 348 |
return nil |
| 349 |
} |
| 350 |
|
| 351 |
if strings.Contains(cmd.Arg(0), " ") { |
| 352 |
fmt.Fprintf(os.Stderr, "The identifier cannot contain a space\n") |
| 353 |
os.Exit(-1) |
| 354 |
} |
| 355 |
|
| 356 |
if *http == "" { |
| 357 |
fmt.Fprintf(os.Stderr, "You *must* pass at least an HTTP URL\n") |
| 358 |
os.Exit(-1) |
| 359 |
} |
| 360 |
|
| 361 |
if utils.HasAnyPrefix(*http, "http://", "https://") { |
| 362 |
_, err := url.Parse(*http) |
| 363 |
if err != nil { |
| 364 |
fmt.Fprintf(os.Stderr, "Can't parse HTTP URL\n") |
| 365 |
os.Exit(-1) |
| 366 |
} |
| 367 |
} else if strings.Contains(*http, "://") { |
| 368 |
fmt.Fprintf(os.Stderr, "The HTTP URL has an invalid scheme\n") |
| 369 |
os.Exit(-1) |
| 370 |
} else { |
| 371 |
// No scheme, yes we do accept it. |
| 372 |
// Note that the documentation of net/url mentions that parsing |
| 373 |
// such URL is "invalid but may not necessarily return an error", |
| 374 |
// so let's add a scheme before we parse it. |
| 375 |
_, err := url.Parse("http://" + *http) |
| 376 |
if err != nil { |
| 377 |
fmt.Fprintf(os.Stderr, "Can't parse HTTP URL\n") |
| 378 |
os.Exit(-1) |
| 379 |
} |
| 380 |
} |
| 381 |
|
| 382 |
mirror := &mirrors.Mirror{ |
| 383 |
Name: cmd.Arg(0), |
| 384 |
HttpURL: *http, |
| 385 |
RsyncURL: *rsync, |
| 386 |
FtpURL: *ftp, |
| 387 |
SponsorName: *sponsorName, |
| 388 |
SponsorURL: *sponsorURL, |
| 389 |
SponsorLogoURL: *sponsorLogo, |
| 390 |
AdminName: *adminName, |
| 391 |
AdminEmail: *adminEmail, |
| 392 |
CustomData: *customData, |
| 393 |
ContinentOnly: *continentOnly, |
| 394 |
CountryOnly: *countryOnly, |
| 395 |
ASOnly: *asOnly, |
| 396 |
Score: *score, |
| 397 |
Comment: *comment, |
| 398 |
} |
| 399 |
|
| 400 |
client := c.GetRPC() |
| 401 |
ctx, cancel := context.WithTimeout(context.Background(), defaultRPCTimeout) |
| 402 |
defer cancel() |
| 403 |
m, err := rpc.MirrorToRPC(mirror) |
| 404 |
if err != nil { |
| 405 |
log.Fatal("edit error:", err) |
| 406 |
} |
| 407 |
reply, err := client.AddMirror(ctx, m) |
| 408 |
if err != nil { |
| 409 |
if err.Error() == rpc.ErrNameAlreadyTaken.Error() { |
| 410 |
log.Fatalf("Mirror %s already exists!\n", mirror.Name) |
| 411 |
} |
| 412 |
log.Fatal("edit error:", err) |
| 413 |
} |
| 414 |
|
| 415 |
for i := 0; i < len(reply.Warnings); i++ { |
| 416 |
fmt.Println(reply.Warnings[i]) |
| 417 |
if i == len(reply.Warnings)-1 { |
| 418 |
fmt.Println("") |
| 419 |
} |
| 420 |
} |
| 421 |
|
| 422 |
if reply.Country != "" { |
| 423 |
fmt.Println("Mirror location:") |
| 424 |
fmt.Printf("Latitude: %.4f\n", reply.Latitude) |
| 425 |
fmt.Printf("Longitude: %.4f\n", reply.Longitude) |
| 426 |
fmt.Printf("Continent: %s\n", reply.Continent) |
| 427 |
fmt.Printf("Country: %s\n", reply.Country) |
| 428 |
fmt.Printf("ASN: %s\n", reply.ASN) |
| 429 |
fmt.Println("") |
| 430 |
} |
| 431 |
|
| 432 |
fmt.Printf("Mirror '%s' added successfully\n", mirror.Name) |
| 433 |
fmt.Printf("Enable this mirror using\n $ mirrorbits enable %s\n", mirror.Name) |
| 434 |
|
| 435 |
return nil |
| 436 |
} |
| 437 |
|
| 438 |
func (c *cli) CmdRemove(args ...string) error { |
| 439 |
cmd := SubCmd("remove", "IDENTIFIER", "Remove an existing mirror") |
| 440 |
force := cmd.Bool("f", false, "Never prompt for confirmation") |
| 441 |
|
| 442 |
if err := cmd.Parse(args); err != nil { |
| 443 |
return nil |
| 444 |
} |
| 445 |
if cmd.NArg() != 1 { |
| 446 |
cmd.Usage() |
| 447 |
return nil |
| 448 |
} |
| 449 |
|
| 450 |
id, name := c.matchMirror(cmd.Arg(0)) |
| 451 |
|
| 452 |
if *force == false { |
| 453 |
fmt.Printf("Removing %s, are you sure? [y/N]", name) |
| 454 |
reader := bufio.NewReader(os.Stdin) |
| 455 |
s, _ := reader.ReadString('\n') |
| 456 |
switch s[0] { |
| 457 |
case 'y', 'Y': |
| 458 |
break |
| 459 |
default: |
| 460 |
return nil |
| 461 |
} |
| 462 |
} |
| 463 |
|
| 464 |
client := c.GetRPC() |
| 465 |
// Use a timeout longer than the default, removing a mirror can take time |
| 466 |
ctx, cancel := context.WithTimeout(context.Background(), time.Second * 60) |
| 467 |
defer cancel() |
| 468 |
_, err := client.RemoveMirror(ctx, &rpc.MirrorIDRequest{ |
| 469 |
ID: int32(id), |
| 470 |
}) |
| 471 |
if err != nil { |
| 472 |
log.Fatal("remove error:", err) |
| 473 |
} |
| 474 |
|
| 475 |
fmt.Printf("Mirror '%s' removed successfully\n", name) |
| 476 |
return nil |
| 477 |
} |
| 478 |
|
| 479 |
func (c *cli) CmdScan(args ...string) error { |
| 480 |
cmd := SubCmd("scan", "[IDENTIFIER]", "(Re-)Scan a mirror") |
| 481 |
enable := cmd.Bool("enable", false, "Enable the mirror automatically if the scan is successful") |
| 482 |
all := cmd.Bool("all", false, "Scan all mirrors at once") |
| 483 |
ftp := cmd.Bool("ftp", false, "Force a scan using FTP") |
| 484 |
rsync := cmd.Bool("rsync", false, "Force a scan using rsync") |
| 485 |
timeout := cmd.Uint("timeout", 0, "Timeout in seconds") |
| 486 |
|
| 487 |
if err := cmd.Parse(args); err != nil { |
| 488 |
return nil |
| 489 |
} |
| 490 |
if !*all && cmd.NArg() != 1 || *all && cmd.NArg() != 0 { |
| 491 |
cmd.Usage() |
| 492 |
return nil |
| 493 |
} |
| 494 |
|
| 495 |
client := c.GetRPC() |
| 496 |
ctx, cancel := context.WithCancel(context.Background()) |
| 497 |
defer cancel() |
| 498 |
|
| 499 |
list := make(map[int]string) |
| 500 |
|
| 501 |
// Get the list of mirrors to scan |
| 502 |
if *all == true { |
| 503 |
reply, err := client.MatchMirror(ctx, &rpc.MatchRequest{ |
| 504 |
Pattern: "", // Match all of them |
| 505 |
}) |
| 506 |
if err != nil { |
| 507 |
return errors.New("Cannot fetch the list of mirrors") |
| 508 |
} |
| 509 |
|
| 510 |
for _, m := range reply.Mirrors { |
| 511 |
list[int(m.ID)] = m.Name |
| 512 |
} |
| 513 |
} else { |
| 514 |
// Single mirror |
| 515 |
id, name := c.matchMirror(cmd.Arg(0)) |
| 516 |
list[id] = name |
| 517 |
} |
| 518 |
|
| 519 |
// Set the method of the scan (if not default) |
| 520 |
var method rpc.ScanMirrorRequest_Method |
| 521 |
if *ftp == false && *rsync == false { |
| 522 |
method = rpc.ScanMirrorRequest_ALL |
| 523 |
} else if *rsync == true { |
| 524 |
method = rpc.ScanMirrorRequest_RSYNC |
| 525 |
} else if *ftp == true { |
| 526 |
method = rpc.ScanMirrorRequest_FTP |
| 527 |
} |
| 528 |
|
| 529 |
for id, name := range list { |
| 530 |
|
| 531 |
if *timeout > 0 { |
| 532 |
ctx, cancel = context.WithTimeout(context.Background(), time.Duration(*timeout)*time.Second) |
| 533 |
defer cancel() |
| 534 |
} |
| 535 |
|
| 536 |
fmt.Printf("Scanning %s... ", name) |
| 537 |
|
| 538 |
reply, err := client.ScanMirror(ctx, &rpc.ScanMirrorRequest{ |
| 539 |
ID: int32(id), |
| 540 |
AutoEnable: *enable, |
| 541 |
Protocol: method, |
| 542 |
}) |
| 543 |
if err != nil { |
| 544 |
s := status.Convert(err) |
| 545 |
if s.Code() == codes.FailedPrecondition || len(list) == 1 { |
| 546 |
return errors.New("\nscan error: " + grpc.ErrorDesc(err)) |
| 547 |
} |
| 548 |
fmt.Println("scan error:", grpc.ErrorDesc(err)) |
| 549 |
continue |
| 550 |
} else { |
| 551 |
fmt.Printf("%d files indexed, %d known and %d removed\n", reply.FilesIndexed, reply.KnownIndexed, reply.Removed) |
| 552 |
if reply.GetTZOffsetMs() != 0 { |
| 553 |
fmt.Printf(" ∟ Timezone offset detected and corrected: %d milliseconds\n", reply.TZOffsetMs) |
| 554 |
} |
| 555 |
if reply.Enabled { |
| 556 |
fmt.Println(" ∟ Enabled") |
| 557 |
} |
| 558 |
} |
| 559 |
} |
| 560 |
|
| 561 |
return nil |
| 562 |
} |
| 563 |
|
| 564 |
func (c *cli) CmdRefresh(args ...string) error { |
| 565 |
cmd := SubCmd("refresh", "", "Scan the local repository") |
| 566 |
rehash := cmd.Bool("rehash", false, "Force a rehash of the files") |
| 567 |
|
| 568 |
if err := cmd.Parse(args); err != nil { |
| 569 |
return nil |
| 570 |
} |
| 571 |
if cmd.NArg() != 0 { |
| 572 |
cmd.Usage() |
| 573 |
return nil |
| 574 |
} |
| 575 |
|
| 576 |
fmt.Print("Refreshing the local repository... ") |
| 577 |
|
| 578 |
client := c.GetRPC() |
| 579 |
ctx, cancel := context.WithCancel(context.Background()) |
| 580 |
defer cancel() |
| 581 |
_, err := client.RefreshRepository(ctx, &rpc.RefreshRepositoryRequest{ |
| 582 |
Rehash: *rehash, |
| 583 |
}) |
| 584 |
if err != nil { |
| 585 |
fmt.Println("") |
| 586 |
log.Fatal(err) |
| 587 |
} |
| 588 |
|
| 589 |
fmt.Println("done") |
| 590 |
|
| 591 |
return nil |
| 592 |
} |
| 593 |
|
| 594 |
func (c *cli) matchMirror(pattern string) (id int, name string) { |
| 595 |
if len(pattern) == 0 { |
| 596 |
return -1, "" |
| 597 |
} |
| 598 |
|
| 599 |
client := c.GetRPC() |
| 600 |
ctx, cancel := context.WithTimeout(context.Background(), defaultRPCTimeout) |
| 601 |
defer cancel() |
| 602 |
reply, err := client.MatchMirror(ctx, &rpc.MatchRequest{ |
| 603 |
Pattern: pattern, |
| 604 |
}) |
| 605 |
if err != nil { |
| 606 |
fmt.Fprintf(os.Stderr, "mirror matching: %s\n", err) |
| 607 |
os.Exit(1) |
| 608 |
} |
| 609 |
|
| 610 |
switch len(reply.Mirrors) { |
| 611 |
case 0: |
| 612 |
fmt.Fprintf(os.Stderr, "No match for '%s'\n", pattern) |
| 613 |
os.Exit(1) |
| 614 |
case 1: |
| 615 |
id, name, err := GetSingle(reply.Mirrors) |
| 616 |
if err != nil { |
| 617 |
log.Fatal("unexpected error:", err) |
| 618 |
} |
| 619 |
return id, name |
| 620 |
default: |
| 621 |
fmt.Fprintln(os.Stderr, "Multiple match:") |
| 622 |
for _, mirror := range reply.Mirrors { |
| 623 |
fmt.Fprintf(os.Stderr, " %s\n", mirror.Name) |
| 624 |
} |
| 625 |
os.Exit(1) |
| 626 |
} |
| 627 |
return |
| 628 |
} |
| 629 |
|
| 630 |
func GetSingle(list []*rpc.MirrorID) (int, string, error) { |
| 631 |
if len(list) == 0 { |
| 632 |
return -1, "", errors.New("list is empty") |
| 633 |
} else if len(list) > 1 { |
| 634 |
return -1, "", errors.New("too many results") |
| 635 |
} |
| 636 |
return int(list[0].ID), list[0].Name, nil |
| 637 |
} |
| 638 |
|
| 639 |
func (c *cli) CmdEdit(args ...string) error { |
| 640 |
cmd := SubCmd("edit", "[IDENTIFIER]", "Edit a mirror") |
| 641 |
|
| 642 |
if err := cmd.Parse(args); err != nil { |
| 643 |
return nil |
| 644 |
} |
| 645 |
if cmd.NArg() != 1 { |
| 646 |
cmd.Usage() |
| 647 |
return nil |
| 648 |
} |
| 649 |
|
| 650 |
// Find the editor to use |
| 651 |
editor := os.Getenv("EDITOR") |
| 652 |
if editor == "" { |
| 653 |
for _, p := range []string{"editor", "vi", "emacs", "nano"} { |
| 654 |
_, err := exec.LookPath(p) |
| 655 |
if err == nil { |
| 656 |
editor = p |
| 657 |
break |
| 658 |
} |
| 659 |
} |
| 660 |
if editor == "" { |
| 661 |
log.Fatal("No text editor found, please set the EDITOR environment variable") |
| 662 |
} |
| 663 |
} |
| 664 |
|
| 665 |
id, _ := c.matchMirror(cmd.Arg(0)) |
| 666 |
|
| 667 |
client := c.GetRPC() |
| 668 |
ctx, cancel := context.WithTimeout(context.Background(), defaultRPCTimeout) |
| 669 |
defer cancel() |
| 670 |
rpcm, err := client.MirrorInfo(ctx, &rpc.MirrorIDRequest{ |
| 671 |
ID: int32(id), |
| 672 |
}) |
| 673 |
if err != nil { |
| 674 |
log.Fatal("edit error:", err) |
| 675 |
} |
| 676 |
mirror, err := rpc.MirrorFromRPC(rpcm) |
| 677 |
if err != nil { |
| 678 |
log.Fatal("edit error:", err) |
| 679 |
} |
| 680 |
|
| 681 |
// Generate a yaml configuration string from the struct |
| 682 |
out, err := yaml.Marshal(mirror) |
| 683 |
|
| 684 |
// Open a temporary file |
| 685 |
f, err := os.CreateTemp("", "edit") |
| 686 |
if err != nil { |
| 687 |
log.Fatal("Cannot create temporary file:", err) |
| 688 |
} |
| 689 |
defer os.Remove(f.Name()) |
| 690 |
f.WriteString("# You can now edit this mirror configuration.\n" + |
| 691 |
"# Just save and quit when you're done.\n\n") |
| 692 |
f.WriteString(string(out)) |
| 693 |
f.WriteString(fmt.Sprintf("\n%s\n\n%s\n", commentSeparator, mirror.Comment)) |
| 694 |
f.Close() |
| 695 |
|
| 696 |
// Checksum the original file |
| 697 |
chk, _ := filesystem.Sha256sum(f.Name()) |
| 698 |
|
| 699 |
reopen: |
| 700 |
// Launch the editor with the filename as first parameter |
| 701 |
exe := exec.Command(editor, f.Name()) |
| 702 |
exe.Stdin = os.Stdin |
| 703 |
exe.Stdout = os.Stdout |
| 704 |
exe.Stderr = os.Stderr |
| 705 |
|
| 706 |
err = exe.Run() |
| 707 |
if err != nil { |
| 708 |
log.Fatal(err) |
| 709 |
} |
| 710 |
|
| 711 |
// Read the file back |
| 712 |
out, err = os.ReadFile(f.Name()) |
| 713 |
if err != nil { |
| 714 |
log.Fatal("Cannot read file", f.Name()) |
| 715 |
} |
| 716 |
|
| 717 |
// Checksum the file back and compare |
| 718 |
chk2, _ := filesystem.Sha256sum(f.Name()) |
| 719 |
if bytes.Compare(chk, chk2) == 0 { |
| 720 |
fmt.Println("Aborted - settings are unmodified, so there is nothing to change.") |
| 721 |
return nil |
| 722 |
} |
| 723 |
|
| 724 |
var comment string |
| 725 |
yamlstr := string(out) |
| 726 |
|
| 727 |
commentIndex := strings.Index(yamlstr, commentSeparator) |
| 728 |
if commentIndex > 0 { |
| 729 |
comment = strings.TrimSpace(yamlstr[commentIndex+len(commentSeparator):]) |
| 730 |
yamlstr = yamlstr[:commentIndex] |
| 731 |
} |
| 732 |
|
| 733 |
reopen := func(err error) bool { |
| 734 |
eagain: |
| 735 |
fmt.Printf("%s\nRetry? [Y/n]", err.Error()) |
| 736 |
reader := bufio.NewReader(os.Stdin) |
| 737 |
s, _ := reader.ReadString('\n') |
| 738 |
switch s[0] { |
| 739 |
case 'y', 'Y', 10: |
| 740 |
return true |
| 741 |
case 'n', 'N': |
| 742 |
fmt.Println("Aborted") |
| 743 |
return false |
| 744 |
default: |
| 745 |
goto eagain |
| 746 |
} |
| 747 |
} |
| 748 |
|
| 749 |
// Fill the struct from the yaml |
| 750 |
err = yaml.Unmarshal([]byte(yamlstr), &mirror) |
| 751 |
if err != nil { |
| 752 |
switch reopen(err) { |
| 753 |
case true: |
| 754 |
goto reopen |
| 755 |
case false: |
| 756 |
return nil |
| 757 |
} |
| 758 |
} |
| 759 |
|
| 760 |
mirror.Comment = comment |
| 761 |
|
| 762 |
ctx, cancel = context.WithTimeout(context.Background(), defaultRPCTimeout) |
| 763 |
defer cancel() |
| 764 |
m, err := rpc.MirrorToRPC(mirror) |
| 765 |
if err != nil { |
| 766 |
log.Fatal("edit error:", err) |
| 767 |
} |
| 768 |
reply, err := client.UpdateMirror(ctx, m) |
| 769 |
if err != nil { |
| 770 |
if err.Error() == rpc.ErrNameAlreadyTaken.Error() { |
| 771 |
switch reopen(errors.New("Name already taken")) { |
| 772 |
case true: |
| 773 |
goto reopen |
| 774 |
case false: |
| 775 |
return nil |
| 776 |
} |
| 777 |
} |
| 778 |
log.Fatal("edit error:", err) |
| 779 |
} |
| 780 |
|
| 781 |
if len(reply.Diff) > 0 { |
| 782 |
fmt.Println(reply.Diff) |
| 783 |
} |
| 784 |
|
| 785 |
fmt.Printf("Mirror '%s' edited successfully\n", mirror.Name) |
| 786 |
|
| 787 |
return nil |
| 788 |
} |
| 789 |
|
| 790 |
func (c *cli) CmdGeoupdate(args ...string) error { |
| 791 |
cmd := SubCmd("geoupdate", "[IDENTIFIER]", "Update geolocation of a mirror") |
| 792 |
force := cmd.Bool("f", false, "Never prompt for confirmation") |
| 793 |
|
| 794 |
if err := cmd.Parse(args); err != nil { |
| 795 |
return nil |
| 796 |
} |
| 797 |
if cmd.NArg() != 1 { |
| 798 |
cmd.Usage() |
| 799 |
return nil |
| 800 |
} |
| 801 |
|
| 802 |
id, name := c.matchMirror(cmd.Arg(0)) |
| 803 |
|
| 804 |
// Get mirror with geolocation updated |
| 805 |
client := c.GetRPC() |
| 806 |
ctx, cancel := context.WithTimeout(context.Background(), defaultRPCTimeout) |
| 807 |
defer cancel() |
| 808 |
reply, err := client.GeoUpdateMirror(ctx, &rpc.MirrorIDRequest{ |
| 809 |
ID: int32(id), |
| 810 |
}) |
| 811 |
if err != nil { |
| 812 |
log.Fatal("edit error:", err) |
| 813 |
} |
| 814 |
|
| 815 |
// Print warnings if any |
| 816 |
for i := 0; i < len(reply.Warnings); i++ { |
| 817 |
fmt.Println(reply.Warnings[i]) |
| 818 |
if i == len(reply.Warnings)-1 { |
| 819 |
fmt.Println("") |
| 820 |
} |
| 821 |
} |
| 822 |
|
| 823 |
// Print diff if any |
| 824 |
if len(reply.Diff) > 0 { |
| 825 |
fmt.Println(reply.Diff) |
| 826 |
} else { |
| 827 |
fmt.Println("Geolocation is up to date, there is nothing to change.") |
| 828 |
return nil |
| 829 |
} |
| 830 |
|
| 831 |
// Ask for confirmation |
| 832 |
if *force == false { |
| 833 |
fmt.Printf("Update mirror %s? [y/N]", name) |
| 834 |
reader := bufio.NewReader(os.Stdin) |
| 835 |
s, _ := reader.ReadString('\n') |
| 836 |
switch s[0] { |
| 837 |
case 'y', 'Y': |
| 838 |
break |
| 839 |
default: |
| 840 |
return nil |
| 841 |
} |
| 842 |
} |
| 843 |
|
| 844 |
// Update the mirror |
| 845 |
ctx, cancel = context.WithTimeout(context.Background(), defaultRPCTimeout) |
| 846 |
defer cancel() |
| 847 |
reply2, err := client.UpdateMirror(ctx, reply.Mirror) |
| 848 |
if err != nil { |
| 849 |
log.Fatal("edit error:", err) |
| 850 |
} |
| 851 |
|
| 852 |
// The diff shouldn't have changed, but let's check |
| 853 |
if reply2.Diff != reply.Diff { |
| 854 |
fmt.Println("Unexpected diff, see below:") |
| 855 |
fmt.Println(reply2.Diff) |
| 856 |
} |
| 857 |
|
| 858 |
fmt.Printf("Mirror '%s' updated successfully\n", name) |
| 859 |
|
| 860 |
return nil |
| 861 |
} |
| 862 |
|
| 863 |
func (c *cli) CmdShow(args ...string) error { |
| 864 |
cmd := SubCmd("show", "[IDENTIFIER]", "Print a mirror configuration") |
| 865 |
|
| 866 |
if err := cmd.Parse(args); err != nil { |
| 867 |
return nil |
| 868 |
} |
| 869 |
if cmd.NArg() != 1 { |
| 870 |
cmd.Usage() |
| 871 |
return nil |
| 872 |
} |
| 873 |
|
| 874 |
id, _ := c.matchMirror(cmd.Arg(0)) |
| 875 |
|
| 876 |
client := c.GetRPC() |
| 877 |
ctx, cancel := context.WithTimeout(context.Background(), defaultRPCTimeout) |
| 878 |
defer cancel() |
| 879 |
rpcm, err := client.MirrorInfo(ctx, &rpc.MirrorIDRequest{ |
| 880 |
ID: int32(id), |
| 881 |
}) |
| 882 |
if err != nil { |
| 883 |
log.Fatal("edit error:", err) |
| 884 |
} |
| 885 |
mirror, err := rpc.MirrorFromRPC(rpcm) |
| 886 |
if err != nil { |
| 887 |
log.Fatal("edit error:", err) |
| 888 |
} |
| 889 |
|
| 890 |
// Generate a yaml configuration string from the struct |
| 891 |
out, err := yaml.Marshal(mirror) |
| 892 |
if err != nil { |
| 893 |
log.Fatal("show error:", err) |
| 894 |
} |
| 895 |
|
| 896 |
fmt.Printf("%s\nComment:\n%s\n", out, mirror.Comment) |
| 897 |
return nil |
| 898 |
} |
| 899 |
|
| 900 |
func (c *cli) CmdExport(args ...string) error { |
| 901 |
cmd := SubCmd("export", "[format]", "Export the mirror database.\n\nAvailable formats: mirmon") |
| 902 |
rsync := cmd.Bool("rsync", true, "Export rsync URLs") |
| 903 |
http := cmd.Bool("http", true, "Export http URLs") |
| 904 |
ftp := cmd.Bool("ftp", true, "Export ftp URLs") |
| 905 |
disabled := cmd.Bool("disabled", true, "Export disabled mirrors") |
| 906 |
|
| 907 |
if err := cmd.Parse(args); err != nil { |
| 908 |
return nil |
| 909 |
} |
| 910 |
if cmd.NArg() != 1 { |
| 911 |
cmd.Usage() |
| 912 |
return nil |
| 913 |
} |
| 914 |
|
| 915 |
if cmd.Arg(0) != "mirmon" { |
| 916 |
fmt.Fprintf(os.Stderr, "Unsupported format\n") |
| 917 |
cmd.Usage() |
| 918 |
return nil |
| 919 |
} |
| 920 |
|
| 921 |
client := c.GetRPC() |
| 922 |
ctx, cancel := context.WithTimeout(context.Background(), defaultRPCTimeout) |
| 923 |
defer cancel() |
| 924 |
list, err := client.List(ctx, &empty.Empty{}) |
| 925 |
if err != nil { |
| 926 |
log.Fatal("export error:", err) |
| 927 |
} |
| 928 |
|
| 929 |
w := new(tabwriter.Writer) |
| 930 |
w.Init(os.Stdout, 0, 8, 1, '\t', 0) |
| 931 |
|
| 932 |
for _, m := range list.Mirrors { |
| 933 |
if *disabled == false { |
| 934 |
if m.Enabled == false { |
| 935 |
continue |
| 936 |
} |
| 937 |
} |
| 938 |
ccodes := strings.Fields(m.CountryCodes) |
| 939 |
|
| 940 |
urls := make([]string, 0, 3) |
| 941 |
if *rsync == true && m.RsyncURL != "" { |
| 942 |
urls = append(urls, m.RsyncURL) |
| 943 |
} |
| 944 |
if *http == true && m.HttpURL != "" { |
| 945 |
if utils.HasAnyPrefix(m.HttpURL, "http://", "https://") { |
| 946 |
urls = append(urls, m.HttpURL) |
| 947 |
} else { |
| 948 |
urls = append(urls, "http://" + m.HttpURL) |
| 949 |
urls = append(urls, "https://" + m.HttpURL) |
| 950 |
} |
| 951 |
} |
| 952 |
if *ftp == true && m.FtpURL != "" { |
| 953 |
urls = append(urls, m.FtpURL) |
| 954 |
} |
| 955 |
|
| 956 |
for _, u := range urls { |
| 957 |
fmt.Fprintf(w, "%s\t%s\t%s\n", ccodes[0], u, m.AdminEmail) |
| 958 |
} |
| 959 |
} |
| 960 |
|
| 961 |
w.Flush() |
| 962 |
return nil |
| 963 |
} |
| 964 |
|
| 965 |
func (c *cli) CmdEnable(args ...string) error { |
| 966 |
cmd := SubCmd("enable", "[IDENTIFIER]", "Enable a mirror") |
| 967 |
|
| 968 |
if err := cmd.Parse(args); err != nil { |
| 969 |
return nil |
| 970 |
} |
| 971 |
if cmd.NArg() != 1 { |
| 972 |
cmd.Usage() |
| 973 |
return nil |
| 974 |
} |
| 975 |
|
| 976 |
c.changeStatus(cmd.Arg(0), true) |
| 977 |
return nil |
| 978 |
} |
| 979 |
|
| 980 |
func (c *cli) CmdDisable(args ...string) error { |
| 981 |
cmd := SubCmd("disable", "[IDENTIFIER]", "Disable a mirror") |
| 982 |
|
| 983 |
if err := cmd.Parse(args); err != nil { |
| 984 |
return nil |
| 985 |
} |
| 986 |
if cmd.NArg() != 1 { |
| 987 |
cmd.Usage() |
| 988 |
return nil |
| 989 |
} |
| 990 |
|
| 991 |
c.changeStatus(cmd.Arg(0), false) |
| 992 |
return nil |
| 993 |
} |
| 994 |
|
| 995 |
func (c *cli) changeStatus(pattern string, enabled bool) { |
| 996 |
id, name := c.matchMirror(pattern) |
| 997 |
|
| 998 |
client := c.GetRPC() |
| 999 |
ctx, cancel := context.WithTimeout(context.Background(), defaultRPCTimeout) |
| 1000 |
defer cancel() |
| 1001 |
_, err := client.ChangeStatus(ctx, &rpc.ChangeStatusRequest{ |
| 1002 |
ID: int32(id), |
| 1003 |
Enabled: enabled, |
| 1004 |
}) |
| 1005 |
if err != nil { |
| 1006 |
if enabled { |
| 1007 |
log.Fatalf("Couldn't enable mirror '%s': %s\n", name, err) |
| 1008 |
} else { |
| 1009 |
log.Fatalf("Couldn't disable mirror '%s': %s\n", name, err) |
| 1010 |
} |
| 1011 |
} |
| 1012 |
|
| 1013 |
if enabled { |
| 1014 |
fmt.Printf("Mirror '%s' enabled successfully\n", name) |
| 1015 |
} else { |
| 1016 |
fmt.Printf("Mirror '%s' disabled successfully\n", name) |
| 1017 |
} |
| 1018 |
return |
| 1019 |
} |
| 1020 |
|
| 1021 |
func (c *cli) CmdStats(args ...string) error { |
| 1022 |
cmd := SubCmd("stats", "[OPTIONS] [mirror|file] [IDENTIFIER|PATTERN]", "Show download stats for a particular mirror or a file pattern") |
| 1023 |
dateStart := cmd.String("start-date", "", "Starting date (format YYYY-MM-DD)") |
| 1024 |
dateEnd := cmd.String("end-date", "", "Ending date (format YYYY-MM-DD)") |
| 1025 |
human := cmd.Bool("h", true, "Human readable version") |
| 1026 |
|
| 1027 |
if err := cmd.Parse(args); err != nil { |
| 1028 |
return nil |
| 1029 |
} |
| 1030 |
if cmd.NArg() != 2 || (cmd.Arg(0) != "mirror" && cmd.Arg(0) != "file") { |
| 1031 |
cmd.Usage() |
| 1032 |
return nil |
| 1033 |
} |
| 1034 |
|
| 1035 |
start, err := time.Parse("2006-1-2", *dateStart) |
| 1036 |
if err != nil { |
| 1037 |
start = time.Now() |
| 1038 |
} |
| 1039 |
startproto, _ := ptypes.TimestampProto(start) |
| 1040 |
|
| 1041 |
end, err := time.Parse("2006-1-2", *dateEnd) |
| 1042 |
if err != nil { |
| 1043 |
end = time.Now() |
| 1044 |
} |
| 1045 |
endproto, _ := ptypes.TimestampProto(end) |
| 1046 |
|
| 1047 |
client := c.GetRPC() |
| 1048 |
ctx, cancel := context.WithTimeout(context.Background(), defaultRPCTimeout) |
| 1049 |
defer cancel() |
| 1050 |
|
| 1051 |
if cmd.Arg(0) == "file" { |
| 1052 |
// File stats |
| 1053 |
|
| 1054 |
reply, err := client.StatsFile(ctx, &rpc.StatsFileRequest{ |
| 1055 |
Pattern: cmd.Arg(1), |
| 1056 |
DateStart: startproto, |
| 1057 |
DateEnd: endproto, |
| 1058 |
}) |
| 1059 |
if err != nil { |
| 1060 |
log.Fatal("file stats error:", err) |
| 1061 |
} |
| 1062 |
|
| 1063 |
// Format the results |
| 1064 |
w := new(tabwriter.Writer) |
| 1065 |
w.Init(os.Stdout, 0, 8, 1, '\t', 0) |
| 1066 |
|
| 1067 |
// Sort keys and count requests |
| 1068 |
var keys []string |
| 1069 |
var requests int64 |
| 1070 |
for k, req := range reply.Files { |
| 1071 |
requests += req |
| 1072 |
keys = append(keys, k) |
| 1073 |
} |
| 1074 |
sort.Strings(keys) |
| 1075 |
|
| 1076 |
for _, k := range keys { |
| 1077 |
fmt.Fprintf(w, "%s:\t%d\n", k, reply.Files[k]) |
| 1078 |
} |
| 1079 |
|
| 1080 |
if len(keys) > 0 { |
| 1081 |
// Add a line separator |
| 1082 |
fmt.Fprintf(w, "\t\n") |
| 1083 |
} |
| 1084 |
|
| 1085 |
fmt.Fprintf(w, "Total download requests:\t%d\n", requests) |
| 1086 |
w.Flush() |
| 1087 |
} else if cmd.Arg(0) == "mirror" { |
| 1088 |
// Mirror stats |
| 1089 |
|
| 1090 |
id, name := c.matchMirror(cmd.Arg(1)) |
| 1091 |
|
| 1092 |
reply, err := client.StatsMirror(ctx, &rpc.StatsMirrorRequest{ |
| 1093 |
ID: int32(id), |
| 1094 |
DateStart: startproto, |
| 1095 |
DateEnd: endproto, |
| 1096 |
}) |
| 1097 |
if err != nil { |
| 1098 |
log.Fatal("mirror stats error:", err) |
| 1099 |
} |
| 1100 |
|
| 1101 |
// Format the results |
| 1102 |
w := new(tabwriter.Writer) |
| 1103 |
w.Init(os.Stdout, 0, 8, 1, '\t', 0) |
| 1104 |
|
| 1105 |
fmt.Fprintf(w, "Identifier:\t%s\n", name) |
| 1106 |
if !reply.Mirror.Enabled { |
| 1107 |
fmt.Fprintf(w, "Status:\tdisabled\n") |
| 1108 |
} else { |
| 1109 |
fmt.Fprintf(w, "Status:\t%s\n", StatusString(reply.Mirror)) |
| 1110 |
} |
| 1111 |
fmt.Fprintf(w, "Download requests:\t%d\n", reply.Requests) |
| 1112 |
fmt.Fprint(w, "Bytes transferred:\t") |
| 1113 |
if *human { |
| 1114 |
fmt.Fprintln(w, utils.ReadableSize(reply.Bytes)) |
| 1115 |
} else { |
| 1116 |
fmt.Fprintln(w, reply.Bytes) |
| 1117 |
} |
| 1118 |
w.Flush() |
| 1119 |
} |
| 1120 |
|
| 1121 |
return nil |
| 1122 |
} |
| 1123 |
|
| 1124 |
func (c *cli) CmdLogs(args ...string) error { |
| 1125 |
cmd := SubCmd("logs", "[IDENTIFIER]", "Print logs of a mirror") |
| 1126 |
maxResults := cmd.Uint("l", 500, "Maximum number of logs to return") |
| 1127 |
|
| 1128 |
if err := cmd.Parse(args); err != nil { |
| 1129 |
return nil |
| 1130 |
} |
| 1131 |
if cmd.NArg() != 1 { |
| 1132 |
cmd.Usage() |
| 1133 |
return nil |
| 1134 |
} |
| 1135 |
|
| 1136 |
id, name := c.matchMirror(cmd.Arg(0)) |
| 1137 |
|
| 1138 |
client := c.GetRPC() |
| 1139 |
ctx, cancel := context.WithTimeout(context.Background(), defaultRPCTimeout) |
| 1140 |
defer cancel() |
| 1141 |
resp, err := client.GetMirrorLogs(ctx, &rpc.GetMirrorLogsRequest{ |
| 1142 |
ID: int32(id), |
| 1143 |
MaxResults: int32(*maxResults), |
| 1144 |
}) |
| 1145 |
if err != nil { |
| 1146 |
log.Fatal("logs error:", err) |
| 1147 |
} |
| 1148 |
|
| 1149 |
if len(resp.Line) == 0 { |
| 1150 |
fmt.Printf("No logs for %s\n", name) |
| 1151 |
return nil |
| 1152 |
} |
| 1153 |
|
| 1154 |
fmt.Printf("Printing logs for %s:\n", name) |
| 1155 |
|
| 1156 |
for _, l := range resp.Line { |
| 1157 |
fmt.Println(l) |
| 1158 |
} |
| 1159 |
|
| 1160 |
return nil |
| 1161 |
} |
| 1162 |
|
| 1163 |
func (c *cli) CmdReload(args ...string) error { |
| 1164 |
cmd := SubCmd("reload", "", "Reload configuration") |
| 1165 |
|
| 1166 |
if err := cmd.Parse(args); err != nil { |
| 1167 |
return nil |
| 1168 |
} |
| 1169 |
if cmd.NArg() != 0 { |
| 1170 |
cmd.Usage() |
| 1171 |
return nil |
| 1172 |
} |
| 1173 |
|
| 1174 |
client := c.GetRPC() |
| 1175 |
ctx, cancel := context.WithTimeout(context.Background(), defaultRPCTimeout) |
| 1176 |
defer cancel() |
| 1177 |
_, err := client.Reload(ctx, &empty.Empty{}) |
| 1178 |
if err != nil { |
| 1179 |
log.Fatal("reload error:", err) |
| 1180 |
} |
| 1181 |
|
| 1182 |
return nil |
| 1183 |
} |
| 1184 |
|
| 1185 |
func (c *cli) CmdUpgrade(args ...string) error { |
| 1186 |
cmd := SubCmd("upgrade", "", "Seamless binary upgrade") |
| 1187 |
|
| 1188 |
if err := cmd.Parse(args); err != nil { |
| 1189 |
return nil |
| 1190 |
} |
| 1191 |
if cmd.NArg() != 0 { |
| 1192 |
cmd.Usage() |
| 1193 |
return nil |
| 1194 |
} |
| 1195 |
|
| 1196 |
client := c.GetRPC() |
| 1197 |
ctx, cancel := context.WithTimeout(context.Background(), defaultRPCTimeout) |
| 1198 |
defer cancel() |
| 1199 |
_, err := client.Upgrade(ctx, &empty.Empty{}) |
| 1200 |
if err != nil { |
| 1201 |
log.Fatal("upgrade error:", err) |
| 1202 |
} |
| 1203 |
|
| 1204 |
return nil |
| 1205 |
} |
| 1206 |
|
| 1207 |
func (c *cli) CmdVersion(args ...string) error { |
| 1208 |
cmd := SubCmd("version", "", "Print version information") |
| 1209 |
|
| 1210 |
if err := cmd.Parse(args); err != nil { |
| 1211 |
return nil |
| 1212 |
} |
| 1213 |
if cmd.NArg() != 0 { |
| 1214 |
cmd.Usage() |
| 1215 |
return nil |
| 1216 |
} |
| 1217 |
|
| 1218 |
fmt.Printf("Client:\n") |
| 1219 |
core.PrintVersion(core.GetVersionInfo()) |
| 1220 |
fmt.Println() |
| 1221 |
|
| 1222 |
client := c.GetRPC() |
| 1223 |
ctx, cancel := context.WithTimeout(context.Background(), defaultRPCTimeout) |
| 1224 |
defer cancel() |
| 1225 |
reply, err := client.GetVersion(ctx, &empty.Empty{}) |
| 1226 |
if err != nil { |
| 1227 |
s := status.Convert(err) |
| 1228 |
return fmt.Errorf("version error: %w", s.Err()) |
| 1229 |
} |
| 1230 |
|
| 1231 |
if reply.Version != "" { |
| 1232 |
fmt.Printf("Server:\n") |
| 1233 |
core.PrintVersion(core.VersionInfo{ |
| 1234 |
Version: reply.Version, |
| 1235 |
Build: reply.Build, |
| 1236 |
GoVersion: reply.GoVersion, |
| 1237 |
OS: reply.OS, |
| 1238 |
Arch: reply.Arch, |
| 1239 |
GoMaxProcs: int(reply.GoMaxProcs), |
| 1240 |
}) |
| 1241 |
} |
| 1242 |
return nil |
| 1243 |
} |