diff --git a/.coderabbit.yaml b/.coderabbit.yaml index 6c8bc6bc..8bf2df99 100644 --- a/.coderabbit.yaml +++ b/.coderabbit.yaml @@ -32,254 +32,7 @@ reviews: unit_tests: enabled: false - # Enforce PR title and description requirements - pre_merge_checks: - title: - mode: "warning" - requirements: | - PR title MUST follow Conventional Commits format: - - Format: : - - Valid types: Refer to https://github.com/linux-system-roles/auto-maintenance/blob/main/pr_title_lint.py#L23 - - Examples: - - "feat: Add backup functionality" - - "fix: Correct OSTree package installation" - - "fix!: Remove deprecated variable (breaking change)" - - "chore(formatting): Fix indentation in my_module.py" - - "refactor(python)!: Use new python library thispy which has breaking api changes" +# Coding conventions, PR title/description requirements, AI attribution, and path-specific instructions live in AGENTS.md at the repo root. +# CodeRabbit auto-discovers AGENTS.md and applies it as review criteria: https://docs.coderabbit.ai/knowledge-base/code-guidelines - custom_checks: - - mode: "warning" - name: "Description Format" - instructions: | - PR description MUST follow the template structure from .github/pull_request_template.md: - - Must contain "Enhancement:" or "Feature:" section describing what changed - - Must contain "Reason:" section explaining why the change was needed - - Must contain "Result:" section describing the outcome or impact - - Can contain optional "Issue Tracker Tickets (Jira or BZ if any):" section - - Must contain "Signed-off-by:" section with your name and email address - use git commit -s - - Can contain optional "Assisted-by:" section with name of the AI coding assistant and models used - Example: - ``` - Feature: Introduce the bootloader_secure_logging variable - - Reason: Currently, all sensitive tasks use hard-coded no_log: true, which makes debugging difficult. - - Result: Users can now set bootloader_secure_logging: false for debugging while maintaining secure defaults. - - Issue Tracker Tickets (Jira or BZ if any): RHEL-12345 - - Signed-off-by: John Doe john.doe@example.com - - Assisted-by: Fish 4.3 using model Swim 6.2 - ``` - - For PRs that are bug fixes, you can use the following template: - - Must contain "Cause:" section explaining the root cause of the bug - - Must contain "Consequences:" section explaining the impact of the bug and how it appears to users - - Must contain "Fix:" section explaining the fix for the bug and how it fixes the bug - - Must contain "Result:" section describing the outcome or impact of the fix - - Can contain optional "Issue Tracker Tickets (Jira or BZ if any):" section - - Must contain "Signed-off-by:" section with your name and email address - use git commit -s - - Can contain optional "Assisted-by:" section with name of the AI coding assistant and models used - - Example: - ``` - Cause: The variable rolename_user_name was being checked for a `none` value but was not being checked for string length greater than 0 if a string. - - Consequences: The role allowed empty user names to be configured which caused the daemon to report "User not found". - - Fix: The variable rolename_user_name is now checked for string length if not `none`, and will report an error in that case if the length is 0. - - Result: The role will not allow the user to provide an empty user name and will report an error if the rolename_user_name length is 0. - - Signed-off-by: John Doe john.doe@example.com - - Assisted-by: Fish 4.3 using model Swim 6.2 - ``` - - - mode: "warning" - name: "AI Agent Attribution" - instructions: | - If the added code was substantially generated by AI, the generated code should contain a - comment in-line or at the top of the file after the SPDX line (if any). The attribution - comment should look like this: - ``` - # Assisted by using model(s) [, ] - ``` - The git commit message/PR body should also contain an attribution line like the following: - ``` - Assisted-by: using model(s) [, ] - ``` - - path_instructions: - # ======================================== - # Ansible Tasks - Core role logic - # ======================================== - - path: "tasks/**/*.yml" - instructions: | - **no_log patterns:** - - For sensitive data (credentials, secrets, passwords, keys): - ```yaml - - name: Task with sensitive data - ansible.builtin.command: sensitive command - no_log: "{{ bootloader_secure_logging }}" - ``` - Ensure `bootloader_secure_logging: true` is defined in defaults/main.yml - - - For facts modules (package_facts, service_facts): - ```yaml - - name: Gather package facts - ansible.builtin.package_facts: - no_log: "{{ ansible_verbosity < 3 }}" - ``` - This hides verbose facts output unless running with -vvv or higher verbosity - - - NEVER use hardcoded `no_log: true` - always parametrize - - **Package installation (OSTree compatibility):** - - ALWAYS use `ansible.builtin.package` module (NEVER yum, dnf, or apt) - - For Red Hat family systems: MUST include `use:` parameter for OSTree compatibility - - For other systems (apt, etc.): OSTree `use:` parameter NOT needed - - **Red Hat family pattern (rpm/dnf/yum based):** - ```yaml - - name: Install packages - ansible.builtin.package: - name: "{{ __bootloader_packages }}" - state: present - use: "{{ (__bootloader_is_ostree | d(false)) | ternary('ansible.posix.rhel_rpm_ostree', omit) }}" - ``` - - **Non-Red Hat systems (apt based) - no OSTree handling needed:** - ```yaml - - name: Install packages - ansible.builtin.package: - name: "{{ __bootloader_packages }}" - state: present - when: ansible_facts["pkg_mgr"] == 'apt' - ``` - - **Pattern explanation:** - - `__bootloader_is_ostree` detects OSTree/rpm-ostree systems (RHEL/CentOS/Fedora variants) - - This variable should already exist in vars/main.yml - do NOT suggest adding it - - `| d(false)` provides safe default if variable undefined - - `ternary()` selects `ansible.posix.rhel_rpm_ostree` for OSTree, `omit` otherwise - - `omit` removes the parameter entirely on non-OSTree Red Hat systems - - **Third-party collections:** - - Avoid using third-party collections (community.general, community.crypto, etc.) - - Use ansible.builtin modules or command module instead - - **Referencing other system roles:** - - Use FQCN: `fedora.linux_system_roles.bootloader` - - **Idempotency:** - - Tasks must be idempotent - safe to run multiple times without unintended changes - - Use proper state parameters (present/absent, started/stopped, etc.) - - Command/shell tasks should use creates/removes or changed_when to avoid false changes - - Example: - ```yaml - - name: Initialize database - ansible.builtin.command: /usr/bin/initialize-db - args: - creates: /var/lib/db/initialized.flag - ``` - - **Check mode support:** - - Critical tasks should support check mode (--check flag) - - Use check_mode: false only when absolutely necessary (e.g., fact gathering) - - Test that role works with --check --diff - - **Test coverage requirement:** - - When adding new tasks to tasks/main.yml or creating new task files, verify that corresponding test coverage exists in tests/ - - New functionality MUST include test files (tests/tests_*.yml) that exercise the new code paths - - Tests should verify both success scenarios and failure/edge cases - - If this PR adds new tasks but does not include new or updated tests, flag it and request test coverage - - # ======================================== - # Handlers - # ======================================== - - path: "handlers/**/*.yml" - instructions: | - - Handlers with sensitive data must use `no_log: "{{ bootloader_secure_logging }}"` - - # ======================================== - # Test Playbooks - # ======================================== - - path: "tests/tests_*.yml" - instructions: | - **CRITICAL: Role invocation pattern:** - - NEVER use `ansible.builtin.include_role` directly - - NEVER use `ansible.builtin.import_role` directly - - NEVER use `roles:` keyword - - ALMOST ALWAYS use the centrally managed wrapper: - ```yaml - - name: Run role - ansible.builtin.include_tasks: tasks/run_role_with_clear_facts.yml - vars: - bootloader_: - ``` - The only exception to this rule is in tests_invalid_inputs.yml where we need to use the include_role directly to test the invalid inputs. - - **Test quality requirements:** - - Tests should verify both success and failure scenarios - - Use assert module to verify expected state after role execution - - Include cleanup tasks to ensure tests are rerunnable - - Tests should be run in a block with an always section that runs the test cleanup to ensure that the cleanup is always run. - - The cleanup tasks should be tagged with `tests::cleanup` so that the cleanup can be skipped for debug purposes. - - Tests should be idempotent - running twice should not cause failures - - Example verification: - ```yaml - - name: Verify service is running - ansible.builtin.assert: - that: - - ansible_facts.services['mssql-server.service'].state == 'running' - fail_msg: "SQL Server service is not running" - ``` - - # ======================================== - # Templates - # ======================================== - - path: "templates/**/*.j2" - instructions: | - **Required headers (in this order):** - 1. ansible_managed header: `{{ ansible_managed | comment }}` - 2. Role fingerprint: `{{ "system_role:bootloader" | comment(prefix="", postfix="") }}` - - # ======================================== - # Variable Definitions - defaults/ - # ======================================== - - path: "defaults/**/*.yml" - instructions: | - - All variables MUST be prefixed with `bootloader_` - - All variables MUST be stored in the file defaults/main.yml, Ansible - doesn't include variables from other files. - - These are user-facing API variables - - For every new variable introduced in this file, verify that it is - documented in README.md with a description and a usage example. - If it is missing from README.md, flag it and request that it be added. - - # ======================================== - # Variable Definitions - vars/ - # ======================================== - - path: "vars/**/*.yml" - instructions: | - - Internal variables MUST be prefixed with `__bootloader_` - - User-facing variables belong in defaults/main.yml, not here - - # ======================================== - # Python Code - # ======================================== - - path: "**/*.py" - instructions: | - - Must follow PEP 8 and be formatted with Python Black - - Run `tox -e black,flake8` before committing - - # ======================================== - # Documentation - # ======================================== - - path: "README.md" - instructions: | - - Document all new user-facing variables - - Include usage examples for new functionality diff --git a/.github/workflows/actions.lock b/.github/workflows/actions.lock index 48ceff46..ba900f25 100644 --- a/.github/workflows/actions.lock +++ b/.github/workflows/actions.lock @@ -100,7 +100,7 @@ dependencies: repo_id: 259445878 'linux-system-roles/.github@main': ref: 'main' - commit: 'sha1-2bca05d79218ca3588c40fe7f1dfc083592af6d2' + commit: 'sha1-f72e2d97b815ff843a106fb57949215d23bbf7df' owner_id: 28014670 repo_id: 320092621 'sclorg/testing-farm-as-github-action@v4.3.1': diff --git a/AGENTS.md b/AGENTS.md new file mode 100644 index 00000000..7939f068 --- /dev/null +++ b/AGENTS.md @@ -0,0 +1,294 @@ +# AGENTS.md - Developer instructions for the bootloader Linux System Role + + + +## For end users + +If you just want to use this role in a playbook, not change its code, *this file isn't for you.* +See [README.md](README.md) for role variables, usage examples, and supported platforms. +See [GitHub Issues](https://github.com/linux-system-roles/bootloader/issues) if you want to report a bug or request a feature without writing code yourself. + +## For developers + +Follow these conventions when you change this role. +See [contribute.html](https://linux-system-roles.github.io/contribute.html) for the full contributor guide. + +### Commit messages, PR titles, and PR descriptions + +**PR title** MUST follow Conventional Commits format: + +- Format: `: ` +- Valid types: see +- Examples: + - `feat: Add backup functionality` + - `fix: Correct OSTree package installation` + - `fix!: Remove deprecated variable (breaking change)` + - `chore(formatting): Fix indentation in my_module.py` + - `refactor(python)!: Use new python library thispy which has breaking api changes` + +**PR description** Enhancements/Features MUST follow the template in `.github/pull_request_template.md`: + +- Must contain "Enhancement:" or "Feature:" section describing what changed +- Must contain "Reason:" section explaining why the change was needed +- Must contain "Result:" section describing the outcome or impact +- Can contain optional "Issue Tracker Tickets (Jira or BZ if any):" section +- Must contain "Signed-off-by:" section with your name and email address - use `git commit -s` +- Can contain optional "Assisted-by:" section with name of the AI coding assistant and models used + +Example: + +```text +Feature: Introduce the bootloader_secure_logging variable + +Reason: Currently, all sensitive tasks use hard-coded no_log: true, which makes debugging difficult. + +Result: Users can now set bootloader_secure_logging: false for debugging while maintaining secure defaults. + +Issue Tracker Tickets (Jira or BZ if any): RHEL-12345 + +Signed-off-by: John Doe john.doe@example.com + +Assisted-by: Fish 4.3 using model Swim 6.2 +``` + +For bug fixes, use this template instead: + +- Must contain "Cause:" section explaining the root cause of the bug +- Must contain "Consequences:" section explaining the impact of the bug and how it appears to users +- Must contain "Fix:" section explaining the fix for the bug and how it fixes the bug +- Must contain "Result:" section describing the outcome or impact of the fix +- Can contain optional "Issue Tracker Tickets (Jira or BZ if any):" section +- Must contain "Signed-off-by:" section with your name and email address - use `git commit -s` +- Can contain optional "Assisted-by:" section with name of the AI coding assistant and models used + +Example: + +```text +Cause: The variable rolename_user_name was being checked for a `none` value but was not being checked for string length greater than 0 if a string. + +Consequences: The role allowed empty user names to be configured which caused the daemon to report "User not found". + +Fix: The variable rolename_user_name is now checked for string length if not `none`, and will report an error in that case if the length is 0. + +Result: The role will not allow the user to provide an empty user name and will report an error if the rolename_user_name length is 0. + +Signed-off-by: John Doe john.doe@example.com + +Assisted-by: Fish 4.3 using model Swim 6.2 +``` + +For other change types (`docs`, `refactor`, and similar), the PR or commit body does not need a fixed template, but it must still be descriptive. + +### AI agent attribution + +If the code you add was substantially generated by AI, mark it: + +- Add an in-line comment next to the generated code, or at the top of the file after the SPDX line (if any): + + ```text + # Assisted by using model(s) [, ] + ``` + +- Add the same attribution in the git commit message and PR body: + + ```text + Assisted-by: using model(s) [, ] + ``` + +### Ansible tasks - `tasks/**/*.yml` + +**`no_log` patterns:** + +- For sensitive data (credentials, secrets, passwords, keys): + + ```yaml + - name: Task with sensitive data + ansible.builtin.command: sensitive command + no_log: "{{ bootloader_secure_logging }}" + ``` + + Ensure `bootloader_secure_logging: true` is defined in `defaults/main.yml`. + +- For facts modules (`package_facts`, `service_facts`): + + ```yaml + - name: Gather package facts + ansible.builtin.package_facts: + no_log: "{{ ansible_verbosity < 3 }}" + ``` + + This hides verbose facts output unless you run with `-vvv` or higher. + +- NEVER use hardcoded `no_log: true` - always parametrize. + +**Package installation (OSTree compatibility):** + +- ALWAYS use the `ansible.builtin.package` module (NEVER `yum`, `dnf`, or `apt` directly). +- ALWAYS include the `use:` parameter for OSTree compatibility. +- It does nothing on non-OSTree systems, so it is safe to always include it. + +Typical package module usage: + +```yaml +- name: Ensure packages + ansible.builtin.package: + name: "{{ __bootloader_packages }}" + state: present + use: "{{ (__bootloader_is_ostree | d(false)) | ternary('ansible.posix.rhel_rpm_ostree', omit) }}" +``` + +Explanation: + +- `__bootloader_is_ostree` is `true` on OSTree/rpm-ostree systems. +- The role sets this in `tasks/set_vars.yml` or similar. +- DO NOT set it again. +- `| d(false)` provides a safe default if the variable is undefined. +- `ternary()` selects `ansible.posix.rhel_rpm_ostree` for OSTree, `omit` otherwise. +- `omit` removes the parameter entirely on non-OSTree systems. + +**Using the `shell:` module:** + +- ALWAYS use the `cmd` parameter to specify the script to run, never the free-form style. +- ALMOST ALWAYS use `executable: /bin/bash` unless you must use another shell. +- ALWAYS use `set -e`, `set -u`, and `set -o pipefail` (or combined as `set -euo pipefail`) as the first line of the script. +- Ansible requires pipefail. +- ALWAYS use `changed_when:`. +- If you need to detect whether the script changed something, have it exit with a specific code or print a value to stdout/stderr, `register:` the result, then use `changed_when:` and `failed_when:` conditionals. + +**Third-party collections:** + +- Avoid third-party collections (`community.general`, `community.crypto`, etc.). +- Use `ansible.builtin` modules or the `command` module instead. + +**Referencing other system roles:** + +- Use the FQCN: `fedora.linux_system_roles.bootloader`. + +**Idempotency:** + +- Tasks must be idempotent - safe to run multiple times without unintended changes. +- Use proper state parameters (`present`/`absent`, `started`/`stopped`, etc.). +- `command`/`shell` tasks should use `creates`/`removes` or `changed_when` to avoid false changes. +- Example: + + ```yaml + - name: Initialize database + ansible.builtin.command: /usr/bin/initialize-db + args: + creates: /var/lib/db/initialized.flag + ``` + +**Check mode support:** + +- All tasks should support check mode (`--check`). +- Use `check_mode: false` only when required (for example, fact gathering). +- Test that the role works with `--check --diff`. + +**Test coverage requirement:** + +- When you add new tasks to `tasks/main.yml` or create new task files, add matching coverage in `tests/`. +- New functionality MUST include test files (`tests/tests_*.yml`) that exercise the new code paths. +- Cover both success and failure or edge cases. +- If a change adds new tasks without new or updated tests, add test coverage before merging. + +### Handlers - `handlers/**/*.yml` + +- Handlers with sensitive data must use `no_log: "{{ bootloader_secure_logging }}"`. + +### Test playbooks - `tests/tests_*.yml` + +**CRITICAL: Role invocation pattern:** + +- NEVER use `ansible.builtin.include_role` directly. +- NEVER use `ansible.builtin.import_role` directly. +- NEVER use the `roles:` keyword. +- ALMOST ALWAYS use the centrally managed wrapper: + + ```yaml + - name: Run role + ansible.builtin.include_tasks: tasks/run_role_with_clear_facts.yml + vars: + bootloader_: + ``` + +- The only exception is `tests_invalid_inputs.yml`, where you can use `include_role` directly to test invalid inputs. +- It is not required, but it can help in some cases. + +**Test quality requirements:** + +- Cover both success and failure scenarios. +- Use the `assert` module to verify expected state after role execution. +- Include cleanup tasks so tests are rerunnable. +- Run tests in a `block` with an `always` section that runs cleanup. +- Tag cleanup tasks with `tests::cleanup` so you can skip cleanup while debugging. +- Tests must be idempotent - a second run must not fail, and must not change anything. +- This is difficult to test. +- Many roles track changes in each task, then use `set_fact` to set a variable you can assert on - for example, `__kernel_settings_changed` in the `kernel_settings` role tests. +- Example verification: + + ```yaml + - name: Verify service is running + ansible.builtin.assert: + that: + - ansible_facts.services['mssql-server.service'].state == 'running' + fail_msg: "SQL Server service is not running" + ``` + +### Templates - `templates/**/*.j2` + +Required headers, in this order: + +1. `ansible_managed` header: `{{ ansible_managed | comment }}` +2. Role fingerprint: `{{ "system_role:bootloader" | comment(prefix="", postfix="") }}` + +### Variable definitions - `defaults/**/*.yml` + +- All variables MUST be prefixed with `bootloader_`. +- All variables which have a definite default value must be declared in `defaults/main.yml`. +- Ansible does not load other files from `defaults/`. +- These are user-facing API variables. +- For every new variable, document it in `README.md` (description and usage example) and in `meta/argument_specs.yml`. +- If either is missing, add it. +- If a variable should have **no default** (remain undefined unless the user sets it), do NOT add it to `defaults/main.yml`. +- Declare and document it only in `meta/argument_specs.yml` (see below). +- A placeholder or empty default defeats the purpose of leaving it undefined. +- In role code, use `is defined` or `| default()` to guard against undefined variable access. + +### Variable definitions - `vars/**/*.yml` + +- Internal variables MUST be prefixed with `__bootloader_`. +- User-facing variables belong in `defaults/main.yml`, not here. + +### Role argument validation - `meta/argument_specs.yml` + +`argument_specs.yml` is the authoritative documentation and type or shape validation for every role variable. +Keep it in sync when you add, rename, or change a variable. + +- **Document every user-facing variable** here: type, whether it is required, its default (if any), `choices`, and a clear `description`. +- Document nested suboptions (dicts or lists of dicts) the same way, recursively. +- **Variables that should stay undefined by default** (no sensible default, and the role or module must see them as unset unless the user sets them) must NOT get a default in `defaults/main.yml`. +- Declare them only in `argument_specs.yml` (without a `default:` key) so Ansible documents and type-checks them while they stay undefined at runtime. +- Do not add a dummy or empty default in `defaults/main.yml` just to define the variable. +- If a variable can be undefined, only check `when: var is defined` - for example, variables declared in `meta/argument_specs.yml` but not in `defaults/main.yml`. +- `argument_specs.yml` validation is limited: it checks types and shapes, but it cannot express cross-field constraints (for example, "`a` requires `b`"), cannot fully validate polymorphic or `raw`-typed fields that accept more than one type, and runs before the role gathers its own facts. +- When you need validation beyond that, add checks in `tasks/assert_role_vars.yml` (create the file if needed, and run it near the start of `tasks/main.yml`). +- Use `tasks/assert_role_vars.yml` when: + - A value depends on another variable (cross-field validation). + - A field accepts multiple shapes or types that `argument_specs.yml` cannot tell apart (for example, a `raw` type that may be a string or a dict). + - See the `network` role's `assert_connection_vars.yml` for an example. + - The check needs gathered facts or other runtime state, not only the variable value. + - Keep these checks minimal. + - Prefer `argument_specs.yml` for everything it can express; use `assert_role_vars.yml` only for the rest. + +### Python code - `**/*.py` + +- Follow PEP 8 and format with Python Black. +- Run `tox -e black,flake8` before you commit. + +### Documentation - `README.md` + +- Document all new user-facing variables. +- Include usage examples for new functionality.