mirror of
https://codeberg.org/scip/esctl.git
synced 2026-08-24 07:54:17 +02:00
add alias+datastream rollover, and auto rollover on index tpl change (#40)
fixes #37
This commit is contained in:
@@ -237,3 +237,23 @@ func DatastreamDelete(conf *cfg.Config, dsname string) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func DatastreamRollover(conf *cfg.Config, ds string) error {
|
||||
res, err := RolloverAlias(conf, ds)
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to rollover data stream: %s", esErrorString(err))
|
||||
}
|
||||
|
||||
table := printer.NewTable(conf, 2, 5)
|
||||
table.Addheaders("rollover response", "value")
|
||||
table.Entries = [][]string{
|
||||
{"acknowledged", fmt.Sprintf("%t", res.Acknowledged)},
|
||||
{"rolled over", fmt.Sprintf("%t", res.RolledOver)},
|
||||
{"shards acknowledged", fmt.Sprintf("%t", res.ShardsAcknowledged)},
|
||||
{"old index", res.OldIndex},
|
||||
{"new index", res.NewIndex},
|
||||
}
|
||||
|
||||
return table.Print()
|
||||
}
|
||||
|
||||
@@ -113,3 +113,23 @@ func IndexAliasDelete(conf *cfg.Config, index, alias string) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func IndexAliasRollover(conf *cfg.Config, alias string) error {
|
||||
res, err := RolloverAlias(conf, alias)
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to rollover index alias: %s", esErrorString(err))
|
||||
}
|
||||
|
||||
table := printer.NewTable(conf, 2, 5)
|
||||
table.Addheaders("rollover response", "value")
|
||||
table.Entries = [][]string{
|
||||
{"acknowledged", fmt.Sprintf("%t", res.Acknowledged)},
|
||||
{"rolled over", fmt.Sprintf("%t", res.RolledOver)},
|
||||
{"shards acknowledged", fmt.Sprintf("%t", res.ShardsAcknowledged)},
|
||||
{"old index", res.OldIndex},
|
||||
{"new index", res.NewIndex},
|
||||
}
|
||||
|
||||
return table.Print()
|
||||
}
|
||||
|
||||
@@ -127,26 +127,28 @@ func IndexTemplateShow(conf *cfg.Config, tplname string) error {
|
||||
table = printer.NewTable(conf, 2, 0)
|
||||
table.Addheaders("index field mapping", "type")
|
||||
|
||||
for name, field := range tpl.IndexTemplate.Template.Mappings.Properties {
|
||||
typeval := ""
|
||||
if tpl.IndexTemplate.Template.Mappings != nil {
|
||||
for name, field := range tpl.IndexTemplate.Template.Mappings.Properties {
|
||||
typeval := ""
|
||||
|
||||
switch val := field.(type) {
|
||||
case *types.IntegerNumberProperty:
|
||||
typeval = val.Type
|
||||
case *types.KeywordProperty:
|
||||
typeval = val.Type
|
||||
case *types.DateProperty:
|
||||
typeval = val.Type
|
||||
switch val := field.(type) {
|
||||
case *types.IntegerNumberProperty:
|
||||
typeval = val.Type
|
||||
case *types.KeywordProperty:
|
||||
typeval = val.Type
|
||||
case *types.DateProperty:
|
||||
typeval = val.Type
|
||||
}
|
||||
|
||||
table.Entries = append(table.Entries, []string{
|
||||
name, typeval,
|
||||
})
|
||||
}
|
||||
|
||||
table.Entries = append(table.Entries, []string{
|
||||
name, typeval,
|
||||
})
|
||||
}
|
||||
|
||||
fmt.Println()
|
||||
if err := table.Print(); err != nil {
|
||||
return err
|
||||
fmt.Println()
|
||||
if err := table.Print(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
@@ -343,6 +345,12 @@ func IndexTemplateModify(conf *cfg.Config, name string, mappings []string) error
|
||||
return fmt.Errorf("failed to modify index template: %s", esErrorString(err))
|
||||
}
|
||||
|
||||
if conf.Rollover {
|
||||
if err := rolloverAliasIndexTemplate(conf, name); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -358,6 +366,72 @@ func IndexTemplateDelete(conf *cfg.Config, name string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Based on given index template find the associated index patterns,
|
||||
// find indices matching those, find their associated aliases and
|
||||
// rollover all we find. Only run when conf.Rollover==true
|
||||
func rolloverAliasIndexTemplate(conf *cfg.Config, name string) error {
|
||||
res, err := conf.DefaultCluster.ES.Indices.GetIndexTemplate().
|
||||
Name(name).
|
||||
Do(context.Background())
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get index template: %s", esErrorString(err))
|
||||
}
|
||||
|
||||
slog.Debug("res", "index template", res)
|
||||
|
||||
if len(res.IndexTemplates) != 1 {
|
||||
return errors.New("multiple or no index template matched the pattern")
|
||||
}
|
||||
|
||||
patterns := res.IndexTemplates[0].IndexTemplate.IndexPatterns
|
||||
aliases := map[string]int{}
|
||||
|
||||
// find all aliases matching the patterns
|
||||
for _, pattern := range patterns {
|
||||
res, err := conf.DefaultCluster.ES.Indices.ResolveIndex(pattern).
|
||||
Do(context.Background())
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to resolve index pattern: %s", esErrorString(err))
|
||||
}
|
||||
|
||||
for _, index := range res.Indices {
|
||||
for _, alias := range index.Aliases {
|
||||
aliases[alias] = 1
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if len(aliases) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
table := printer.NewTable(conf, 4, 0)
|
||||
table.Addheaders("rollover alias", "status", "ack", "new index")
|
||||
|
||||
// apply rollover to all matching aliases, if any
|
||||
for alias := range aliases {
|
||||
res, err := RolloverAlias(conf, alias)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
table.Entries = append(table.Entries, []string{
|
||||
alias,
|
||||
fmt.Sprintf("%t", res.RolledOver),
|
||||
fmt.Sprintf("%t", res.Acknowledged),
|
||||
res.NewIndex,
|
||||
})
|
||||
|
||||
if err := table.Print(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
}
|
||||
|
||||
func modMappings(mappings []string) (types.TypeMappingVariant, error) {
|
||||
typemaps := esdsl.NewTypeMapping()
|
||||
|
||||
|
||||
65
pkg/es/rollover.go
Normal file
65
pkg/es/rollover.go
Normal file
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
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"
|
||||
|
||||
"codeberg.org/scip/esctl/pkg/cfg"
|
||||
"github.com/elastic/go-elasticsearch/v9/typedapi/esdsl"
|
||||
"github.com/elastic/go-elasticsearch/v9/typedapi/indices/rollover"
|
||||
"github.com/elastic/go-elasticsearch/v9/typedapi/types"
|
||||
)
|
||||
|
||||
func RolloverConditions(conf *cfg.Config) types.RolloverConditionsVariant {
|
||||
cond := esdsl.NewRolloverConditions()
|
||||
|
||||
if conf.MaxAge != "" {
|
||||
cond.MaxAge(esdsl.NewDuration().String(conf.MaxAge))
|
||||
}
|
||||
if conf.MaxDocs > 0 {
|
||||
cond.MaxDocs(int64(conf.MaxDocs))
|
||||
}
|
||||
if conf.MaxShardSize > 0 {
|
||||
bs := esdsl.NewByteSize()
|
||||
bs.Int64(int64(conf.MaxShardSize))
|
||||
cond.MaxPrimaryShardSize(bs)
|
||||
}
|
||||
if conf.MaxShardDocs > 0 {
|
||||
cond.MaxPrimaryShardDocs(int64(conf.MaxShardDocs))
|
||||
}
|
||||
|
||||
return cond
|
||||
}
|
||||
|
||||
func RolloverAlias(conf *cfg.Config, alias string) (*rollover.Response, error) {
|
||||
roll := conf.DefaultCluster.ES.Indices.Rollover(alias)
|
||||
|
||||
if conf.Wait {
|
||||
roll.WaitForActiveShards("all")
|
||||
}
|
||||
|
||||
if conf.DryRun {
|
||||
roll.DryRun(true)
|
||||
}
|
||||
|
||||
cond := RolloverConditions(conf)
|
||||
|
||||
roll.Conditions(cond)
|
||||
|
||||
return roll.Do(context.Background())
|
||||
}
|
||||
Reference in New Issue
Block a user