github.com/cloud-foundations/dominator@v0.0.0-20221004181915-6e4fee580046/cmd/ami-publisher/launchInstances.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "os" 6 "path" 7 "time" 8 9 "github.com/Cloud-Foundations/Dominator/imagepublishers/amipublisher" 10 libjson "github.com/Cloud-Foundations/Dominator/lib/json" 11 "github.com/Cloud-Foundations/Dominator/lib/log" 12 libtags "github.com/Cloud-Foundations/Dominator/lib/tags" 13 ) 14 15 func launchInstancesSubcommand(args []string, logger log.DebugLogger) error { 16 if err := launchInstances(args[0], logger); err != nil { 17 return fmt.Errorf("Error launching instances: %s", err) 18 } 19 return nil 20 } 21 22 func launchInstances(bootImage string, logger log.DebugLogger) error { 23 bootImage = path.Clean(bootImage) 24 tags["Name"] = *instanceName 25 searchTags["Name"] = bootImage 26 results, err := amipublisher.LaunchInstances(targets, skipTargets, 27 searchTags, vpcSearchTags, subnetSearchTags, securityGroupSearchTags, 28 *instanceType, *rootVolumeSize, *sshKeyName, tags, *replaceInstances, 29 logger) 30 if err != nil { 31 return err 32 } 33 if err := libjson.WriteWithIndent(os.Stdout, " ", results); err != nil { 34 return err 35 } 36 for _, result := range results { 37 if result.Error != nil { 38 return result.Error 39 } 40 } 41 return nil 42 } 43 44 func launchInstancesForImagesSubcommand(args []string, 45 logger log.DebugLogger) error { 46 if err := launchInstancesForImages(args, logger); err != nil { 47 return fmt.Errorf("Error launching instances: %s", err) 48 } 49 return nil 50 } 51 52 func launchInstancesForImages(resourcesFiles []string, 53 logger log.DebugLogger) error { 54 resources := make([]amipublisher.Resource, 0) 55 for _, resourcesFile := range resourcesFiles { 56 fileRes := make([]amipublisher.Resource, 0) 57 if err := libjson.ReadFromFile(resourcesFile, &fileRes); err != nil { 58 return err 59 } 60 resources = append(resources, fileRes...) 61 } 62 if tags == nil { 63 tags = make(libtags.Tags) 64 } 65 tags["Name"] = *instanceName 66 if *expiresIn > 0 { 67 expirationTime := time.Now().Add(*expiresIn) 68 tags["ExpiresAt"] = expirationTime.UTC().Format( 69 amipublisher.ExpiresAtFormat) 70 } 71 results, err := amipublisher.LaunchInstancesForImages(resources, 72 vpcSearchTags, subnetSearchTags, securityGroupSearchTags, 73 *instanceType, *rootVolumeSize, *sshKeyName, tags, logger) 74 if err != nil { 75 return err 76 } 77 if err := libjson.WriteWithIndent(os.Stdout, " ", results); err != nil { 78 return err 79 } 80 for _, result := range results { 81 if result.Error != nil { 82 return result.Error 83 } 84 } 85 return nil 86 }