Skip to content
This repository has been archived by the owner on Jan 23, 2025. It is now read-only.

Commit

Permalink
Merge pull request #17 from silinternational/develop
Browse files Browse the repository at this point in the history
fix GetInstanceTypeForAsg to work with Launch Template
  • Loading branch information
briskt authored Jun 5, 2023
2 parents 95ba262 + be1e621 commit de2e356
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 14 deletions.
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Ensure this version tracks with go.mod and codeship/Dockerfile
FROM golang:1.19 as builder

# Ensure go build env is correct
Expand All @@ -14,7 +15,7 @@ WORKDIR /src
RUN go get ./...
RUN go build -ldflags="-s -w" -o awsops

FROM alpine:latest
FROM alpine:3
RUN apk update && \
apk add ca-certificates && \
rm -rf /var/cache/apk/*
Expand Down
3 changes: 2 additions & 1 deletion codeship/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
FROM golang:latest
# Ensure this version tracks with go.mod and /Dockerfile
FROM golang:1.19

RUN apt-get update && apt-get install -y awscli

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module github.com/silinternational/awsops

// Ensure this version tracks with /Dockerfile and codeship/Dockerfile
go 1.19

require (
Expand Down
76 changes: 64 additions & 12 deletions lib/asg.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package lib

import (
"fmt"
"math"
"os"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/autoscaling"
"github.com/aws/aws-sdk-go/service/ec2"
"math"
"os"
"time"
)

func GetAsgNameForEcsCluster(awsSess *session.Session, cluster string) string {
Expand Down Expand Up @@ -73,30 +74,81 @@ func GetInstanceListForAsg(awsSess *session.Session, asgName string) []*string {
return instanceIds
}

func GetInstanceTypeForAsg(awsSess *session.Session, asgName string) string {
svc := autoscaling.New(awsSess)

asg := GetAsg(awsSess, asgName)

func GetInstanceTypeFromLaunchConfiguration(awsSess *session.Session, launchConfigurationName string) string {
input := &autoscaling.DescribeLaunchConfigurationsInput{
LaunchConfigurationNames: []*string{
aws.String(*asg.LaunchConfigurationName),
aws.String(launchConfigurationName),
},
}

lc, err := svc.DescribeLaunchConfigurations(input)
lc, err := autoscaling.New(awsSess).DescribeLaunchConfigurations(input)
if err != nil {
fmt.Println("Unable to describe launch configuration, err: ", err.Error())
fmt.Println("Unable to describe launch configuration: ", err.Error())
os.Exit(1)
}

if len(lc.LaunchConfigurations) != 1 {
fmt.Println("DescribeLaunchConfigurations did not return expected number of results. Expected: 1, Actual: ", len(lc.LaunchConfigurations))
fmt.Println("Expected one Launch Configuration, received ", len(lc.LaunchConfigurations))
os.Exit(1)
}

return *lc.LaunchConfigurations[0].InstanceType
}

func GetInstanceTypeFromLaunchTemplate(awsSess *session.Session, launchTemplateName string) string {
input := &ec2.DescribeLaunchTemplatesInput{
LaunchTemplateNames: []*string{
aws.String(launchTemplateName),
},
}

ec2Client := ec2.New(awsSess)

lt, err := ec2Client.DescribeLaunchTemplates(input)
if err != nil {
fmt.Println("Unable to describe Launch Template, err: ", err.Error())
os.Exit(1)
}

if len(lt.LaunchTemplates) != 1 {
fmt.Println("Expected one Launch Template, found ", len(lt.LaunchTemplates))
os.Exit(1)
}

ltvInput := ec2.DescribeLaunchTemplateVersionsInput{
LaunchTemplateId: lt.LaunchTemplates[0].LaunchTemplateId,
Versions: []*string{aws.String("$Latest")},
}
ltv, err := ec2Client.DescribeLaunchTemplateVersions(&ltvInput)
if err != nil {
fmt.Println("Unable to describe Launch Template version, error: ", err.Error())
os.Exit(1)
}

if len(ltv.LaunchTemplateVersions) != 1 {
fmt.Println(`Expected one "$Latest" Launch Template version, received `, len(lt.LaunchTemplates))
os.Exit(1)
}

return *ltv.LaunchTemplateVersions[0].LaunchTemplateData.InstanceType
}

func GetInstanceTypeForAsg(awsSess *session.Session, asgName string) string {
asg := GetAsg(awsSess, asgName)

if asg.LaunchConfigurationName != nil {
return GetInstanceTypeFromLaunchConfiguration(awsSess, *asg.LaunchConfigurationName)
}

if asg.LaunchTemplate != nil {
return GetInstanceTypeFromLaunchTemplate(awsSess, *asg.LaunchTemplate.LaunchTemplateName)
}

fmt.Println("Unable to determine the ASG instance type. No Launch Template nor Launch Configuration is defined.")
os.Exit(1)
return ""
}

func HowManyServersNeededForAsg(serverType string, memory, cpu int64) int64 {
instanceSpecs, valid := InstanceTypes[serverType]
if !valid {
Expand Down

0 comments on commit de2e356

Please sign in to comment.