From 2a54a2ae93261a15ebd3a3aae42316a2d0dfa220 Mon Sep 17 00:00:00 2001 From: Daniel Cormier Date: Fri, 22 Sep 2023 17:37:39 -0400 Subject: [PATCH] added code to start and stop aws servers --- pdm.lock | 162 ++++++++++++++++++++++++++++++++++++++++++++++++- pyproject.toml | 1 + src/program.py | 35 ++++++++++- 3 files changed, 195 insertions(+), 3 deletions(-) diff --git a/pdm.lock b/pdm.lock index f1ef038..d1e69b3 100644 --- a/pdm.lock +++ b/pdm.lock @@ -6,7 +6,7 @@ groups = ["default"] cross_platform = true static_urls = false lock_version = "4.3" -content_hash = "sha256:9f24a270abbc9f70627bb4317b39f10a0c034d7e566c824e0271b830b3bffd1c" +content_hash = "sha256:4f96d5d4f42e83e8d1d1508e6011fca71612d4b23e40596c57b586f245fa8dbf" [[package]] name = "aiofiles" @@ -133,6 +133,42 @@ files = [ {file = "boto3-1.28.52.tar.gz", hash = "sha256:a34fc153cb2f6fb2f79a764286c967392e8aae9412381d943bddc576c4f7631a"}, ] +[[package]] +name = "boto3-stubs" +version = "1.28.52" +requires_python = ">=3.7" +summary = "Type annotations for boto3 1.28.52 generated with mypy-boto3-builder 7.19.0" +dependencies = [ + "botocore-stubs", + "types-s3transfer", + "typing-extensions>=4.1.0; python_version < \"3.12\"", +] +files = [ + {file = "boto3-stubs-1.28.52.tar.gz", hash = "sha256:12d7e5865aeec52e1f73b935b1c6a42e61325538fc2cb83a87a83e41e9485241"}, + {file = "boto3_stubs-1.28.52-py3-none-any.whl", hash = "sha256:3ea81a225e062f3bcb205467891086ea031519697ad54622e61251b52609b8d6"}, +] + +[[package]] +name = "boto3-stubs" +version = "1.28.52" +extras = ["essential"] +requires_python = ">=3.7" +summary = "Type annotations for boto3 1.28.52 generated with mypy-boto3-builder 7.19.0" +dependencies = [ + "boto3-stubs==1.28.52", + "mypy-boto3-cloudformation<1.29.0,>=1.28.0", + "mypy-boto3-dynamodb<1.29.0,>=1.28.0", + "mypy-boto3-ec2<1.29.0,>=1.28.0", + "mypy-boto3-lambda<1.29.0,>=1.28.0", + "mypy-boto3-rds<1.29.0,>=1.28.0", + "mypy-boto3-s3<1.29.0,>=1.28.0", + "mypy-boto3-sqs<1.29.0,>=1.28.0", +] +files = [ + {file = "boto3-stubs-1.28.52.tar.gz", hash = "sha256:12d7e5865aeec52e1f73b935b1c6a42e61325538fc2cb83a87a83e41e9485241"}, + {file = "boto3_stubs-1.28.52-py3-none-any.whl", hash = "sha256:3ea81a225e062f3bcb205467891086ea031519697ad54622e61251b52609b8d6"}, +] + [[package]] name = "botocore" version = "1.31.52" @@ -148,6 +184,19 @@ files = [ {file = "botocore-1.31.52.tar.gz", hash = "sha256:6d09881c5a8be34b497872ca3936f8757d886a6f42f2a8703411928189cfedc0"}, ] +[[package]] +name = "botocore-stubs" +version = "1.31.52" +requires_python = ">=3.7,<4.0" +summary = "Type annotations and code completion for botocore" +dependencies = [ + "types-awscrt", +] +files = [ + {file = "botocore_stubs-1.31.52-py3-none-any.whl", hash = "sha256:11431ac0faa35cad6deed6e87002f312f8ea6358d8106a241b60bcead2f84279"}, + {file = "botocore_stubs-1.31.52.tar.gz", hash = "sha256:2fa9b78c7a335a94d918079773dc3198786de741586187d847a8710b9c337009"}, +] + [[package]] name = "certifi" version = "2023.7.22" @@ -446,6 +495,97 @@ files = [ {file = "multidict-6.0.4.tar.gz", hash = "sha256:3666906492efb76453c0e7b97f2cf459b0682e7402c0489a95484965dbc1da49"}, ] +[[package]] +name = "mypy-boto3-cloudformation" +version = "1.28.48" +requires_python = ">=3.7" +summary = "Type annotations for boto3.CloudFormation 1.28.48 service generated with mypy-boto3-builder 7.19.0" +dependencies = [ + "typing-extensions>=4.1.0; python_version < \"3.12\"", +] +files = [ + {file = "mypy-boto3-cloudformation-1.28.48.tar.gz", hash = "sha256:efbe4073397800824287c8d52a65383112862f903b16fd587b5113449652371b"}, + {file = "mypy_boto3_cloudformation-1.28.48-py3-none-any.whl", hash = "sha256:653e14414abc9fab8d29d693f138639a1325322fb6ba1b06ca90a6ccb11dfd94"}, +] + +[[package]] +name = "mypy-boto3-dynamodb" +version = "1.28.36" +requires_python = ">=3.7" +summary = "Type annotations for boto3.DynamoDB 1.28.36 service generated with mypy-boto3-builder 7.18.0" +dependencies = [ + "typing-extensions>=4.1.0; python_version < \"3.12\"", +] +files = [ + {file = "mypy-boto3-dynamodb-1.28.36.tar.gz", hash = "sha256:5fe1d336fdc8c58f345c9c1b4e4c1a2d164660531cf3a074d4598975fb2687de"}, + {file = "mypy_boto3_dynamodb-1.28.36-py3-none-any.whl", hash = "sha256:9a3b49385d17e421661ab8639fc09cc64a706198be20287f82d83511289294a3"}, +] + +[[package]] +name = "mypy-boto3-ec2" +version = "1.28.51" +requires_python = ">=3.7" +summary = "Type annotations for boto3.EC2 1.28.51 service generated with mypy-boto3-builder 7.19.0" +dependencies = [ + "typing-extensions>=4.1.0; python_version < \"3.12\"", +] +files = [ + {file = "mypy-boto3-ec2-1.28.51.tar.gz", hash = "sha256:e62204a712e89a14e0663d3d18813ac8072706684b0e89a1b04a7d01c10f9ebe"}, + {file = "mypy_boto3_ec2-1.28.51-py3-none-any.whl", hash = "sha256:f9162f00b144cf2fab5b8b56d7883674b1d4096c79a7226b592918eae17e6235"}, +] + +[[package]] +name = "mypy-boto3-lambda" +version = "1.28.36" +requires_python = ">=3.7" +summary = "Type annotations for boto3.Lambda 1.28.36 service generated with mypy-boto3-builder 7.18.0" +dependencies = [ + "typing-extensions>=4.1.0; python_version < \"3.12\"", +] +files = [ + {file = "mypy-boto3-lambda-1.28.36.tar.gz", hash = "sha256:70498e6ff6bfd60b758553d27fadf691ba169572faca01c2bd457da0b48b9cff"}, + {file = "mypy_boto3_lambda-1.28.36-py3-none-any.whl", hash = "sha256:edb1f49279f7713929a70eaab00cf3d4ba65a10016db636805d022b2eaf14c84"}, +] + +[[package]] +name = "mypy-boto3-rds" +version = "1.28.41" +requires_python = ">=3.7" +summary = "Type annotations for boto3.RDS 1.28.41 service generated with mypy-boto3-builder 7.18.2" +dependencies = [ + "typing-extensions>=4.1.0; python_version < \"3.12\"", +] +files = [ + {file = "mypy-boto3-rds-1.28.41.tar.gz", hash = "sha256:19b7d6a1e3e53ff7d03157052885ceead29861fba2d9cbddf701f6238159cb72"}, + {file = "mypy_boto3_rds-1.28.41-py3-none-any.whl", hash = "sha256:2935be3736147b726964e3cebcb918d0fc394e4012b9b7d3d0d35c12aa740ddf"}, +] + +[[package]] +name = "mypy-boto3-s3" +version = "1.28.52" +requires_python = ">=3.7" +summary = "Type annotations for boto3.S3 1.28.52 service generated with mypy-boto3-builder 7.19.0" +dependencies = [ + "typing-extensions>=4.1.0; python_version < \"3.12\"", +] +files = [ + {file = "mypy-boto3-s3-1.28.52.tar.gz", hash = "sha256:179cb7542cc5ef656f1323ad51eb237afcba77d1e5ed07d21a013fe36effb8b2"}, + {file = "mypy_boto3_s3-1.28.52-py3-none-any.whl", hash = "sha256:a75cd5ff28f1cb5109dd50db94259436701208fa97c61b5a2cc0689e169b7cba"}, +] + +[[package]] +name = "mypy-boto3-sqs" +version = "1.28.36" +requires_python = ">=3.7" +summary = "Type annotations for boto3.SQS 1.28.36 service generated with mypy-boto3-builder 7.18.0" +dependencies = [ + "typing-extensions>=4.1.0; python_version < \"3.12\"", +] +files = [ + {file = "mypy-boto3-sqs-1.28.36.tar.gz", hash = "sha256:d9c159e020f0ef225a6d5850a3673e8b236327243ba5ffe0d13762ae4fdc0e21"}, + {file = "mypy_boto3_sqs-1.28.36-py3-none-any.whl", hash = "sha256:8457aa9f2a6da44e8543e547597773f67a04e517f6a398989117cf1fa3f70d6e"}, +] + [[package]] name = "nicegui" version = "1.3.14" @@ -760,6 +900,26 @@ files = [ {file = "starlette-0.27.0.tar.gz", hash = "sha256:6a6b0d042acb8d469a01eba54e9cda6cbd24ac602c4cd016723117d6a7e73b75"}, ] +[[package]] +name = "types-awscrt" +version = "0.19.1" +requires_python = ">=3.7,<4.0" +summary = "Type annotations and code completion for awscrt" +files = [ + {file = "types_awscrt-0.19.1-py3-none-any.whl", hash = "sha256:68fffeb75396e9e7614cd930b2d52295f680230774750907bcafb56f11514043"}, + {file = "types_awscrt-0.19.1.tar.gz", hash = "sha256:61833aa140e724a9098025610f4b8cde3dcf65b842631d7447378f9f5db4e1fd"}, +] + +[[package]] +name = "types-s3transfer" +version = "0.6.2" +requires_python = ">=3.7,<4.0" +summary = "Type annotations and code completion for s3transfer" +files = [ + {file = "types_s3transfer-0.6.2-py3-none-any.whl", hash = "sha256:1068877b6e59be5226fa3006ae64371ac9d5bc590dfdbd9c66fd0a075d3254ac"}, + {file = "types_s3transfer-0.6.2.tar.gz", hash = "sha256:4ba9b483796fdcd026aa162ee03bdcedd2bf7d08e9387c820dcdd158b0102057"}, +] + [[package]] name = "typing-extensions" version = "4.8.0" diff --git a/pyproject.toml b/pyproject.toml index aba3e0f..7f75354 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -9,6 +9,7 @@ dependencies = [ "rich>=13.5.3", "nicegui>=1.3.14", "boto3>=1.28.52", + "boto3-stubs[essential]>=1.28.52", ] requires-python = ">=3.11" readme = "README.md" diff --git a/src/program.py b/src/program.py index ff783da..1906477 100644 --- a/src/program.py +++ b/src/program.py @@ -1,9 +1,18 @@ import logging +from enum import Enum +import boto3 +from nicegui import ui from rich.console import Console from rich.logging import RichHandler from rich.traceback import install + +class InstanceMode(Enum): + START = (1,) + STOP = 2 + + install(show_locals=True) console = Console() logging.basicConfig( @@ -16,9 +25,31 @@ logging.basicConfig( log = logging.getLogger("rich") +def aws_launch_ec2(name: str = "foundry", mode: InstanceMode = InstanceMode.START): + ec2_resource = boto3.resource("ec2") + for instance in ec2_resource.instances.all(): + for tag_dict in instance.tags: + if tag_dict["Key"] == "Name" and tag_dict["Value"] == name: + if ( + instance.state["Code"] == 80 and mode == InstanceMode.START + ): # stopped + log.info(f"Starting instance {instance.id}") + instance.start() + elif instance.state["Code"] == 16 and mode == InstanceMode.STOP: + log.info(f"Stopping instance {instance.id}") + instance.stop() + + +def nicegui_ui(): + ui.button("Start Foundry AWS Server", on_click=lambda: aws_launch_ec2()) + ui.button("Stop Foundry AWS Server", on_click=lambda: aws_launch_ec2(mode=InstanceMode.STOP)) + + ui.run() + + def main() -> None: - pass + nicegui_ui() -if __name__ == "__main__": +if __name__ in {"__main__", "__mp_main__"}: main()