Skip to content

Commit

Permalink
refactor to set squid instance type in 1 place only
Browse files Browse the repository at this point in the history
  • Loading branch information
paulineribeyre committed Dec 11, 2024
1 parent 15114ce commit 5542a42
Showing 1 changed file with 14 additions and 17 deletions.
31 changes: 14 additions & 17 deletions hatchery/nextflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,12 @@ func createNextflowResources(userName string, nextflowGlobalConfig NextflowGloba
}

// create the VPC if it doesn't exist
vpcid, subnetids, err := setupVpcAndSquid(ec2Svc, userName, hostname, nextflowConfig.InstanceType)
// launch squid
// TODO: read the squid instance type from the hatchery config (would need to change
// `launchSquidInstance` function to update the instance type if the instance already
// exists) (MIDRC-751)
squidInstanceType := "t2.micro"
vpcid, subnetids, err := setupVpcAndSquid(ec2Svc, userName, hostname, nextflowConfig.InstanceType, squidInstanceType)
if err != nil {
Config.Logger.Printf("Unable to setup VPC: %v", err)
return "", "", err
Expand Down Expand Up @@ -456,7 +461,7 @@ var getNextflowAwsSettings = func(sess *session.Session, payModel *PayModel, use
}

// Create VPC for aws batch compute environment
func setupVpcAndSquid(ec2Svc *ec2.EC2, userName string, hostname string, instanceType string) (*string, *[]string, error) {
func setupVpcAndSquid(ec2Svc *ec2.EC2, userName string, hostname string, computeEnvInstanceType string, squidInstanceType string) (*string, *[]string, error) {
// TODO: make base CIDR configurable? (MIDRC-747)
cidrstring := "192.168.0.0/16"
_, IPNet, _ := net.ParseCIDR(cidrstring)
Expand Down Expand Up @@ -522,7 +527,7 @@ func setupVpcAndSquid(ec2Svc *ec2.EC2, userName string, hostname string, instanc
// create subnets
for i, subnet := range subnets {
subnetName := fmt.Sprintf("%s-nf-subnet-%s-%d", hostname, userName, i)
subnetId, err := setupSubnet(subnetName, subnet, vpcid, ec2Svc, instanceType)
subnetId, err := setupSubnet(subnetName, subnet, vpcid, ec2Svc, computeEnvInstanceType)
if err != nil {
return nil, nil, err
}
Expand All @@ -548,7 +553,7 @@ func setupVpcAndSquid(ec2Svc *ec2.EC2, userName string, hostname string, instanc
}

// setup Squid
fwSubnetId, err := setupSquid(hostname, userName, cidrstring, ec2Svc, vpcid, igw, fwRouteTableId, routeTableId)
fwSubnetId, err := setupSquid(hostname, userName, cidrstring, ec2Svc, vpcid, igw, fwRouteTableId, routeTableId, squidInstanceType)
if err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -580,7 +585,7 @@ func ensureLaunchTemplate(ec2Svc *ec2.EC2, userName string, hostname string, job
Config.Logger.Printf("Debug: Launch template '%s' does not exist, creating it", launchTemplateName)
launchTemplate, err := ec2Svc.CreateLaunchTemplate(&ec2.CreateLaunchTemplateInput{
LaunchTemplateName: aws.String(launchTemplateName),
LaunchTemplateData: &ec2.RequestLaunchTemplateData{
LaunchTemplateData: &ec2.RequestLaunchTemplateData{ // if changed, need to update launch template and compute env
UserData: aws.String(userData),
},
})
Expand Down Expand Up @@ -1004,7 +1009,7 @@ func createS3bucket(nextflowGlobalConfig NextflowGlobalConfig, s3Svc *s3.S3, kms
}

// Function to set up squid and subnets for squid
func setupSquid(hostname string, userName string, cidrstring string, ec2svc *ec2.EC2, vpcid string, igw *string, fwRouteTableId *string, routeTableId *string) (*string, error) {
func setupSquid(hostname string, userName string, cidrstring string, ec2svc *ec2.EC2, vpcid string, igw *string, fwRouteTableId *string, routeTableId *string, instanceType string) (*string, error) {
_, IPNet, _ := net.ParseCIDR(cidrstring)
subnet, err := cidr.Subnet(IPNet, 2, 3)
if err != nil {
Expand All @@ -1015,9 +1020,7 @@ func setupSquid(hostname string, userName string, cidrstring string, ec2svc *ec2
// create subnet
subnetName := fmt.Sprintf("%s-nf-subnet-fw-%s", hostname, userName)
Config.Logger.Printf("Debug: Creating subnet '%s' with name '%s'", subnet, subnetName)

// TODO: read instance type from config. (MIDRC-751)
subnetId, err := setupSubnet(subnetName, subnetString, vpcid, ec2svc, "t2.micro")
subnetId, err := setupSubnet(subnetName, subnetString, vpcid, ec2svc, instanceType)
if err != nil {
return nil, err
}
Expand All @@ -1043,8 +1046,7 @@ func setupSquid(hostname string, userName string, cidrstring string, ec2svc *ec2
}
Config.Logger.Printf("Debug: Associated route table '%s' to subnet '%s'", *fwRouteTableId, *subnetId)

// launch squid
squidInstanceId, err := launchSquidInstance(hostname, userName, ec2svc, subnetId, vpcid, subnetString)
squidInstanceId, err := launchSquidInstance(hostname, userName, ec2svc, subnetId, vpcid, subnetString, instanceType)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -1257,7 +1259,7 @@ func associateRouteTablesToSubnets(ec2svc *ec2.EC2, subnets []string, routeTable
return nil
}

func launchSquidInstance(hostname string, userName string, ec2svc *ec2.EC2, subnetId *string, vpcId string, subnet string) (*string, error) {
func launchSquidInstance(hostname string, userName string, ec2svc *ec2.EC2, subnetId *string, vpcId string, subnet string, instanceType string) (*string, error) {
instanceName := fmt.Sprintf("%s-nf-squid-%s", hostname, userName)

// check if instance already exists, if it does start it
Expand Down Expand Up @@ -1370,11 +1372,6 @@ $(command -v docker) run --name squid --restart=always --network=host -d \
return nil, err
}

// instance type
// TODO: we could make this configurable via hatchery config (would need to change this
// function to update the instance type if the instance already exists) (MIDRC-751)
instanceType := "t2.micro"

// Launch EC2 instance
squid, err := ec2svc.RunInstances(&ec2.RunInstancesInput{
ImageId: amiId,
Expand Down

0 comments on commit 5542a42

Please sign in to comment.