8000 Add phoebus_save_restore role, with Molecule tests by ralphlange · Pull Request #106 · epics-training/training-vm · GitHub
[go: up one dir, main page]

Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions .github/workflows/molecule.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ jobs:
# Don't let a failure on one architecture hide the result on the other
fail-fast: false
matrix:
# kafka, elasticsearch and phoebus_alarm_logger run in a container
# with systemd (ansible/molecule/systemd) and test the services
# functionally; phoebus_alarm_logger builds Phoebus and covers the
# whole alarm system (Kafka, alarm server, Elasticsearch, logger)
# kafka, elasticsearch, phoebus_alarm_logger and phoebus_save_restore
# run in a container with systemd (ansible/molecule/systemd) and test
# the services functionally; phoebus_alarm_logger builds Phoebus and
# covers the whole alarm system (Kafka, alarm server, Elasticsearch,
# logger), phoebus_save_restore builds Phoebus and covers the
# save-and-restore service with Elasticsearch
role:
- initial_setup
- m_base
Expand All @@ -31,6 +33,7 @@ jobs:
- kafka
- elasticsearch
- phoebus_alarm_logger
- phoebus_save_restore
# The molecule platform image (ubuntu:22.04) is multi-arch, so each
# runner exercises the role natively on its own architecture
arch:
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/test-roles-phoebus-aa.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ on:
- 'ansible/roles/phoebus_alarm/**'
- 'ansible/roles/kafka/**'
- 'ansible/roles/phoebus_alarm_logger/**'
- 'ansible/roles/phoebus_save_restore/**'
- 'ansible/roles/elasticsearch/**'
- 'ansible/roles/java/**'
- 'ansible/roles/archiver_appliance/**'
Expand All @@ -27,6 +28,7 @@ on:
- 'ansible/roles/phoebus_alarm/**'
- 'ansible/roles/kafka/**'
- 'ansible/roles/phoebus_alarm_logger/**'
- 'ansible/roles/phoebus_save_restore/**'
- 'ansible/roles/elasticsearch/**'
- 'ansible/roles/java/**'
- 'ansible/roles/archiver_appliance/**'
Expand Down
1 change: 1 addition & 0 deletions ansible/group_vars/all.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ catrust: false
phoebus: false
phoebus_alarm: false
phoebus_alarm_logger: false
phoebus_save_restore: false
kafka: false
elasticsearch: false
oac_tree: false
Expand Down
2 changes: 1 addition & 1 deletion ansible/molecule/systemd/Dockerfile.j2
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Molecule platform image with systemd as PID 1
#
# Shared by the Molecule scenarios that test services (kafka, elasticsearch,
# phoebus_alarm_logger):
# phoebus_alarm_logger, phoebus_save_restore):
# with a real systemd the roles run with in_container=false, i.e. exactly as on a VM,
# and the tests can check that the services are actually running and working.
# Needs a privileged container sharing the host's cgroup namespace - see the
Expand Down
15 changes: 12 additions & 3 deletions ansible/playbook.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@

- name: Decide if graphics should be installed
ansible.builtin.set_fact:
install_graphics: "{{ install_graphics | bool or phoebus | default(false) | bool or phoebus_alarm | default(false) | bool or phoebus_alarm_logger | default(false) | bool or oac_tree | default(false) | bool }}"
install_graphics: "{{ install_graphics | bool or phoebus | default(false) | bool or phoebus_alarm | default(false) | bool or phoebus_alarm_logger | default(false) | bool or phoebus_save_restore | default(false) | bool or oac_tree | default(false) | bool }}"
tags: always

- name: Initial setup
Expand Down Expand Up @@ -149,14 +149,16 @@
include_role:
name: bluesky

# Also runs for phoebus_alarm(_logger): the Phoebus build produces the alarm services
# Also runs for phoebus_alarm(_logger) and phoebus_save_restore:
# the Phoebus build produces the services
- name: Phoebus
include_role:
name: phoebus
when: >-
(phoebus is defined and phoebus | bool) or
(phoebus_alarm is defined and phoebus_alarm | bool) or
(phoebus_alarm_logger is defined and phoebus_alarm_logger | bool)
(phoebus_alarm_logger is defined and phoebus_alarm_logger | bool) or
(phoebus_save_restore is defined and phoebus_save_restore | bool)
tags: phoebus

- name: Kafka
Expand Down Expand Up @@ -185,6 +187,13 @@
when: phoebus_alarm_logger is defined and phoebus_alarm_logger | bool
tags: phoebus_alarm_logger

