diff --git a/roles/kthxbye/defaults/main.yaml b/roles/kthxbye/defaults/main.yaml new file mode 100644 index 0000000..7e64756 --- /dev/null +++ b/roles/kthxbye/defaults/main.yaml @@ -0,0 +1,47 @@ +--- +kthxbye_go_arch_map: + i386: '386' + x86_64: 'amd64' + +kthxbye_go_arch: "{{ kthxbye_go_arch_map[ansible_architecture] | default('amd64') }}" + +kthxbye_sidecar_service_name: kthxbye-sidecar.service +kthxbye_sidecar_service_enabled: true +kthxbye_sidecar_service_state: started + +kthxbye_service_name: kthxbye.service +kthxbye_service_enabled: true +kthxbye_service_state: started + +kthxbye_version_regex: (.+) + +kthxbye_checksum_algo: sha512 +kthxbye_github_rel_path: prymitive/kthxbye +kthxbye_github_project_url: "https://github.com/{{ kthxbye_github_rel_path }}" +kthxbye_release_file: "kthxbye-{{ ansible_system | lower }}-{{ kthxbye_go_arch }}.tar.gz" +kthxbye_release_url: "{{ kthxbye_github_project_url }}/releases/download/v{{ kthxbye_version }}/{{ kthxbye_release_file }}" +kthxbye_checksum_url: "{{ kthxbye_github_project_url }}/releases/download/v{{ kthxbye_version }}/{{ kthxbye_checksum_algo }}sum.txt" +kthxbye_download_path: "/tmp/{{ kthxbye_release_file }}" +kthxbye_unarchive_dest_path: /tmp +kthxbye_extracted_path: "{{ kthxbye_download_path | replace('.tar.gz', '') }}" +kthxbye_binaries: + - kthxbye + +kthxbye_user: kthxbye +kthxbye_user_state: present +kthxbye_user_shell: /usr/sbin/nologin + +kthxbye_group: kthxbye +kthxbye_group_state: "{{ kthxbye_user_state | default('present') }}" + +kthxbye_etc_path: /etc/kthxbye +kthxbye_etc_owner: root +kthxbye_etc_group: root +kthxbye_etc_mode: "0755" + +kthxbye_var_path: /var/lib/kthxbye +kthxbye_var_owner: "{{ kthxbye_user }}" +kthxbye_var_group: "{{ kthxbye_group }}" +kthxbye_var_mode: "0755" + +kthxbye_bin_path: /usr/local/bin diff --git a/roles/kthxbye/handlers/main.yaml b/roles/kthxbye/handlers/main.yaml new file mode 100644 index 0000000..17363bf --- /dev/null +++ b/roles/kthxbye/handlers/main.yaml @@ -0,0 +1,6 @@ +--- +- name: restart kthxbye + systemd: + name: kthxbye.service + daemon_reload: true + state: restarted diff --git a/roles/kthxbye/tasks/configure.yaml b/roles/kthxbye/tasks/configure.yaml new file mode 100644 index 0000000..ab178b3 --- /dev/null +++ b/roles/kthxbye/tasks/configure.yaml @@ -0,0 +1,47 @@ +--- +- name: create group + group: + name: "{{ kthxbye_group }}" + system: true + state: "{{ kthxbye_group_state | default('present') }}" + +- name: create user + user: + name: "{{ kthxbye_user }}" + system: true + shell: "{{ kthxbye_user_shell }}" + group: "{{ kthxbye_group }}" + createhome: false + home: "{{ kthxbye_var_path }}" + state: "{{ kthxbye_user_state | default('present') }}" + +- name: create etc path + file: + path: "{{ kthxbye_etc_path }}" + state: directory + owner: "{{ kthxbye_etc_owner }}" + group: "{{ kthxbye_etc_group }}" + mode: "{{ kthxbye_etc_mode }}" + +- name: create var path + file: + path: "{{ kthxbye_var_path }}" + state: directory + owner: "{{ kthxbye_var_owner }}" + group: "{{ kthxbye_var_group }}" + mode: "{{ kthxbye_var_mode }}" + +- name: configure systemd template + template: + src: kthxbye.service.j2 + dest: /etc/systemd/system/kthxbye.service + owner: root + group: root + mode: 0444 + notify: restart kthxbye + +- name: manage service + service: + name: "{{ kthxbye_service_name }}" + enabled: "{{ kthxbye_service_enabled }}" + state: "{{ kthxbye_service_state }}" diff --git a/roles/kthxbye/tasks/default.yaml b/roles/kthxbye/tasks/default.yaml new file mode 100644 index 0000000..e69de29 diff --git a/roles/kthxbye/tasks/install.yaml b/roles/kthxbye/tasks/install.yaml new file mode 100644 index 0000000..ee63577 --- /dev/null +++ b/roles/kthxbye/tasks/install.yaml @@ -0,0 +1,30 @@ +--- +- block: + - name: download tar + get_url: + url: "{{ kthxbye_release_url }}" + dest: "{{ kthxbye_download_path }}" + checksum: "{{ kthxbye_checksum }}" + register: dl + until: dl is success + retries: 5 + delay: 10 + + - name: extract tar + unarchive: + src: "{{ kthxbye_download_path }}" + dest: "{{ kthxbye_unarchive_dest_path }}" + creates: "{{ kthxbye_extracted_path }}" + remote_src: true + + - name: install binaries + copy: + src: "{{ kthxbye_extracted_path }}" + dest: "{{ kthxbye_bin_path }}/{{ item }}" + owner: root + group: root + mode: 0755 + remote_src: true + loop: "{{ kthxbye_binaries }}" + notify: restart kthxbye + when: kthxbye_version != kthxbye_local_version diff --git a/roles/kthxbye/tasks/main.yaml b/roles/kthxbye/tasks/main.yaml new file mode 100644 index 0000000..89aed0c --- /dev/null +++ b/roles/kthxbye/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/kthxbye/tasks/pre.yaml b/roles/kthxbye/tasks/pre.yaml new file mode 100644 index 0000000..c57a166 --- /dev/null +++ b/roles/kthxbye/tasks/pre.yaml @@ -0,0 +1,54 @@ +--- +- name: determine if installed + stat: + path: "{{ kthxbye_bin_path }}/kthxbye" + register: st + +- name: set kthxbye_installed + set_fact: + kthxbye_installed: "{{ st.stat.exists | bool }}" + +- block: + - name: determine latest version + uri: + url: "https://api.github.com/repos/{{ kthxbye_github_rel_path }}/releases/latest" + return_content: true + body_format: json + register: _latest_version + until: _latest_version.status == 200 + retries: 3 + + - name: set kthxbye_version + set_fact: + kthxbye_version: "{{ _latest_version.json['tag_name'] | regex_replace('^v', '') }}" + +- block: + - name: determine installed version + command: "{{ kthxbye_bin_path }}/kthxbye --version" + register: _installed_version_string + changed_when: false + + - name: set kthxbye_local_version + set_fact: + kthxbye_local_version: "{{ _installed_version_string.stdout | regex_search(kthxbye_version_regex, '\\1') | first }}" + rescue: + - name: set kthxbye_local_version + set_fact: + kthxbye_local_version: "{{ _installed_version_string.stderr | regex_search(kthxbye_version_regex, '\\1') | first }}" + when: kthxbye_installed + +- name: set kthxbye_local_version to 0 + set_fact: + kthxbye_local_version: "0" + when: not kthxbye_installed + +- block: + - name: get checksums + set_fact: + _checksums: "{{ lookup('url', kthxbye_checksum_url, wantlist=True) }}" + + - name: set kthxbye_checksum + set_fact: + kthxbye_checksum: "sha512:{{ item.split(' ') | first }}" + loop: "{{ _checksums }}" + when: "kthxbye_release_file in item" diff --git a/roles/kthxbye/templates/kthxbye.service.j2 b/roles/kthxbye/templates/kthxbye.service.j2 new file mode 100644 index 0000000..886aece --- /dev/null +++ b/roles/kthxbye/templates/kthxbye.service.j2 @@ -0,0 +1,20 @@ +{{ ansible_managed | comment }} + +[Unit] +Description=Kthxbye +Wants=network-online.target +After=network-online.target +After=alertmanager.service + +[Service] +Type=simple +User={{ kthxbye_user }} +Group={{ kthxbye_group }} +WorkingDirectory={{ kthxbye_etc_path }} +ExecStart={{ kthxbye_bin_path }}/kthxbye \ +{% if kthxbye_listen %} + -listen={{ kthxbye_listen }} +{% endif %} + +[Install] +WantedBy=multi-user.target diff --git a/roles/kthxbye/vars/default.yaml b/roles/kthxbye/vars/default.yaml new file mode 100644 index 0000000..e69de29