|
| 1 | +// |
| 2 | +// DISCLAIMER |
| 3 | +// |
| 4 | +// Copyright 2018 ArangoDB GmbH, Cologne, Germany |
| 5 | +// |
| 6 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +// you may not use this file except in compliance with the License. |
| 8 | +// You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, software |
| 13 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +// See the License for the specific language governing permissions and |
| 16 | +// limitations under the License. |
| 17 | +// |
| 18 | +// Copyright holder is ArangoDB GmbH, Cologne, Germany |
| 19 | +// |
| 20 | +// Author Ewout Prangsma |
| 21 | +// |
| 22 | + |
| 23 | +package main |
| 24 | + |
| 25 | +import ( |
| 26 | + "context" |
| 27 | + "fmt" |
| 28 | + "net/url" |
| 29 | + "sort" |
| 30 | + "strings" |
| 31 | + "time" |
| 32 | + |
| 33 | + "github.com/spf13/cobra" |
| 34 | + |
| 35 | + "github.com/arangodb-helper/arangodb/client" |
| 36 | +) |
| 37 | + |
| 38 | +var ( |
| 39 | + cmdUpgrade = &cobra.Command{ |
| 40 | + Use: "upgrade", |
| 41 | + Short: "Start the ArangoDB starter in the background", |
| 42 | + Run: cmdUpgradeRun, |
| 43 | + } |
| 44 | + upgradeOptions struct { |
| 45 | + starterEndpoint string |
| 46 | + } |
| 47 | +) |
| 48 | + |
| 49 | +func init() { |
| 50 | + f := cmdUpgrade.Flags() |
| 51 | + f.StringVar(&upgradeOptions.starterEndpoint, "starter.endpoint", "", "The endpoint of the starter to connect to. E.g. http://localhost:8528") |
| 52 | + |
| 53 | + cmdMain.AddCommand(cmdUpgrade) |
| 54 | +} |
| 55 | + |
| 56 | +func cmdUpgradeRun(cmd *cobra.Command, args []string) { |
| 57 | + // Setup logging |
| 58 | + consoleOnly := true |
| 59 | + configureLogging(consoleOnly) |
| 60 | + |
| 61 | + // Check options |
| 62 | + if upgradeOptions.starterEndpoint == "" { |
| 63 | + log.Fatal().Msg("--starter.endpoint must be set") |
| 64 | + } |
| 65 | + ep, err := url.Parse(upgradeOptions.starterEndpoint) |
| 66 | + if err != nil { |
| 67 | + log.Fatal().Err(err).Msg("--starter.endpoint is invalid") |
| 68 | + } |
| 69 | + |
| 70 | + // Create starter client |
| 71 | + c, err := client.NewArangoStarterClient(*ep) |
| 72 | + if err != nil { |
| 73 | + log.Fatal().Err(err).Msg("Failed to create Starter client") |
| 74 | + } |
| 75 | + ctx := context.Background() |
| 76 | + force := false |
| 77 | + if err := c.StartDatabaseUpgrade(ctx, force); err != nil { |
| 78 | + log.Fatal().Err(err).Msg("Failed to starter database automatic upgrade") |
| 79 | + } |
| 80 | + log.Info().Msg("Database automatic upgrade has been started") |
| 81 | + |
| 82 | + // Wait for the upgrade to finish |
| 83 | + remaining := "" |
| 84 | + finished := "" |
| 85 | + for { |
| 86 | + status, err := c.UpgradeStatus(ctx) |
| 87 | + if err != nil { |
| 88 | + log.Error().Err(err).Msg("Failed to fetch upgrade status") |
| 89 | + } |
| 90 | + if status.Failed { |
| 91 | + log.Error().Str("reason", status.Reason).Msg("Database upgrade has failed") |
| 92 | + return |
| 93 | + } |
| 94 | + if status.Ready { |
| 95 | + log.Info().Msg("Database upgrade has finished") |
| 96 | + return |
| 97 | + } |
| 98 | + r, f := formatServerStatusList(status.ServersRemaining), formatServerStatusList(status.ServersUpgraded) |
| 99 | + if remaining != r || finished != f { |
| 100 | + remaining, finished = r, f |
| 101 | + log.Info().Msgf("Servers upgraded: %s, remaining servers: %s", finished, remaining) |
| 102 | + } |
| 103 | + time.Sleep(time.Second) |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +// formatServerStatusList formats the given server status list in a human readable format. |
| 108 | +func formatServerStatusList(list []client.UpgradeStatusServer) string { |
| 109 | + counts := make(map[client.ServerType]int) |
| 110 | + for _, e := range list { |
| 111 | + counts[e.Type] = counts[e.Type] + 1 |
| 112 | + } |
| 113 | + if len(counts) == 0 { |
| 114 | + return "none" |
| 115 | + } |
| 116 | + strList := make([]string, 0, len(counts)) |
| 117 | + for t, c := range counts { |
| 118 | + name := string(t) |
| 119 | + if c > 1 { |
| 120 | + name = name + "s" |
| 121 | + } |
| 122 | + strList = append(strList, fmt.Sprintf("%d %s", c, name)) |
| 123 | + } |
| 124 | + sort.Slice(strList, func(i, j int) bool { |
| 125 | + // Sort past the number |
| 126 | + a := strings.Split(strList[i], " ")[1] |
| 127 | + b := strings.Split(strList[j], " ")[1] |
| 128 | + return a < b |
| 129 | + }) |
| 130 | + return strings.Join(strList, ", ") |
| 131 | +} |
0 commit comments