github.com/cloud-foundations/dominator@v0.0.0-20221004181915-6e4fee580046/cmd/ami-publisher/publish.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"path"
     7  	"strings"
     8  	"time"
     9  
    10  	"github.com/Cloud-Foundations/Dominator/imagepublishers/amipublisher"
    11  	libjson "github.com/Cloud-Foundations/Dominator/lib/json"
    12  	"github.com/Cloud-Foundations/Dominator/lib/log"
    13  )
    14  
    15  func publishSubcommand(args []string, logger log.DebugLogger) error {
    16  	imageServerAddr := fmt.Sprintf("%s:%d",
    17  		*imageServerHostname, *imageServerPortNum)
    18  	if err := publish(imageServerAddr, args[0], args[1], logger); err != nil {
    19  		return fmt.Errorf("Error publishing image: %s", err)
    20  	}
    21  	return nil
    22  }
    23  
    24  func publish(imageServerAddress string, streamName string, imageLeafName string,
    25  	logger log.DebugLogger) error {
    26  	streamName = path.Clean(streamName)
    27  	imageLeafName = path.Clean(imageLeafName)
    28  	if *expiresIn > 0 {
    29  		expirationTime := time.Now().Add(*expiresIn)
    30  		tags["ExpiresAt"] = expirationTime.UTC().Format(
    31  			amipublisher.ExpiresAtFormat)
    32  	}
    33  	results, err := amipublisher.Publish(imageServerAddress, streamName,
    34  		imageLeafName, *minFreeBytes, *amiName, tags, targets, skipTargets,
    35  		*instanceName, *s3Bucket, *s3Folder, *sharingAccountName,
    36  		amipublisher.PublishOptions{
    37  			EnaSupport: *enaSupport,
    38  		},
    39  		logger)
    40  	if err != nil {
    41  		return err
    42  	}
    43  	if *ignoreMissingUnpackers {
    44  		newResults := make(amipublisher.Results, 0, len(results))
    45  		for _, result := range results {
    46  			if result.Error != nil &&
    47  				strings.Contains(result.Error.Error(),
    48  					"no ImageUnpacker instances found") {
    49  				continue
    50  			}
    51  			newResults = append(newResults, result)
    52  		}
    53  		results = newResults
    54  	}
    55  	if err := libjson.WriteWithIndent(os.Stdout, "    ", results); err != nil {
    56  		return err
    57  	}
    58  	for _, result := range results {
    59  		if result.Error != nil {
    60  			return result.Error
    61  		}
    62  	}
    63  	return nil
    64  }