Skip to content

Commit

Permalink
AWS better check and election (#143)
Browse files Browse the repository at this point in the history
Co-authored-by: DavidGOrtega <g.ortega.david@gmail.com>
Co-authored-by: 0x2b3bfa0 <0x2b3bfa0+git@googlemail.com>
  • Loading branch information
DavidGOrtega and 0x2b3bfa0 authored Jun 18, 2021
1 parent 0c60583 commit d7e00bb
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 8 deletions.
33 changes: 28 additions & 5 deletions iterative/aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
instanceName := d.Get("name").(string)
pairName := d.Id()
hddSize := d.Get("instance_hdd_size").(int)
region := getRegion(d.Get("region").(string))
region := GetRegion(d.Get("region").(string))
instanceType := getInstanceType(d.Get("instance_type").(string), d.Get("instance_gpu").(string))
ami := d.Get("image").(string)
keyPublic := d.Get("ssh_public").(string)
Expand All @@ -38,7 +38,6 @@ func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
}

svc, err := awsClient(region)

if err != nil {
return decodeAWSError(region, err)
}
Expand Down Expand Up @@ -70,6 +69,10 @@ func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
Name: aws.String("architecture"),
Values: []*string{aws.String("x86_64")},
},
{
Name: aws.String("owner-id"),
Values: []*string{aws.String("099720109477")},
},
},
})

Expand Down Expand Up @@ -101,11 +104,19 @@ func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
securityGroup = "iterative"

vpcsDesc, _ := svc.DescribeVpcs(&ec2.DescribeVpcsInput{})

if len(vpcsDesc.Vpcs) == 0 {
return errors.New("no VPCs found")
}
vpcID = *vpcsDesc.Vpcs[0].VpcId

for _, vpc := range vpcsDesc.Vpcs {
if *vpc.IsDefault {
vpcID = *vpc.VpcId
break
}
}

gpResult, err := svc.CreateSecurityGroup(&ec2.CreateSecurityGroupInput{
GroupName: aws.String(securityGroup),
Description: aws.String("Iterative security group"),
Expand Down Expand Up @@ -177,6 +188,16 @@ func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
if len(subDesc.Subnets) == 0 {
return errors.New("no subnets found")
}
var subnetID string
for _, subnet := range subDesc.Subnets {
if *subnet.AvailableIpAddressCount > 0 && *subnet.MapPublicIpOnLaunch {
subnetID = *subnet.SubnetId
break
}
}
if subnetID == "" {
return errors.New("No subnet found with public IPs available or able to create new public IPs on creation")
}

blockDeviceMappings := []*ec2.BlockDeviceMapping{
{
Expand All @@ -200,7 +221,7 @@ func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
KeyName: aws.String(pairName),
InstanceType: aws.String(instanceType),
SecurityGroupIds: []*string{aws.String(sgID)},
SubnetId: aws.String(*subDesc.Subnets[0].SubnetId),
SubnetId: aws.String(subnetID),
BlockDeviceMappings: blockDeviceMappings,
},
InstanceCount: aws.Int64(1),
Expand Down Expand Up @@ -287,14 +308,15 @@ func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
instanceDesc := descResult.Reservations[0].Instances[0]
d.Set("instance_ip", instanceDesc.PublicIpAddress)
d.Set("instance_launch_time", instanceDesc.LaunchTime.Format(time.RFC3339))
d.Set("image", *imagesRes.Images[0].Name)

return nil
}

//ResourceMachineDelete deletes AWS instance
func ResourceMachineDelete(ctx context.Context, d *schema.ResourceData, m interface{}) error {
id := aws.String(d.Id())
region := getRegion(d.Get("region").(string))
region := GetRegion(d.Get("region").(string))

svc, err := awsClient(region)
if err != nil {
Expand Down Expand Up @@ -359,7 +381,8 @@ var ImageRegions = []string{
"sa-east-1",
}

func getRegion(region string) string {
//GetRegion maps region to real cloud regions
func GetRegion(region string) string {
instanceRegions := make(map[string]string)
instanceRegions["us-east"] = "us-east-1"
instanceRegions["us-west"] = "us-west-1"
Expand Down
5 changes: 3 additions & 2 deletions iterative/azure/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf

username := "ubuntu"
customData := d.Get("startup_script").(string)
region := getRegion(d.Get("region").(string))
region := GetRegion(d.Get("region").(string))
instanceType := getInstanceType(d.Get("instance_type").(string), d.Get("instance_gpu").(string))
keyPublic := d.Get("ssh_public").(string)
hddSize := int32(d.Get("instance_hdd_size").(int))
Expand Down Expand Up @@ -354,7 +354,8 @@ func getVMClient(subscriptionID string) (compute.VirtualMachinesClient, error) {
//ImageRegions provider available image regions
var ImageRegions = []string{}

func getRegion(region string) string {
//GetRegion maps region to real cloud regions
func GetRegion(region string) string {
instanceRegions := make(map[string]string)
instanceRegions["us-east"] = "eastus"
instanceRegions["us-west"] = "westus2"
Expand Down
10 changes: 9 additions & 1 deletion iterative/resource_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -437,8 +437,16 @@ func isAMIAvailable(cloud string, region string) bool {
regions = azure.ImageRegions
}

var cloudRegion string
switch cloud {
case "aws":
cloudRegion = aws.GetRegion(region)
case "azure":
cloudRegion = azure.GetRegion(region)
}

for _, item := range regions {
if item == region {
if item == cloudRegion {
return true
}
}
Expand Down
18 changes: 18 additions & 0 deletions iterative/resource_runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ func TestScript(t *testing.T) {
assert.NotContains(t, script, "sudo ubuntu-drivers autoinstall")
})

t.Run("AWS known generic region should not add the NVIDA drivers", func(t *testing.T) {
data := make(map[string]interface{})
data["ami"] = isAMIAvailable("aws", "us-west")

script, err := renderScript(data)
assert.Nil(t, err)
assert.NotContains(t, script, "sudo ubuntu-drivers autoinstall")
})

t.Run("AWS unknown region should add the NVIDA drivers", func(t *testing.T) {
data := make(map[string]interface{})
data["ami"] = isAMIAvailable("aws", "us-east-99")
Expand All @@ -39,6 +48,15 @@ func TestScript(t *testing.T) {
assert.Contains(t, script, "sudo ubuntu-drivers autoinstall")
})

t.Run("Azure known generic region should add the NVIDA drivers", func(t *testing.T) {
data := make(map[string]interface{})
data["ami"] = isAMIAvailable("azure", "us-west")

script, err := renderScript(data)
assert.Nil(t, err)
assert.Contains(t, script, "sudo ubuntu-drivers autoinstall")
})

t.Run("Azure unknown region should add the NVIDA drivers", func(t *testing.T) {
data := make(map[string]interface{})
data["ami"] = isAMIAvailable("azure", "us-east-99")
Expand Down

0 comments on commit d7e00bb

Please sign in to comment.