feat: add graphical user interface (#34)

This commit is contained in:
Stephan Lüscher 2024-05-10 16:03:05 +02:00 committed by GitHub
parent c3ed45a21b
commit 4f2130bcce
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
43 changed files with 2939 additions and 971 deletions

12
anvil/.containerignore Normal file
View file

@ -0,0 +1,12 @@
# Don't add the following files to the container
## Ansible files
**/collections/ansible_collections
**/fact_cache
## Python cache
**/__pycache__
## Podman build files
.containerignore
Containerfile

39
anvil/Containerfile Normal file
View file

@ -0,0 +1,39 @@
# Source Image
FROM docker.io/library/python:3.11-alpine3.19
# Environment vars
ENV PROJECT_DIR="/anvil"
ENV ANSIBLE_DIR="${PROJECT_DIR}/ansible"
ENV NICEGUI_DIR="${PROJECT_DIR}/nicegui"
ENV PIPX_BIN_DIR="/usr/local/py-utils"
ENV PIPX_HOME="/usr/local/pipx"
ENV POETRY_VERSION="1.8.2"
ENV POETRY_VIRTUALENVS_CREATE="true"
ENV POETRY_VIRTUALENVS_IN_PROJECT="true"
ENV PATH="${PATH}:${PIPX_BIN_DIR}:${PIPX_HOME}/venvs/poetry/bin:/${PROJECT_DIR}/.venv/bin"
## Ansible settings
ENV ANSIBLE_INVENTORY="${ANSIBLE_DIR}/inventory.yml"
ENV ANSIBLE_CACHE_PLUGIN="community.general.yaml"
ENV ANSIBLE_CACHE_PLUGIN_CONNECTION="${ANSIBLE_DIR}/fact_cache"
ENV ANSIBLE_ROLES_PATH="${ANSIBLE_DIR}/roles"
ENV ANSIBLE_COLLECTIONS_PATH="${ANSIBLE_DIR}/collections"
ENV ANSIBLE_PRIVATE_KEY_FILE="/certs/ssh/ublue-os_forge-id_ed25519"
ENV ANSIBLE_DISPLAY_SKIPPED_HOSTS="False"
ENV ANSIBLE_STDOUT_CALLBACK="yaml"
ENV ANSIBLE_CALLBACKS_ENABLED="ansible.posix.profile_tasks"
ENV ANSIBLE_HOST_KEY_CHECKING="False"
# Install system dependencies
RUN apk --no-cache add pipx openssh bash
RUN pipx install poetry==${POETRY_VERSION}
# Install ansible and dependencies
WORKDIR ${PROJECT_DIR}
COPY . .
RUN poetry install --no-root
CMD poetry env use .venv/bin/python && \
chmod +x entrypoint.sh
RUN ansible-galaxy collection install -r ./ansible/collections/requirements.yml
# Set entrypoint
ENTRYPOINT [ "/anvil/entrypoint.sh" ]

View file

@ -0,0 +1,12 @@
---
# .ansible-lint
# https://ansible-lint.readthedocs.io/profiles/
profile: production
# Ansible-lint does not fail on warnings from the rules or tags listed below
warn_list:
- galaxy[version-incorrect]
- no-jinja-nesting
- package-latest
- name[template]

View file

@ -0,0 +1,8 @@
---
collections:
- name: ansible.posix
version: 1.5.4
- name: community.general
version: 8.6.0
- name: containers.podman
version: 1.13.0

View file

@ -0,0 +1,5 @@
---
# git variables
forge_git_repository_url: "https://github.com/ublue-os/bluefin.git"
forge_git_repository_destination: "{{ forge_data_volume_mountpoint }}/data/bluefin"
forge_git_repository_version: "main"

View file

@ -0,0 +1,3 @@
---
# container registry variables
forge_registry_url: "registry.ublue.local"

View file

@ -0,0 +1,4 @@
---
# localhost variables
ansible_connection: local
ansible_python_interpreter: "{{ ansible_playbook_python }}"

View file

@ -0,0 +1,9 @@
---
all:
hosts:
localhost:
host.ublue.local:
ansible_host: host.containers.internal
ansible_user: "{{ lookup('ansible.builtin.env', 'ANSIBLE_HOST_USER') }}"
ansible_become_password: "{{ lookup('ansible.builtin.env', 'ANSIBLE_HOST_BECOME_PASSWORD') }}"
children:

View file

@ -0,0 +1,54 @@
---
- name: Configure host system
hosts: host.ublue.local
gather_facts: true
pre_tasks:
- name: Get information on podman volume ublue-os_forge_data
containers.podman.podman_volume_info:
name: ublue-os_forge-data
register: __podman_volume_info
- name: Persist podman ublue-os_forge_data mountpoint
ansible.builtin.set_fact:
forge_data_volume_mountpoint: "{{ __podman_volume_info.volumes[0].Mountpoint }}"
cacheable: true
tasks:
- name: Add ublue.local entries to /etc/hosts
ansible.builtin.lineinfile:
path: /etc/hosts
search_string: 127.0.0.1 rvproxy.ublue.local registry.ublue.local forge.ublue.local
line: 127.0.0.1 rvproxy.ublue.local registry.ublue.local forge.ublue.local
state: present
become: true
- name: Add ublue.local TSL root certificate to trust anchors
ansible.builtin.copy:
src: /certs/tls/ublue-os_forge-root.pem
dest: /etc/pki/ca-trust/source/anchors/ublue-os_forge-root.pem
force: true
mode: "0644"
become: true
- name: Update ca-trust store
ansible.builtin.command:
cmd: update-ca-trust
changed_when: false
become: true
- name: Create example extra-vars configuration file
ansible.builtin.copy:
dest: "{{ forge_data_volume_mountpoint }}/forge_example_vars.yml"
content: |
## ublue-os forge extra-vars example configuration
## For more details got to https://github.com/ublue-os/forge/blob/main/docs/variables.md
---
{% for item in __vars_used %}
{{ item }}: {{ lookup('ansible.builtin.vars', item) }}
{% endfor %}
backup: true
owner: "{{ ansible_facts.env.USER }}"
mode: "0644"
vars:
__vars_used: "{{ lookup('ansible.builtin.varnames', __regex_search, wantlist=true) }}"
__regex_search: ^forge_(?!data).+

View file

@ -0,0 +1,29 @@
---
- name: Build project
hosts: host.ublue.local
gather_facts: true
pre_tasks:
- name: DEBUG - forge variables
ansible.builtin.include_role:
name: debug_forge_vars
tasks:
- name: Build and push image to registry
containers.podman.podman_image:
name: "{{ forge_git_repository_url | regex_search(__regex_search) }}"
tag: latest
path: "{{ forge_git_repository_destination }}"
build:
file: Containerfile
format: oci
pull: false
push: true
push_args:
dest: "{{ forge_registry_url }}"
vars:
__regex_search: (?<=/)[^/]+(?=\.git)
register: __podman_image_info
- name: INFO | Status from build and push
ansible.builtin.debug:
msg: "{{ __podman_image_info }}"

View file

@ -0,0 +1,19 @@
---
- name: Clone project
hosts: host.ublue.local
gather_facts: true
pre_tasks:
- name: DEBUG - forge variables
ansible.builtin.include_role:
name: debug_forge_vars
tasks:
- name: Clone project
ansible.builtin.git:
repo: "{{ forge_git_repository_url }}"
dest: "{{ forge_git_repository_destination }}"
version: "{{ forge_git_repository_version }}"
register: __git_clone_info
- name: INFO | Status from git clone
ansible.builtin.debug:
msg: "{{ __git_clone_info }}"

View file

@ -0,0 +1,46 @@
# Role - debug_forge_vars
This role is used for debugging purposes only.
## Variables
The role has the following variables defined.
### default/main.yml
In the [main.yml](./defaults/main.yml/) all variables are defined which are
used in the context of debugging. Usually end-users should not worry about them to much.
<!-- markdownlint-disable MD013 -->
| name | type | default value | description |
| ------------------------------- | ---- | ------------- | ------------------------------------------------------------------------------------------- |
| `forge_debug_vars_regex_search` | str | ^forge\_.+ | Python regex search term. Useful if you want to print out all variables starting with `xyz` |
<!-- markdownlint-enable MD013 -->
## Example Playbook Usage
This role is best included in a playbook as pre-task:
```yaml
pre_tasks:
- name: DEBUG - forge variables
ansible.builtin.include_role:
name: debug_forge_vars
```
With the role included you can launch the playbook in verbose mode `ansible-playbook -v`.
This will print all variables found with the regex search term defined in the `forge_debug_vars_regex_search`
variable.
You can modify the `forge_debug_vars_regex_search` term by changing it via the vars statement
```yaml
pre_tasks:
- name: DEBUG - forge git variables
ansible.builtin.include_role:
name: debug_forge_vars
vars:
forge_debug_vars_regex_search: ^forge_git.+
```

View file

@ -0,0 +1,2 @@
# Default vars for this role
forge_debug_vars_regex_search: "{{ lookup('ansible.builtin.env', 'FORGE_DEBUG_VARS_REGEX_SEARCH', default='^forge_.+') }}"

View file

@ -0,0 +1,13 @@
---
# main task file for this role
- name: DEBUG | forge variables
ansible.builtin.debug:
msg: "{{ item }}: {{ lookup('ansible.builtin.vars', item) }}"
verbosity: 1
loop: "{{ __forge_vars_used }}"
loop_control:
extended: true
label: "{{ ansible_loop.index }}/{{ ansible_loop.length }}"
vars:
__forge_vars_used: "{{ lookup('ansible.builtin.varnames', forge_debug_vars_regex_search, wantlist=true) }}"

3
anvil/entrypoint.sh Executable file
View file

@ -0,0 +1,3 @@
#! /bin/bash
## Start nicegui
poetry env use .venv/bin/python && poetry run python ${NICEGUI_DIR}/main.py

35
anvil/nicegui/main.py Normal file
View file

@ -0,0 +1,35 @@
import pages
import pages.about
import pages.ansible
import pages.home
import pages.registry
import theme
from nicegui import ui
@ui.page("/")
def index_page() -> None:
with theme.frame("Home"):
pages.home.content()
@ui.page("/ansible")
def ansible_page() -> None:
with theme.frame("Ansible"):
pages.ansible.content()
@ui.page("/registry")
def registry_page() -> None:
with theme.frame("Registry"):
pages.registry.content()
@ui.page("/about")
def about_page() -> None:
with theme.frame("About"):
pages.about.content()
ui.run(title="uBlue Forge", port=3000)

8
anvil/nicegui/menu.py Normal file
View file

@ -0,0 +1,8 @@
from nicegui import ui
def menu() -> None:
ui.link("Home", "/").classes(replace="text-white")
ui.link("Ansible", "/ansible").classes(replace="text-white")
ui.link("Registry", "/registry").classes(replace="text-white")
ui.link("About", "/about").classes(replace="text-white")

View file

@ -0,0 +1 @@
from nicegui import ui

View file

@ -0,0 +1,10 @@
from nicegui import ui
import os
def content() -> None:
project_root = os.environ['NICEGUI_DIR']
ui.label("Work in progress...").classes("text-h6")
ui.image(project_root + "/pages/assets/work-in-progress.png").classes(
"w-[200%]"
)

View file

@ -0,0 +1,121 @@
import ansible_runner
import re
import asyncio
import os
from nicegui import ui
from theme import GuiProgressSpinner
from utils import local_file_picker
ANSIBLE_EXTRA_VARS = None
# Ansible integration
@ui.refreshable # https://www.reddit.com/r/nicegui/comments/1bphjk5/comment/kx7l5kj/?utm_source=share&utm_medium=web3x&utm_name=web3xcss&utm_term=1&utm_content=share_button
async def load_configuration_file() -> None:
global ANSIBLE_EXTRA_VARS
result = await local_file_picker(
directory="/data", multiple=False, file_name_filter=".yml"
)
file_path = result[0]
with open(file_path, "r") as file:
data = file.read()
## Give feedback to user
ui.notify(f"You chose {file_path}")
## Display content
ui.code(content=data, language="yaml")
## Preserve configuration file path for ansible-playbook --extra-vars
ANSIBLE_EXTRA_VARS = f"@{file_path}"
async def run_ansible_playbook(
playbook_name: str, gui_log: ui.log, gui_spinner: GuiProgressSpinner
) -> None:
# Clear log console
gui_log.clear()
# Enable spinner
gui_spinner.enable()
# Run ansible playbook
project_root = os.environ["ANSIBLE_DIR"]
playbook_path = f"{project_root}/playbooks/"
extra_vars_file = ANSIBLE_EXTRA_VARS
thread, runner = ansible_runner.interface.run_command_async(
executable_cmd="ansible-playbook",
cmdline_args=[
f"{playbook_path}/{playbook_name}",
# playbook_path + playbook_name,
"--extra-vars",
extra_vars_file,
],
)
# Parse and display output from ansible playbook
## Remove color characters from response until clear how to display them in a log
output_parser = re.compile(r"\x1B(?:[@-Z\\-_]|\[[0-9;#]+[mGK]?)")
# show log from asynchronous job
processed_events = set() # Set to store processed events
while runner.rc is None:
for event in runner.events:
# Make sure log is displayed during playbook run
await asyncio.sleep(0.1)
# Check if event has been processed already
event_key = (event["uuid"], event["counter"])
if event_key not in processed_events:
# Add event to processed set
processed_events.add(event_key)
# Process event
ansible_log = format(output_parser.sub("", event["stdout"]))
# Push log to ui
gui_log.push(ansible_log)
# Disable spinner
gui_spinner.disable()
# Page content
def content() -> None:
with ui.row().classes("w-full"):
with ui.row().classes("w-full"):
with ui.card().classes("h-full"):
ui.button(
text="Load configuration file",
on_click=lambda: load_configuration_file.refresh(),
icon="folder",
)
## show configuration file with ui.code
load_configuration_file()
with ui.row().classes("w-full"):
# First Row
with ui.card().classes("h-full"):
with ui.row().classes("no-wrap"):
ui.label("Build").classes("text-h5")
gui_build_progress = GuiProgressSpinner()
ui.button(
text="Clone project",
on_click=lambda: run_ansible_playbook(
playbook_name="project_clone.yml",
gui_log=gui_playbook_log,
gui_spinner=gui_build_progress,
),
)
ui.button(
text="Build project",
on_click=lambda: run_ansible_playbook(
"project_build.yml",
gui_log=gui_playbook_log,
gui_spinner=gui_build_progress,
),
)
# Second Row
with ui.card().classes("h-full"):
with ui.row().classes("no-wrap"):
ui.label("Deploy").classes("text-h6")
gui_deploy_progress = GuiProgressSpinner
ui.button(
"Deploy VM",
on_click=lambda: ui.notify("This playbook is not implemented yet"),
)
with ui.row().classes("w-full"):
with ui.card().classes("w-full"):
ui.label("Playbook Log").classes("text-h6")
ui.button("Clear Log", on_click=lambda: gui_playbook_log.clear())
gui_playbook_log = ui.log().classes("w-full h-full")

Binary file not shown.

After

Width:  |  Height:  |  Size: 524 KiB

View file

@ -0,0 +1,10 @@
from nicegui import ui
import os
def content() -> None:
project_root = os.environ['NICEGUI_DIR']
ui.label("Work in progress...").classes("text-h6")
ui.image(project_root + "/pages/assets/work-in-progress.png").classes(
"w-[200%]"
)

View file

@ -0,0 +1,10 @@
from nicegui import ui
import os
def content() -> None:
project_root = os.environ['NICEGUI_DIR']
ui.label("Work in progress...").classes("text-h6")
ui.image(project_root + "/pages/assets/work-in-progress.png").classes(
"w-[200%]"
)

44
anvil/nicegui/theme.py Normal file
View file

@ -0,0 +1,44 @@
from contextlib import contextmanager
from menu import menu
from nicegui import ui
class GuiProgressSpinner(ui.spinner):
def __init__(
self,
*,
type: str = "dots",
size: str = "lg",
color: str | None = "red",
thickness: float = 5
) -> None:
super().__init__(type, size=size, color=color, thickness=thickness)
with self, ui.spinner():
self.visible = False
def enable(self) -> None:
self.set_visibility(True)
def disable(self) -> None:
self.set_visibility(False)
@contextmanager
def frame(navigation_title: str, enable_right_drawer: bool = False):
"""Custom page frame to share the same styling and behavior across all pages"""
ui.colors(primary="#4051b5", secondary="#dddbff", accent="#171d9a")
with ui.header():
with ui.row():
menu()
ui.space()
with ui.link(target="https://github.com/ublue-os/forge", new_tab=True):
ui.icon("eva-github").classes("text-2xl")
with ui.column().classes():
ui.label(navigation_title).classes("text-h4")
yield
with ui.footer(value=False):
ui.add_head_html(
'<link href="https://unpkg.com/eva-icons@1.1.3/style/eva-icons.css" rel="stylesheet" />'
)

116
anvil/nicegui/utils.py Normal file
View file

@ -0,0 +1,116 @@
from pathlib import Path
import platform
from typing import Optional
from nicegui import events, ui
# most of the local_file_picker class is from https://github.com/zauberzeug/nicegui/blob/main/examples/local_file_picker/local_file_picker.py
# i just added the file_name_filter
class local_file_picker(ui.dialog):
def __init__(
self,
directory: str,
*,
upper_limit: Optional[str] = ...,
multiple: bool = False,
show_hidden_files: bool = False,
file_name_filter: Optional[str] = None,
) -> None:
"""Local File Picker
This is a simple file picker that allows you to select a file from the local filesystem where NiceGUI is running.
:param directory: The directory to start in.
:param upper_limit: The directory to stop at (None: no limit, default: same as the starting directory).
:param multiple: Whether to allow multiple files to be selected.
:param show_hidden_files: Whether to show hidden files.
:param file_name_filter: Filter files in directory based on file name
"""
super().__init__()
self.path = Path(directory).expanduser()
if upper_limit is None:
self.upper_limit = None
else:
self.upper_limit = Path(
directory if upper_limit == ... else upper_limit
).expanduser()
self.show_hidden_files = show_hidden_files
self.file_name_filter = file_name_filter
with self, ui.card():
self.add_drives_toggle()
self.grid = (
ui.aggrid(
{
"columnDefs": [{"field": "name", "headerName": "File"}],
"rowSelection": "multiple" if multiple else "single",
},
html_columns=[0],
)
.classes("w-96")
.on("cellDoubleClicked", self.handle_double_click)
)
with ui.row().classes("w-full justify-end"):
ui.button("Cancel", on_click=self.close).props("outline")
ui.button("Ok", on_click=self._handle_ok)
self.update_grid()
def add_drives_toggle(self):
if platform.system() == "Windows":
import win32api
drives = win32api.GetLogicalDriveStrings().split("\000")[:-1]
self.drives_toggle = ui.toggle(
drives, value=drives[0], on_change=self.update_drive
)
def update_drive(self):
self.path = Path(self.drives_toggle.value).expanduser()
self.update_grid()
def update_grid(self) -> None:
paths = list(self.path.glob("*"))
if not self.show_hidden_files:
paths = [p for p in paths if not p.name.startswith(".")]
if self.file_name_filter: # Add this condition
paths = [
p for p in paths if p.is_file() and p.suffix == self.file_name_filter
]
paths.sort(key=lambda p: p.name.lower())
paths.sort(key=lambda p: not p.is_dir())
self.grid.options["rowData"] = [
{
"name": f"📁 <strong>{p.name}</strong>" if p.is_dir() else p.name,
"path": str(p),
}
for p in paths
]
if (
self.upper_limit is None
and self.path != self.path.parent
or self.upper_limit is not None
and self.path != self.upper_limit
):
self.grid.options["rowData"].insert(
0,
{
"name": "📁 <strong>..</strong>",
"path": str(self.path.parent),
},
)
self.grid.update()
def handle_double_click(self, e: events.GenericEventArguments) -> None:
self.path = Path(e.args["data"]["path"])
if self.path.is_dir():
self.update_grid()
else:
self.submit([str(self.path)])
async def _handle_ok(self):
rows = await ui.run_javascript(
f"getElement({self.grid.id}).gridOptions.api.getSelectedRows()"
)
self.submit([r["path"] for r in rows])

2238
anvil/poetry.lock generated Normal file

File diff suppressed because it is too large Load diff

21
anvil/pyproject.toml Normal file
View file

@ -0,0 +1,21 @@
[tool.poetry]
name = "ublue-os_forge"
version = "0.1.0"
description = "Ansible automation for Universal Blue"
authors = ["Stephan Lüscher <stephan.luescher@mykolab.com>"]
license = "../LICENSE"
readme = "../README.md"
[tool.poetry.dependencies]
python = "^3.11"
ansible-core = "^2.16"
jmespath = "^1.0"
nicegui = "^1.4.23"
ansible-runner = "^2.3.6"
[tool.poetry.group.dev.dependencies]
ansible-lint = { version = "^24.2", markers = 'platform_system != "Windows"' } # https://github.com/ansible/ansible-lint/issues/2730#issuecomment-1330406601
[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"