added code to start and stop aws servers

This commit is contained in:
2023-09-22 17:37:39 -04:00
parent fd2a930a0a
commit 2a54a2ae93
3 changed files with 195 additions and 3 deletions

View File

@@ -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()