<commit_before>package common

import (
	"fmt"
	"github.com/mitchellh/goamz/ec2"
	"github.com/mitchellh/multistep"
	"github.com/mitchellh/packer/packer"
	"io/ioutil"
	"log"
)

type StepRunSourceInstance struct {
	ExpectedRootDevice string
	InstanceType       string
	UserData           string
	UserDataFile       string
	SourceAMI          string
	IamInstanceProfile string
	SubnetId           string
	BlockDevices       BlockDevices

	instance *ec2.Instance
}

func (s *StepRunSourceInstance) Run(state map[string]interface{}) multistep.StepAction {
	ec2conn := state["ec2"].(*ec2.EC2)
	keyName := state["keyPair"].(string)
	securityGroupId := state["securityGroupId"].(string)
	ui := state["ui"].(packer.Ui)

	userData := s.UserData
	if s.UserDataFile != "" {
		contents, err := ioutil.ReadFile(s.UserDataFile)
		if err != nil {
			state["error"] = fmt.Errorf("Problem reading user data file: %s", err)
			return multistep.ActionHalt
		}

		userData = string(contents)
	}

	runOpts := &ec2.RunInstances{
		KeyName:            keyName,
		ImageId:            s.SourceAMI,
		InstanceType:       s.InstanceType,
		UserData:           []byte(userData),
		MinCount:           0,
		MaxCount:           0,
		SecurityGroups:     []ec2.SecurityGroup{ec2.SecurityGroup{Id: securityGroupId}},
		IamInstanceProfile: s.IamInstanceProfile,
		SubnetId:           s.SubnetId,
		BlockDevices:       s.BlockDevices.BuildLaunchDevices(),
	}

	ui.Say("Launching a source AWS instance...")
	imageResp, err := ec2conn.Images([]string{s.SourceAMI}, ec2.NewFilter())
	if err != nil {
		state["error"] = fmt.Errorf("There was a problem with the source AMI: %s", err)
		return multistep.ActionHalt
	}

	if len(imageResp.Images) != 1 {
		state["error"] = fmt.Errorf("The source AMI '%s' could not be found.", s.SourceAMI)
		return multistep.ActionHalt
	}

	if s.ExpectedRootDevice != "" && imageResp.Images[0].RootDeviceType != s.ExpectedRootDevice {
		state["error"] = fmt.Errorf(
			"The provided source AMI has an invalid root device type.\n"+
				"Expected '%s', got '%s'.",
			s.ExpectedRootDevice, imageResp.Images[0].RootDeviceType)
		return multistep.ActionHalt
	}

	runResp, err := ec2conn.RunInstances(runOpts)
	if err != nil {
		err := fmt.Errorf("Error launching source instance: %s", err)
		state["error"] = err
		ui.Error(err.Error())
		return multistep.ActionHalt
	}

	s.instance = &runResp.Instances[0]
	log.Printf("instance id: %s", s.instance.InstanceId)

	ui.Say(fmt.Sprintf("Waiting for instance (%s) to become ready...", s.instance.InstanceId))
	stateChange := StateChangeConf{
		Conn:      ec2conn,
		Pending:   []string{"pending"},
		Target:    "running",
		Refresh:   InstanceStateRefreshFunc(ec2conn, s.instance),
		StepState: state,
	}
	latestInstance, err := WaitForState(&stateChange)
	s.instance = latestInstance.(*ec2.Instance)
	if err != nil {
		err := fmt.Errorf("Error waiting for instance (%s) to become ready: %s", s.instance.InstanceId, err)
		state["error"] = err
		ui.Error(err.Error())
		return multistep.ActionHalt
	}

	state["instance"] = s.instance

	return multistep.ActionContinue
}

func (s *StepRunSourceInstance) Cleanup(state map[string]interface{}) {
	if s.instance == nil {
		return
	}

	ec2conn := state["ec2"].(*ec2.EC2)
	ui := state["ui"].(packer.Ui)

	ui.Say("Terminating the source AWS instance...")
	if _, err := ec2conn.TerminateInstances([]string{s.instance.InstanceId}); err != nil {
		ui.Error(fmt.Sprintf("Error terminating instance, may still be around: %s", err))
		return
	}

	stateChange := StateChangeConf{
		Conn:    ec2conn,
		Pending: []string{"pending", "running", "shutting-down", "stopped", "stopping"},
		Refresh: InstanceStateRefreshFunc(ec2conn, s.instance),
		Target:  "terminated",
	}

	WaitForState(&stateChange)
}<commit_msg>builder/amazon: fix crash case if SIGINT while waiting for instance<commit_after>package common

