<commit_before>package amazon

import (
	"context"
	"fmt"
	"io"
	"strconv"
	"strings"
	"time"

	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/service/ec2"
	"github.com/aws/aws-sdk-go/service/ec2/ec2iface"
	"github.com/pkg/errors"
	"github.com/sirupsen/logrus"
	"github.com/supergiant/supergiant/pkg/clouds"
	"github.com/supergiant/supergiant/pkg/node"
	"github.com/supergiant/supergiant/pkg/util"
	"github.com/supergiant/supergiant/pkg/workflows/steps"
)

const (
	StepNameCreateEC2Instance = "aws_create_instance"
	IPAttempts                = 5
	SleepSecondsPerAttempt    = 6
	timeout                   = time.Second * 10
)

type StepCreateInstance struct {
	GetEC2 GetEC2Fn
}

//InitCreateMachine adds the step to the registry
func InitCreateMachine(fn func(steps.AWSConfig) (ec2iface.EC2API, error)) {
	steps.RegisterStep(StepNameCreateEC2Instance, NewCreateInstance(fn))
}

func NewCreateInstance(fn GetEC2Fn) *StepCreateInstance {
	return &StepCreateInstance{
		GetEC2: fn,
	}
}

func (s *StepCreateInstance) Run(ctx context.Context, w io.Writer, cfg *steps.Config) error {
	log := util.GetLogger(w)

	role := node.RoleMaster
	if !cfg.IsMaster {
		role = node.RoleNode
	}

	nodeName := util.MakeNodeName(cfg.ClusterName, cfg.TaskID, cfg.IsMaster)

	cfg.Node = node.Node{
		Name:     nodeName,
		TaskID:   cfg.TaskID,
		Region:   cfg.AWSConfig.Region,
		Role:     role,
		Provider: clouds.AWS,
		State:    node.StatePlanned,
	}

	// Update node state in cluster
	cfg.NodeChan() <- cfg.Node

	var secGroupID *string

	//Determining a sec group in AWS for EC2 instance to be spawned.
	if cfg.IsMaster {
		secGroupID = &cfg.AWSConfig.MastersSecurityGroupID
	} else {
		secGroupID = &cfg.AWSConfig.NodesSecurityGroupID
	}

	EC2, err := s.GetEC2(cfg.AWSConfig)
	if err != nil {
		logrus.Errorf("[%s] - failed to authorize in AWS: %v", s.Name(), err)
		return errors.Wrap(ErrAuthorization, err.Error())
	}

	amiID, err := s.FindAMI(ctx, w, EC2)
	if err != nil {
		logrus.Errorf("[%s] - failed to find AMI for Ubuntu: %v", s.Name(), err)
		return errors.Wrap(err, "failed to find AMI")
	}

	isEbs, err := strconv.ParseBool(cfg.AWSConfig.EbsOptimized)
	volumeSize, err := strconv.Atoi(cfg.AWSConfig.VolumeSize)
	hasPublicAddress, err := strconv.ParseBool(cfg.AWSConfig.HasPublicAddr)

	runInstanceInput := &ec2.RunInstancesInput{
		BlockDeviceMappings: []*ec2.BlockDeviceMapping{
			{
				DeviceName: aws.String("/dev/xvda"),
				Ebs: &ec2.EbsBlockDevice{
					DeleteOnTermination: aws.Bool(true),
					VolumeType:          aws.String("gp2"),
					VolumeSize:          aws.Int64(int64(volumeSize)),
				},
			},
		},
		Placement: &ec2.Placement{<commit_msg>Disable EBS optimized by default (#909)<commit_after>package amazon

import (
	"context"
	"fmt"
	"io"
	"strconv"
	"strings"
	"time"

	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/service/ec2"
	"github.com/aws/aws-sdk-go/service/ec2/ec2iface"
	"github.com/pkg/errors"
	"github.com/sirupsen/logrus"
	"github.com/supergiant/supergiant/pkg/clouds"
	"github.com/supergiant/supergiant/pkg/node"
	"github.com/supergiant/supergiant/pkg/util"
	"github.com/supergiant/supergiant/pkg/workflows/steps"
)

const (
	StepNameCreateEC2Instance = "aws_create_instance"
	IPAttempts                = 5
	SleepSecondsPerAttempt    = 6
	timeout                   = time.Second * 10
)

type StepCreateInstance struct {
	GetEC2 GetEC2Fn
}

//InitCreateMachine adds the step to the registry
func InitCreateMachine(fn func(steps.AWSConfig) (ec2iface.EC2API, error)) {
	steps.RegisterStep(StepNameCreateEC2Instance, NewCreateInstance(fn))
}

func NewCreateInstance(fn GetEC2Fn) *StepCreateInstance {
	return &StepCreateInstance{
		GetEC2: fn,
	}
}

func (s *StepCreateInstance) Run(ctx context.Context, w io.Writer, cfg *steps.Config) error {
	log := util.GetLogger(w)

	role := node.RoleMaster
	if !cfg.IsMaster {
		role = node.RoleNode
	}

	nodeName := util.MakeNodeName(cfg.ClusterName, cfg.TaskID, cfg.IsMaster)

	cfg.Node = node.Node{
		Name:     nodeName,
		TaskID:   cfg.TaskID,
		Region:   cfg.AWSConfig.Region,
		Role:     role,
		Provider: clouds.AWS,
		State:    node.StatePlanned,
	}

	// Update node state in cluster
	cfg.NodeChan() <- cfg.Node

	var secGroupID *string

	//Determining a sec group in AWS for EC2 instance to be spawned.
	if cfg.IsMaster {
		secGroupID = &cfg.AWSConfig.MastersSecurityGroupID
	} else {
		secGroupID = &cfg.AWSConfig.NodesSecurityGroupID
	}

	EC2, err := s.GetEC2(cfg.AWSConfig)
	if err != nil {
		logrus.Errorf("[%s] - failed to authorize in AWS: %v", s.Name(), err)
		return errors.Wrap(ErrAuthorization, err.Error())
	}

	amiID, err := s.FindAMI(ctx, w, EC2)
	if err != nil {
		logrus.Errorf("[%s] - failed to find AMI for Ubuntu: %v", s.Name(), err)
		return errors.Wrap(err, "failed to find AMI")
	}

	isEbs := false
	volumeSize, err := strconv.Atoi(cfg.AWSConfig.VolumeSize)
	hasPublicAddress, err := strconv.ParseBool(cfg.AWSConfig.HasPublicAddr)

	runInstanceInput := &ec2.RunInstancesInput{
		BlockDeviceMappings: []*ec2.BlockDeviceMapping{
			{
				DeviceName: aws.String("/dev/xvda"),
				Ebs: &ec2.EbsBlockDevice{
					DeleteOnTermination: aws.Bool(true),
					VolumeType:          aws.String("gp2"),
					VolumeSize:          aws.Int64(int64(volumeSize)),
				},
			},
		},
		Placement: &ec2.Placement{
{'fraction_non_alphanumeric': 0.13226716839134525, 'fraction_numerical': 0.01110065851364064, 'mean_word_length': 3.7978339350180503, 'pattern_counts': {'":': 0, '<': 5, '<?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': '1359773', 'n_tokens_mistral': 2118, 'n_tokens_neox': 1984, 'n_words': 377}