cami

package
v1.3.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 9, 2023 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package cami provides a simple service for keeping AMIs clean.

Example
package main

import (
	"errors"
	"fmt"
	"strings"

	"github.com/lingrino/cami/cami"
)

func main() {
	var err error

	aws, err := cami.NewAWS(&cami.Config{DryRun: false})
	if err != nil {
		fmt.Printf("ERROR: %v\n", err)
	}

	err = aws.Auth()
	if err != nil {
		fmt.Printf("ERROR: %v\n", err)
	}

	deleted, err := aws.DeleteUnusedAMIs()
	if len(deleted) == 0 && err == nil {
		fmt.Println("nothing to delete")
	}

	var eda *cami.DeleteAMIError
	if err != nil {
		if errors.As(err, &eda) {
			fmt.Printf("Failed to delete:\n  %s\n", strings.Join(eda.IDs, "\n  "))
		} else {
			fmt.Printf("UNKNOWN ERROR: %v\n", err)
		}
	}
	if len(deleted) > 0 {
		fmt.Printf("Successfully deleted:\n  %s\n", strings.Join(deleted, "\n  "))
	}
}
Output:

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// ErrCreateSession is when we fail to create an AWS session.
	ErrCreateSession = errors.New("create session")
	// ErrDesribeImages is when we fail to describe EC2 images.
	ErrDesribeImages = errors.New("describe images")
	// ErrDesribeInstances is when we fail to describe EC2 instances.
	ErrDesribeInstances = errors.New("describe instances")
	// ErrDeregisterImage is when we fail to deregister an image (AMI).
	ErrDeregisterImage = errors.New("deregister image")
	// ErrDeleteSnapshot is when we fail to delete a snapshot.
	ErrDeleteSnapshot = errors.New("delete snapshot")
	// ErrFilterAMIs is when when we fail to filter AMIs and EC2 instances.
	ErrFilterAMIs = errors.New("filter AMIs")
)

Functions

This section is empty.

Types

type AWS

type AWS struct {
	// contains filtered or unexported fields
}

AWS is the main struct that holds our client and info.

func NewAWS

func NewAWS(c *Config) (*AWS, error)

NewAWS returns a new AWS struct.

func (*AWS) AMIs

func (a *AWS) AMIs() ([]types.Image, error)

AMIs returns a list of all our AMIs.

func (*AWS) Auth

func (a *AWS) Auth() error

Auth sets up our AWS session and service clients.

func (*AWS) DeleteAMIs

func (a *AWS) DeleteAMIs(amis []types.Image) ([]string, error)

DeleteAMIs deregisters all AMIs in the provided list and deletes the snapshots associated with the deregistered AMI. Returns a list of IDs that were successfully deleted. If DryDrun == true does not actually delete.

func (*AWS) DeleteUnusedAMIs

func (a *AWS) DeleteUnusedAMIs() ([]string, error)

DeleteUnusedAMIs finds and deletes all AMIs (and their associated snapshots) that are not being used by any current EC2 instances in the same account.

func (*AWS) EC2s

func (a *AWS) EC2s(amis []types.Image) ([]types.Instance, error)

EC2s returns a list of all our EC2 instances using one of the AMIs in the list provided.

func (*AWS) FilterAMIs

func (a *AWS) FilterAMIs(amis []types.Image, ec2s []types.Instance) ([]types.Image, error)

FilterAMIs returns back the list of AMIs with images in ec2s removed.

type Config

type Config struct {
	// Set to true to run non-destructively
	DryRun bool
}

Config holds the configuration for our AWS struct.

type DeleteAMIError added in v1.3.5

type DeleteAMIError struct {
	// IDs is the list of AMI and snapshot IDs we failed to delete
	IDs []string
}

DeleteAMIError is when we fail to delete (deregister image + snapshot delete) an image (AMI).

func (*DeleteAMIError) Append added in v1.3.5

func (e *DeleteAMIError) Append(ids ...string)

Append adds a new ID to the list of IDs.

func (*DeleteAMIError) Error added in v1.3.5

func (e *DeleteAMIError) Error() string

Error returns the error string for DeleteAMIError.

func (*DeleteAMIError) ErrorOrNil added in v1.3.5

func (e *DeleteAMIError) ErrorOrNil() error

ErrorOrNil returns nil if IDs is empty and the error otherwise.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL