Skip to content

Instantly share code, notes, and snippets.

@Wolf480pl
Created April 29, 2023 22:15
Show Gist options
  • Save Wolf480pl/85cb1ee7d5f013e1737427cb1b45df01 to your computer and use it in GitHub Desktop.
Save Wolf480pl/85cb1ee7d5f013e1737427cb1b45df01 to your computer and use it in GitHub Desktop.
# This is the bullshit we have to do, because:
# 1. alertmanager upstream insists on embedding their assets inside the binary
# this is against Debian Policy, so Debian has to patch it out downstream, but
# 2. there's no elm-compiler package in Debian
# so Debian can't build script.js from source
#
# instead Debian provides a horrible generate-ui.sh script that downloads binary elm compiler
# from github and runs it as root without any hash verification or anything
#
# This means that neither Debian nor upsteram provide a prebuilt JS in a sane format.
# Fortunately upstream commits their generated golang source with embedded vfs into git
# which is silly but in this case it helps
- name: Install frontend dependencies
become: "{{ not ansible_check_mode }}"
ansible.builtin.package:
state: present
name:
- libjs-bootstrap4
- fonts-font-awesome
- golang-github-prometheus-alertmanager-dev
- name: Detect alertmanager version
check_mode: false
changed_when: false
ansible.builtin.shell:
cmd: "dpkg-query -W prometheus-alertmanager |cut -f2 |cut -d+ -f1"
register: prometheus_result_alertmanager_version
- ansible.builtin.set_fact:
prometheus_alertmanager_installed_version: "{{ prometheus_result_alertmanager_version.stdout_lines[0] }}"
- name: Create a directory for alertmanager frontend bullshit
ansible.builtin.file:
state: directory
path: "{{ prometheus_alertmanager_frontend_extract_dir }}/{{ prometheus_alertmanager_installed_version }}"
- name: Copy generated golang source with embedded alertmanager assets
ansible.builtin.copy:
remote_src: true
unsafe_writes: "{{ ansible_check_mode }}" # broken unprivileged check mode otherwise
src: /usr/share/gocode/src/github.com/prometheus/alertmanager/asset/assets_vfsdata.go
dest: "{{ prometheus_alertmanager_frontend_extract_dir }}/{{ prometheus_alertmanager_installed_version }}/assets_vfsdata.go"
- name: Extract compressed hexstring from the generated source
check_mode: false
changed_when: false
ansible.builtin.shell:
chdir: "{{ prometheus_alertmanager_frontend_extract_dir }}/{{ prometheus_alertmanager_installed_version }}"
cmd: >
awk '
/static\/{{ item }}.*vfsgen۰CompressedFileInfo/{S=1}
(S==1 && /compressedContent/){print; S=0}
' assets_vfsdata.go
|cut -d'"' -f2 |tr -d '\\x' >"{{ item }}.gz.hex"
loop:
- script.js
- name: Dehexify and decompress the extracted files
check_mode: false
changed_when: false
ansible.builtin.shell:
chdir: "{{ prometheus_alertmanager_frontend_extract_dir }}/{{ prometheus_alertmanager_installed_version }}"
cmd: >
xxd -r -p "{{ item }}.gz.hex" |zcat >"{{ item }}"
loop:
- script.js
- name: Prepare alertmanager asset dir (mkdirs)
become: "{{ not ansible_check_mode }}"
ansible.builtin.file:
state: directory
path: "{{ prometheus_alertmanager_frontend_dest_dir }}{{ item }}"
loop:
- ""
- "/lib"
- name: Prepare alertmanager asset dir (symlinks)
become: "{{ not ansible_check_mode }}"
ansible.builtin.file:
state: link
src: "{{ item.value }}"
path: "{{ prometheus_alertmanager_frontend_dest_dir }}/{{ item.key }}"
loop: "{{ links |dict2items }}"
vars:
links:
lib/font-awesome: /usr/share/fonts-font-awesome
lib/font-awesome-4.7.0: /usr/share/fonts-font-awesome
lib/bootstrap4: /usr/share/nodejs/bootstrap/dist
lib/bootstrap-4.0.0-alpha.6-dist: /usr/share/nodejs/bootstrap/dist
- name: Prepare alertmanager asset dir (copies)
become: "{{ not ansible_check_mode }}"
ansible.builtin.copy:
remote_src: true
unsafe_writes: "{{ ansible_check_mode }}" # broken unprivileged check mode otherwise
src: "{{ item.value }}"
dest: "{{ prometheus_alertmanager_frontend_dest_dir }}/{{ item.key }}"
loop: "{{ files |dict2items }}"
vars:
files:
index.html: /usr/share/gocode/src/github.com/prometheus/alertmanager/ui/app/index.html
favicon.ico: /usr/share/gocode/src/github.com/prometheus/alertmanager/ui/app/favicon.ico
script.js: "{{ prometheus_alertmanager_frontend_extract_dir }}/{{ prometheus_alertmanager_installed_version }}/script.js"
- name: Configure alertmanager args
become: "{{ not ansible_check_mode }}"
ansible.builtin.lineinfile:
path: /etc/default/prometheus-alertmanager
regex: "^ARGS=.*"
line: "ARGS=\"--web.ui-path={{ prometheus_alertmanager_frontend_dest_dir }}"
notify: prometheus-restart-alertmanager
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment