diff --git a/galaxy.yml b/galaxy.yml index d0b5340..e2fa07a 100644 --- a/galaxy.yml +++ b/galaxy.yml @@ -1,7 +1,7 @@ --- namespace: happydns name: happydomain -version: 0.2.2 +version: 0.2.3 readme: README.md authors: - happyDomain Team diff --git a/roles/happydomain/defaults/main.yml b/roles/happydomain/defaults/main.yml index bd94136..5c7d50a 100644 --- a/roles/happydomain/defaults/main.yml +++ b/roles/happydomain/defaults/main.yml @@ -1,4 +1,7 @@ --- +use_container: false +state: present + instance_name: "happyDomain" happydomain_version: "latest" diff --git a/roles/happydomain/files/happydomain.initd b/roles/happydomain/files/happydomain.initd new file mode 100644 index 0000000..5a41efa --- /dev/null +++ b/roles/happydomain/files/happydomain.initd @@ -0,0 +1,10 @@ +#!/sbin/openrc-run +# Copyright 2016-2017 Gentoo Foundation +# Distributed under the terms of the GNU General Public License v2 + +description="happyDomain Domain Managment Server" +pidfile=${pidfile:-"/run/${SVCNAME}.pid"} +command="/usr/bin/happydomain" +command_background="true" +start_stop_daemon_args="--stdout /var/log/${SVCNAME}/${SVCNAME}.log \ + --stderr /var/log/${SVCNAME}/${SVCNAME}.log -u happydomain -g happydomain" diff --git a/roles/happydomain/handlers/main.yml b/roles/happydomain/handlers/main.yml new file mode 100644 index 0000000..6a0a717 --- /dev/null +++ b/roles/happydomain/handlers/main.yml @@ -0,0 +1,9 @@ +--- +- name: restart happyDomain + ansible.builtin.service: + name: happydomain + state: restarted + +- name: reload systemd + ansible.builtin.systemd: + daemon_reload: true diff --git a/roles/happydomain/tasks/docker.yml b/roles/happydomain/tasks/docker.yml index 8250dfa..eb56397 100644 --- a/roles/happydomain/tasks/docker.yml +++ b/roles/happydomain/tasks/docker.yml @@ -1,6 +1,6 @@ --- - name: "launch happyDomain container ({{ instance_name }})" - docker_container: + community.general.docker_container: name: "{{ instance_name }}" image: "happydomain/happydomain:{{ happydomain_version }}" pull: true diff --git a/roles/happydomain/tasks/download.yml b/roles/happydomain/tasks/download.yml new file mode 100644 index 0000000..377ae89 --- /dev/null +++ b/roles/happydomain/tasks/download.yml @@ -0,0 +1,79 @@ +--- +- name: Download happydomain binary + ansible.builtin.get_url: + url: "https://get.happydomain.org/{% if happydomain_version == 'latest' %}master{% else %}{{ happydomain_version }}{% endif %}/happydomain-{{ ansible_system | lower }}-{% if ansible_architecture == 'armv7l' %}armv7{% elif ansible_architecture == 'aarch64' %}arm64{% elif ansible_architecture == 'x86_64' %}amd64{% else %}{{ ansible_architecture }}{% endif %}" + dest: /usr/bin/happydomain + mode: '0755' + +- name: "ensure happydomain group exists" + ansible.builtin.group: + name: happydomain + gid: 533 + system: true + +- name: "ensure happydomain user exists" + ansible.builtin.user: + name: happydomain + comment: happyDomain user + shell: /sbin/nologin + uid: 533 + group: happydomain + system: true + home: /var/lib/happydomain + +- name: "configure happyDomain" + ansible.builtin.template: + src: happydomain.conf.j2 + dest: "/etc/happydomain.conf" + mode: 0755 + notify: + - restart happyDomain + +- name: Create data directory + ansible.builtin.file: + path: "{% if happydomain_data_dir != '' %}{{ happydomain_data_dir }}{% else %}/var/lib/happydomain{% if instance_name != 'happyDomain' %}.{{ instance_name }}{% endif %}{% endif %}" + owner: happydomain + group: happydomain + state: directory + +- name: "setup init script for happyDomain" + ansible.builtin.copy: + src: happydomain.initd + dest: "/etc/init.d/happydomain{% if instance_name is defined and instance_name != 'happyDomain' %}.{{ instance_name }}{% endif %}" + mode: 0755 + when: ansible_service_mgr == "openrc" + +- name: "create log dir for happydomain" + ansible.builtin.file: + path: "/var/log/happydomain{% if instance_name is defined and instance_name != 'happyDomain' %}.{{ instance_name }}{% endif %}" + mode: 0755 + owner: happydomain + group: happydomain + state: directory + when: ansible_service_mgr == "openrc" + +- name: "setup systemd service for happyDomain" + ansible.builtin.template: + src: happydomain.service.j2 + dest: "/lib/systemd/system/happydomain{% if instance_name is defined and instance_name != 'happyDomain' %}-{{ instance_name }}{% endif %}.service" + mode: 0644 + notify: + - reload systemd + when: ansible_service_mgr == "systemd" + +- name: Flush handlers + meta: flush_handlers + +- name: "ensure happyDomain is running and enabled" + ansible.builtin.service: + name: "happydomain{% if instance_name is defined and instance_name != 'happyDomain' %}.{{ instance_name }}{% endif %}" + enabled: yes + state: started + when: ansible_service_mgr == "openrc" + +- name: "ensure happyDomain is running and enabled" + ansible.builtin.service: + name: "happydomain{% if instance_name is defined and instance_name != 'happyDomain' %}-{{ instance_name }}{% endif %}" + enabled: yes + state: started + when: ansible_service_mgr == "systemd" diff --git a/roles/happydomain/tasks/main.yml b/roles/happydomain/tasks/main.yml index fe9a78b..8617cb6 100644 --- a/roles/happydomain/tasks/main.yml +++ b/roles/happydomain/tasks/main.yml @@ -1,9 +1,19 @@ --- -- include_tasks: docker.yml +- block: + - include_tasks: download.yml + when: not use_container -- name: Ensure cleaning job runs every day. - cron: - name: "run {{ instance_name }} database cleaning" - hour: "3" - minute: "0" - job: "docker exec {{ instance_name }} hadmin /api/tidy -X POST" + - include_tasks: docker.yml + when: use_container + + - name: Ensure cleaning job runs every day. + ansible.builtin.cron: + name: "run {{ instance_name }} database cleaning" + hour: "3" + minute: "0" + job: "docker exec {{ instance_name }} hadmin /api/tidy -X POST" + + when: state == 'present' + +- include_tasks: remove.yml + when: state == 'absent' diff --git a/roles/happydomain/tasks/remove.yml b/roles/happydomain/tasks/remove.yml new file mode 100644 index 0000000..65f81fd --- /dev/null +++ b/roles/happydomain/tasks/remove.yml @@ -0,0 +1,33 @@ +--- +- name: Stop the service + ansible.builtin.service: + name: happydomain + state: stopped + enabled: false + +- name: Remove happydomain binary + ansible.builtin.file: + path: /usr/bin/happydomain + state: absent + +- name: Remove happydomain user + ansible.builtin.user: + name: happydomain + state: absent + +- name: Remove happydomain group + ansible.builtin.group: + name: happydomain + state: absent + +- name: Remove init script + ansible.builtin.file: + path: /etc/init.d/happydomain + state: absent + +- name: Remove systemd service + ansible.builtin.file: + path: /lib/systemd/system/happydomain.service + state: absent + notify: + - reload systemd diff --git a/roles/happydomain/templates/happydomain.conf.j2 b/roles/happydomain/templates/happydomain.conf.j2 new file mode 100644 index 0000000..bdee1c8 --- /dev/null +++ b/roles/happydomain/templates/happydomain.conf.j2 @@ -0,0 +1,58 @@ +{% if happydomain_admin_bind is defined and happydomain_admin_bind != "" %} +admin-bind={{ happydomain_admin_bind }} +{% endif %} +{% if happydomain_bind is defined and happydomain_bind != "" %} +bind={{ happydomain_bind }} +{% endif %} +{% if happydomain_baseurl is defined and happydomain_baseurl != "" %} +baseurl={{ happydomain_baseurl }} +{% endif %} +{% if happydomain_custom_head_html is defined and happydomain_custom_head_html != "" %} +custom-head-html={{ happydomain_custom_head_html }} +{% endif %} +{% if happydomain_custom_body_html is defined and happydomain_custom_body_html != "" %} +custom-body-html={{ happydomain_custom_body_html }} +{% endif %} +{% if happydomain_default_nameserver is defined and happydomain_default_nameserver != "" %} +default-ns={{ happydomain_default_nameserver }} +{% endif %} +{% if happydomain_external_auth is defined and happydomain_external_auth != "" %} +external-auth={{ happydomain_external_auth }} +{% endif %} +{% if happydomain_external_url is defined and happydomain_external_url != "" %} +externalurl={{ happydomain_external_url }} +{% endif %} +{% if happydomain_jwt_secret_key is defined and happydomain_jwt_secret_key != "" %} +jwt-secret-key={{ happydomain_jwt_secret_key }} +{% endif %} +leveldb-path={{ happydomain_storage_leveldb_path | default(happydomain_data_dir + '/happydomain.db') }} +{% if happydomain_mail_from is defined and happydomain_mail_from != "" %} +mail-from={{ happydomain_mail_from }} +{% endif %} +{% if happydomain_mail_smtp_host is defined and happydomain_mail_smtp_host != "" %} +mail-smtp-host={{ happydomain_mail_smtp_host }} +{% endif %} +{% if happydomain_mail_smtp_port is defined and happydomain_mail_smtp_port != "" %} +mail-smtp-port={{ happydomain_mail_smtp_port }} +{% endif %} +{% if happydomain_mail_smtp_username is defined and happydomain_mail_smtp_username != "" %} +mail-smtp-username={{ happydomain_mail_smtp_username }} +{% endif %} +{% if happydomain_mail_smtp_password is defined and happydomain_mail_smtp_password != "" %} +mail-smtp-password={{ happydomain_mail_smtp_password }} +{% endif %} +{% if happydomain_mail_smtp_tls_no_verify is defined and happydomain_mail_smtp_tls_no_verify != "" %} +mail-smtp-tls-no-verify={{ happydomain_mail_smtp_tls_no_verify }} +{% endif %} +{% if happydomain_no_auth is defined and happydomain_no_auth != "" %} +no-auth={{ happydomain_no_auth }} +{% endif %} +{% if happydomain_storage_engine is defined and happydomain_storage_engine != "" %} +storage-engine={{ happydomain_storage_engine }} +{% endif %} +{% if happydomain_ovh_application_key is defined and happydomain_ovh_application_key != "" %} +ovh-application-key={{ happydomain_ovh_application_key }} +{% endif %} +{% if happydomain_ovh_application_secret is defined and happydomain_ovh_application_secret != "" %} +ovh-application-secret={{ happydomain_ovh_application_secret }} +{% endif %} diff --git a/roles/happydomain/templates/happydomain.service.j2 b/roles/happydomain/templates/happydomain.service.j2 new file mode 100644 index 0000000..c3be263 --- /dev/null +++ b/roles/happydomain/templates/happydomain.service.j2 @@ -0,0 +1,14 @@ +[Unit] +Description=happyDomain Domain Managment Server +After=network.target nss-lookup.target + +[Service] +Type=exec +ExecStart=/usr/bin/happydomain +WorkingDirectory={% if happydomain_data_dir != '' %}{{ happydomain_data_dir }}{% else %}/var/lib/happydomain{% if instance_name != 'happyDomain' %}.{{ instance_name }}{% endif %}{% endif %} + +User=happydomain +Group=happydomain + +[Install] +WantedBy=multi-user.target \ No newline at end of file