Skip to content

Commit

Permalink
Fixes for installing conda environment on EC2 (#51)
Browse files Browse the repository at this point in the history
* fix to download conda installation file from internet

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* changes for MANIFEST file

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* minor conflict change

* reverted minor changes

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
mpvgithub and pre-commit-ci[bot] authored Jan 10, 2024
1 parent 14114f0 commit 666100b
Show file tree
Hide file tree
Showing 12 changed files with 86 additions and 8 deletions.
7 changes: 5 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -118,5 +118,8 @@ ipython_config.py
*.ipynb

# Terraform
covalent_ec2_plugin/infra/.terraform.lock.hcl
covalent_ec2_plugin/infra/.terraform
**/.terraform/**
**/*.tfstate*
**/.terraform.lock.hcl
**/*.tfvars
**/*.plan
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,19 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [UNRELEASED]

## Changed

- Moved the terraform files into **covalent_ec2_plugin/assets/infra** folder.
- Minor modifications on the plugin core to handle the above folder changes.

## Fixed

- Fixed security group, enabling ec2 instances to connect to internet and download conda installation file.

## Added

- Added tftpl file to project configuration value

## [0.13.2] - 2023-11-01

## Changed
Expand Down
4 changes: 3 additions & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
include VERSION
include requirements.txt
recursive-include covalent_ec2_plugin/infra/ *
include covalent_ec2_plugin/assets/infra/*
exclude covalent_ec2_plugin/assets/infra/*.swp
include covalent_ec2_plugin/assets/infra/*.tf
File renamed without changes.
8 changes: 8 additions & 0 deletions covalent_ec2_plugin/assets/infra/ec2.conf.tftpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[ec2]
credentials = ${credentials}
profile = ${profile}
region = ${region}
key_name = ${key_name}
volume_size = ${volume_size}
vpc = ${vpc}
subnet = ${subnet}
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ resource "aws_instance" "covalent_ec2_instance" {
subnet_id = var.vpc_id == "" ? module.vpc.public_subnets[0] : var.subnet_id
associate_public_ip_address = true

key_name = var.key_name # Name of a valid key pair
monitoring = true
key_name = var.key_name # Name of a valid key pair
monitoring = true

root_block_device {
volume_type = "gp2"
Expand Down Expand Up @@ -92,3 +92,22 @@ resource "null_resource" "deps_install" {
host = aws_instance.covalent_ec2_instance.public_ip
}
}

data "template_file" "executor_config" {
template = file("${path.module}/ec2.conf.tftpl")

vars = {
credentials = var.aws_credentials
profile = var.aws_profile
region = var.aws_region
key_name = var.key_name
volume_size = var.disk_size
vpc = var.vpc_id == "" ? module.vpc.vpc_id : var.vpc_id
subnet = var.vpc_id == "" ? module.vpc.public_subnets[0] : var.subnet_id
}
}

resource "local_file" "executor_config" {
content = data.template_file.executor_config.rendered
filename = "${path.module}/ec2.conf"
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,15 @@ resource "aws_security_group" "covalent_firewall" {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["${chomp(data.http.myip.body)}/32"]
cidr_blocks = ["0.0.0.0/0"]
}

ingress {
description = "Allow HTTPS Access For Conda Installation"
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}

egress {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
29 changes: 27 additions & 2 deletions covalent_ec2_plugin/ec2.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from covalent_aws_plugins import AWSExecutor
from covalent_ssh_plugin.ssh import _EXECUTOR_PLUGIN_DEFAULTS as _SSH_EXECUTOR_PLUGIN_DEFAULTS
from covalent_ssh_plugin.ssh import SSHExecutor
from pydantic import BaseModel

executor_plugin_name = "EC2Executor"

Expand All @@ -50,6 +51,31 @@
}
)


class ExecutorPluginDefaults(BaseModel):
profile: str = ""
credentials_file: str = ""
region: str = ""
instance_type: str = "t2.micro"
volume_size: str = "8"
vpc: str = ""
subnet: str = ""
key_name: str = ""
conda_env: str = "covalent"


class ExecutorInfraDefaults(BaseModel):
profile: str = ""
credentials_file: str = ""
region: str = ""
instance_type: str = "t2.micro"
volume_size: str = "8"
vpc: str = ""
subnet: str = ""
key_name: str = ""
conda_env: str = "covalent"


EC2_KEYPAIR_NAME = "covalent-ec2-executor-keypair"
EC2_SSH_DIR = "~/.ssh/covalent"

Expand Down Expand Up @@ -77,7 +103,7 @@ class EC2Executor(SSHExecutor, AWSExecutor):
it can also include the extras if needed as "[qiskit, braket]==0.220.0.post2"
"""

_TF_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), "infra"))
_TF_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), "assets", "infra"))

def __init__(
self,
Expand Down Expand Up @@ -279,7 +305,6 @@ async def teardown(self, task_metadata: Dict) -> None:
"""
Invokes Terraform to terminate the instance and teardown supporting resources
"""

state_file = self._get_tf_statefile_path(task_metadata)

if not os.path.exists(state_file):
Expand Down

0 comments on commit 666100b

Please sign in to comment.