Skip to content

Commit

Permalink
generate random uuid for prefix variable
Browse files Browse the repository at this point in the history
  • Loading branch information
mpvgithub committed Feb 1, 2024
1 parent 666100b commit bcded11
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 14 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
19 changes: 14 additions & 5 deletions covalent_ec2_plugin/assets/infra/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -53,7 +62,7 @@ resource "aws_instance" "covalent_ec2_instance" {
}

tags = {
"Name" = var.name
"Name" = "covalent-ec2-${local.prefix}"
}
}

Expand Down
6 changes: 3 additions & 3 deletions covalent_ec2_plugin/assets/infra/networking.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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

Expand Down
8 changes: 4 additions & 4 deletions covalent_ec2_plugin/assets/infra/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

Expand Down
5 changes: 3 additions & 2 deletions covalent_ec2_plugin/ec2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -65,6 +65,7 @@ class ExecutorPluginDefaults(BaseModel):


class ExecutorInfraDefaults(BaseModel):
prefix: Optional[str] = ""
profile: str = ""
credentials_file: str = ""
region: str = ""
Expand Down Expand Up @@ -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}",
Expand Down

0 comments on commit bcded11

Please sign in to comment.