Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimo automation #9

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions .terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions Module/EC2/config/optimo.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[Unit]
Description=Optimo Service
After=docker.service
Requires=docker.service

[Service]
Restart=always
ExecStartPre=-/usr/bin/docker stop %n
ExecStartPre=-/usr/bin/docker rm %n
ExecStop=-/usr/bin/docker stop %n
ExecStart=/usr/bin/docker run \
--name=%n \
-e OPTIMO_BASIC_AUTH_PASSWORD= \
valohai/optimo:20231130


[Install]
WantedBy=multi-user.target
6 changes: 3 additions & 3 deletions Module/EC2/config/roi.config
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ ENABLE_SAML_LOGIN=false
ALLOW_SAML_BINDING_TO_EXISTING_USER=false
SAML_AUTO_ADD_TO_ORGANIZATION=true

# OPTIMO_ROOT_URL=
# OPTIMO_BASIC_AUTH_USERNAME=
# OPTIMO_BASIC_AUTH_PASSWORD=
OPTIMO_ROOT_URL=
OPTIMO_BASIC_AUTH_USERNAME=user
OPTIMO_BASIC_AUTH_PASSWORD=

# ENABLE_GITHUB_INTEGRATION=false
# GITHUB_SITE_URL=https://github.com
Expand Down
31 changes: 29 additions & 2 deletions Module/EC2/config/user_data.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ sudo systemctl stop roi
export ROI_AUTO_MIGRATE=true

echo "${file("${module_path}/config/roi.config")}" > /etc/roi.config
echo "${file("${module_path}/config/optimo.service")}" > /etc/systemd/system/optimo.service

export REPO_PRIVATE_KEY=`aws ssm get-parameter --name ${repo_private_key} --with-decryption | sed -n 's|.*"Value": *"\([^"]*\)".*|\1|p'`
export SECRET_KEY=`aws ssm get-parameter --name ${secret_key} --with-decryption | sed -n 's|.*"Value": *"\([^"]*\)".*|\1|p'`
Expand All @@ -24,6 +25,32 @@ sed -i "s|REPO_PRIVATE_KEY_SECRET=|REPO_PRIVATE_KEY_SECRET=$REPO_PRIVATE_KEY|" /
sed -i "s|SECRET_KEY=|SECRET_KEY=$SECRET_KEY|" /etc/roi.config
sed -i "s|STATS_JWT_KEY=|STATS_JWT_KEY=$JWT_KEY|" /etc/roi.config

#docker pull valohai/optimo:20231130
#echo "Waiting for Docker image to be pulled..."

echo "Starting the Optimo service"
sudo systemctl start optimo
sudo systemctl enable optimo

OPTIMO_BASIC_AUTH_PASSWORD=$(echo $RANDOM | md5sum | cut -d' ' -f1)

