/
server.go
747 lines (678 loc) · 22.4 KB
/
server.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
//
// DISCLAIMER
//
// Copyright 2017-2024 ArangoDB GmbH, Cologne, Germany
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Copyright holder is ArangoDB GmbH, Cologne, Germany
//
package service
import (
"context"
"crypto/tls"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"net/url"
"os"
"strconv"
"github.com/rs/zerolog"
driver "github.com/arangodb/go-driver"
"github.com/arangodb-helper/arangodb/client"
"github.com/arangodb-helper/arangodb/pkg/definitions"
"github.com/arangodb-helper/arangodb/pkg/logging"
)
var (
httpClient = client.DefaultHTTPClient()
)
const (
contentTypeJSON = "application/json"
)
// HelloRequest is the data structure send of the wire in a `/hello` POST request.
type HelloRequest struct {
SlaveID string // Unique ID of the slave
SlaveAddress string // IP address used to reach the slave (if empty, this will be derived from the request)
SlavePort int // Port used to reach the slave
DataDir string // Directory used for data by this slave
IsSecure bool // If set, servers started by this peer are using an SSL connection
Agent *bool `json:",omitempty"` // If not nil, sets if server gets an agent or not. If nil, default handling applies
DBServer *bool `json:",omitempty"` // If not nil, sets if server gets an dbserver or not. If nil, default handling applies
Coordinator *bool `json:",omitempty"` // If not nil, sets if server gets an coordinator or not. If nil, default handling applies
}
type httpServer struct {
//config Config
log zerolog.Logger
server *http.Server
context httpServerContext
versionInfo client.VersionInfo
idInfo client.IDInfo
runtimeServerManager *runtimeServerManager
masterPort int
}
// httpServerContext provides a context for the httpServer.
type httpServerContext interface {
ClientBuilder
// ClusterConfig returns the current cluster configuration and the current peer
ClusterConfig() (ClusterConfig, *Peer, ServiceMode)
// IsRunningMaster returns if the starter is the running master.
IsRunningMaster() (isRunningMaster, isRunning bool, masterURL string)
// serverHostLogFile returns the path of the logfile (in host namespace) to which the given server will write its logs.
serverHostLogFile(serverType definitions.ServerType) (string, error)
// sendMasterLeaveCluster informs the master that we're leaving for good.
// The master will remove the database servers from the cluster and update
// the cluster configuration.
sendMasterLeaveCluster() error
// Stop the peer
Stop()
// UpgradeManager returns the database upgrade manager
UpgradeManager() UpgradeManager
// HandleHello - handle a hello request.
// If req==nil, this is a GET request, otherwise it is a POST request.
HandleHello(ownAddress, remoteAddress string, req *HelloRequest, isUpdateRequest bool) (ClusterConfig, error)
// HandleGoodbye removes the database servers started by the peer with given id
// from the cluster and alters the cluster configuration, removing the peer.
HandleGoodbye(id string, force bool) (peerRemoved bool, err error)
// DatabaseVersion returns the version of the `arangod` binary that is being
// used by this starter.
DatabaseVersion(context.Context) (driver.Version, bool, error)
GetLocalFolder() string
DatabaseFeatures() DatabaseFeatures
serverHostDir(serverType definitions.ServerType) (string, error)
}
// newHTTPServer initializes and an HTTP server.
func newHTTPServer(log zerolog.Logger, context httpServerContext, runtimeServerManager *runtimeServerManager, config Config, serverID string) *httpServer {
// Create HTTP server
return &httpServer{
log: log,
context: context,
server: &http.Server{
ErrorLog: logging.NewAdapter(log.With().Str("logger", "http-server").Logger()),
},
idInfo: client.IDInfo{
ID: serverID,
},
versionInfo: client.VersionInfo{
Version: config.ProjectVersion,
Build: config.ProjectBuild,
},
runtimeServerManager: runtimeServerManager,
masterPort: config.MasterPort,
}
}
// Start listening for requests.
// This method will return directly after starting.
func (s *httpServer) Start(hostAddr, containerAddr string, tlsConfig *tls.Config) {
go func() {
if err := s.Run(hostAddr, containerAddr, tlsConfig, false); err != nil {
s.log.Error().Err(err).Msgf("Failed to listen on %s", containerAddr)
}
}()
}
// Run listening for requests.
// This method will return after the server has been closed.
func (s *httpServer) Run(hostAddr, containerAddr string, tlsConfig *tls.Config, idOnly bool) error {
mux := http.NewServeMux()
if !idOnly {
// Starter to starter API
mux.HandleFunc("/hello", s.helloHandler)
mux.HandleFunc("/goodbye", s.goodbyeHandler)
}
// External API
mux.HandleFunc("/id", s.idHandler)
if !idOnly {
mux.HandleFunc("/local/inventory", s.localInventory)
mux.HandleFunc("/cluster/inventory", s.clusterInventory)
mux.HandleFunc("/process", s.processListHandler)
mux.HandleFunc("/endpoints", s.endpointsHandler)
mux.HandleFunc("/logs/agent", s.agentLogsHandler)
mux.HandleFunc("/logs/dbserver", s.dbserverLogsHandler)
mux.HandleFunc("/logs/coordinator", s.coordinatorLogsHandler)
mux.HandleFunc("/logs/single", s.singleLogsHandler)
mux.HandleFunc("/version", s.versionHandler)
mux.HandleFunc("/database-version", s.databaseVersionHandler)
mux.HandleFunc("/shutdown", s.shutdownHandler)
mux.HandleFunc("/database-auto-upgrade", s.databaseAutoUpgradeHandler)
// JWT Rotation
s.registerJWTFunctions(mux)
}
s.server.Addr = containerAddr
s.server.Handler = mux
if tlsConfig != nil {
s.log.Info().Msgf("ArangoDB Starter listening on %s (%s) using TLS", containerAddr, hostAddr)
s.server.TLSConfig = tlsConfig
if err := s.server.ListenAndServeTLS("", ""); err != nil && err != http.ErrServerClosed {
return maskAny(err)
}
} else {
s.log.Info().Msgf("ArangoDB Starter listening on %s (%s)", containerAddr, hostAddr)
if err := s.server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
return maskAny(err)
}
}
return nil
}
// Close the server
func (s *httpServer) Close() error {
if err := s.server.Close(); err != nil {
return maskAny(err)
}
return nil
}
// HTTP service function:
func (s *httpServer) helloHandler(w http.ResponseWriter, r *http.Request) {
s.log.Debug().Msgf("Received %s /hello request from %s", r.Method, r.RemoteAddr)
// Derive own address
host, _, err := net.SplitHostPort(r.Host)
if err != nil {
writeError(w, http.StatusBadRequest, fmt.Sprintf("Cannot derive own host address: %v", err))
return
}
ownAddress := normalizeHostName(host)
isUpdateRequest, _ := strconv.ParseBool(r.FormValue("update"))
var result ClusterConfig
if r.Method == "GET" {
// Let service handle get request
result, err = s.context.HandleHello(ownAddress, r.RemoteAddr, nil, isUpdateRequest)
if err != nil {
handleError(w, err)
return
}
} else if r.Method == "POST" {
// Read request
var req HelloRequest
defer r.Body.Close()
if body, err := ioutil.ReadAll(r.Body); err != nil {
writeError(w, http.StatusBadRequest, fmt.Sprintf("Cannot read request body: %v", err.Error()))
return
} else if err := json.Unmarshal(body, &req); err != nil {
writeError(w, http.StatusBadRequest, fmt.Sprintf("Cannot parse request body: %v", err.Error()))
return
}
// Let service handle post request
result, err = s.context.HandleHello(ownAddress, r.RemoteAddr, &req, false)
if err != nil {
handleError(w, err)
return
}
} else {
// Invalid method
writeError(w, http.StatusMethodNotAllowed, "GET or POST required")
return
}
// Send result
b, err := json.Marshal(result)
if err != nil {
writeError(w, http.StatusInternalServerError, err.Error())
} else {
w.Write(b)
}
}
// goodbyeHandler handles a `/goodbye` request that removes a peer from the list of peers.
func (s *httpServer) goodbyeHandler(w http.ResponseWriter, r *http.Request) {
// Check method
if r.Method != "POST" {
writeError(w, http.StatusMethodNotAllowed, "POST required")
return
}
// Parse request
force, _ := strconv.ParseBool(r.FormValue("force"))
var req client.GoodbyeRequest
defer r.Body.Close()
body, err := ioutil.ReadAll(r.Body)
if err != nil {
writeError(w, http.StatusBadRequest, fmt.Sprintf("Cannot read request body: %v", err.Error()))
return
}
if err := json.Unmarshal(body, &req); err != nil {
writeError(w, http.StatusBadRequest, fmt.Sprintf("Cannot parse request body: %v", err.Error()))
return
}
// Check request
if req.SlaveID == "" {
writeError(w, http.StatusBadRequest, "SlaveID must be set.")
return
}
// Check state
ctx := r.Context()
isRunningMaster, isRunning, masterURL := s.context.IsRunningMaster()
if !isRunning {
// Must be running first
writeError(w, http.StatusServiceUnavailable, "Starter is not in running phase")
} else if !isRunningMaster {
// Redirect to master
if masterURL != "" {
// Forward the request to the leader.
c, err := createMasterClient(masterURL)
if err != nil {
handleError(w, err)
} else {
if err := c.RemovePeer(ctx, req.SlaveID, force); err != nil {
s.log.Debug().Err(err).Msg("Forwarding RemovePeer failed")
handleError(w, err)
} else {
w.WriteHeader(http.StatusOK)
w.Write([]byte("OK"))
}
}
} else {
writeError(w, http.StatusServiceUnavailable, "No runtime master known")
}
} else {
// Remove the peer
s.log.Info().Bool("force", force).Msgf("Goodbye requested for peer %s", req.SlaveID)
if removed, err := s.context.HandleGoodbye(req.SlaveID, force); err != nil {
// Failure
handleError(w, err)
} else if !removed {
// ID not found
writeError(w, http.StatusNotFound, "Unknown ID")
} else {
// Peer removed
w.WriteHeader(http.StatusOK)
w.Write([]byte("BYE"))
}
}
}
// idHandler returns a JSON object containing the ID of this starter.
func (s *httpServer) idHandler(w http.ResponseWriter, r *http.Request) {
data, err := json.Marshal(s.idInfo)
if err != nil {
s.log.Error().Err(err).Msg("Failed to marshal ID response")
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
} else {
w.WriteHeader(http.StatusOK)
w.Write(data)
}
}
// processListHandler returns process information of all launched servers.
func (s *httpServer) processListHandler(w http.ResponseWriter, r *http.Request) {
clusterConfig, myPeer, mode := s.context.ClusterConfig()
isSecure := clusterConfig.IsSecure()
// Gather processes
resp := client.ProcessList{}
expectedServers := 0
if myPeer != nil {
portOffset := myPeer.PortOffset
ip := myPeer.Address
if myPeer.HasAgent() {
expectedServers++
}
if myPeer.HasDBServer() {
expectedServers++
}
if myPeer.HasCoordinator() {
expectedServers++
}
createServerProcess := func(serverType definitions.ServerType, p Process) client.ServerProcess {
return client.ServerProcess{
Type: client.ServerType(serverType),
IP: ip,
Port: s.masterPort + portOffset + serverType.PortOffset(),
ProcessID: p.ProcessID(),
ContainerID: p.ContainerID(),
ContainerIP: p.ContainerIP(),
IsSecure: isSecure,
}
}
if w := s.runtimeServerManager.agentProc; w != nil {
if p := w.Process(); p != nil {
resp.Servers = append(resp.Servers, createServerProcess(definitions.ServerTypeAgent, p))
}
}
if w := s.runtimeServerManager.coordinatorProc; w != nil {
if p := w.Process(); p != nil {
resp.Servers = append(resp.Servers, createServerProcess(definitions.ServerTypeCoordinator, p))
}
}
if w := s.runtimeServerManager.dbserverProc; w != nil {
if p := w.Process(); p != nil {
resp.Servers = append(resp.Servers, createServerProcess(definitions.ServerTypeDBServer, p))
}
}
if w := s.runtimeServerManager.singleProc; w != nil {
if p := w.Process(); p != nil {
resp.Servers = append(resp.Servers, createServerProcess(definitions.ServerTypeSingle, p))
}
}
}
if mode.IsSingleMode() {
expectedServers = 1
}
resp.ServersStarted = len(resp.Servers) == expectedServers
b, err := json.Marshal(resp)
if err != nil {
writeError(w, http.StatusInternalServerError, err.Error())
} else {
w.Write(b)
}
}
func urlListToStringSlice(list []url.URL) []string {
result := make([]string, len(list))
for i, u := range list {
result[i] = u.String()
}
return result
}
// endpointsHandler returns the URL's needed to reach all starters, agents & coordinators in the cluster.
func (s *httpServer) endpointsHandler(w http.ResponseWriter, r *http.Request) {
// IsRunningMaster returns if the starter is the running master.
isRunningMaster, isRunning, masterURL := s.context.IsRunningMaster()
// Check state
if isRunning && !isRunningMaster {
// Redirect to master
if masterURL != "" {
location, err := getURLWithPath(masterURL, "/endpoints")
if err != nil {
handleError(w, err)
} else {
handleError(w, RedirectError{Location: location})
}
} else {
writeError(w, http.StatusServiceUnavailable, "No runtime master known")
}
} else {
// Gather & send endpoints list
clusterConfig, _, _ := s.context.ClusterConfig()
// Gather endpoints
resp := client.EndpointList{}
if endpoints, err := clusterConfig.GetPeerEndpoints(); err != nil {
writeError(w, http.StatusInternalServerError, err.Error())
return
} else {
resp.Starters = endpoints
}
if isRunning {
if endpoints, err := clusterConfig.GetAgentEndpoints(); err != nil {
writeError(w, http.StatusInternalServerError, err.Error())
return
} else {
resp.Agents = endpoints
}
if endpoints, err := clusterConfig.GetCoordinatorEndpoints(); err != nil {
writeError(w, http.StatusInternalServerError, err.Error())
return
} else {
resp.Coordinators = endpoints
}
}
b, err := json.Marshal(resp)
if err != nil {
writeError(w, http.StatusInternalServerError, err.Error())
} else {
w.Write(b)
}
}
}
// agentLogsHandler serves the entire agent log (if any).
// If there is no agent running a 404 is returned.
func (s *httpServer) agentLogsHandler(w http.ResponseWriter, r *http.Request) {
_, myPeer, _ := s.context.ClusterConfig()
if myPeer != nil && myPeer.HasAgent() {
s.logsHandler(w, r, definitions.ServerTypeAgent)
} else {
w.WriteHeader(http.StatusNotFound)
}
}
// dbserverLogsHandler serves the entire dbserver log.
func (s *httpServer) dbserverLogsHandler(w http.ResponseWriter, r *http.Request) {
_, myPeer, _ := s.context.ClusterConfig()
if myPeer != nil && myPeer.HasDBServer() {
s.logsHandler(w, r, definitions.ServerTypeDBServer)
} else {
w.WriteHeader(http.StatusNotFound)
}
}
// coordinatorLogsHandler serves the entire coordinator log.
func (s *httpServer) coordinatorLogsHandler(w http.ResponseWriter, r *http.Request) {
_, myPeer, _ := s.context.ClusterConfig()
if myPeer != nil && myPeer.HasCoordinator() {
s.logsHandler(w, r, definitions.ServerTypeCoordinator)
} else {
w.WriteHeader(http.StatusNotFound)
}
}
// singleLogsHandler serves the entire single server log.
func (s *httpServer) singleLogsHandler(w http.ResponseWriter, r *http.Request) {
s.logsHandler(w, r, definitions.ServerTypeSingle)
}
func (s *httpServer) logsHandler(w http.ResponseWriter, r *http.Request, serverType definitions.ServerType) {
// Find log path
logPath, err := s.context.serverHostLogFile(serverType)
if err != nil {
// Not ready yet
w.WriteHeader(http.StatusServiceUnavailable)
return
}
s.log.Debug().Msgf("Fetching logs in %s", logPath)
rd, err := os.Open(logPath)
if os.IsNotExist(err) {
// Log file not there (yet), we allow this
w.WriteHeader(http.StatusOK)
} else if err != nil {
s.log.Error().Err(err).Msgf("Failed to open log file '%s'", logPath)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
} else {
// Log open
defer rd.Close()
w.WriteHeader(http.StatusOK)
io.Copy(w, rd)
}
}
// versionHandler returns a JSON object containing the current version & build number.
func (s *httpServer) versionHandler(w http.ResponseWriter, r *http.Request) {
data, err := json.Marshal(s.versionInfo)
if err != nil {
s.log.Error().Err(err).Msg("Failed to marshal version response")
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
} else {
w.WriteHeader(http.StatusOK)
w.Write(data)
}
}
// databaseVersionHandler returns a JSON object containing the current arangod version.
func (s *httpServer) databaseVersionHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
version, _, err := s.context.DatabaseVersion(r.Context())
if err != nil {
handleError(w, err)
} else {
data, err := json.Marshal(client.DatabaseVersionResponse{
Version: version,
})
if err != nil {
s.log.Error().Err(err).Msg("Failed to marshal datbase-version response")
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
} else {
w.WriteHeader(http.StatusOK)
w.Write(data)
}
}
}
// shutdownHandler initiates a shutdown of this process and all servers started by it.
func (s *httpServer) shutdownHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
if r.FormValue("mode") == "goodbye" {
// Inform the master we're leaving for good
if err := s.context.sendMasterLeaveCluster(); err != nil {
s.log.Error().Err(err).Msg("Failed to send master goodbye")
handleError(w, err)
return
}
}
// Stop my services
s.context.Stop()
w.WriteHeader(http.StatusOK)
w.Write([]byte("OK"))
}
// databaseAutoUpgradeHandler initiates an upgrade of the database version.
func (s *httpServer) databaseAutoUpgradeHandler(w http.ResponseWriter, r *http.Request) {
// IsRunningMaster returns if the starter is the running master.
isRunningMaster, isRunning, masterURL := s.context.IsRunningMaster()
_, _, mode := s.context.ClusterConfig()
if !isRunning {
// We must have reached the running state before we can handle this kind of request
s.log.Debug().Msg("Received /database-auto-upgrade request while not in running phase")
writeError(w, http.StatusBadRequest, "Must be in running state to do upgrades")
return
}
ctx := r.Context()
switch r.Method {
case "POST":
forceMinorUpgrade, _ := strconv.ParseBool(r.URL.Query().Get("forceMinorUpgrade"))
// Start the upgrade process
if isRunningMaster || mode.IsSingleMode() {
// We're the starter leader, process the request
if err := s.context.UpgradeManager().StartDatabaseUpgrade(ctx, forceMinorUpgrade); err != nil {
handleError(w, err)
} else {
w.WriteHeader(http.StatusOK)
w.Write([]byte("OK"))
}
} else {
// We're not the starter leader.
// Forward the request to the leader.
c, err := createMasterClient(masterURL)
if err != nil {
handleError(w, err)
} else {
if err := c.StartDatabaseUpgrade(ctx, forceMinorUpgrade); err != nil {
s.log.Debug().Err(err).Msg("Forwarding StartDatabaseUpgrade failed")
handleError(w, err)
} else {
w.WriteHeader(http.StatusOK)
w.Write([]byte("OK"))
}
}
}
case "PUT":
// Retry the upgrade process
if !isRunningMaster {
// We're not the starter leader.
// Forward the request to the leader.
c, err := createMasterClient(masterURL)
if err != nil {
handleError(w, err)
} else {
if err := c.RetryDatabaseUpgrade(ctx); err != nil {
s.log.Debug().Err(err).Msg("Forwarding RetryDatabaseUpgrade failed")
handleError(w, err)
} else {
w.WriteHeader(http.StatusOK)
w.Write([]byte("OK"))
}
}
} else {
// We're the starter leader, process the request
if err := s.context.UpgradeManager().RetryDatabaseUpgrade(ctx); err != nil {
handleError(w, err)
} else {
w.WriteHeader(http.StatusOK)
w.Write([]byte("OK"))
}
}
case "DELETE":
// Abort the upgrade process
if !isRunningMaster {
// We're not the starter leader.
// Forward the request to the leader.
c, err := createMasterClient(masterURL)
if err != nil {
handleError(w, err)
} else {
if err := c.AbortDatabaseUpgrade(ctx); err != nil {
s.log.Debug().Err(err).Msg("Forwarding AbortDatabaseUpgrade failed")
handleError(w, err)
} else {
w.WriteHeader(http.StatusOK)
w.Write([]byte("OK"))
}
}
} else {
// We're the starter leader, process the request
if err := s.context.UpgradeManager().AbortDatabaseUpgrade(ctx); err != nil {
handleError(w, err)
} else {
w.WriteHeader(http.StatusOK)
w.Write([]byte("OK"))
}
}
case "GET":
if status, err := s.context.UpgradeManager().Status(ctx); err != nil {
handleError(w, err)
} else {
b, err := json.Marshal(status)
if err != nil {
writeError(w, http.StatusInternalServerError, err.Error())
} else {
w.Write(b)
}
}
default:
w.WriteHeader(http.StatusMethodNotAllowed)
}
}
func handleError(w http.ResponseWriter, err error) {
if loc, ok := IsRedirect(err); ok {
header := w.Header()
header.Add("Location", loc)
w.WriteHeader(http.StatusTemporaryRedirect)
} else if client.IsBadRequest(err) {
writeError(w, http.StatusBadRequest, err.Error())
} else if client.IsPreconditionFailed(err) {
writeError(w, http.StatusPreconditionFailed, err.Error())
} else if client.IsServiceUnavailable(err) {
writeError(w, http.StatusServiceUnavailable, err.Error())
} else if st, ok := client.IsStatusError(err); ok {
writeError(w, st, err.Error())
} else {
writeError(w, http.StatusInternalServerError, err.Error())
}
}
func writeError(w http.ResponseWriter, status int, message string) {
if message == "" {
message = "Unknown error"
}
resp := client.ErrorResponse{Error: message}
b, _ := json.Marshal(resp)
w.Header().Set("Content-Type", contentTypeJSON)
w.WriteHeader(status)
w.Write(b)
}
func createMasterClient(masterURL string) (client.API, error) {
if masterURL == "" {
return nil, maskAny(fmt.Errorf("Starter master is not known"))
}
ep, err := url.Parse(masterURL)
if err != nil {
return nil, maskAny(err)
}
c, err := client.NewArangoStarterClient(*ep)
if err != nil {
return nil, maskAny(err)
}
return c, nil
}