diff --git a/roles/karma/defaults/main.yaml b/roles/karma/defaults/main.yaml new file mode 100644 index 0000000..65ee988 --- /dev/null +++ b/roles/karma/defaults/main.yaml @@ -0,0 +1,45 @@ +--- +karma_go_arch_map: + i386: '386' + x86_64: 'amd64' + +karma_go_arch: "{{ karma_go_arch_map[ansible_architecture] | default('amd64') }}" + +karma_service_name: karma.service +karma_service_enabled: true +karma_service_state: started + +karma_version_regex: ^(.+) + +karma_github_project_url: https://github.com/prymitive/karma +karma_release_file: "karma-{{ ansible_system | lower }}-{{ karma_go_arch }}.tar.gz" +karma_release_url: "{{ karma_github_project_url }}/releases/download/v{{ karma_version }}/{{ karma_release_file }}" +karma_checksum_url: "{{ karma_github_project_url }}/releases/download/v{{ karma_version }}/sha512sum.txt" +karma_download_path: "/tmp/{{ karma_release_file }}" +karma_unarchive_dest_path: /tmp +karma_extracted_path: "{{ karma_download_path | replace('.tar.gz', '') }}" + +karma_user: karma +karma_user_state: present +karma_user_shell: /usr/sbin/nologin + +karma_group: karma +karma_group_state: "{{ karma_user_state | default('present') }}" + +karma_etc_path: /etc/karma +karma_etc_owner: root +karma_etc_group: root +karma_etc_mode: "0755" + +karma_config_path: "{{ karma_etc_path }}/karma.yml" + +karma_var_path: /var/lib/karma +karma_var_owner: "{{ karma_user }}" +karma_var_group: "{{ karma_group }}" +karma_var_mode: "0755" + +karma_bin_path: /usr/local/bin + +karma_port: 8080 + +karma_config: {} diff --git a/roles/karma/handlers/main.yaml b/roles/karma/handlers/main.yaml new file mode 100644 index 0000000..9912df0 --- /dev/null +++ b/roles/karma/handlers/main.yaml @@ -0,0 +1,6 @@ +--- +- name: restart karma + systemd: + name: karma.service + daemon_reload: true + state: restarted diff --git a/roles/karma/tasks/configure.yaml b/roles/karma/tasks/configure.yaml new file mode 100644 index 0000000..9cfe8ce --- /dev/null +++ b/roles/karma/tasks/configure.yaml @@ -0,0 +1,56 @@ +--- +- name: create group + group: + name: "{{ karma_group }}" + system: true + state: "{{ karma_group_state | default('present') }}" + +- name: create user + user: + name: "{{ karma_user }}" + system: true + shell: "{{ karma_user_shell }}" + group: "{{ karma_group }}" + createhome: false + home: "{{ karma_var_path }}" + state: "{{ karma_user_state | default('present') }}" + +- name: create etc path + file: + path: "{{ karma_etc_path }}" + state: directory + owner: "{{ karma_etc_owner }}" + group: "{{ karma_etc_group }}" + mode: "{{ karma_etc_mode }}" + +- name: create var path + file: + path: "{{ karma_var_path }}" + state: directory + owner: "{{ karma_var_owner }}" + group: "{{ karma_var_group }}" + mode: "{{ karma_var_mode }}" + +- name: configure + copy: + dest: "{{ karma_config_path }}" + content: "{{ (karma_config | default({})) | to_nice_yaml }}" + owner: root + group: root + mode: 0444 + notify: restart karma + +- name: configure systemd template + template: + src: karma.service.j2 + dest: /etc/systemd/system/karma.service + owner: root + group: root + mode: 0444 + notify: restart karma + +- name: manage service + service: + name: "{{ karma_service_name }}" + enabled: "{{ karma_service_enabled }}" + state: "{{ karma_service_state }}" diff --git a/roles/karma/tasks/default.yaml b/roles/karma/tasks/default.yaml new file mode 100644 index 0000000..e69de29 diff --git a/roles/karma/tasks/install.yaml b/roles/karma/tasks/install.yaml new file mode 100644 index 0000000..a83ccb9 --- /dev/null +++ b/roles/karma/tasks/install.yaml @@ -0,0 +1,29 @@ +--- +- block: + - name: download tar + get_url: + url: "{{ karma_release_url }}" + dest: "{{ karma_download_path }}" + checksum: "{{ karma_checksum }}" + register: dl + until: dl is success + retries: 5 + delay: 10 + + - name: extract tar + unarchive: + src: "{{ karma_download_path }}" + dest: "{{ karma_unarchive_dest_path }}" + creates: "{{ karma_extracted_path }}" + remote_src: true + + - name: install binaries + copy: + src: "{{ karma_extracted_path }}" + dest: "{{ karma_bin_path }}/karma" + owner: root + group: root + mode: 0755 + remote_src: true + notify: restart karma + when: karma_version != karma_local_version diff --git a/roles/karma/tasks/main.yaml b/roles/karma/tasks/main.yaml new file mode 100644 index 0000000..89aed0c --- /dev/null +++ b/roles/karma/tasks/main.yaml @@ -0,0 +1,30 @@ +--- +- name: gather os specific variables + include_vars: "{{ lookup('first_found', possible_files) }}" + vars: + possible_files: + files: + - "{{ ansible_distribution }}-{{ ansible_distribution_version }}.yaml" + - "{{ ansible_distribution }}.yaml" + - "{{ ansible_os_family }}.yaml" + - "default.yaml" + paths: + - vars + +- name: include os specific tasks + include_tasks: "{{ lookup('first_found', possible_files) }}" + vars: + possible_files: + files: + - "{{ ansible_distribution }}-{{ ansible_distribution_version }}.yaml" + - "{{ ansible_distribution }}.yaml" + - "{{ ansible_os_family }}.yaml" + - "default.yaml" + paths: + - tasks + +- include: pre.yaml + +- include: install.yaml + +- include: configure.yaml diff --git a/roles/karma/tasks/pre.yaml b/roles/karma/tasks/pre.yaml new file mode 100644 index 0000000..8bbf32e --- /dev/null +++ b/roles/karma/tasks/pre.yaml @@ -0,0 +1,50 @@ +--- +- name: determine if installed + stat: + path: "{{ karma_bin_path }}/karma" + register: st + +- name: set karma_installed + set_fact: + karma_installed: "{{ st.stat.exists | bool }}" + +- block: + - name: determine latest version + uri: + url: https://api.github.com/repos/prymitive/karma/releases/latest + return_content: true + body_format: json + register: _latest_version + until: _latest_version.status == 200 + retries: 3 + + - name: set karma_version + set_fact: + karma_version: "{{ _latest_version.json['tag_name'] | regex_replace('^v', '') }}" + +- block: + - name: determine installed version + command: "{{ karma_bin_path }}/karma --version" + register: _installed_version_string + changed_when: false + + - name: set karma_local_version + set_fact: + karma_local_version: "{{ _installed_version_string.stdout | regex_search(karma_version_regex, '\\1') | first }}" + when: karma_installed + +- name: set karma_local_version to 0 + set_fact: + karma_local_version: "0" + when: not karma_installed + +- block: + - name: get checksums + set_fact: + _checksums: "{{ lookup('url', karma_checksum_url, wantlist=True) }}" + + - name: set karma_checksum + set_fact: + karma_checksum: "sha512:{{ item.split(' ') | first }}" + loop: "{{ _checksums }}" + when: "karma_release_file in item" diff --git a/roles/karma/templates/karma.service.j2 b/roles/karma/templates/karma.service.j2 new file mode 100644 index 0000000..fa583d3 --- /dev/null +++ b/roles/karma/templates/karma.service.j2 @@ -0,0 +1,18 @@ +{{ ansible_managed | comment }} + +[Unit] +Description=Karma Alertmanager dashboard +Wants=network-online.target +After=network-online.target +After=alertmanager.service + +[Service] +Type=simple +User={{ karma_user }} +Group={{ karma_group }} +WorkingDirectory={{ karma_etc_path }} +ExecStart={{ karma_bin_path }}/karma \ + --config.file={{ karma_config_path }} + +[Install] +WantedBy=multi-user.target diff --git a/roles/karma/vars/default.yaml b/roles/karma/vars/default.yaml new file mode 100644 index 0000000..e69de29