<commit_before>package amipublisher

import (
	"errors"
	"github.com/Symantec/Dominator/lib/awsutil"
	"github.com/Symantec/Dominator/lib/log"
	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/aws/session"
	"github.com/aws/aws-sdk-go/service/ec2"
)

func launchInstances(targets awsutil.TargetList, skipList awsutil.TargetList,
	imageSearchTags, vpcSearchTags, subnetSearchTags,
	securityGroupSearchTags awsutil.Tags, instanceType string,
	sshKeyName string, tags map[string]string, logger log.Logger) (
	[]InstanceResult, error) {
	if imageSearchTags["Name"] == "" {
		return nil, errors.New("no image Name search tag")
	}
	resultsChannel := make(chan InstanceResult, 1)
	numTargets, err := awsutil.ForEachTarget(targets, skipList,
		func(awsService *ec2.EC2, account, region string, logger log.Logger) {
			instanceId, privateIp, err := launchInstanceInTarget(awsService,
				imageSearchTags, vpcSearchTags, subnetSearchTags,
				securityGroupSearchTags, instanceType, sshKeyName, tags, logger)
			if err != nil {
				logger.Println(err)
			}
			resultsChannel <- InstanceResult{
				awsutil.Target{account, region},
				instanceId,
				privateIp,
				err,
			}
		},
		logger)
	// Collect results.
	results := make([]InstanceResult, 0, numTargets)
	for i := 0; i < numTargets; i++ {
		result := <-resultsChannel
		if result.AccountName == "" || result.Region == "" {
			continue
		}
		results = append(results, result)
	}
	return results, err
}

func launchInstanceInTarget(awsService *ec2.EC2,
	imageSearchTags, vpcSearchTags, subnetSearchTags,
	securityGroupSearchTags awsutil.Tags,
	instanceType string, sshKeyName string, tags awsutil.Tags,
	logger log.Logger) (string, string, error) {
	instances, err := getInstances(awsService, tags["Name"])
	if err != nil {
		return "", "", err
	}
	if len(instances) > 0 {
		return "", "", nil
	}
	image, err := findImage(awsService, imageSearchTags)
	if err != nil {
		return "", "", err
	}
	if image == nil {
		// TODO(rgooch): Create bootstrap image (for unpackers only).
		return "", "", errors.New("no image found")
	}
	instance, err := launchInstance(awsService, image, tags, vpcSearchTags,
		subnetSearchTags, securityGroupSearchTags, instanceType, sshKeyName)
	if err != nil {
		return "", "", err
	}
	instanceId := aws.StringValue(instance.InstanceId)
	privateIp := aws.StringValue(instance.PrivateIpAddress)
	logger.Printf("launched: %s with private IP: \n", instanceId, privateIp)
	err = awsService.WaitUntilInstanceRunning(&ec2.DescribeInstancesInput{
		InstanceIds: aws.StringSlice([]string{instanceId}),
	})
	if err != nil {
		return "", "", err
	}
	logger.Printf("running: %s\n", instanceId)
	return instanceId, privateIp, nil
}

