From bcded11018597594ef4941f370f32221376759bc Mon Sep 17 00:00:00 2001 From: Manjunath PV Date: Thu, 1 Feb 2024 16:58:47 +0530 Subject: [PATCH] generate random uuid for prefix variable --- CHANGELOG.md | 4 ++++ covalent_ec2_plugin/assets/infra/main.tf | 19 ++++++++++++++----- .../assets/infra/networking.tf | 6 +++--- covalent_ec2_plugin/assets/infra/variables.tf | 8 ++++---- covalent_ec2_plugin/ec2.py | 5 +++-- 5 files changed, 28 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 422e133..d0fda19 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [UNRELEASED] +## Added + +- Generate random UUID for prefix variable to avoid name conflicting deployed resources + ## Changed - Moved the terraform files into **covalent_ec2_plugin/assets/infra** folder. diff --git a/covalent_ec2_plugin/assets/infra/main.tf b/covalent_ec2_plugin/assets/infra/main.tf index 5affced..68a4e23 100644 --- a/covalent_ec2_plugin/assets/infra/main.tf +++ b/covalent_ec2_plugin/assets/infra/main.tf @@ -14,15 +14,24 @@ # See the License for the specific language governing permissions and # limitations under the License. -provider "aws" { - profile = var.aws_profile - region = var.aws_region +provider "aws" {} + +data "aws_region" "current" {} + +resource "random_string" "default_prefix" { + length = 9 + upper = false + special = false } locals { - username = "ubuntu" + prefix = var.prefix == "" ? random_string.default_prefix.result : var.prefix + subnet_id = var.subnet_id == "" ? aws_default_subnet.default.id : var.subnet_id + region = var.region == "" ? data.aws_region.current.name : var.region + username = "ubuntu" } + data "aws_ami" "ubuntu" { most_recent = true @@ -53,7 +62,7 @@ resource "aws_instance" "covalent_ec2_instance" { } tags = { - "Name" = var.name + "Name" = "covalent-ec2-${local.prefix}" } } diff --git a/covalent_ec2_plugin/assets/infra/networking.tf b/covalent_ec2_plugin/assets/infra/networking.tf index ba07249..17c84c5 100644 --- a/covalent_ec2_plugin/assets/infra/networking.tf +++ b/covalent_ec2_plugin/assets/infra/networking.tf @@ -19,10 +19,10 @@ module "vpc" { create_vpc = (var.vpc_id == "") - name = "${var.name}-vpc" + name = "covalent-${local.prefix}-vpc" cidr = var.vpc_cidr - azs = ["${var.aws_region}a"] + azs = ["${var.region}a"] public_subnets = [ cidrsubnet(var.vpc_cidr, 0, 0) @@ -40,7 +40,7 @@ data "http" "myip" { } resource "aws_security_group" "covalent_firewall" { - name = "${var.name}-firewall" + name = "covalent-${var.prefix}-firewall" description = "Allow traffic to Covalent server" vpc_id = var.vpc_id == "" ? module.vpc.vpc_id : var.vpc_id diff --git a/covalent_ec2_plugin/assets/infra/variables.tf b/covalent_ec2_plugin/assets/infra/variables.tf index 2b42afb..830f576 100644 --- a/covalent_ec2_plugin/assets/infra/variables.tf +++ b/covalent_ec2_plugin/assets/infra/variables.tf @@ -14,13 +14,13 @@ # See the License for the specific language governing permissions and # limitations under the License. -variable "name" { - default = "covalent-executor-ec2" +variable "prefix" { + default = "" description = "Name used to prefix AWS resources" } -variable "aws_region" { - default = "us-east-1" +variable "region" { + default = "" description = "Region where resources for the EC2 plugin are deployed" } diff --git a/covalent_ec2_plugin/ec2.py b/covalent_ec2_plugin/ec2.py index cb45402..6a18427 100644 --- a/covalent_ec2_plugin/ec2.py +++ b/covalent_ec2_plugin/ec2.py @@ -21,7 +21,7 @@ import os import subprocess from pathlib import Path -from typing import Dict, List +from typing import Dict, List, Optional import boto3 from covalent._shared_files import logger @@ -65,6 +65,7 @@ class ExecutorPluginDefaults(BaseModel): class ExecutorInfraDefaults(BaseModel): + prefix: Optional[str] = "" profile: str = "" credentials_file: str = "" region: str = "" @@ -270,7 +271,7 @@ async def setup(self, task_metadata: Dict) -> None: self.infra_vars = [ f"-var=aws_region={region}", f"-var=aws_profile={profile}", - f"-var=name=covalent-ec2-task-{task_metadata['dispatch_id']}-{task_metadata['node_id']}", + f"-var=prefix=covalent-ec2-task-{task_metadata['dispatch_id']}-{task_metadata['node_id']}", f"-var=instance_type={self.instance_type}", f"-var=disk_size={self.volume_size}", f"-var=key_file={self.ssh_key_file}",