for i in {1..5}; do
if [ "$(sudo docker ps -q -f name=optimo.service)" ]; then
OPTIMO_ROOT_URL=$(sudo docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' optimo.service) && break
fi
echo "Waiting for the Docker container to start and get an IP address..."
sleep 3
done

if [ -z "$OPTIMO_ROOT_URL" ]; then
echo "Warning: Failed to get Docker container IP after 5 attempts."
else
sed -i "s|OPTIMO_ROOT_URL=|OPTIMO_ROOT_URL=http://$OPTIMO_ROOT_URL:80/|" /etc/roi.config
echo "The Optimo service is running at: http://$OPTIMO_ROOT_URL:80/"
fi

sed -i "s|OPTIMO_BASIC_AUTH_PASSWORD=|OPTIMO_BASIC_AUTH_PASSWORD=$OPTIMO_BASIC_AUTH_PASSWORD|" /etc/roi.config

sudo systemctl enable roi-setup
sudo systemctl start roi-setup
sudo systemctl enable roi
Expand All @@ -47,8 +74,8 @@ echo " ${aws_instance_types}" >> /home/ubuntu/prep_template.yaml
echo " ${aws_spot_instance_types}" >> /home/ubuntu/prep_template.yaml

set +xeuo pipefail
sudo docker exec roi.service python manage.py shell -c "from roi.models import Organization, User;import os;org = Organization.objects.create_user(username='${organization}', email='[email protected]', is_organization=True)"
export ORG_ID=$(sudo docker exec roi.service python manage.py shell -c "from roi.models import Organization, User;org = User.objects.get(username='${organization}');print(str(org.id))")
o docker exec roi.service python manage.py shell -c "from roi.models import Organization, User;import os;org = Organization.objects.create_user(username='${organization}', email='[email protected]', is_organization=True)"
export ORG_ID=$(sudo docsudker exec roi.service python manage.py shell -c "from roi.models import Organization, User;org = User.objects.get(username='${organization}');print(str(org.id))")
sed -i "s|valohai-env-owner-id: ''|valohai-env-owner-id: '$ORG_ID'|" /home/ubuntu/prep_template.yaml

su ubuntu -c "python3 -m prep --config-yaml /home/ubuntu/prep_template.yaml aws"
2 changes: 1 addition & 1 deletion Module/EC2/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,4 @@ variable "add_spot_instances" {
variable "aws_spot_instance_types" {
description = "A list of AWS spot instance types that should be created"
type = list(string)
}
}
2 changes: 1 addition & 1 deletion Module/Postgres/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ resource "aws_kms_key" "valohai_db_kms_key" {

resource "aws_kms_alias" "valohai_kms_alias" {
target_key_id = aws_kms_key.valohai_db_kms_key.key_id
name = "alias/dev-valohai-kmsa-valohaidb"
name = "alias/dev-valohai-jordan-kmsa-valohaidb"
}

resource "random_password" "password" {
Expand Down
2 changes: 1 addition & 1 deletion main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,4 @@ module "ASG-spots" {
depends_on = [
module.IAM_Workers, module.EC2
]
}
}
Binary file added valohai-init
Binary file not shown.
2 changes: 1 addition & 1 deletion variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,4 @@ variable "aws_spot_instance_types" {
description = "List of AWS spot instance types that should be created"
type = list(string)
default = []
}
}
32 changes: 16 additions & 16 deletions variables.tfvars
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
aws_profile = "valohai-sandbox"
aws_profile = "valohai-jordan"
aws_region = "us-east-1"
aws_account_id = "450886142693"
ec2_key = ".valohai-key.pub" # path to your .pub key
aws_account_id = "767397824220"
ec2_key = "/Users/jordancoaten/.valohai-key.pub" # path to your .pub key
lb_subnet_ids = [ # Subnets for the ELB
"subnet-09fa71506c5274edf",
"subnet-0ef2e899c53ce8e3f"
"subnet-0f97f9a6a41a2a6f0",
"subnet-0ab97968d7c45b2fc"
]
roi_subnet_id = "subnet-0876140165953bf66" # Subnet for the Valohai app
roi_subnet_id = "subnet-0c46f06a1af307492" # Subnet for the Valohai app
db_subnet_ids = [
"subnet-0876140165953bf66",
"subnet-095202b0a4f855773"
"subnet-0c46f06a1af307492",
"subnet-0a5e36b16a23758af"
]
worker_subnet_ids = [
"subnet-0876140165953bf66",
"subnet-095202b0a4f855773"
"subnet-0c46f06a1af307492",
"subnet-0a5e36b16a23758af"
]

vpc_id = "vpc-066122736a3c21fc2"
vpc_id = "vpc-078bf49eff57047b9"
environment_name = "Valohai"
s3_bucket_name = "valohai-data"
s3_logs_name = "valohai-data-logs"
domain = "https://test.valohai.com"
s3_bucket_name = "valohai-data-jordan-s3"
s3_logs_name = "valohai-data-logs-jordan"
domain = "http://dev-valohai-alb-valohai-1648300628.us-east-1.elb.amazonaws.com"
certificate_arn = ""
organization = "TestOrg"
ami_id = "ami-012e015931dca494c" # AMI id from your Valohai contact
ami_id = "ami-0dc651d908245c9d5" # AMI id from your Valohai contact
aws_instance_types = [
"t3.small",
"c5.2xlarge",
"p3.2xlarge"
]
add_spot_instances = false
aws_spot_instance_types = []
aws_spot_instance_types = []