# Pulls in the elasticsearch role as a dependency
- name: Phoebus Save-and-Restore
include_role:
name: phoebus_save_restore
when: phoebus_save_restore is defined and phoebus_save_restore | bool
tags: phoebus_save_restore

- name: OAC Tree
include_role:
name: oac_tree
Expand Down
16 changes: 15 additions & 1 deletion ansible/roles/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,19 @@ them to the Phoebus "Alarm Log" table through its REST API
`phoebus` role's client preferences like the alarm server's settings).
Built by the `phoebus` role and pulls in `phoebus_alarm` (alarm server, Kafka).

### phoebus_save_restore
Phoebus save-and-restore service as a systemd service: stores
configurations and snapshots in Elasticsearch (`elasticsearch` role, a
dependency) and serves the Phoebus "Save & Restore" application through its
REST API (`save_restore_url`, default `http://localhost:8080/save-restore`,
shared with the `phoebus` role's client preferences like the alarm
settings). Built by the `phoebus` role.
Takes snapshots and restores through CA/PVA with the EPICS defaults; other
EPICS settings go into `save_restore_epics_env` (`EPICS_*` environment
variables), as the service does not read the client's `preferences.ini`.
Reading needs no login; creating and changing needs one of the built-in demo
users (`user`/`userPass`, `admin`/`adminPass`).

### archiver_appliance
Minimal install of the EPICS Archiver Appliance.

Expand Down Expand Up @@ -92,7 +105,8 @@ as a systemd service - the transport of the Phoebus alarm system.

