Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add command to delete stale elastic indices #12

Draft
wants to merge 20 commits into
base: main
Choose a base branch
from
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Refactored confirmation.
GROwen committed Feb 20, 2024
commit d285229d0b9e290a3808c55c17411fb0cdba9ad2
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -21,6 +21,24 @@ This will store the encrypted file at `keys/production/oauth.pem.asc`.
cat oauth.pen.asc | bay kms decrypt > oauth.pem
```

## Elastic Cloud
Commands for querying and interacting with the Elastic Cloud API.

#### Required environment variables

> [!CAUTION]
> Variables are deployment specific - make sure the deployment you are targeting is not a production deployment.

* EC_API_KEY - Generated from the deployments Kibana settings
* EC_CLOUD_ID - Found on the deployments Elastic Cloud 'manage' page

#### Usage
`delete-stale` Delete indices that are greater than 30 days old

```
bay-cli elastic-cloud delete-stale
```

# Installation

## Homebrew (OSX)
57 changes: 49 additions & 8 deletions cmd/elastic-cloud/index.go
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@ package elastic_cloud
import (
"context"
"encoding/json"
"errors"
"fmt"
"log"
"os"
@@ -12,6 +13,7 @@ import (

"github.com/elastic/go-elasticsearch/v8"
"github.com/elastic/go-elasticsearch/v8/esapi"
"github.com/manifoldco/promptui"
"github.com/urfave/cli/v2"

envconfig "github.com/sethvargo/go-envconfig"
@@ -36,8 +38,8 @@ type Indices map[string]IndexSettings

var setupLog = ctrl.Log.WithName("setup")

func GetIndex(c *cli.Context) error {
dryRun := c.Bool("dry-run")
func DeleteStaleIndices(c *cli.Context) error {
force := c.Bool("force")
deleteList := make([]string, 0)

var config EsConfig
@@ -52,9 +54,13 @@ func GetIndex(c *cli.Context) error {
return err
}

settings, _ := esapi.IndicesGetSettingsRequest{FilterPath: []string{"*.settings.index.creation_date"}}.Do(context.TODO(), client)
settings, err := esapi.IndicesGetSettingsRequest{FilterPath: []string{"*.settings.index.creation_date"}}.Do(context.TODO(), client)

var list Indices
if err != nil {
return err
}

list := Indices{}

if err := json.NewDecoder(settings.Body).Decode(&list); err != nil {
log.Fatalf("Error parsing the response body: %s", err)
@@ -75,18 +81,53 @@ func GetIndex(c *cli.Context) error {
}
}
if c := len(deleteList); c > 0 {
if !dryRun {
if force {
fmt.Println("Deleting indices marked for deletion.")
_, err := esapi.IndicesDeleteRequest{Index: deleteList}.Do(context.TODO(), client)
statusCode, err := deleteIndices(client, deleteList, c)
if err != nil {
return err
} else {
fmt.Printf("%+v indices successfully deleted.", c)
if statusCode == 200 {
fmt.Printf("Deletion request failed. Status code %+v", statusCode)
} else {
fmt.Printf("%+v indices successfully deleted.", c)
}
}
} else {
fmt.Printf("The 'dry-run' flag is set - no further action taken. There are %+v indices marked for deletion", c)
prompt := promptui.Prompt{
Label: "Delete indices",
IsConfirm: true,
}

prompt_result, _ := prompt.Run()

if prompt_result == "y" {
_, err := deleteIndices(client, deleteList, c)
if err != nil {
return err
}
} else {
fmt.Printf("Operation cancelled.\nThere are %+v indices marked for deletion.\n", c)
}
}
} else {
fmt.Printf("No indices meet the criteria for deletion.")
}
}
return nil
}

func deleteIndices(client *elasticsearch.Client, deleteList []string, c int) (int, error) {
res, err := esapi.IndicesDeleteRequest{Index: deleteList}.Do(context.TODO(), client)
if err != nil {
return res.StatusCode, err
} else {
if res.StatusCode == 200 {
fmt.Printf("Deletion request failed. Status code %+v", res.StatusCode)
return res.StatusCode, errors.New("non 200 status code")
} else {
fmt.Printf("%+v indices successfully deleted.", c)
return res.StatusCode, nil
}
}
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -10,6 +10,7 @@ require (
github.com/aws/aws-sdk-go-v2/service/kms v1.27.9
github.com/elastic/go-elasticsearch v0.0.0
github.com/elastic/go-elasticsearch/v8 v8.12.0
github.com/manifoldco/promptui v0.9.0
github.com/pkg/errors v0.9.1
github.com/rs/zerolog v1.32.0
github.com/sethvargo/go-envconfig v1.0.0
@@ -36,6 +37,7 @@ require (
github.com/aws/smithy-go v1.19.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.3 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/elastic/elastic-transport-go/v8 v8.4.0 // indirect
7 changes: 7 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -58,6 +58,10 @@ github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e h1:fY5BOSpyZCqRo5OhCuC+XN+r/bBCmeuuJtjz+bCNIf8=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
@@ -141,6 +145,8 @@ github.com/machinebox/graphql v0.2.2 h1:dWKpJligYKhYKO5A2gvNhkJdQMNZeChZYyBbrZkB
github.com/machinebox/graphql v0.2.2/go.mod h1:F+kbVMHuwrQ5tYgU9JXlnskM8nOaFxCAEolaQybkjWA=
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
github.com/manifoldco/promptui v0.9.0 h1:3V4HzJk1TtXW1MTZMP7mdlwbBpIinw3HztaIlYthEiA=
github.com/manifoldco/promptui v0.9.0/go.mod h1:ka04sppxSGFAtxX0qhlYQjISsg9mR4GWtQEhdbn6Pgg=
github.com/matryer/is v1.4.0 h1:sosSmIWwkYITGrxZ25ULNDeKiMNzFSr4V/eqBQP0PeE=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
@@ -230,6 +236,7 @@ golang.org/x/oauth2 v0.12.0/go.mod h1:A74bZ3aGXgCY0qaIC9Ahg6Lglin4AMAco8cIv9baba
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20181122145206-62eef0e2fa9b/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
14 changes: 7 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
@@ -88,17 +88,17 @@ func main() {
},
{
Name: "elastic-cloud",
Usage: "commands to show Elastic Cloud deployment daat",
Usage: "commands to interact with Elastic Cloud deployments",
Subcommands: []*cli.Command{
{
Name: "index-metadata",
Usage: "shows metadata related to an index",
UsageText: "bay elastic-cloud index-metadata",
Action: elastic_cloud.GetIndex,
Name: "delete-stale",
Usage: "deletes stale indices (> 30 days old)",
UsageText: "bay elastic-cloud delete-stale",
Action: elastic_cloud.DeleteStaleIndices,
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "dry-run",
Usage: "Do not execute any mutations",
Name: "force",
Usage: "Execute any mutations",
},
},
},