mirror of
https://codeberg.org/scip/esctl.git
synced 2026-08-24 10:04:18 +02:00
88 lines
2.4 KiB
Go
88 lines
2.4 KiB
Go
|
|
/*
|
||
|
|
Copyright © 2026 Thomas von Dein
|
||
|
|
|
||
|
|
This program is free software: you can redistribute it and/or modify
|
||
|
|
it under the terms of the GNU General Public License as published by
|
||
|
|
the Free Software Foundation, either version 3 of the License, or
|
||
|
|
(at your option) any later version.
|
||
|
|
|
||
|
|
This program is distributed in the hope that it will be useful,
|
||
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
|
|
GNU General Public License for more details.
|
||
|
|
|
||
|
|
You should have received a copy of the GNU General Public License
|
||
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||
|
|
*/
|
||
|
|
package es
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"errors"
|
||
|
|
"fmt"
|
||
|
|
"log/slog"
|
||
|
|
|
||
|
|
"codeberg.org/scip/esctl/pkg/cfg"
|
||
|
|
"github.com/elastic/go-elasticsearch/v9/typedapi/types"
|
||
|
|
)
|
||
|
|
|
||
|
|
const (
|
||
|
|
shard_pause = `cluster.routing.allocation.enable`
|
||
|
|
)
|
||
|
|
|
||
|
|
func CcrShardPause(conf *cfg.Config) error {
|
||
|
|
return ClusterSettingsSetSingle(conf, shard_pause, "none")
|
||
|
|
}
|
||
|
|
|
||
|
|
func CcrShardResume(conf *cfg.Config) error {
|
||
|
|
return ClusterSettingsSetSingle(conf, shard_pause, "all")
|
||
|
|
}
|
||
|
|
|
||
|
|
func CcrStatus(conf *cfg.Config, leader, follower string) error {
|
||
|
|
if !checkClusterIsLeader(conf, leader) {
|
||
|
|
if !checkClusterIsLeader(conf, follower) {
|
||
|
|
return errors.New("leader/follower attribution is invalid, both clusters are followers")
|
||
|
|
}
|
||
|
|
|
||
|
|
// reverse attribution
|
||
|
|
f := follower
|
||
|
|
follower = leader
|
||
|
|
leader = f
|
||
|
|
slog.Debug("leader/follower attribution is invalid, reversing", "leader", leader, "follower", follower)
|
||
|
|
}
|
||
|
|
|
||
|
|
indices := ClusterIndices{}
|
||
|
|
|
||
|
|
for _, alias := range []string{leader, follower} {
|
||
|
|
cat := conf.Clusters[alias].ES.Cat.Indices().
|
||
|
|
// we need to add custom request headers, required for older ES instances
|
||
|
|
Header("content-type", "application/json").
|
||
|
|
Header("accept", "application/json")
|
||
|
|
|
||
|
|
res, err := cat.Do(context.Background())
|
||
|
|
if err != nil {
|
||
|
|
return fmt.Errorf("failed to get indicies on %s: %s", alias, err)
|
||
|
|
}
|
||
|
|
|
||
|
|
indices[alias] = make(map[string]*types.IndicesRecord, len(res))
|
||
|
|
|
||
|
|
for _, index := range res {
|
||
|
|
indices[alias][*index.Index] = &index
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
if !checkClusterStatus(conf, leader, follower) {
|
||
|
|
return errors.New("one of the two clusters is in a failed state")
|
||
|
|
}
|
||
|
|
|
||
|
|
findIlmErrors(conf, leader, follower)
|
||
|
|
|
||
|
|
if findIndicesOnlyOnLeader(conf, indices, leader, follower) &&
|
||
|
|
findOrphanedIndices(conf, indices, leader, follower) &&
|
||
|
|
findFailedFollowerIndices(conf, indices, follower) {
|
||
|
|
fmt.Println("everything's hunky-dory.")
|
||
|
|
}
|
||
|
|
|
||
|
|
return nil
|
||
|
|
}
|