Skip to content

Commit

Permalink
Merge pull request #3 from fujiwara/actions
Browse files Browse the repository at this point in the history
Add Actions to create a issue on GitHub
  • Loading branch information
fujiwara authored Apr 14, 2023
2 parents 77baa8f + b7c80c9 commit c4b400c
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 5 deletions.
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,33 @@ $ riex 30 --expired 60
{"service":"Redshift","name":"140aad98-3ab6-435d-bcd4-60d1e65375bc","description":"","instance_type":"ra3.xlplus","count":1,"start_time":"2021-12-21T09:17:32.937Z","end_time":"2022-12-21T09:17:32.937Z","state":"active"}
```

### GitHub Actions

`fujiwara/riex@v0` composite action can be used in GitHub Actions.
This action checks RI expiration and create an issue if RI will be expired within specified days. (default: 30 days)

```yaml
name: Check RI Expiration

on:
schedule:
- cron: '0 0 * * *'

jobs:
check_ri_expiration:
runs-on: ubuntu-latest
steps:
- name: Check RI expiration
uses: fujiwara/riex@v0
with:
days_left: '30'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} # or use aws-actions/configure-aws-credentials in before step
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
AWS_REGION: 'your-aws-region'


## LICENSE

MIT
61 changes: 61 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
name: "Check RI Expiration"
description: "Check if any Reserved Instances are expiring within a given number of days and create an issue if necessary"
inputs:
days_left:
description: "Number of days left before the RI expires"
required: true
default: "30"
dummy_end_time:
description: "Dummy end time for testing"
required: false
version:
description: "Version of riex to use"
required: true
default: "0.0.5"

runs:
using: "composite"
steps:
- name: Download riex
run: |
curl -sL https://github.com/fujiwara/riex/releases/download/v${{ inputs.version }}/riex_${{ inputs.version }}_linux_amd64.tar.gz -o riex.tar.gz
tar -xzf riex.tar.gz
install -m 755 riex /usr/local/bin
shell: bash
working-directory: /tmp

- name: Check RI expiration and create an issue if necessary
run: |
days_left=${{ inputs.days_left }}
if [[ "${{ inputs.dummy_end_time }}" != "" ]]; then
opts="--dummy-output --dummy-end-time ${{ inputs.dummy_end_time }}"
fi
output=$(riex $days_left --format markdown $opts)
if [ -z "$output" ]; then
echo "No expiring RIs found."
exit 0
fi
issue_title="RI Expiring in $days_left Days"
issue_body="The following Reserved Instances are expiring within $days_left days:\n\n$output"
# Check if an issue with the same title already exists
existing_issue=$(gh issue list --search "$issue_title" --state open --json 'number,body' -q '.[] | .number')
if [ -z "$existing_issue" ]; then
# Check if there's a closed issue with the same title
last_closed_issue=$(gh issue list --search "$issue_title" --state closed --json 'number,body' -q 'first(1) | .[] | .number')
if [ -z "$last_closed_issue" ]; then
# Create a new issue only if no open issue exists and there is no closed issue with the same title
gh issue create --title "$issue_title" --body "$issue_body"
else
echo "A closed issue with the same title already exists: Issue #$last_closed_issue"
fi
else
echo "An open issue with the same title already exists: Issue #$existing_issue"
fi
shell: bash
env:
GITHUB_TOKEN: ${{ github.token }}
17 changes: 12 additions & 5 deletions cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@ package riex

import (
"context"
"time"

"github.com/alecthomas/kong"
)

type Option struct {
Active bool `help:"Show active reserved instances."`
Expired int `help:"Show reserved instances expired in the last specified days."`
Days int `arg:"" help:"Show reserved instances that will be expired within specified days."`
Format string `enum:"json,markdown,tsv" help:"Output format.(json, markdown, tsv)" default:"json"`
Active bool `help:"Show active reserved instances."`
Expired int `help:"Show reserved instances expired in the last specified days."`
Days int `arg:"" help:"Show reserved instances that will be expired within specified days."`
Format string `enum:"json,markdown,tsv" help:"Output format.(json, markdown, tsv)" default:"json"`
DummyOutput bool `help:"Dummy output for testing."`
DummyEndTime time.Time `help:"Endtime for testing. works only with --dummy-output."`
}

func RunCLI(ctx context.Context, args []string) error {
Expand All @@ -26,5 +29,9 @@ func RunCLI(ctx context.Context, args []string) error {
if err != nil {
return err
}
return app.Run(ctx)
if cli.DummyOutput {
return app.RunForDummy(ctx, cli.DummyEndTime)
} else {
return app.Run(ctx)
}
}
22 changes: 22 additions & 0 deletions riex.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,28 @@ func New(ctx context.Context, opt *Option) (*Riex, error) {
return app, nil
}

func (app *Riex) RunForDummy(ctx context.Context, endTime time.Time) error {
now := time.Now()
ri := ReservedInstance{
Service: "DummyService",
InstanceType: "dummy.t3.micro",
Name: "dummy reservation",
Description: "dummy description",
Count: 1,
StartTime: endTime.Add(-time.Duration(1) * 24 * time.Hour * 365),
EndTime: endTime,
}
if now.Before(ri.EndTime) {
ri.State = "active"
} else {
ri.State = "expired"
}
if app.isPrintable(ri) {
return app.Print([]ReservedInstance{ri}, os.Stdout)
}
return nil
}

func (app *Riex) Run(ctx context.Context) error {
funcs := []func(context.Context) (*ReservedInstances, error){
app.detectEC2,
Expand Down

0 comments on commit c4b400c

Please sign in to comment.