Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Use vars/RedHat_N.yml symlink for CentOS, Rocky, Alma wherever possible #74

Merged
merged 1 commit into from
Oct 25, 2024
Merged
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
2 changes: 2 additions & 0 deletions inventory/group_vars/active_roles.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ present_templates:
- .github/workflows/weekly_ci.yml
- .github/workflows/woke.yml
- README-ansible.md
- tests/vars/rh_distros_vars.yml
absent_files:
- .github/workflows/commitlint.yml
- .github/workflows/tox.yml
Expand Down Expand Up @@ -60,3 +61,4 @@ lsr_name: linux_system_roles
lsr_role_namespace: linux_system_roles # for ansible-lint
gha_checkout_action: actions/checkout@v4
tox_lsr_url: "git+https://github.com/linux-system-roles/[email protected]"
lsr_rh_distros: "{{ ['AlmaLinux', 'CentOS', 'RedHat', 'Rocky'] + lsr_rh_distros_extra | d([]) }}"
1 change: 1 addition & 0 deletions inventory/host_vars/network.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ present_badges:
- "[![Coverage Status](https://coveralls.io/repos/github/linux-system-roles/network/badge.svg)](https://coveralls.io/github/linux-system-roles/network)"
- "[![Code Style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/ambv/black)"
- "[![Language grade: Python](https://img.shields.io/lgtm/grade/python/g/linux-system-roles/network.svg?logo=lgtm&logoWidth=18)](https://lgtm.com/projects/g/linux-system-roles/network/context:python)"
lsr_rh_distros_extra: [OracleLinux]
7 changes: 7 additions & 0 deletions playbooks/scripts/update_meta_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@
if role != "mssql":
tags.add("el10")

# uncomment when all of the roles have full support
# if not "rocky" in tags:
# tags.add("rocky")

# if not "almalinux" in tags:
# tags.add("almalinux")

def normalize_key(key):
ary = [key]
match = re.match("([a-z]+)([0-9]+)", key)
Expand Down
16 changes: 16 additions & 0 deletions playbooks/templates/tests/vars/rh_distros_vars.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# vars for handling conditionals for RedHat and clones
# DO NOT EDIT - file is auto-generated
richm marked this conversation as resolved.
Show resolved Hide resolved
# repo is https://github.com/linux-system-roles/.github
# file is playbooks/templates/tests/vars/rh_distros_vars.yml
---
# Ansible distribution identifiers that the role treats like RHEL
__{{ inventory_hostname }}_rh_distros:
{{ lsr_rh_distros | sort | to_nice_yaml(indent=2) | indent(width=2, first=true) }}
# Same as above but includes Fedora
__{{ inventory_hostname }}_rh_distros_fedora: "{{ '{{' }} __{{ inventory_hostname }}_rh_distros + ['Fedora'] {{ '}}' }}"

# Use this in conditionals to check if distro is Red Hat or clone
__{{ inventory_hostname }}_is_rh_distro: "{{ '{{' }} ansible_distribution in __{{ inventory_hostname }}_rh_distros {{ '}}' }}"

