fixed code to reset table every poll

This commit is contained in:
2023-10-06 16:19:19 -04:00
parent bcb346b754
commit f35b2c2360
3 changed files with 250 additions and 7 deletions

View File

@@ -2,7 +2,9 @@ import logging
from enum import Enum
import boto3
from mypy_boto3_ec2 import EC2ServiceResource
from nicegui import ui
from nicegui.element import Element
from rich.console import Console
from rich.logging import RichHandler
from rich.traceback import install
@@ -10,6 +12,11 @@ from rich.traceback import install
class InstanceMode(Enum):
PENDING = 0
RUNNING = 16
SHUTTING_DOWN = 32
TERMINATED = 48
STOPPING = 64
STOPPED = 80
START = 1
STOP = 2
@@ -31,9 +38,10 @@ log = logging.getLogger("rich")
def aws_launch_ec2(
b: ui.button, name: str = "foundry", mode: InstanceMode = InstanceMode.START
ec2_resource: EC2ServiceResource,
name: str = "foundry",
mode: InstanceMode = InstanceMode.START,
) -> None:
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:
@@ -47,15 +55,39 @@ def aws_launch_ec2(
log.info(instance.stop())
def update_instance_table(
ec2_resource: EC2ServiceResource, container: Element, columns: list
) -> None:
container.clear()
with container:
rows = []
table = ui.table(columns=columns, rows=rows, pagination=10)
for instance in ec2_resource.instances.all():
name = instance.id
for tag in instance.tags:
if tag["Key"] == "Name":
name = tag["Value"]
table.add_rows({"name": name, "status": instance.state["Name"]})
def nicegui_ui() -> None:
ec2_resource = boto3.resource("ec2")
with ui.row():
start = ui.button(
"Start Foundry AWS Server", on_click=lambda: aws_launch_ec2(start)
ui.button(
"Start Foundry AWS Server", on_click=lambda: aws_launch_ec2(ec2_resource)
)
stop = ui.button(
ui.button(
"Stop Foundry AWS Server",
on_click=lambda: aws_launch_ec2(stop, mode=InstanceMode.STOP),
on_click=lambda: aws_launch_ec2(ec2_resource, mode=InstanceMode.STOP),
)
columns = [
{"name": "name", "label": "Name", "field": "name", "required": True},
{"name": "status", "label": "Status", "field": "status"},
]
rows = []
with ui.element() as container:
ui.table(columns=columns, rows=rows, pagination=10)
ui.timer(15, lambda: update_instance_table(ec2_resource, container, columns))
ui.run()