Skip to content

Commit dbcbbdd

Browse files
gursewak1997jlebon
authored andcommitted
mantle/ore: add ore aws delete-image
This enables us to deregister aws AMIs and/or snapshots. It also handles the cases of resource IDs unavailable and notFound by not returning an error.
1 parent 443b9da commit dbcbbdd

File tree

2 files changed

+111
-0
lines changed

2 files changed

+111
-0
lines changed

mantle/cmd/ore/aws/delete-image.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Copyright 2017 CoreOS, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package aws
16+
17+
import (
18+
"fmt"
19+
"os"
20+
21+
"github.com/spf13/cobra"
22+
)
23+
24+
var (
25+
cmdDeleteImage = &cobra.Command{
26+
Use: "delete-image --ami <ami_id> --snapshot <snapshot_id> ...",
27+
Short: "Delete AMI and/or snapshot",
28+
Run: runDeleteImage,
29+
}
30+
amiID string
31+
snapshotID string
32+
allowMissing bool
33+
)
34+
35+
func init() {
36+
// Initialize the command and its flags
37+
AWS.AddCommand(cmdDeleteImage)
38+
cmdDeleteImage.Flags().StringVar(&amiID, "ami", "", "AWS ami tag")
39+
cmdDeleteImage.Flags().StringVar(&snapshotID, "snapshot", "", "AWS snapshot tag")
40+
cmdDeleteImage.Flags().BoolVar(&allowMissing, "allow-missing", false, "Do not error out on the resource not existing")
41+
}
42+
43+
func runDeleteImage(cmd *cobra.Command, args []string) {
44+
// Check if either amiID or snapshotID is provided
45+
if amiID == "" && snapshotID == "" {
46+
fmt.Fprintf(os.Stderr, "Provide --ami or --snapshot to delete\n")
47+
os.Exit(1)
48+
}
49+
50+
// Remove resources based on provided flags
51+
if amiID != "" {
52+
err := API.RemoveByAmiTag(amiID, allowMissing)
53+
if err != nil {
54+
fmt.Fprintf(os.Stderr, "Could not delete %v: %v\n", amiID, err)
55+
os.Exit(1)
56+
}
57+
}
58+
59+
if snapshotID != "" {
60+
err := API.RemoveBySnapshotTag(snapshotID, allowMissing)
61+
if err != nil {
62+
fmt.Fprintf(os.Stderr, "Could not delete %v: %v\n", snapshotID, err)
63+
os.Exit(1)
64+
}
65+
}
66+
67+
os.Exit(0)
68+
}

mantle/platform/api/aws/images.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,49 @@ func (a *API) FindImage(name string) (string, error) {
733733
return "", nil
734734
}
735735

736+
// Deregisters the ami.
737+
func (a *API) RemoveByAmiTag(imageID string, allowMissing bool) error {
738+
_, err := a.ec2.DeregisterImage(&ec2.DeregisterImageInput{ImageId: &imageID})
739+
if err != nil {
740+
if allowMissing {
741+
if awsErr, ok := err.(awserr.Error); ok {
742+
if awsErr.Code() == "InvalidAMIID.NotFound" {
743+
plog.Infof("%s does not exist.", imageID)
744+
return nil
745+
}
746+
if awsErr.Code() == "InvalidAMIID.Unavailable" {
747+
plog.Infof("%s is no longer available.", imageID)
748+
return nil
749+
}
750+
}
751+
}
752+
return err
753+
}
754+
plog.Infof("Deregistered existing AMI %s", imageID)
755+
return nil
756+
}
757+
758+
func (a *API) RemoveBySnapshotTag(snapshotID string, allowMissing bool) error {
759+
_, err := a.ec2.DeleteSnapshot(&ec2.DeleteSnapshotInput{SnapshotId: &snapshotID})
760+
if err != nil {
761+
if allowMissing {
762+
if awsErr, ok := err.(awserr.Error); ok {
763+
if awsErr.Code() == "InvalidSnapshot.NotFound" {
764+
plog.Infof("%s does not exist.", snapshotID)
765+
return nil
766+
}
767+
if awsErr.Code() == "InvalidSnapshot.Unavailable" {
768+
plog.Infof("%s is no longer available.", snapshotID)
769+
return nil
770+
}
771+
}
772+
}
773+
return err
774+
}
775+
plog.Infof("Deregistered existing snapshot %s", snapshotID)
776+
return nil
777+
}
778+
736779
func (a *API) describeImage(imageID string) (*ec2.Image, error) {
737780
describeRes, err := a.ec2.DescribeImages(&ec2.DescribeImagesInput{
738781
ImageIds: aws.StringSlice([]string{imageID}),

0 commit comments

Comments
 (0)