<commit_before>package awsresourcemanager

import (
	"encoding/base64"
	"errors"
	log "github.com/Sirupsen/logrus"
	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/aws/awserr"
	"github.com/aws/aws-sdk-go/aws/credentials"
	"github.com/aws/aws-sdk-go/service/ec2"
)

// We'll use this constant to define the states of our instances.  There doesn't appear
// to be one in the AWS SDK, so we'll have to roll our own
const (
	INSTANCE_STATE_PENDING       int64 = 0
	INSTANCE_STATE_RUNNING       int64 = 16
	INSTANCE_STATE_SHUTTING_DOWN int64 = 32
	INSTANCE_STATE_TERMINATED    int64 = 48
	INSTANCE_STATE_STOPPING      int64 = 64
	INSTANCE_STATE_STOPPED       int64 = 80
)

func getEC2Client(key, private, region string) *ec2.EC2 {
	creds := credentials.NewStaticCredentials(key, private, "")

	return ec2.New(&aws.Config{
		Region:      region,
		Credentials: creds,
	})
}

// This function will take several pieces of information and launch a new AWS instance
// It requires the ID of the image, the security group to apply, the subnet ID to
// start the instance in, the name of the key for authentication, the type of instance
// as well as the number of instances to start.  It'll return the reservation object
// That we get back from the API.
func launchInstance(amiid, secgrpid, subnet, instancetype, ca, crt, key string, number int, ec2client *ec2.EC2) (ec2.Reservation, error) {
	cloudconfig := `#cloud-config
# vim: syntax=yaml
#
write_files:
-   content: |
` + ca + `
    path: /etc/cracklord/ssl/cracklord_ca.pem
-   content: |
` + crt + `
    path: /etc/cracklord/ssl/resource.pem
-   content: |
` + key + `
    path: /etc/cracklord/ssl/resource.key`

	userdata := base64.StdEncoding.EncodeToString([]byte(cloudconfig))

	// Build our request, converting the go base types into the pointers required by the SDK
	instanceReq := ec2.RunInstancesInput{
		ImageID:      aws.String(amiid),
		MaxCount:     aws.Long(int64(number)),
		MinCount:     aws.Long(int64(number)),
		InstanceType: aws.String(instancetype),
		// Because we're making this VPC aware, we also have to include a network interface specification
		NetworkInterfaces: []*ec2.InstanceNetworkInterfaceSpecification{
			{
				AssociatePublicIPAddress: aws.Boolean(true),
				DeviceIndex:              aws.Long(0),
				SubnetID:                 aws.String(subnet),
				Groups: []*string{
					aws.String(secgrpid),<commit_msg>Change filename for userdata in aws resource manager<commit_after>package awsresourcemanager

import (
	"encoding/base64"
	"errors"
	log "github.com/Sirupsen/logrus"
	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/aws/awserr"
	"github.com/aws/aws-sdk-go/aws/credentials"
	"github.com/aws/aws-sdk-go/service/ec2"
)

// We'll use this constant to define the states of our instances.  There doesn't appear
// to be one in the AWS SDK, so we'll have to roll our own
const (
	INSTANCE_STATE_PENDING       int64 = 0
	INSTANCE_STATE_RUNNING       int64 = 16
	INSTANCE_STATE_SHUTTING_DOWN int64 = 32
	INSTANCE_STATE_TERMINATED    int64 = 48
	INSTANCE_STATE_STOPPING      int64 = 64
	INSTANCE_STATE_STOPPED       int64 = 80
)

func getEC2Client(key, private, region string) *ec2.EC2 {
	creds := credentials.NewStaticCredentials(key, private, "")

	return ec2.New(&aws.Config{
		Region:      region,
		Credentials: creds,
	})
}

// This function will take several pieces of information and launch a new AWS instance
// It requires the ID of the image, the security group to apply, the subnet ID to
// start the instance in, the name of the key for authentication, the type of instance
// as well as the number of instances to start.  It'll return the reservation object
// That we get back from the API.
func launchInstance(amiid, secgrpid, subnet, instancetype, ca, crt, key string, number int, ec2client *ec2.EC2) (ec2.Reservation, error) {
	cloudconfig := `#cloud-config
# vim: syntax=yaml
#
write_files:
-   content: |
` + ca + `
    path: /etc/cracklord/ssl/cracklord_ca.pem
-   content: |
` + crt + `
    path: /etc/cracklord/ssl/resource.crt
-   content: |
` + key + `
    path: /etc/cracklord/ssl/resource.key`

	userdata := base64.StdEncoding.EncodeToString([]byte(cloudconfig))

	// Build our request, converting the go base types into the pointers required by the SDK
	instanceReq := ec2.RunInstancesInput{
		ImageID:      aws.String(amiid),
		MaxCount:     aws.Long(int64(number)),
		MinCount:     aws.Long(int64(number)),
		InstanceType: aws.String(instancetype),
		// Because we're making this VPC aware, we also have to include a network interface specification
		NetworkInterfaces: []*ec2.InstanceNetworkInterfaceSpecification{
			{
				AssociatePublicIPAddress: aws.Boolean(true),
				DeviceIndex:              aws.Long(0),
				SubnetID:                 aws.String(subnet),
				Groups: []*string{
					aws.String(secgrpid),
{'fraction_non_alphanumeric': 0.10521972354033422, 'fraction_numerical': 0.017742933773468125, 'mean_word_length': 3.617142857142857, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '3418951', 'n_tokens_mistral': 1693, 'n_tokens_neox': 1587, 'n_words': 454}