added code to start and stop aws servers
This commit is contained in:
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user