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

     1  package main
     2  
     3  import (
     4  	"flag"
     5  	"fmt"
     6  	"os"
     7  	"time"
     8  
     9  	"github.com/Cloud-Foundations/Dominator/lib/awsutil"
    10  	"github.com/Cloud-Foundations/Dominator/lib/constants"
    11  	"github.com/Cloud-Foundations/Dominator/lib/flags/commands"
    12  	"github.com/Cloud-Foundations/Dominator/lib/flags/loadflags"
    13  	"github.com/Cloud-Foundations/Dominator/lib/log/cmdlogger"
    14  	"github.com/Cloud-Foundations/Dominator/lib/srpc/setupclient"
    15  	libtags "github.com/Cloud-Foundations/Dominator/lib/tags"
    16  )
    17  
    18  var (
    19  	amiName    = flag.String("amiName", "", "AMI Name property")
    20  	enaSupport = flag.Bool("enaSupport", false,
    21  		"If true, set the Enhanced Networking Adaptor capability flag")
    22  	excludeSearchTags libtags.Tags
    23  	expiresIn         = flag.Duration("expiresIn", time.Hour,
    24  		"Date to set for the ExpiresAt tag")
    25  	ignoreMissingUnpackers = flag.Bool("ignoreMissingUnpackers", false,
    26  		"If true, do not generate an error for missing unpackers")
    27  	imageServerHostname = flag.String("imageServerHostname", "localhost",
    28  		"Hostname of imageserver")
    29  	imageServerPortNum = flag.Uint("imageServerPortNum",
    30  		constants.ImageServerPortNumber, "Port number of imageserver")
    31  	instanceName = flag.String("instanceName", "ImageUnpacker",
    32  		"The Name tag value for image unpacker instances")
    33  	instanceType = flag.String("instanceType", "t2.medium",
    34  		"Instance type to launch")
    35  	marketplaceImage = flag.String("marketplaceImage",
    36  		"3f8t6t8fp5m9xx18yzwriozxi",
    37  		"Product code (default Debian Jessie amd64)")
    38  	marketplaceLoginName = flag.String("marketplaceLoginName", "admin",
    39  		"Login name for instance booted from marketplace image")
    40  	maxIdleTime = flag.Duration("maxIdleTime", time.Minute*5,
    41  		"Maximum idle time for image unpacker instances")
    42  	minFreeBytes = flag.Uint64("minFreeBytes", 1<<28,
    43  		"minimum number of free bytes in image")
    44  	minImageAge = flag.Duration("minImageAge", time.Hour*24,
    45  		"Minimum image age when listing or deleting unused images")
    46  	oldImageInstancesCsvFile = flag.String("oldImageInstancesCsvFile", "",
    47  		"File to write CSV listing old image instances")
    48  	replaceInstances = flag.Bool("replaceInstances", false,
    49  		"If true, replace old instances when launching, else skip on old")
    50  	rootVolumeSize = flag.Uint("rootVolumeSize", 0,
    51  		"Size of root volume when launching instances")
    52  	s3Bucket = flag.String("s3Bucket", "",
    53  		"S3 bucket to upload bundle to (default is EBS-backed AMIs)")
    54  	s3Folder = flag.String("s3Folder", "",
    55  		"S3 folder to upload bundle to (default is EBS-backed AMIs)")
    56  	searchTags              = libtags.Tags{"Preferred": "true"}
    57  	securityGroupSearchTags libtags.Tags
    58  	sharingAccountName      = flag.String("sharingAccountName", "",
    59  		"Account from which to share AMIs (for S3-backed)")
    60  	skipTargets awsutil.TargetList
    61  	sshKeyName  = flag.String("sshKeyName", "",
    62  		"Name of SSH key for instance")
    63  	subnetSearchTags    libtags.Tags = libtags.Tags{"Network": "Private"}
    64  	tags                libtags.Tags
    65  	targets             awsutil.TargetList
    66  	unusedImagesCsvFile = flag.String("unusedImagesCsvFile", "",
    67  		"File to write CSV listing unused images")
    68  	vpcSearchTags libtags.Tags = libtags.Tags{"Preferred": "true"}
    69  )
    70  
    71  func init() {
    72  	flag.Var(&excludeSearchTags, "excludeSearchTags",
    73  		"Name of exclude tags to use when searching for resources")
    74  	flag.Var(&searchTags, "searchTags",
    75  		"Name of tags to use when searching for resources")
    76  	flag.Var(&securityGroupSearchTags, "securityGroupSearchTags",
    77  		"Restrict security group search to given tags")
    78  	flag.Var(&skipTargets, "skipTargets",
    79  		"List of targets to skip (default none). No wildcards permitted")
    80  	flag.Var(&subnetSearchTags, "subnetSearchTags",
    81  		"Restrict subnet search to given tags")
    82  	flag.Var(&tags, "tags", "Tags to apply")
    83  	flag.Var(&targets, "targets",
    84  		"List of targets (default all accounts and regions)")
    85  	flag.Var(&vpcSearchTags, "vpcSearchTags",
    86  		"Restrict VPC search to given tags")
    87  }
    88  
    89  func printUsage() {
    90  	w := flag.CommandLine.Output()
    91  	fmt.Fprintln(w, "Usage: ami-publisher [flags...] publish [args...]")
    92  	fmt.Fprintln(w, "Common flags:")
    93  	flag.PrintDefaults()
    94  	fmt.Fprintln(w, "Commands:")
    95  	commands.PrintCommands(w, subcommands)
    96  }
    97  
    98  var subcommands = []commands.Command{
    99  	{"add-volumes", "sizeInGiB", 1, 1, addVolumesSubcommand},
   100  	{"copy-bootstrap-image", "stream-name", 1, 1, copyBootstrapImageSubcommand},
   101  	{"delete", "results-file...", 1, -1, deleteSubcommand},
   102  	{"delete-tags", "tag-key results-file...", 2, -1, deleteTagsSubcommand},
   103  	{"delete-tags-on-unpackers", "tag-key", 1, 1,
   104  		deleteTagsOnUnpackersSubcommand},
   105  	{"delete-unused-images", "", 0, 0, deleteUnusedImagesSubcommand},
   106  	{"expire", "", 0, 0, expireSubcommand},
   107  	{"import-key-pair", "name pub-key-file", 2, 2, importKeyPairSubcommand},
   108  	{"launch-instances", "boot-image", 1, 1, launchInstancesSubcommand},
   109  	{"launch-instances-for-images", "results-file...", 0, -1,
   110  		launchInstancesForImagesSubcommand},
   111  	{"list-images", "", 0, 0, listImagesSubcommand},
   112  	{"list-streams", "", 0, 0, listStreamsSubcommand},
   113  	{"list-unpackers", "", 0, 0, listUnpackersSubcommand},
   114  	{"list-unused-images", "", 0, 0, listUnusedImagesSubcommand},
   115  	{"list-used-images", "", 0, 0, listUsedImagesSubcommand},
   116  	{"prepare-unpackers", "[stream-name]", 0, 1, prepareUnpackersSubcommand},
   117  	{"publish", "image-leaf-name", 2, 2, publishSubcommand},
   118  	{"remove-unused-volumes", "", 0, 0, removeUnusedVolumesSubcommand},
   119  	{"set-exclusive-tags", "key value results-file...", 2, -1,
   120  		setExclusiveTagsSubcommand},
   121  	{"set-tags-on-unpackers", "", 0, 0, setTagsSubcommand},
   122  	{"start-instances", "", 0, 0, startInstancesSubcommand},
   123  	{"stop-idle-unpackers", "", 0, 0, stopIdleUnpackersSubcommand},
   124  	{"terminate-instances", "", 0, 0, terminateInstancesSubcommand},
   125  }
   126  
   127  func doMain() int {
   128  	if err := loadflags.LoadForCli("ami-publisher"); err != nil {
   129  		fmt.Fprintln(os.Stderr, err)
   130  		return 1
   131  	}
   132  	cmdlogger.SetDatestampsDefault(true)
   133  	flag.Usage = printUsage
   134  	flag.Parse()
   135  	if flag.NArg() < 1 {
   136  		printUsage()
   137  		return 2
   138  	}
   139  	logger := cmdlogger.New()
   140  	if err := setupclient.SetupTls(true); err != nil {
   141  		logger.Println(err)
   142  		return 1
   143  	}
   144  	return commands.RunCommands(subcommands, printUsage, logger)
   145  }
   146  
   147  func main() {
   148  	os.Exit(doMain())
   149  }