# Use this in conditionals to check if distro is Red Hat or clone, or Fedora
__{{ inventory_hostname }}_is_rh_distro_fedora: "{{ '{{' }} ansible_distribution in __{{ inventory_hostname }}_rh_distros_fedora {{ '}}' }}"
121 changes: 110 additions & 11 deletions playbooks/update_files.yml
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@
path: "{{ git_dir }}/README.md"
regex: \[\!.*
firstmatch: true
line: "{%-for file in basenames -%}\
line: "{%- for file in basenames -%}\
[![{{ file }}]\
(https://github.com/linux-system-roles/{{ inventory_hostname }}/actions/workflows/{{ file }}/badge.svg)]\
(https://github.com/linux-system-roles/{{ inventory_hostname }}/actions/workflows/{{ file }})\
Expand Down Expand Up @@ -203,6 +203,7 @@
fi
fi
done
changed_when: true

- name: Create ignore file for ansible-test 2.17
shell:
Expand All @@ -221,6 +222,7 @@
rm .sanity-ansible-ignore-2.17.txt
fi
fi
changed_when: true

- name: Create vars, ostree files for el10
shell:
Expand All @@ -241,6 +243,7 @@
git add "$dest"
fi
done
changed_when: true

- name: Add el10 to meta/main.yml
script: scripts/update_meta_main.py {{ __meta_main | quote }}
Expand All @@ -249,6 +252,91 @@
vars:
__meta_main: "{{ git_dir }}/meta/main.yml"

# NOTE: Due to https://github.com/linux-system-roles/cockpit/issues/130
# we cannot automatically create symlinks for OracleLinux
- name: Create vars symlinks for CentOS, Rocky, Alma
shell:
chdir: "{{ git_dir }}"
cmd: |
set -euxo pipefail
same_vars_files() {
python -c 'import sys; import yaml
a = yaml.safe_load(open(sys.argv[1]))
b = yaml.safe_load(open(sys.argv[2]))
rv = 0 if a == b else 1
sys.exit(rv)' "$1" "$2"
}
for dir in vars roles/rsyslog/vars tests/vars; do
if [ ! -d "$dir" ]; then continue; fi
pushd "$dir"
for file in RedHat_*.yml RedHat-*.yml; do
if [[ "$file" =~ ^RedHat([_-])([0-9]+).yml$ ]]; then
sep="${BASH_REMATCH[1]}"
ver="${BASH_REMATCH[2]}"
for clone in CentOS Rocky AlmaLinux; do
clone_file="${clone}${sep}$ver.yml"
if [ "$clone" = Rocky ] || [ "$clone" = AlmaLinux ]; then
if [ "$ver" = 6 ] || [ "$ver" = 7 ]; then
if [ -e "$clone_file" ]; then
git rm -f "$clone_file" # if there is already a file or link, remove it
fi
continue # no 6 or 7 for Alma or Rocky
fi
fi
if [ ! -e "$clone_file" ]; then
ln -s "$file" "$clone_file"
git add "$clone_file"
elif [ -f "$clone_file" ] && same_vars_files "$file" "$clone_file"; then
git rm -f "$clone_file"
ln -s "$file" "$clone_file"
git add "$clone_file"
else
echo {{ inventory_hostname }} "$clone_file" exists and is different than "$file"
fi
done
fi
done
popd
done
changed_when: true

- name: See if vars main exists
stat:
path: "{{ git_dir }}/vars/main.yml"
register: __vars_main

# some roles have no vars/ and use defaults/main.yml instead
- name: Add vars block to main
ansible.builtin.blockinfile:
path: "{{ git_dir ~ '/vars/main.yml' if __vars_main.stat.exists
else git_dir ~ '/defaults/main.yml' }}"
insertafter: EOF
marker: "# {mark} - DO NOT EDIT THIS BLOCK - rh distros variables"
prepend_newline: true
block: |
# Ansible distribution identifiers that the role treats like RHEL
__{{ inventory_hostname }}_rh_distros:
{{ lsr_rh_distros | sort | to_nice_yaml(indent=2) | indent(width=2, first=true) }}
# Same as above but includes Fedora
__{{ inventory_hostname }}_rh_distros_fedora: "{{ '{{' }} __{{ inventory_hostname }}_rh_distros + ['Fedora'] {{ '}}' }}"

# Use this in conditionals to check if distro is Red Hat or clone
__{{ inventory_hostname }}_is_rh_distro: "{{ '{{' }} ansible_distribution in __{{ inventory_hostname }}_rh_distros {{ '}}' }}"

# Use this in conditionals to check if distro is Red Hat or clone, or Fedora
__{{ inventory_hostname }}_is_rh_distro_fedora: "{{ '{{' }} ansible_distribution in __{{ inventory_hostname }}_rh_distros_fedora {{ '}}' }}"
register: __vars_main_block

- name: Add to git transaction
command:
cmd: git add {{ path | quote }}
chdir: "{{ git_dir }}"
when: __vars_main_block is changed
changed_when: true
vars:
path: "{{ 'vars/main.yml' if __vars_main.stat.exists
else 'defaults/main.yml' }}"

- name: Create git commit, PR
changed_when: false
shell:
Expand All @@ -271,22 +359,30 @@
# nothing to do
echo No updates for role {{ inventory_hostname }}
exit 0
fi
commit_file={{ update_files_commit_file | quote }}
branch={{ update_files_branch | quote }}
if [ "{{ __is_new | ternary('true', 'false') }}" = true ]; then
git commit -s -F "$commit_file"
else
git commit -s -F {{ update_files_commit_file | quote }}
force_flag="{{ __is_new | ternary('', '-f') }}"
if [ {{ lsr_dry_run | d('true') }} = false ]; then
git push $force_flag -u origin {{ update_files_branch | quote }}
echo
echo git push $force_flag -u origin {{ update_files_branch | quote }}
fi
# commit orig head, then amend, and ensure commit is signed
git commit -C ORIG_HEAD
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is needed so that you can edit existing PRs

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right now I see, that's smart

git commit -s --amend -F "$commit_file"
fi
force_flag="{{ __is_new | ternary('', '-f') }}"
if [ {{ lsr_dry_run | d('true') }} = false ]; then
git push $force_flag -u origin "$branch"
echo
echo git push $force_flag -u origin "$branch"
fi
prnum="$(gh pr list -H {{ update_files_branch | quote }} --json number --jq '.[].number')"
prnum="$(gh pr list -H "$branch" --json number --jq '.[].number')"
if [ -z "$prnum" ]; then
cmd=(gh pr create --assignee @me --fill --base {{ __main_br | quote }})
if [ {{ lsr_dry_run | d('true') }} = false ]; then
retries=0
backoff=10
while [ "$retries" -lt 10 ]; do
if gh pr create --fill --base "{{ __main_br }}"; then
if "${cmd[@]}"; then
break
else
echo backing off "$backoff" seconds . . .
Expand All @@ -296,11 +392,14 @@
fi
done
else
echo gh pr create --fill --base "{{ __main_br }}"
echo "${cmd[@]}"
fi
else
echo Existing PR "$prnum"
echo {{ github_url_prefix ~ github_org ~ '/' ~ inventory_hostname ~ '/pull/' }}$prnum
# update PR assignee, title, and body
tail -n +3 "$commit_file" | \
gh pr edit "$prnum" -F - --add-assignee @me --title "$(head -1 "$commit_file")"
fi
always:
- name: Clean up temp working dir
Expand Down