refactor(packages): directly pipe python script into ansible

This commit is contained in:
Matthieu Bessat 2024-07-11 12:05:59 +02:00
parent 8945449613
commit bc98c00950
4 changed files with 85 additions and 74 deletions

View file

@ -1,13 +1,7 @@
import argparse
import json import json
import sys import sys
import yaml import yaml
import fileinput
packages_tree = None
yaml_config = ''.join(sys.stdin.readlines())
packages_tree = yaml.safe_load(yaml_config)
assert packages_tree is not None, "Must have load valid package tree"
def flatten_packages(item): def flatten_packages(item):
packages = [] packages = []
@ -19,24 +13,46 @@ def flatten_packages(item):
return [flatten_packages(p) for p in item] return [flatten_packages(p) for p in item]
if isinstance(item, dict): if isinstance(item, dict):
packages = [] packages = []
for k, v in item.items(): for v in item.values():
packages += flatten_packages(v) packages += flatten_packages(v)
return packages return packages
return [] return []
def output_packages(packages_tree, aur_filter: bool = False):
flat_packages = flatten_packages(packages_tree) flat_packages = flatten_packages(packages_tree)
native_names = [] names = []
aur_names = []
for p in flat_packages: for p in flat_packages:
assert isinstance(p, dict) assert isinstance(p, dict)
if p['name'].startswith('aur/'): is_aur = p['name'].startswith('aur/')
aur_names.append(p['name'].split('/')[1]) if (aur_filter and not is_aur) or (not aur_filter and is_aur):
continue continue
native_names.append(p['name']) names.append(
p['name'].split('/')[1] if '/' in p['name'] else p['name']
)
return names
print(json.dumps({ def cli():
"native": native_names, parser = argparse.ArgumentParser(description='Optional app description')
"aur": aur_names parser.add_argument('categories', type=str, help="Categories to include, all for all")
})) parser.add_argument('--aur', action='store_true', help="Also output AUR packages")
# print(' '.join(names)) args = parser.parse_args()
# load data from stdin
packages_tree = None
yaml_config = ''.join(sys.stdin.readlines())
packages_tree = yaml.safe_load(yaml_config)
assert packages_tree is not None, "Must have load valid package tree"
tree: dict = {}
if args.categories == 'all':
tree = packages_tree
else:
selected_categories = args.categories.split(',')
for category in selected_categories:
if category not in packages_tree:
continue
tree = tree | {category: packages_tree[category]}
print(json.dumps(output_packages(tree, args.aur)))
cli()

View file

@ -33,9 +33,6 @@
- name: Load organization profile - name: Load organization profile
when: organization is defined and "ssh" in organization_customize when: organization is defined and "ssh" in organization_customize
become: true
copy: copy:
src: "{{ home }}/.dots/profiles/{{ organization }}/configs/ssh" src: "{{ home }}/.dots/profiles/{{ organization }}/configs/ssh"
dest: "{{ home }}/.ssh/profiles/{{ organization }}" dest: "{{ home }}/.ssh/profiles/{{ organization }}"
template:

View file

@ -10,14 +10,6 @@ export ANSIBLE_CONFIG=$base/ansible.cfg
#export ANSIBLE_DEBUG=1 #export ANSIBLE_DEBUG=1
export ANSIBLE_LOG_PATH=ansible_run.log export ANSIBLE_LOG_PATH=ansible_run.log
cd $base
cat arch_packages.yaml | python3 parse_arch_packages.py > arch_packages.json
cd $workdir
cd $base
cat pip_packages.yaml | python3 parse_arch_packages.py > pip_packages.json
cd $workdir
rm $base/vm_files rm $base/vm_files
ln -s $workdir $base/vm_files ln -s $workdir $base/vm_files

View file

@ -117,11 +117,48 @@
- shell: "rm -rf /etc/pacman.d/gnupg && pacman-key --init && pacman-key --populate archlinux" - shell: "rm -rf /etc/pacman.d/gnupg && pacman-key --init && pacman-key --populate archlinux"
- shell: "mkdir -p {{ home }}/.cache/monakhos; echo -n $(date --iso-8601=d) > {{ home }}/.cache/monakhos/pacman_key_state" - shell: "mkdir -p {{ home }}/.cache/monakhos; echo -n $(date --iso-8601=d) > {{ home }}/.cache/monakhos/pacman_key_state"
# INSTALL from YAML # AUR SETUP
- name: Install packages from YAML files (excluding AUR) - name: Create the aur_builder user
become: yes
ansible.builtin.user:
name: aur_builder
create_home: yes
group: wheel
- name: Allow the `aur_builder` user to run `sudo pacman` without a password
become: yes
ansible.builtin.lineinfile:
path: /etc/sudoers.d/11-install-aur_builder
line: 'aur_builder ALL=(ALL) NOPASSWD: /usr/bin/pacman'
create: yes
mode: 0644
validate: 'visudo -cf %s'
- name: Install yay
include_role:
name: aur
vars:
packages:
- yay-bin
- name: Stub
file:
path: "{{ home }}/.stub"
state: touch
# INSTALL normal packages from YAML
- name: Install non-AUR packages
become: true become: true
community.general.pacman: community.general.pacman:
name: "{{ (lookup('file', 'arch_packages.json') | from_json)['native'] }}" # the python script will return a list of packages name: "{{ lookup('pipe', ('cat arch_packages.yaml | python3 parse_arch_packages.py ' + item)) | from_json }}"
with_items: "{{ packages_categories }}"
- name: Install AUR packages
include_role:
name: aur
vars:
packages: "{{ lookup('pipe', ('cat arch_packages.yaml | python3 parse_arch_packages.py --aur ' + item)) | from_json }}"
with_items: "{{ packages_categories }}"
- name: Install sway - name: Install sway
include_role: include_role:
@ -175,41 +212,10 @@
src: fish/machine.fish src: fish/machine.fish
dest: "{{ home }}/.config/fish/machine.fish" dest: "{{ home }}/.config/fish/machine.fish"
- name: Create the aur_builder user
become: yes
ansible.builtin.user:
name: aur_builder
create_home: yes
group: wheel
- name: Allow the `aur_builder` user to run `sudo pacman` without a password
become: yes
ansible.builtin.lineinfile:
path: /etc/sudoers.d/11-install-aur_builder
line: 'aur_builder ALL=(ALL) NOPASSWD: /usr/bin/pacman'
create: yes
mode: 0644
validate: 'visudo -cf %s'
- name: Setup xremap - name: Setup xremap
include_role: include_role:
name: xremap name: xremap
# AUR packages
- name: Install yay
import_role:
name: aur
vars:
packages:
- yay-bin
- name: Install AUR packages from YAML file
become: true
import_role:
name: aur
vars:
packages: "{{ (lookup('file', 'arch_packages.json') | from_json)['aur'] }}"
# SYSTEMD user services # SYSTEMD user services
- name: Setup systemd user services folder - name: Setup systemd user services folder
file: file:
@ -315,7 +321,7 @@
- name: Install pip packages - name: Install pip packages
community.general.pipx: community.general.pipx:
name: "{{ item }}" name: "{{ item }}"
loop: "{{ (lookup('file', 'pip_packages.json') | from_json)['native'] }}" with_items: "{{ lookup('pipe', 'cat pip_packages.yaml | python3 parse_arch_packages.py all') | from_json }}"
- name: Enable bluetooth service - name: Enable bluetooth service
become: true become: true