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

set HOME env variable; handle SENDGRID_API_KEY env variable #91

Merged
merged 1 commit into from
Jan 24, 2024
Merged
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
9 changes: 7 additions & 2 deletions src/utils/studies_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@

import httpx
from google.cloud import firestore
from google.cloud.firestore_v1 import AsyncDocumentReference, DocumentReference, FieldFilter
from google.cloud.firestore_v1 import AsyncDocumentReference, FieldFilter
from python_http_client.exceptions import HTTPError
from quart import current_app, g
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Email, Mail
from werkzeug.exceptions import BadRequest

from src.api_utils import APIException
from src.auth import get_service_account_headers
Expand Down Expand Up @@ -41,7 +42,11 @@ async def email(inviter: str, recipient: str, invitation_message: str, study_tit
:return: The status code of the email sending operation.
"""
doc_ref_dict: dict = (await current_app.config["DATABASE"].collection("meta").document("sendgrid").get()).to_dict()
sg = SendGridAPIClient(api_key=doc_ref_dict.get("api_key", ""))

api_key = os.getenv("SENDGRID_API_KEY") or doc_ref_dict.get("api_key")
if not api_key:
raise BadRequest("No SendGrid API key found")
sg = SendGridAPIClient(api_key=api_key)

html_content = email_template.substitute(
inviter=escape(inviter),
Expand Down
1 change: 1 addition & 0 deletions src/vm_scripts/startup-script.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ handle_error() {
}

trap handle_error ERR
export HOME=/root

sudo -s

Expand Down
1 change: 1 addition & 0 deletions src/vm_scripts/startup-script_user_cp0.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/bin/bash

sudo -s
export HOME=/root

if [[ -f startup_was_launched ]]; then exit 0; fi
touch startup_was_launched
Expand Down
33 changes: 17 additions & 16 deletions src/web/study.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,17 @@ async def restart_study() -> Response:
doc_ref = db.collection("studies").document(study_id)
doc_ref_dict: dict = (await doc_ref.get()).to_dict() or {}

for role, v in enumerate(doc_ref_dict["participants"]):
participant = doc_ref_dict["personal_parameters"][v]
if (gcp_project := participant.get("GCP_PROJECT").get("value")) != "":
google_cloud_compute = GoogleCloudCompute(study_id, gcp_project)
for instance in google_cloud_compute.list_instances():
if instance == format_instance_name(google_cloud_compute.study_id, str(role)):
google_cloud_compute.delete_instance(instance)

google_cloud_compute.delete_firewall("")
logger.info("Successfully Deleted gcp instances and firewalls")
if not constants.TERRA: # TODO: add equivalent for terra
for role, v in enumerate(doc_ref_dict["participants"]):
participant = doc_ref_dict["personal_parameters"][v]
if (gcp_project := participant.get("GCP_PROJECT").get("value")) != "":
google_cloud_compute = GoogleCloudCompute(study_id, gcp_project)
for instance in google_cloud_compute.list_instances():
if instance == format_instance_name(google_cloud_compute.study_id, str(role)):
google_cloud_compute.delete_instance(instance)

google_cloud_compute.delete_firewall("")
logger.info("Successfully Deleted gcp instances and firewalls")

for participant in doc_ref_dict["participants"]:
doc_ref_dict["status"][participant] = "ready to begin protocol" if participant == get_cp0_id() else ""
Expand Down Expand Up @@ -141,12 +142,12 @@ async def delete_study() -> Response:
doc_ref = db.collection("studies").document(study_id)
doc_ref_dict: dict = (await doc_ref.get()).to_dict() or {}

for participant in doc_ref_dict["personal_parameters"].values():
if (gcp_project := participant.get("GCP_PROJECT").get("value")) != "":
google_cloud_compute = GoogleCloudCompute(study_id, gcp_project)
google_cloud_compute.delete_everything()

logger.info("Successfully deleted GCP instances and other related resources")
if not constants.TERRA: # TODO: add equivalent for terra
for participant in doc_ref_dict["personal_parameters"].values():
if (gcp_project := participant.get("GCP_PROJECT").get("value")) != "":
google_cloud_compute = GoogleCloudCompute(study_id, gcp_project)
google_cloud_compute.delete_everything()
logger.info("Successfully deleted GCP instances and other related resources")

for participant in doc_ref_dict["personal_parameters"].values():
if (auth_key := participant.get("AUTH_KEY").get("value")) != "":
Expand Down
Loading