func launchInstancesForImages(resources []Resource,
	vpcSearchTags, subnetSearchTags, securityGroupSearchTags awsutil.Tags,
	instanceType string, sshKeyName string, tags map[string]string,
	logger log.Logger) ([]InstanceResult, error) {
	resultsChannel := make(chan InstanceResult, 1)
	err := forEachResource(resources, false,
		func(session *session.Session, awsService *ec2.EC2, resource Resource,
			logger log.Logger) error {
			instanceId, privateIp, err := launchInstanceForImage(awsService,
				resource, vpcSearchTags, subnetSearchTags,
				securityGroupSearchTags, instanceType, sshKeyName, tags, logger)
			if err != nil {
				logger.Println(err)
			}<commit_msg>Logging fix in imagepublishers/amipublisher.LaunchInstances().<commit_after>package amipublisher

import (
	"errors"
	"github.com/Symantec/Dominator/lib/awsutil"
	"github.com/Symantec/Dominator/lib/log"
	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/aws/session"
	"github.com/aws/aws-sdk-go/service/ec2"
)

func launchInstances(targets awsutil.TargetList, skipList awsutil.TargetList,
	imageSearchTags, vpcSearchTags, subnetSearchTags,
	securityGroupSearchTags awsutil.Tags, instanceType string,
	sshKeyName string, tags map[string]string, logger log.Logger) (
	[]InstanceResult, error) {
	if imageSearchTags["Name"] == "" {
		return nil, errors.New("no image Name search tag")
	}
	resultsChannel := make(chan InstanceResult, 1)
	numTargets, err := awsutil.ForEachTarget(targets, skipList,
		func(awsService *ec2.EC2, account, region string, logger log.Logger) {
			instanceId, privateIp, err := launchInstanceInTarget(awsService,
				imageSearchTags, vpcSearchTags, subnetSearchTags,
				securityGroupSearchTags, instanceType, sshKeyName, tags, logger)
			if err != nil {
				logger.Println(err)
			}
			resultsChannel <- InstanceResult{
				awsutil.Target{account, region},
				instanceId,
				privateIp,
				err,
			}
		},
		logger)
	// Collect results.
	results := make([]InstanceResult, 0, numTargets)
	for i := 0; i < numTargets; i++ {
		result := <-resultsChannel
		if result.AccountName == "" || result.Region == "" {
			continue
		}
		results = append(results, result)
	}
	return results, err
}

func launchInstanceInTarget(awsService *ec2.EC2,
	imageSearchTags, vpcSearchTags, subnetSearchTags,
	securityGroupSearchTags awsutil.Tags,
	instanceType string, sshKeyName string, tags awsutil.Tags,
	logger log.Logger) (string, string, error) {
	instances, err := getInstances(awsService, tags["Name"])
	if err != nil {
		return "", "", err
	}
	if len(instances) > 0 {
		return "", "", nil
	}
	image, err := findImage(awsService, imageSearchTags)
	if err != nil {
		return "", "", err
	}
	if image == nil {
		// TODO(rgooch): Create bootstrap image (for unpackers only).
		return "", "", errors.New("no image found")
	}
	instance, err := launchInstance(awsService, image, tags, vpcSearchTags,
		subnetSearchTags, securityGroupSearchTags, instanceType, sshKeyName)
	if err != nil {
		return "", "", err
	}
	instanceId := aws.StringValue(instance.InstanceId)
	privateIp := aws.StringValue(instance.PrivateIpAddress)
	logger.Printf("launched: %s with private IP: %s\n", instanceId, privateIp)
	err = awsService.WaitUntilInstanceRunning(&ec2.DescribeInstancesInput{
		InstanceIds: aws.StringSlice([]string{instanceId}),
	})
	if err != nil {
		return "", "", err
	}
	logger.Printf("running: %s\n", instanceId)
	return instanceId, privateIp, nil
}

func launchInstancesForImages(resources []Resource,
	vpcSearchTags, subnetSearchTags, securityGroupSearchTags awsutil.Tags,
	instanceType string, sshKeyName string, tags map[string]string,
	logger log.Logger) ([]InstanceResult, error) {
	resultsChannel := make(chan InstanceResult, 1)
	err := forEachResource(resources, false,
		func(session *session.Session, awsService *ec2.EC2, resource Resource,
			logger log.Logger) error {
			instanceId, privateIp, err := launchInstanceForImage(awsService,
				resource, vpcSearchTags, subnetSearchTags,
				securityGroupSearchTags, instanceType, sshKeyName, tags, logger)
			if err != nil {
				logger.Println(err)
			}
{'fraction_non_alphanumeric': 0.12568064753495217, 'fraction_numerical': 0.0038263428991905813, 'mean_word_length': 5.008841732979664, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4547305', 'n_tokens_mistral': 2339, 'n_tokens_neox': 2207, 'n_words': 501}