-
-
Notifications
You must be signed in to change notification settings - Fork 150
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
Upload recording feature #787
base: main
Are you sure you want to change the base?
Changes from 3 commits
cc75fdf
68a973f
dbf76be
7fee87d
cd33510
23238a0
3554bb4
489d28b
da83b18
f673cb8
8e4bf17
99ea0f3
4a5b80e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
"""Entrypoint to deploy the uploader to AWS Lambda.""" | ||
|
||
import os | ||
import pathlib | ||
import re | ||
import subprocess | ||
|
||
from loguru import logger | ||
|
@@ -10,31 +12,64 @@ | |
CURRENT_DIR = pathlib.Path(__file__).parent | ||
|
||
|
||
def main(region_name: str = "us-east-1", guided: bool = True) -> None: | ||
def main(region_name: str = "us-east-1", destroy: bool = False) -> None: | ||
"""Deploy the uploader to AWS Lambda. | ||
|
||
Args: | ||
region_name (str): The AWS region to deploy the Lambda function to. | ||
guided (bool): Whether to use the guided SAM deployment. | ||
destroy (bool): Whether to delete the Lambda function. | ||
""" | ||
s3 = boto3.client( | ||
"s3", | ||
region_name=region_name, | ||
endpoint_url=f"https://s3.{region_name}.amazonaws.com", | ||
) | ||
bucket = "openadapt" | ||
|
||
s3.create_bucket( | ||
ACL="private", | ||
Bucket=bucket, | ||
) | ||
|
||
# deploy the code to AWS Lambda | ||
commands = ["sam", "deploy"] | ||
if guided: | ||
commands.append("--guided") | ||
subprocess.run(commands, cwd=CURRENT_DIR, check=True) | ||
logger.info("Lambda function deployed successfully.") | ||
# check if aws credentials are set | ||
if os.getenv("AWS_ACCESS_KEY_ID") is None: | ||
raise ValueError("AWS_ACCESS_KEY_ID is not set") | ||
if os.getenv("AWS_SECRET_ACCESS_KEY") is None: | ||
raise ValueError("AWS_SECRET_ACCESS_KEY is not set") | ||
if destroy: | ||
commands = ["sam", "delete", "--no-prompts"] | ||
else: | ||
s3 = boto3.client( | ||
"s3", | ||
region_name=region_name, | ||
endpoint_url=f"https://s3.{region_name}.amazonaws.com", | ||
) | ||
bucket = "openadapt" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you think about defining this is config.py? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The same reason as above, plus this is hardcoded because this script will be run once in a while (only when the lambda function is changed). And ideally we won't be changing bucket names between runs. |
||
|
||
s3.create_bucket( | ||
ACL="private", | ||
Bucket=bucket, | ||
) | ||
commands = ["sam", "deploy", "--no-fail-on-empty-changeset"] | ||
try: | ||
std_kwargs = {} | ||
if not destroy: | ||
std_kwargs["stderr"] = subprocess.PIPE | ||
std_kwargs["stdout"] = subprocess.PIPE | ||
ret = subprocess.run( | ||
commands, cwd=CURRENT_DIR, check=True, shell=True, **std_kwargs | ||
) | ||
if destroy: | ||
logger.info("Lambda function deleted successfully.") | ||
else: | ||
stdout = ret.stdout.decode("utf-8") if ret.stdout else "" | ||
# find the url, which is in the format https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/upload/ | ||
url_match = re.search( | ||
r"https://([^\.]+)\.execute-api\.([^\.]+)\.amazonaws\.com/Prod/upload/", | ||
stdout, | ||
) | ||
if url_match: | ||
logger.info( | ||
f"Lambda function deployed successfully. URL: {url_match.group(0)}," | ||
" copy it to your config." | ||
) | ||
else: | ||
logger.error("Lambda function deployed, but failed to find the URL") | ||
print(stdout) | ||
except subprocess.CalledProcessError as e: | ||
if destroy: | ||
logger.error("Failed to delete Lambda function") | ||
else: | ||
logger.error("Failed to deploy Lambda function") | ||
raise e | ||
|
||
|
||
if __name__ == "__main__": | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
"""add_upload_fields | ||
|
||
Revision ID: 46d03b666cd4 | ||
Revises: 98505a067995 | ||
Create Date: 2024-11-10 23:14:21.187860 | ||
|
||
""" | ||
from alembic import op | ||
import sqlalchemy as sa | ||
|
||
import openadapt | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = "46d03b666cd4" | ||
down_revision = "98505a067995" | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade() -> None: | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.create_table( | ||
"replay", | ||
sa.Column("id", sa.Integer(), nullable=False), | ||
sa.Column( | ||
"timestamp", | ||
openadapt.models.ForceFloat(precision=10, scale=2, asdecimal=False), | ||
nullable=True, | ||
), | ||
sa.Column("strategy_name", sa.String(), nullable=True), | ||
sa.Column("strategy_args", sa.JSON(), nullable=True), | ||
sa.Column("git_hash", sa.String(), nullable=True), | ||
sa.PrimaryKeyConstraint("id", name=op.f("pk_replay")), | ||
) | ||
with op.batch_alter_table("recording", schema=None) as batch_op: | ||
batch_op.add_column( | ||
sa.Column( | ||
"upload_status", | ||
sa.Enum("NOT_UPLOADED", "UPLOADING", "UPLOADED", name="uploadstatus"), | ||
nullable=True, | ||
) | ||
) | ||
batch_op.add_column(sa.Column("uploaded_key", sa.String(), nullable=True)) | ||
batch_op.add_column( | ||
sa.Column("uploaded_to_custom_bucket", sa.Boolean(), nullable=True) | ||
) | ||
|
||
# update all recordings to not uploaded | ||
op.execute("UPDATE recording SET upload_status = 'NOT_UPLOADED' WHERE 1=1") | ||
# update all recordings to not uploaded to custom bucket | ||
op.execute("UPDATE recording SET uploaded_to_custom_bucket = FALSE WHERE 1=1") | ||
|
||
# ### end Alembic commands ### | ||
|
||
|
||
def downgrade() -> None: | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
with op.batch_alter_table("recording", schema=None) as batch_op: | ||
batch_op.drop_column("uploaded_to_custom_bucket") | ||
batch_op.drop_column("uploaded_key") | ||
batch_op.drop_column("upload_status") | ||
|
||
op.drop_table("replay") | ||
# ### end Alembic commands ### |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
export async function get<T>(url: string, options: Partial<RequestInit> = {}): Promise<T> { | ||
return fetch(url, options).then((res) => res.json()); | ||
export async function get<T>( | ||
url: string, | ||
options: Partial<RequestInit> = {} | ||
): Promise<T> { | ||
return fetch(url, options).then((res) => res.json()) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not read this from
config
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This script is not supposed to be part of the OpenAdapt app, its an admin script that needs to be run by the owner (you) on a machine that has the relevant aws creds in its environment. From the PR description
Because config.py is more closely related to settings of the app, I didn't think it'd be useful to add these there.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we want to read from config.py.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How would you suggest a user override these settings? The default values will be empty in
config.py
andconfig.defaults.json
, so the only ways are to manually edit theconfig.json
file in thedata
folder, or we expose it in the dashboard settings page? A regular user won't be needing to edit this, and might get confused if its in the dashboard settings