-
Notifications
You must be signed in to change notification settings - Fork 212
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
737ec89
commit 6bc4507
Showing
7 changed files
with
161 additions
and
22 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
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,26 @@ | ||
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
# SPDX-License-Identifier: MIT | ||
|
||
name: Dedicated Host Daily Resources Cleaner | ||
|
||
on: | ||
schedule: | ||
- cron: "0 0 * * *" # Run Every Day At Midnight | ||
workflow_dispatch: | ||
|
||
jobs: | ||
clean-dedicated-hosts: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v2 | ||
- uses: actions/setup-go@v3 | ||
|
||
- name: Configure AWS Credentials | ||
uses: aws-actions/configure-aws-credentials@v1 | ||
with: | ||
aws-access-key-id: ${{ secrets.TERRAFORM_AWS_ACCESS_KEY_ID }} | ||
aws-secret-access-key: ${{ secrets.TERRAFORM_AWS_SECRET_ACCESS_KEY }} | ||
aws-region: us-west-2 | ||
|
||
- name: Clean old dedicated host | ||
run: go run ./integration/clean/clean_dedicated_host/clean_dedicated_host.go --tags=clean |
27 changes: 27 additions & 0 deletions
27
.github/workflows/internal-pipeline-dedicated-host-cleaner.yml
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,27 @@ | ||
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
# SPDX-License-Identifier: MIT | ||
|
||
# @TODO delete this when we remove the internal cw agent integration pipeline | ||
name: Dedicated Host Daily Resources Cleaner | ||
|
||
on: | ||
schedule: | ||
- cron: "0 0 * * *" # Run Every Day At Midnight | ||
workflow_dispatch: | ||
|
||
jobs: | ||
clean-dedicated-hosts: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v2 | ||
- uses: actions/setup-go@v3 | ||
|
||
- name: Configure AWS Credentials | ||
uses: aws-actions/configure-aws-credentials@v1 | ||
with: | ||
aws-access-key-id: ${{ secrets.INTERNAL_AWS_ACCESS_KEY_ID }} | ||
aws-secret-access-key: ${{ secrets.INTERNAL_AWS_SECRET_ACCESS_KEY }} | ||
aws-region: us-west-2 | ||
|
||
- name: Clean old dedicated host | ||
run: go run ./integration/clean/clean_dedicated_host/clean_dedicated_host.go --tags=clean |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,18 @@ | ||
**What does the cleaner do?** | ||
|
||
Cleaner cleans out old ami (ami older than 60 days) | ||
###Cleaner cleans out old ami (ami older than 60 days) | ||
|
||
The cleaner first searches for ami names (these are the ami created by the pipeline for use int he integration tests) | ||
1. cloudwatch-agent-integration-test* | ||
|
||
Then checks to see if the creation date is greater than 60 days. (The aws sdk v2 gives creation date as a pointer to string. To convert to golang time we use the aws smithy go time. This allows us to compare to 60 days in past time) | ||
|
||
If the ami is older than 60 days old then we delete the ami | ||
If the ami is older than 60 days old then we delete the ami | ||
|
||
###Cleans dedicated hosts for mac | ||
|
||
The cleaner first searches for dedicated host tag Name:IntegrationTestMacDedicatedHost | ||
|
||
Then checks to see if the creation date is greater than 26 hours and host status is available | ||
|
||
Delete is true |
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
80 changes: 80 additions & 0 deletions
80
integration/clean/clean_dedicated_host/clean_dedicated_host.go
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,80 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
//go:build clean | ||
// +build clean | ||
|
||
package main | ||
|
||
import ( | ||
"context" | ||
"github.com/aws/aws-sdk-go-v2/aws" | ||
"github.com/aws/aws-sdk-go-v2/config" | ||
"github.com/aws/aws-sdk-go-v2/service/ec2" | ||
"github.com/aws/aws-sdk-go-v2/service/ec2/types" | ||
"log" | ||
"time" | ||
) | ||
|
||
// Can't release a host if it was being used within the last 24 hr add 2 hr as a buffer | ||
const keepDurationDedicatedHost = -1 * time.Hour * 26 | ||
const tagName = "tag:Name" | ||
const tagValue = "IntegrationTestMacDedicatedHost" | ||
|
||
func main() { | ||
err := cleanDedicatedHost() | ||
if err != nil { | ||
log.Fatalf("errors cleaning %v", err) | ||
} | ||
} | ||
|
||
func cleanDedicatedHost() error { | ||
log.Print("Begin to clean EC2 Dedicated Host") | ||
|
||
expirationDateDedicatedHost := time.Now().UTC().Add(keepDurationDedicatedHost) | ||
cxt := context.Background() | ||
defaultConfig, err := config.LoadDefaultConfig(cxt) | ||
if err != nil { | ||
return err | ||
} | ||
ec2client := ec2.NewFromConfig(defaultConfig) | ||
|
||
dedicatedHosts, err := getDedicatedHost(cxt, ec2client) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
dedicatedHostIds := make([]string, 0) | ||
for _, dedicatedHost := range dedicatedHosts { | ||
log.Printf("dedicated host id %v experation date %v dedicated host creation date raw %v", | ||
*dedicatedHost.HostId, expirationDateDedicatedHost, *dedicatedHost.AllocationTime) | ||
if expirationDateDedicatedHost.After(*dedicatedHost.AllocationTime) && dedicatedHost.State == types.AllocationStateAvailable { | ||
log.Printf("Try to delete dedicated host %s tags %v launch-date %s", *dedicatedHost.HostId, dedicatedHost.Tags, *dedicatedHost.AllocationTime) | ||
dedicatedHostIds = append(dedicatedHostIds, *dedicatedHost.HostId) | ||
} | ||
} | ||
|
||
if len(dedicatedHostIds) == 0 { | ||
log.Printf("No dedicated hosts to release") | ||
return nil | ||
} | ||
|
||
log.Printf("Dedicated hosts to release %v", dedicatedHostIds) | ||
releaseDedicatedHost := ec2.ReleaseHostsInput{HostIds: dedicatedHostIds} | ||
_, err = ec2client.ReleaseHosts(cxt, &releaseDedicatedHost) | ||
return err | ||
} | ||
|
||
func getDedicatedHost(cxt context.Context, ec2client *ec2.Client) ([]types.Host, error) { | ||
// Get list of dedicated host | ||
nameFilter := types.Filter{Name: aws.String(tagName), Values: []string{ | ||
tagValue, | ||
}} | ||
|
||
describeDedicatedHostInput := ec2.DescribeHostsInput{Filter: []types.Filter{nameFilter}} | ||
describeDedicatedHostOutput, err := ec2client.DescribeHosts(cxt, &describeDedicatedHostInput) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return describeDedicatedHostOutput.Hosts, nil | ||
} |