### elasticsearch
Elasticsearch (single node, localhost only, security disabled)
as a systemd service - the storage of the Phoebus alarm logger.
as a systemd service - the storage of the Phoebus alarm logger and
save-and-restore service.
Follows `phoebus_services_elasticsearch` of
[ansible-phoebus](https://github.com/ControlSystemStudio/ansible-phoebus):
everything under `/opt/epics-tools/services/elasticsearch` (`release/`
Expand Down
3 changes: 2 additions & 1 deletion ansible/roles/elasticsearch/defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
# https://github.com/ControlSystemStudio/ansible-phoebus:
# one service root with release/, data/, logs/ and run/, all owned by the
# service account; the configuration lives in release/config.
# Used by the Phoebus alarm logger (phoebus_alarm_logger role).
# Used by the Phoebus alarm logger (phoebus_alarm_logger role) and the
# save-and-restore service (phoebus_save_restore role).
#
# Stays on the 8.x line: Phoebus 6 uses the Elasticsearch 8 Java client,
# which is not supported against a 9.x server.
Expand Down
2 changes: 1 addition & 1 deletion ansible/roles/elasticsearch/meta/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ galaxy_info:
role_name: elasticsearch
namespace: epics
author: Ralph Lange
description: Elasticsearch (single node) for the Phoebus alarm logger
description: Elasticsearch (single node) for the Phoebus services
license: EPICS open license
min_ansible_version: "2.15"
# Elasticsearch runs on the JDK installed by the java role
Expand Down
5 changes: 5 additions & 0 deletions ansible/roles/phoebus/defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ alarm_kafka_server: >-
# REST API of the alarm logger (port 9000 is the Phoebus client default)
alarm_logger_url: "http://localhost:9000"

# REST API of the save-and-restore service the client uses (the Phoebus
# client default). The phoebus_save_restore role uses the same variable
# name, so setting it in a setup file configures client and service.
save_restore_url: "http://localhost:8080/save-restore"

# Documentation build: virtualenv location, and the distro's Python package
# to use where the system python3 is older than 3.11 (Rocky 9, Ubuntu 22.04)
phoebus_docs_src: "/opt/epics-tools/lib/phoebus/docs"
Expand Down
2 changes: 1 addition & 1 deletion ansible/roles/phoebus/templates/preferences.ini.j2
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,4 @@ org.phoebus.applications.alarm/config_names={{ alarm_config_name }}
org.phoebus.applications.alarm.logging.ui/service_uri={{ alarm_logger_url }}

# Save Restore
org.phoebus.applications.saveandrestore.client/jmasar.service.url=http://epics-services-tst:23181
org.phoebus.applications.saveandrestore.client/jmasar.service.url={{ save_restore_url }}
61 changes: 61 additions & 0 deletions ansible/roles/phoebus_save_restore/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
---
# Defaults for the Phoebus save-and-restore service
#
# Modelled on the phoebus_services_save_restore role of
# https://github.com/ControlSystemStudio/ansible-phoebus
#
# The service is built by the phoebus role (Phoebus' top-level Maven build
# includes services/), so this role only runs the already-built service.
# It stores its data in Elasticsearch (elasticsearch role, a dependency) and
# serves the Phoebus "Save & Restore" application through its REST API.
# The phoebus role uses the same variable name (save_restore_url) for the
# client preferences, so setting it in a setup file configures client and
# service consistently.

save_restore_user: save-and-restore
save_restore_group: save-and-restore
# Working directory: the service reads EPICS settings from settings.ini there
save_restore_home: "/var/lib/save-and-restore"
save_restore_conf_dir: "/etc/save-and-restore"
save_restore_target_dir: "/opt/epics-tools/lib/phoebus/services/save-and-restore/target"
save_restore_java_heap_opts: "-Xms128M -Xmx512M"

# EPICS (CA/PVA) access for taking snapshots and restoring on the server side.
# The service does not read the Phoebus client's preferences.ini: as
# documented for the service, it takes its EPICS settings from the EPICS_*
# environment variables (for CA because the wrapper sets -Djca.use_env=true).
# Default protocol for PV names without a ca:// or pva:// prefix
save_restore_default_protocol: ca
# EPICS_* environment variables of the service, e.g.
# save_restore_epics_env:
# EPICS_CA_ADDR_LIST: "192.168.1.255"
# EPICS_CA_AUTO_ADDR_LIST: "NO"
# EPICS_PVA_ADDR_LIST: "192.168.1.255"
# EPICS_PVA_AUTO_ADDR_LIST: "NO"
# Empty: the EPICS defaults (automatic address lists), which find the IOCs
# running on the VM
save_restore_epics_env: {}

# REST API of the service (port 8080 and /save-restore are the Phoebus
# client defaults). Must be http:// with an explicit, unprivileged port
# (checked by the role), as the service runs as an unprivileged user.
save_restore_url: "http://localhost:8080/save-restore"
# Port and context path of the REST API - taken from that URL
save_restore_port: "{{ save_restore_url | urlsplit('port') }}"
save_restore_context_path: >-
{{ save_restore_url | urlsplit('path') | regex_replace('/+$', '') }}

save_restore_es_host: "{{ elasticsearch_network_host }}"
save_restore_es_port: "{{ elasticsearch_http_port }}"

# Authentication: the built-in demo users (training VM, localhost only).
# Reading needs no credentials; creating, changing and deleting does.
# With authorization_permitall every authenticated user may do everything.
save_restore_auth_impl: demo
save_restore_demo_user: user
save_restore_demo_user_password: userPass
save_restore_demo_admin: admin
save_restore_demo_admin_password: adminPass
save_restore_demo_readonly: johndoe
save_restore_demo_readonly_password: "1234"
save_restore_authorization_permitall: true
10 changes: 10 additions & 0 deletions ansible/roles/phoebus_save_restore/handlers/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
# Handlers for the Phoebus save-and-restore service

- name: Restart save-and-restore
become: true
when: not in_container
8 ansibl 10BC8 e.builtin.systemd_service:
name: save-and-restore
state: restarted
daemon_reload: true
15 changes: 15 additions & 0 deletions ansible/roles/phoebus_save_restore/meta/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
# galaxy_info is required by Molecule's prerun (role name check)
galaxy_info:
role_name: phoebus_save_restore
namespace: epics
author: Ralph Lange
description: Phoebus save-and-restore service
license: EPICS open license
min_ansible_version: "2.15"
# Dependencies for the Phoebus save-and-restore service
# (the service binaries come from the phoebus role,
# which playbook.yml runs whenever phoebus_save_restore is enabled)
dependencies:
- role: java
- role: elasticsearch
42 changes: 42 additions & 0 deletions ansible/roles/phoebus_save_restore/molecule/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Molecule tests for the phoebus_save_restore role

Functional tests of the Phoebus save-and-restore service: the `default`
scenario builds Phoebus and sets up Elasticsearch and the save-and-restore
service (roles `initial_setup` + `phoebus` + `phoebus_save_restore`, which
pulls in `elasticsearch`) in an `ubuntu:22.04` container, then runs the
testinfra tests in `resources/tests/test_save_restore.py`.
The container runs systemd as PID 1 (privileged, host cgroup namespace;
image definition shared in `ansible/molecule/systemd/Dockerfile.j2`), so the
role runs with `in_container: false`, exactly as on a VM, and the services
really start.

The end-to-end tests use the REST API the way the Phoebus "Save & Restore"
application does: as the demo user `user` they create a folder and, in it, a
configuration with one PV, then let the service take and save a snapshot.
The PV is a local PV (`loc://molecule_sar_test(42)`) of the service's own PV
pool, so it has a value without an IOC. Node names carry a timestamp, so the
tests can be re-run (`molecule verify`).

| Test | Checks |
|---|---|
| `test_service_is_running_and_enabled` | `elasticsearch` and `save-and-restore` are running and enabled |
| `test_runs_as_unprivileged_user` | the service runs as the `save-and-restore` user |
| `test_epics_settings_reach_the_service` | the service runs with `-Djca.use_env=true` and `-DdefaultProtocol=ca`, and has the `EPICS_*` variables of `save_restore_epics_env` (set in `molecule.yml`) in its environment |
| `test_rest_api_reports_elasticsearch_connected` | the REST API (port 8080, `/save-restore`) answers and reports Elasticsearch as connected |
| `test_phoebus_client_uses_the_service` | `preferences.ini` points the client (`jmasar.service.url`) at the service |
| `test_root_folder_exists` | the root folder of the tree is there |
| `test_changes_need_authentication` | creating a node without credentials is rejected (401) |
| `test_folder_is_listed_under_root` | the new folder is a child of the root folder, owned by `user` |
| `test_configuration_is_stored` | the configuration holds the test PV |
| `test_snapshot_holds_the_pv_value` | the snapshot taken by the service holds the PV's value (42) |
| `test_snapshot_is_found_by_search` | `/search` finds the snapshot by name |
| `test_data_is_stored_in_elasticsearch` | the snapshot data is in the `saveandrestore_snapshot` index |

Building Phoebus makes this one of the slowest scenarios.
CI runs it on x86_64 and aarch64 (`.github/workflows/molecule.yml`).
To run it locally (needs Docker):

```bash
cd ansible/roles/phoebus_save_restore
molecule test
```
46 changes: 46 additions & 0 deletions ansible/roles/phoebus_save_restore/molecule/default/molecule.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
---
dependency:
name: galaxy
lint: |
set -e
yamllint .
ansible-lint
flake8
provisioner:
name: ansible
playbooks:
converge: ../resources/converge.yml
inventory:
host_vars:
phoebus-save-restore-systemd:
# Real systemd in the container: run the services as on a VM
in_container: false
# Same as the EPICS defaults; set to check that they reach the service
save_restore_epics_env:
EPICS_CA_AUTO_ADDR_LIST: "YES"
EPICS_PVA_AUTO_ADDR_LIST: "YES"
group_vars:
all:

scenario:
name: default
verifier:
name: testinfra
directory: ../resources/tests/
driver:
name: docker
# Container with systemd as PID 1 (shared image definition in ansible/molecule/systemd)
platforms:
- name: phoebus-save-restore-systemd
image: ubuntu:22.04
# relative to the scenario directory
dockerfile: ../../../../molecule/systemd/Dockerfile.j2
pre_build_image: false
command: /lib/systemd/systemd
privileged: true
cgroupns_mode: host
volumes:
- /sys/fs/cgroup:/sys/fs/cgroup:rw
tmpfs:
- /run
- /run/lock
35 changes: 35 additions & 0 deletions ansible/roles/phoebus_save_restore/molecule/resources/converge.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
- name: Converge
hosts: all
become: true

# Molecule runs the roles directly, bypassing playbook.yml, so the OS and
# architecture variables the roles rely on have to be loaded here too.
# Same first-found logic as playbook.yml's pre_tasks. Everything else
# comes from the roles' own defaults.
pre_tasks:
- name: Load OS-specific variables with fallback
ansible.builtin.include_vars: "{{ item }}"
with_first_found:
- files:
- "{{ ansible_facts['distribution'] }}_\
{{ ansible_facts['distribution_major_version'] }}.yml"
- "{{ ansible_facts['distribution'] }}.yml"
- "{{ ansible_facts['os_family'] }}.yml"
- "default.yml"
paths:
- "{{ playbook_dir }}/../../../../vars/os"

- name: Load architecture-specific variables with fallback
ansible.builtin.include_vars: "{{ item }}"
with_first_found:
- files:
- "{{ ansible_facts['architecture'] }}.yml"
- "default.yml"
paths:
- "{{ playbook_dir }}/../../../../vars/arch"

roles:
- role: initial_setup
- role: phoebus
- role: phoebus_save_restore
Loading
0