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