Skip to content

Commit

Permalink
ore: Make upload --force deregister AMI, delete snapshot too
Browse files Browse the repository at this point in the history
This matches the semantic intended for coreos-assembler,
where currently it may reuse version numbers in the case
of failed builds, and we want any existing content replaced.

Closes: coreos#1035
  • Loading branch information
cgwalters committed Aug 8, 2019
1 parent 7ebd0ca commit 769a28c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
6 changes: 5 additions & 1 deletion cmd/ore/aws/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func init() {
cmdUpload.Flags().UintVarP(&uploadDiskSizeGiB, "disk-size-gib", "", aws.ContainerLinuxDiskSizeGiB, "AMI disk size in GiB")
cmdUpload.Flags().BoolVar(&uploadDiskSizeInspect, "disk-size-inspect", false, "set AMI disk size to size of local file")
cmdUpload.Flags().BoolVar(&uploadDeleteObject, "delete-object", true, "delete uploaded S3 object after snapshot is created")
cmdUpload.Flags().BoolVar(&uploadForce, "force", false, "overwrite existing S3 object rather than reusing")
cmdUpload.Flags().BoolVar(&uploadForce, "force", false, "overwrite any existing S3 object, snapshot, and AMI")
cmdUpload.Flags().StringVar(&uploadSourceSnapshot, "source-snapshot", "", "the snapshot ID to base this AMI on (default: create new snapshot)")
cmdUpload.Flags().Var(&uploadObjectFormat, "object-format", fmt.Sprintf("object format: %s or %s (default: %s)", aws.EC2ImageFormatVmdk, aws.EC2ImageFormatRaw, aws.EC2ImageFormatVmdk))
cmdUpload.Flags().StringVar(&uploadAMIName, "ami-name", "", "name of the AMI to create (default: Container-Linux-$USER-$VERSION)")
Expand Down Expand Up @@ -197,6 +197,10 @@ func runUpload(cmd *cobra.Command, args []string) error {
s3BucketName := s3URL.Host
s3ObjectPath := strings.TrimPrefix(s3URL.Path, "/")

if uploadForce {
API.RemoveImage(amiName, s3BucketName, s3ObjectPath)
}

// if no snapshot was specified, check for an existing one or a
// snapshot task in progress
sourceSnapshot := uploadSourceSnapshot
Expand Down
37 changes: 37 additions & 0 deletions platform/api/aws/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,43 @@ func (a *API) CreatePVImage(snapshotID string, diskSizeGiB uint, name string, de
return a.createImage(params)
}

// Remove all uploaded data associated with an AMI.
func (a *API) RemoveImage(name, s3BucketName, s3ObjectPath string) error {
err := a.DeleteObject(s3BucketName, s3ObjectPath)
if err != nil {
if awsErr, ok := err.(awserr.Error); ok {
if awsErr.Code() != "NoSuchKey" {
return err
}
} else {
return err
}
}

imageID, err := a.FindImage(name)
if err != nil {
return err
}
if imageID != "" {
_, err := a.ec2.DeregisterImage(&ec2.DeregisterImageInput{ImageId: &imageID})
if err != nil {
return err
}
}

snapshot, err := a.FindSnapshot(name)
if err != nil {
return err
}
if snapshot != nil {
// We explicitly ignore errors here in case somehow another AMI was based
// on that snapshot
a.ec2.DeleteSnapshot(&ec2.DeleteSnapshotInput{SnapshotId: &snapshot.SnapshotID})
}

return nil
}

func (a *API) createImage(params *ec2.RegisterImageInput) (string, error) {
res, err := a.ec2.RegisterImage(params)

Expand Down

0 comments on commit 769a28c

Please sign in to comment.