|
| 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 | + |
| 28 | + "github.com/spf13/cobra" |
| 29 | +) |
| 30 | + |
| 31 | +var ( |
| 32 | + cmdRemove = &cobra.Command{ |
| 33 | + Use: "remove", |
| 34 | + Short: "Remove something", |
| 35 | + Run: cmdShowUsage, |
| 36 | + } |
| 37 | + cmdRemoveStarter = &cobra.Command{ |
| 38 | + Use: "starter", |
| 39 | + Short: "Remove a starter from the cluster", |
| 40 | + Run: cmdRemoveStarterRun, |
| 41 | + } |
| 42 | + removeStarterOptions struct { |
| 43 | + starterEndpoint string |
| 44 | + starterID string |
| 45 | + force bool |
| 46 | + } |
| 47 | +) |
| 48 | + |
| 49 | +func init() { |
| 50 | + f := cmdRemoveStarter.Flags() |
| 51 | + f.StringVar(&removeStarterOptions.starterEndpoint, "starter.endpoint", "", "The endpoint of the starter to connect to. E.g. http://localhost:8528") |
| 52 | + f.StringVar(&removeStarterOptions.starterID, "starter.id", "", "The ID of the starter to remove") |
| 53 | + f.BoolVar(&removeStarterOptions.force, "force", false, "If set to true, the starter will be removed even if the servers cannot be properly shutdown") |
| 54 | + |
| 55 | + cmdMain.AddCommand(cmdRemove) |
| 56 | + cmdRemove.AddCommand(cmdRemoveStarter) |
| 57 | +} |
| 58 | + |
| 59 | +func cmdRemoveStarterRun(cmd *cobra.Command, args []string) { |
| 60 | + // Setup logging |
| 61 | + consoleOnly := true |
| 62 | + configureLogging(consoleOnly) |
| 63 | + |
| 64 | + // Create starter client |
| 65 | + c := mustCreateStarterClient(removeStarterOptions.starterEndpoint) |
| 66 | + |
| 67 | + // Fetch the ID of the starter for which the endpoint is given |
| 68 | + ctx := context.Background() |
| 69 | + info, err := c.ID(ctx) |
| 70 | + if err != nil { |
| 71 | + log.Fatal().Err(err).Msg("Failed to fetch ID from starter") |
| 72 | + } |
| 73 | + |
| 74 | + // Compare ID with requested. |
| 75 | + if removeStarterOptions.starterID == "" || removeStarterOptions.starterID == info.ID { |
| 76 | + // Shutdown (with goodbye) the starter at given endpoint |
| 77 | + goodbye := true |
| 78 | + if err := c.Shutdown(ctx, goodbye); err != nil { |
| 79 | + log.Fatal().Err(err).Msg("Removing starter from cluster failed") |
| 80 | + } else { |
| 81 | + log.Info().Msg("Starter has been shutdown and removed from cluster") |
| 82 | + } |
| 83 | + } else { |
| 84 | + // Remove another starter from the cluster |
| 85 | + if err := c.RemovePeer(ctx, removeStarterOptions.starterID, removeStarterOptions.force); err != nil { |
| 86 | + log.Fatal().Err(err).Msg("Removing starter from cluster failed") |
| 87 | + } else { |
| 88 | + log.Info().Msg("Starter has been removed from cluster") |
| 89 | + } |
| 90 | + } |
| 91 | +} |
0 commit comments