func main() { auth, err := aws.ProfileCreds(Defaults["credential"], Defaults["profile"], time.Hour*24) if err != nil { panic(err.Error()) } // TODO: Load Which Regions regions := []string{"us-east-1", "us-west-1", "us-west-2"} for _, r := range regions { ec2Region := ec2.New(auth, r, nil) vpcResp, err := ec2Region.DescribeVPCs(nil) if err != nil { panic(err) } for _, v := range vpcResp.VPCs { tagValue, err := findTag(v.Tags, "Short Name") if err == nil { VPCs[*v.VPCID] = VPCConfig{Name: tagValue} } } resp, err := ec2Region.DescribeInstances(nil) if err != nil { panic(err) } ProcessInstances(resp, func(e *ec2.Instance) { s := *e.State.Name if s == "running" || s == "stopped" { host := AnsibleHostNew(e, Defaults["profile"]) AnsibleInstances[host.HostName] = host } }) } hostFile := GetHostFile() sshFile := GetSSHFile() defer hostFile.Close() defer sshFile.Close() WriteAllInstances(hostFile) WriteGroups(hostFile) WriteVPC(hostFile) // Only have to do this once WriteSSHHost(sshFile) }
func amis(args *docopt.Args) { auth, err := aws.EnvCreds() if err != nil { log.Fatal(err) } manifest := &release.EC2Manifest{} if err := json.NewDecoder(os.Stdin).Decode(manifest); err != nil { log.Fatal(err) } for _, s := range strings.Split(args.String["<ids>"], ",") { regionID := strings.SplitN(s, ":", 2) svc := ec2.New(auth, regionID[0], nil) resp, err := svc.DescribeImages(&ec2.DescribeImagesRequest{ImageIDs: []string{regionID[1]}}) if err != nil { log.Fatal(err) } if len(resp.Images) < 1 { log.Fatalln("Could not find image", regionID[1]) } image := resp.Images[0] var snapshotID string for _, mapping := range image.BlockDeviceMappings { if *mapping.DeviceName == *image.RootDeviceName { snapshotID = *mapping.EBS.SnapshotID } } if snapshotID == "" { log.Fatalln("Could not determine RootDeviceSnapshotID for", regionID[1]) } manifest.Add(args.String["<version>"], &release.EC2Image{ ID: *image.ImageID, Name: *image.Name, Region: regionID[0], OwnerID: *image.OwnerID, RootDeviceType: *image.RootDeviceType, RootDeviceName: *image.RootDeviceName, RootDeviceSnapshotID: snapshotID, VirtualizationType: *image.VirtualizationType, Hypervisor: *image.Hypervisor, }) } if err := json.NewEncoder(os.Stdout).Encode(manifest); err != nil { log.Fatal(err) } }
func (c *AWSCluster) SetCreds(creds *Credential) error { if creds == nil || creds.ID == "aws_env" { c.base.CredentialID = "aws_env" awsCreds, err := aws.EnvCreds() if err != nil { return err } c.creds = awsCreds } else { c.base.credential = creds c.base.CredentialID = creds.ID c.creds = aws.Creds(creds.ID, creds.Secret, "") } c.ec2 = ec2.New(c.creds, c.Region, nil) c.cf = cloudformation.New(c.creds, c.Region, nil) return nil }