import (
	"fmt"
	"github.com/mitchellh/goamz/ec2"
	"github.com/mitchellh/multistep"
	"github.com/mitchellh/packer/packer"
	"io/ioutil"
	"log"
)

type StepRunSourceInstance struct {
	ExpectedRootDevice string
	InstanceType       string
	UserData           string
	UserDataFile       string
	SourceAMI          string
	IamInstanceProfile string
	SubnetId           string
	BlockDevices       BlockDevices

	instance *ec2.Instance
}

func (s *StepRunSourceInstance) Run(state map[string]interface{}) multistep.StepAction {
	ec2conn := state["ec2"].(*ec2.EC2)
	keyName := state["keyPair"].(string)
	securityGroupId := state["securityGroupId"].(string)
	ui := state["ui"].(packer.Ui)

	userData := s.UserData
	if s.UserDataFile != "" {
		contents, err := ioutil.ReadFile(s.UserDataFile)
		if err != nil {
			state["error"] = fmt.Errorf("Problem reading user data file: %s", err)
			return multistep.ActionHalt
		}

		userData = string(contents)
	}

	runOpts := &ec2.RunInstances{
		KeyName:            keyName,
		ImageId:            s.SourceAMI,
		InstanceType:       s.InstanceType,
		UserData:           []byte(userData),
		MinCount:           0,
		MaxCount:           0,
		SecurityGroups:     []ec2.SecurityGroup{ec2.SecurityGroup{Id: securityGroupId}},
		IamInstanceProfile: s.IamInstanceProfile,
		SubnetId:           s.SubnetId,
		BlockDevices:       s.BlockDevices.BuildLaunchDevices(),
	}

	ui.Say("Launching a source AWS instance...")
	imageResp, err := ec2conn.Images([]string{s.SourceAMI}, ec2.NewFilter())
	if err != nil {
		state["error"] = fmt.Errorf("There was a problem with the source AMI: %s", err)
		return multistep.ActionHalt
	}

	if len(imageResp.Images) != 1 {
		state["error"] = fmt.Errorf("The source AMI '%s' could not be found.", s.SourceAMI)
		return multistep.ActionHalt
	}

	if s.ExpectedRootDevice != "" && imageResp.Images[0].RootDeviceType != s.ExpectedRootDevice {
		state["error"] = fmt.Errorf(
			"The provided source AMI has an invalid root device type.\n"+
				"Expected '%s', got '%s'.",
			s.ExpectedRootDevice, imageResp.Images[0].RootDeviceType)
		return multistep.ActionHalt
	}

	runResp, err := ec2conn.RunInstances(runOpts)
	if err != nil {
		err := fmt.Errorf("Error launching source instance: %s", err)
		state["error"] = err
		ui.Error(err.Error())
		return multistep.ActionHalt
	}

	s.instance = &runResp.Instances[0]
	log.Printf("instance id: %s", s.instance.InstanceId)

	ui.Say(fmt.Sprintf("Waiting for instance (%s) to become ready...", s.instance.InstanceId))
	stateChange := StateChangeConf{
		Conn:      ec2conn,
		Pending:   []string{"pending"},
		Target:    "running",
		Refresh:   InstanceStateRefreshFunc(ec2conn, s.instance),
		StepState: state,
	}
	latestInstance, err := WaitForState(&stateChange)
	if err != nil {
		err := fmt.Errorf("Error waiting for instance (%s) to become ready: %s", s.instance.InstanceId, err)
		state["error"] = err
		ui.Error(err.Error())
		return multistep.ActionHalt
	}

	s.instance = latestInstance.(*ec2.Instance)
	state["instance"] = s.instance

	return multistep.ActionContinue
}

func (s *StepRunSourceInstance) Cleanup(state map[string]interface{}) {
	if s.instance == nil {
		return
	}

	ec2conn := state["ec2"].(*ec2.EC2)
	ui := state["ui"].(packer.Ui)

	ui.Say("Terminating the source AWS instance...")
	if _, err := ec2conn.TerminateInstances([]string{s.instance.InstanceId}); err != nil {
		ui.Error(fmt.Sprintf("Error terminating instance, may still be around: %s", err))
		return
	}

	stateChange := StateChangeConf{
		Conn:    ec2conn,
		Pending: []string{"pending", "running", "shutting-down", "stopped", "stopping"},
		Refresh: InstanceStateRefreshFunc(ec2conn, s.instance),
		Target:  "terminated",
	}

	WaitForState(&stateChange)
}
{'fraction_non_alphanumeric': 0.1416557161629435, 'fraction_numerical': 0.007358738501971091, 'mean_word_length': 3.94541910331384, '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': '3100799', 'n_tokens_mistral': 2722, 'n_tokens_neox': 2566, 'n_words': 561}