github.com/cloud-foundations/dominator@v0.0.0-20221004181915-6e4fee580046/imagepublishers/amipublisher/api.go (about)

     1  package amipublisher
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/Cloud-Foundations/Dominator/lib/awsutil"
     7  	"github.com/Cloud-Foundations/Dominator/lib/filesystem"
     8  	"github.com/Cloud-Foundations/Dominator/lib/log"
     9  	libtags "github.com/Cloud-Foundations/Dominator/lib/tags"
    10  )
    11  
    12  const ExpiresAtFormat = "2006-01-02 15:04:05"
    13  
    14  type Image struct {
    15  	awsutil.Target
    16  	AmiId        string
    17  	AmiName      string
    18  	CreationDate string
    19  	Description  string
    20  	Size         uint // Size in GiB.
    21  	Tags         libtags.Tags
    22  }
    23  
    24  type Instance struct {
    25  	awsutil.Target
    26  	AmiId      string
    27  	InstanceId string
    28  	LaunchTime string
    29  	Tags       libtags.Tags
    30  }
    31  
    32  type InstanceResult struct {
    33  	awsutil.Target
    34  	InstanceId string
    35  	PrivateIp  string
    36  	Error      error
    37  }
    38  
    39  func (v InstanceResult) MarshalJSON() ([]byte, error) {
    40  	return v.marshalJSON()
    41  }
    42  
    43  type publishData struct {
    44  	imageServerAddress string
    45  	streamName         string
    46  	imageLeafName      string
    47  	minFreeBytes       uint64
    48  	amiName            string
    49  	tags               map[string]string
    50  	unpackerName       string
    51  	s3BucketExpression string
    52  	s3Folder           string
    53  	sharingAccountName string
    54  	publishOptions     *PublishOptions
    55  	// Computed data follow.
    56  	fileSystem *filesystem.FileSystem
    57  }
    58  
    59  type PublishOptions struct {
    60  	EnaSupport bool
    61  }
    62  
    63  type Resource struct {
    64  	awsutil.Target
    65  	SharedFrom     string
    66  	SnapshotId     string
    67  	S3Bucket       string
    68  	S3ManifestFile string
    69  	AmiId          string
    70  }
    71  
    72  type Results []TargetResult
    73  
    74  type TargetResult struct {
    75  	awsutil.Target
    76  	SharedFrom     string
    77  	SnapshotId     string
    78  	S3Bucket       string
    79  	S3ManifestFile string
    80  	AmiId          string
    81  	Size           uint // Size in GiB.
    82  	Error          error
    83  }
    84  
    85  type TargetUnpackers struct {
    86  	awsutil.Target
    87  	Unpackers []Unpacker
    88  }
    89  
    90  type Unpacker struct {
    91  	InstanceId        string
    92  	IpAddress         string
    93  	State             string
    94  	TimeSinceLastUsed string `json:",omitempty"`
    95  }
    96  
    97  type UnusedImagesResult struct {
    98  	UnusedImages []Image
    99  	OldInstances []Instance
   100  }
   101  
   102  type UsedImagesResult struct {
   103  	UsedImages     []Image
   104  	UsingInstances []Instance
   105  }
   106  
   107  func (v TargetResult) MarshalJSON() ([]byte, error) {
   108  	return v.marshalJSON()
   109  }
   110  
   111  func AddVolumes(targets awsutil.TargetList, skipList awsutil.TargetList,
   112  	tags libtags.Tags, unpackerName string, size uint64,
   113  	logger log.Logger) error {
   114  	return addVolumes(targets, skipList, tags, unpackerName, size, logger)
   115  }
   116  
   117  func CopyBootstrapImage(streamName string, targets awsutil.TargetList,
   118  	skipList awsutil.TargetList, marketplaceImage, marketplaceLoginName string,
   119  	newImageTags libtags.Tags, unpackerName string,
   120  	vpcSearchTags, subnetSearchTags, securityGroupSearchTags libtags.Tags,
   121  	instanceType string, sshKeyName string, logger log.Logger) error {
   122  	return copyBootstrapImage(streamName, targets, skipList, marketplaceImage,
   123  		marketplaceLoginName, newImageTags, unpackerName, vpcSearchTags,
   124  		subnetSearchTags, securityGroupSearchTags, instanceType, sshKeyName,
   125  		logger)
   126  }
   127  
   128  func DeleteResources(resources []Resource, logger log.Logger) error {
   129  	return deleteResources(resources, logger)
   130  }
   131  
   132  func DeleteTags(resources []Resource, tagKeys []string,
   133  	logger log.Logger) error {
   134  	return deleteTags(resources, tagKeys, logger)
   135  }
   136  
   137  func DeleteTagsOnUnpackers(targets awsutil.TargetList,
   138  	skipList awsutil.TargetList, name string, tagKeys []string,
   139  	logger log.Logger) error {
   140  	return deleteTagsOnUnpackers(targets, skipList, name, tagKeys, logger)
   141  }
   142  
   143  func DeleteUnusedImages(targets awsutil.TargetList, skipList awsutil.TargetList,
   144  	searchTags, excludeSearchTags libtags.Tags, minImageAge time.Duration,
   145  	logger log.DebugLogger) (UnusedImagesResult, error) {
   146  	return deleteUnusedImages(targets, skipList, searchTags, excludeSearchTags,
   147  		minImageAge, logger)
   148  }
   149  
   150  func ExpireResources(targets awsutil.TargetList, skipList awsutil.TargetList,
   151  	logger log.Logger) error {
   152  	return expireResources(targets, skipList, logger)
   153  }
   154  
   155  func ImportKeyPair(targets awsutil.TargetList, skipList awsutil.TargetList,
   156  	keyName string, publicKey []byte, logger log.Logger) error {
   157  	return importKeyPair(targets, skipList, keyName, publicKey, logger)
   158  }
   159  
   160  func LaunchInstances(targets awsutil.TargetList, skipList awsutil.TargetList,
   161  	imageSearchTags, vpcSearchTags, subnetSearchTags,
   162  	securityGroupSearchTags libtags.Tags, instanceType string,
   163  	rootVolumeSize uint, sshKeyName string, tags map[string]string,
   164  	replaceInstances bool, logger log.Logger) ([]InstanceResult, error) {
   165  	return launchInstances(targets, skipList, imageSearchTags, vpcSearchTags,
   166  		subnetSearchTags, securityGroupSearchTags, instanceType,
   167  		rootVolumeSize, sshKeyName, tags, replaceInstances, logger)
   168  }
   169  
   170  func LaunchInstancesForImages(images []Resource,
   171  	vpcSearchTags, subnetSearchTags, securityGroupSearchTags libtags.Tags,
   172  	instanceType string, rootVolumeSize uint, sshKeyName string,
   173  	tags map[string]string, logger log.Logger) ([]InstanceResult, error) {
   174  	return launchInstancesForImages(images, vpcSearchTags,
   175  		subnetSearchTags, securityGroupSearchTags, instanceType,
   176  		rootVolumeSize, sshKeyName, tags, logger)
   177  }
   178  
   179  func ListImages(targets awsutil.TargetList, skipList awsutil.TargetList,
   180  	searchTags, excludeSearchTags libtags.Tags, minImageAge time.Duration,
   181  	logger log.DebugLogger) ([]Image, error) {
   182  	return listImages(targets, skipList, searchTags, excludeSearchTags,
   183  		minImageAge, logger)
   184  }
   185  
   186  func ListStreams(targets awsutil.TargetList, skipList awsutil.TargetList,
   187  	unpackerName string, logger log.Logger) (map[string]struct{}, error) {
   188  	return listStreams(targets, skipList, unpackerName, logger)
   189  }
   190  
   191  func ListUnpackers(targets awsutil.TargetList, skipList awsutil.TargetList,
   192  	unpackerName string, logger log.Logger) (
   193  	[]TargetUnpackers, error) {
   194  	return listUnpackers(targets, skipList, unpackerName, logger)
   195  }
   196  
   197  func ListUnusedImages(targets awsutil.TargetList, skipList awsutil.TargetList,
   198  	searchTags, excludeSearchTags libtags.Tags, minImageAge time.Duration,
   199  	logger log.DebugLogger) (UnusedImagesResult, error) {
   200  	return listUnusedImages(targets, skipList, searchTags, excludeSearchTags,
   201  		minImageAge, logger)
   202  }
   203  
   204  func ListUsedImages(targets awsutil.TargetList, skipList awsutil.TargetList,
   205  	searchTags, excludeSearchTags libtags.Tags,
   206  	logger log.DebugLogger) (UsedImagesResult, error) {
   207  	return listUsedImages(targets, skipList, searchTags, excludeSearchTags,
   208  		logger)
   209  }
   210  
   211  func PrepareUnpackers(streamName string, targets awsutil.TargetList,
   212  	skipList awsutil.TargetList, name string, logger log.Logger) error {
   213  	return prepareUnpackers(streamName, targets, skipList, name, logger)
   214  }
   215  
   216  func Publish(imageServerAddress string, streamName string, imageLeafName string,
   217  	minFreeBytes uint64, amiName string, tags map[string]string,
   218  	targets awsutil.TargetList, skipList awsutil.TargetList,
   219  	unpackerName string, s3Bucket string, s3Folder string,
   220  	sharingAccountName string, publishOptions PublishOptions,
   221  	logger log.Logger) (Results, error) {
   222  	pData := &publishData{
   223  		imageServerAddress: imageServerAddress,
   224  		streamName:         streamName,
   225  		imageLeafName:      imageLeafName,
   226  		minFreeBytes:       minFreeBytes,
   227  		amiName:            amiName,
   228  		tags:               tags,
   229  		unpackerName:       unpackerName,
   230  		s3BucketExpression: s3Bucket,
   231  		s3Folder:           s3Folder,
   232  		sharingAccountName: sharingAccountName,
   233  		publishOptions:     &publishOptions,
   234  	}
   235  	return pData.publish(targets, skipList, logger)
   236  }
   237  
   238  func RemoveUnusedVolumes(targets awsutil.TargetList,
   239  	skipList awsutil.TargetList, unpackerName string, logger log.Logger) error {
   240  	return removeUnusedVolumes(targets, skipList, unpackerName, logger)
   241  }
   242  
   243  func SetExclusiveTags(resources []Resource, tagKey string, tagValue string,
   244  	logger log.Logger) error {
   245  	return setExclusiveTags(resources, tagKey, tagValue, logger)
   246  }
   247  
   248  func SetTags(targets awsutil.TargetList, skipList awsutil.TargetList,
   249  	name string, tags map[string]string, logger log.Logger) error {
   250  	return setTags(targets, skipList, name, tags, logger)
   251  }
   252  
   253  func StartInstances(targets awsutil.TargetList,
   254  	skipList awsutil.TargetList, name string, logger log.Logger) (
   255  	[]InstanceResult, error) {
   256  	return startInstances(targets, skipList, name, logger)
   257  }
   258  
   259  func StopIdleUnpackers(targets awsutil.TargetList, skipList awsutil.TargetList,
   260  	name string, idleTimeout time.Duration, logger log.Logger) error {
   261  	return stopIdleUnpackers(targets, skipList, name, idleTimeout, logger)
   262  }
   263  
   264  func TerminateInstances(targets awsutil.TargetList,
   265  	skipList awsutil.TargetList, name string, logger log.Logger) error {
   266  	return terminateInstances(targets, skipList, name, logger)
   267  }