-
Notifications
You must be signed in to change notification settings - Fork 448
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #383 from sgotti/stolonctl_removekeeper
stolonctl: add removekeeper command
- Loading branch information
Showing
3 changed files
with
204 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// Copyright 2017 Sorint.lab | ||
// | ||
// 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. | ||
|
||
package main | ||
|
||
import ( | ||
"github.com/sorintlab/stolon/pkg/cluster" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var removeKeeperCmd = &cobra.Command{ | ||
Use: "removekeeper [keeper uid]", | ||
Short: "Removes keeper from cluster data", | ||
Run: removeKeeper, | ||
} | ||
|
||
func init() { | ||
cmdStolonCtl.AddCommand(removeKeeperCmd) | ||
} | ||
|
||
func removeKeeper(cmd *cobra.Command, args []string) { | ||
if len(args) > 1 { | ||
die("too many arguments") | ||
} | ||
|
||
if len(args) == 0 { | ||
die("keeper uid required") | ||
} | ||
|
||
keeperID := args[0] | ||
|
||
store, err := NewStore() | ||
if err != nil { | ||
die("cannot create store: %v", err) | ||
} | ||
|
||
cd, pair, err := getClusterData(store) | ||
if err != nil { | ||
die("cannot get cluster data: %v", err) | ||
} | ||
if cd.Cluster == nil { | ||
die("no cluster spec available") | ||
} | ||
if cd.Cluster.Spec == nil { | ||
die("no cluster spec available") | ||
} | ||
|
||
newCd := cd.DeepCopy() | ||
keeperInfo := newCd.Keepers[keeperID] | ||
if keeperInfo == nil { | ||
die("keeper doesn't exist") | ||
} | ||
|
||
keeperDb := getDbForKeeper(newCd.DBs, keeperID) | ||
|
||
if keeperDb != nil { | ||
if newCd.Cluster.Status.Master == keeperDb.UID { | ||
die("keeper assigned db is the current cluster master db.") | ||
} | ||
} | ||
|
||
delete(newCd.Keepers, keeperID) | ||
if keeperDb != nil { | ||
delete(newCd.DBs, keeperDb.UID) | ||
} | ||
|
||
// NOTE: if the removed db is listed inside another db.Followers it'll will | ||
// be cleaned by the sentinels | ||
|
||
_, err = store.AtomicPutClusterData(newCd, pair) | ||
if err != nil { | ||
die("cannot update cluster data: %v", err) | ||
} | ||
} | ||
|
||
func getDbForKeeper(dbs cluster.DBs, keeperID string) *cluster.DB { | ||
for _, db := range dbs { | ||
if db.Spec.KeeperUID == keeperID { | ||
return db | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters