Skip to content

Commit f69eadf

Browse files
committed
Added arangodb upgrade command
1 parent 25b49de commit f69eadf

File tree

4 files changed

+139
-5
lines changed

4 files changed

+139
-5
lines changed

main.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,8 @@ func cmdShowVersionRun(cmd *cobra.Command, args []string) {
462462

463463
func cmdMainRun(cmd *cobra.Command, args []string) {
464464
// Setup log level
465-
configureLogging()
465+
consoleOnly := false
466+
configureLogging(consoleOnly)
466467

467468
log.Info().Msgf("Starting %s version %s, build %s", projectName, projectVersion, projectBuild)
468469

@@ -495,12 +496,12 @@ func cmdMainRun(cmd *cobra.Command, args []string) {
495496
}
496497

497498
// configureLogging configures the log object according to command line arguments.
498-
func configureLogging() {
499+
func configureLogging(consoleOnly bool) {
499500
logOpts := logging.LoggerOutputOptions{
500501
Stderr: logOutput.Console,
501502
Color: logOutput.Color,
502503
}
503-
if logOutput.File {
504+
if logOutput.File && !consoleOnly {
504505
if logDir != "" {
505506
logOpts.LogFile = filepath.Join(logDir, logFileName)
506507
} else {

start.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ func init() {
5656

5757
func cmdStartRun(cmd *cobra.Command, args []string) {
5858
// Setup logging
59-
configureLogging()
59+
consoleOnly := true
60+
configureLogging(consoleOnly)
6061

6162
log.Info().Msgf("Starting %s version %s, build %s in the background", projectName, projectVersion, projectBuild)
6263

stop.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ func init() {
4646

4747
func cmdStopRun(cmd *cobra.Command, args []string) {
4848
// Setup logging
49-
configureLogging()
49+
consoleOnly := true
50+
configureLogging(consoleOnly)
5051

5152
// Create starter client
5253
scheme := "http"

upgrade.go

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
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

Comments
 (0)