From bc5ecd1de6ed9a989e7c4b57f27caded105b4b04 Mon Sep 17 00:00:00 2001 From: dark Date: Sun, 26 Dec 2021 04:19:45 +0100 Subject: [PATCH] Add 6h of work --- .travis.yml | 29 +++++++++++ README.md | 38 ++++++++++++++ defaults/main.yml | 59 +++++++++++++++++++++ handlers/main.yml | 11 ++++ meta/main.yml | 52 +++++++++++++++++++ tasks/main.yml | 83 +++++++++++++++++++++++++++++ templates/docker-compose.yml.j2 | 92 +++++++++++++++++++++++++++++++++ 7 files changed, 364 insertions(+) create mode 100644 .travis.yml create mode 100644 README.md create mode 100644 defaults/main.yml create mode 100644 handlers/main.yml create mode 100644 meta/main.yml create mode 100644 tasks/main.yml create mode 100644 templates/docker-compose.yml.j2 diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..36bbf62 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,29 @@ +--- +language: python +python: "2.7" + +# Use the new container infrastructure +sudo: false + +# Install ansible +addons: + apt: + packages: + - python-pip + +install: + # Install ansible + - pip install ansible + + # Check ansible version + - ansible --version + + # Create ansible.cfg with correct roles_path + - printf '[defaults]\nroles_path=../' >ansible.cfg + +script: + # Basic role syntax check + - ansible-playbook tests/test.yml -i tests/inventory --syntax-check + +notifications: + webhooks: https://galaxy.ansible.com/api/v1/notifications/ \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..225dd44 --- /dev/null +++ b/README.md @@ -0,0 +1,38 @@ +Role Name +========= + +A brief description of the role goes here. + +Requirements +------------ + +Any pre-requisites that may not be covered by Ansible itself or the role should be mentioned here. For instance, if the role uses the EC2 module, it may be a good idea to mention in this section that the boto package is required. + +Role Variables +-------------- + +A description of the settable variables for this role should go here, including any variables that are in defaults/main.yml, vars/main.yml, and any variables that can/should be set via parameters to the role. Any variables that are read from other roles and/or the global scope (ie. hostvars, group vars, etc.) should be mentioned here as well. + +Dependencies +------------ + +A list of other roles hosted on Galaxy should go here, plus any details in regards to parameters that may need to be set for other roles, or variables that are used from other roles. + +Example Playbook +---------------- + +Including an example of how to use your role (for instance, with variables passed in as parameters) is always nice for users too: + + - hosts: servers + roles: + - { role: username.rolename, x: 42 } + +License +------- + +BSD + +Author Information +------------------ + +An optional section for the role authors to include contact information, or a website (HTML is not allowed). diff --git a/defaults/main.yml b/defaults/main.yml new file mode 100644 index 0000000..fc57f14 --- /dev/null +++ b/defaults/main.yml @@ -0,0 +1,59 @@ +--- +kimai: + general: + healthchecks: true + base_path: "/srv/compose-kimai/" + volumes_path: "data/" + volume: + data: "public" + disk: false + + main: + service_name: "kimai" + trusted_hosts: ",0.0.0.0" + + admin: + mail: "admin@kimai.local" + password: "changemeplease" + + users: + superadmin: + roles: + ROLE_SUPER_ADMIN: true + dark: + mail: "dark@kimai.local" + password: "changemeplease" + roles: + ROLE_SUPER_ADMIN: + + + services: + mysql: + enabled: true + service_name: "kimaidb" + user: kimai + db: kimai + password: "changemeplease" + root_password: "changemeplease" + nginx: + enabled: true + service_name: "kimai_nginx" + port_maps: + - "8001:80" + + postfix: + enabled: true + service_name: "kimai_postfix" + domain: "neontribe.co.uk" + smtp: + user: + name: "kimai" + password: "kimai" + + volumes: + mysql: + data: "mysql" + disk: true + wait_for_it: + data: "wait-for-it" + disk: true \ No newline at end of file diff --git a/handlers/main.yml b/handlers/main.yml new file mode 100644 index 0000000..3207855 --- /dev/null +++ b/handlers/main.yml @@ -0,0 +1,11 @@ +--- +# handlers file for compose-kimai +- name: install required packages + ansible.builtin.package: + name: + - docker + - docker-compose + - mysql-default-client + +- name: Create users + meta: "noop" diff --git a/meta/main.yml b/meta/main.yml new file mode 100644 index 0000000..c572acc --- /dev/null +++ b/meta/main.yml @@ -0,0 +1,52 @@ +galaxy_info: + author: your name + description: your role description + company: your company (optional) + + # If the issue tracker for your role is not on github, uncomment the + # next line and provide a value + # issue_tracker_url: http://example.com/issue/tracker + + # Choose a valid license ID from https://spdx.org - some suggested licenses: + # - BSD-3-Clause (default) + # - MIT + # - GPL-2.0-or-later + # - GPL-3.0-only + # - Apache-2.0 + # - CC-BY-4.0 + license: license (GPL-2.0-or-later, MIT, etc) + + min_ansible_version: 2.1 + + # If this a Container Enabled role, provide the minimum Ansible Container version. + # min_ansible_container_version: + + # + # Provide a list of supported platforms, and for each platform a list of versions. + # If you don't wish to enumerate all versions for a particular platform, use 'all'. + # To view available platforms and versions (or releases), visit: + # https://galaxy.ansible.com/api/v1/platforms/ + # + # platforms: + # - name: Fedora + # versions: + # - all + # - 25 + # - name: SomePlatform + # versions: + # - all + # - 1.0 + # - 7 + # - 99.99 + + galaxy_tags: [] + # List tags for your role here, one per line. A tag is a keyword that describes + # and categorizes the role. Users find roles by searching for tags. Be sure to + # remove the '[]' above, if you add tags to this list. + # + # NOTE: A tag is limited to a single word comprised of alphanumeric characters. + # Maximum 20 tags per role. + +dependencies: [] + # List your role dependencies here, one per line. Be sure to remove the '[]' above, + # if you add dependencies to this list. diff --git a/tasks/main.yml b/tasks/main.yml new file mode 100644 index 0000000..c8cb446 --- /dev/null +++ b/tasks/main.yml @@ -0,0 +1,83 @@ +--- + +- name: create kimai directory + file: + path: "{{ kimai.general.base_path }}" + state: directory + recurse: true + notify: install required packages + +- name: process docker-compose file and setup kimai + template: + src: "docker-compose.yml.j2" + dest: "{{ kimai.general.base_path }}/docker-compose.yml" + +# notify: +# - restart kimai +# - fixup var +# - fixup kimai install + +- name: restart kimais mysql + docker_compose: + project_src: "{{ kimai.general.base_path }}" + restarted: true + services: + - "{{ kimai.services.mysql.service_name }}" + timeout: 30 +- name: gather mysql ip + shell: docker inspect -f {% raw %}'{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}'{% endraw %} $(docker-compose ps -q {{ kimai.services.mysql.service_name }} ) + args: + chdir: "{{ kimai.general.base_path }}" + register: kimai_db + +- name: Wait for kimai db become ready + ansible.builtin.wait_for: + host: "{{ kimai_db.stdout }}" + port: 3306 + delay: 10 + +- name: restart kimais normal services + docker_compose: + project_src: "{{ kimai.general.base_path }}" + restarted: true + services: + - "{{ kimai.services.nginx.service_name }}" + - "{{ kimai.services.postfix.service_name }}" + - "{{ kimai.main.service_name }}" + timeout: 10 + +- name: fixup var + shell: 'cd {{ kimai.general.base_path }}; docker-compose exec -u root {{ kimai.main.service_name }} bash -c "chmod 777 -R /opt/kimai/var";' + +- name: Create admins + block: + - name: Register existing users as facts + shell: > + docker-compose exec {{ kimai.services.mysql.service_name }} mysql -u{{ kimai.services.mysql.user }} -p{{ kimai.services.mysql.password}} --execute "SELECT username,roles FROM {{ kimai.services.mysql.db }}.kimai2_users;" -sN 2>/dev/null | awk '{ gsub(/^[ \t]+|[ \t]+$/, ""); gsub(/[ ]+/," "); print $1,"|",$2}' > /kimai_users.txt + args: + chdir: "{{ kimai.general.base_path }}" + + - name: Check existing kimai_users + command: grep -Fqe "{{ item.key }}" /kimai_users.txt + check_mode: 'no' + ignore_errors: 'yes' + register: admin_lines + loop: "{{ kimai.main.users | dict2items | selectattr('value.roles', 'search', 'ROLE_SUPER_ADMIN')}}" + changed_when: admin_lines.rc != 0 + failed_when: admin_lines is not defined or admin_lines.rc > 1 + + - name: Debug admin_lines + debug: + msg: "Creating user: {{ result.item.key }}" + loop: "{{ admin_lines.results | selectattr('changed', 'equalto', True) | rejectattr('failed', 'equalto', True) }}" + loop_control: + loop_var: result + +# TODO: NOT WORKING YET! Missing parts for mail, password and such. But it's triggered just with absent users already! + - name: Create non-existent admins + shell: > + docker-compose exec {{ kimai.main.service_name }} bash -c '/opt/kimai/bin/console kimai:user:create {{ result.item.key }}' + loop: "{{ admin_lines.results | selectattr('changed', 'equalto', True) | rejectattr('failed', 'equalto', True) }}" + loop_control: + loop_var: result + changed_when: result.changed \ No newline at end of file diff --git a/templates/docker-compose.yml.j2 b/templates/docker-compose.yml.j2 new file mode 100644 index 0000000..f1d0592 --- /dev/null +++ b/templates/docker-compose.yml.j2 @@ -0,0 +1,92 @@ +version: '2' +{% set volume_path = kimai.general.base_path + kimai.general.volumes_path %} +services: + {% if kimai.services.mysql.enabled -%} + {{ kimai.services.mysql.service_name }}: + image: mysql:5.7 + environment: + - MYSQL_DATABASE={{ kimai.services.mysql.db }} + - MYSQL_USER={{ kimai.services.mysql.user }} + - MYSQL_PASSWORD={{ kimai.services.mysql.password }} + - MYSQL_ROOT_PASSWORD={{ kimai.services.mysql.root_password }} + volumes: + {% if kimai.volumes.mysql.disk %} + - {{ volume_path + kimai.volumes.mysql.data }}/:/var/lib/mysql + {% else %} + - {{ kimai.volumes.mysql.data }}:/var/lib/mysql + {% endif -%} + command: --default-storage-engine innodb + restart: unless-stopped + {% if kimai.general.healthchecks -%} + healthcheck: + test: mysqladmin -p$$MYSQL_ROOT_PASSWORD ping -h localhost + interval: 20s + start_period: 10s + timeout: 10s + retries: 3 + {% endif -%} + {%- endif %} + + {% if kimai.services.nginx.enabled -%} + {{ kimai.services.nginx.service_name }}: + image: tobybatch/nginx-fpm-reverse-proxy + ports: + {%- for port_map in kimai.services.nginx.port_maps %} + - {{ port_map }} + {% endfor -%} + volumes: + {% if kimai.general.volume.disk -%} + - {{ volume_path + kimai.general.volume.data }}/:/opt/kimai/public:ro + {% else %} + - {{ kimai.general.volume.data }}:/opt/kimai/public:ro + {% endif -%} + restart: unless-stopped + depends_on: + - {{ kimai.main.service_name }} + {% if kimai.general.healthchecks -%} + healthcheck: + test: wget --spider http://nginx/health || exit 1 + interval: 20s + start_period: 10s + timeout: 10s + retries: 3 + {% endif %} + {% endif %} + + {% if kimai.services.postfix.enabled -%} + {{ kimai.services.postfix.service_name }}: + image: catatnight/postfix:latest + environment: + maildomain: {{ kimai.services.postfix.domain }} + smtp_user: {{ kimai.services.postfix.smtp.user.name }}:{{ kimai.services.postfix.smtp.user.password }} + restart: unless-stopped + {% endif %} + + {{ kimai.main.service_name }}: # This is the latest FPM image of kimai + image: kimai/kimai2:fpm + environment: + - ADMINMAIL=admin@kimai.local + - ADMINPASS=changemeplease + - DATABASE_URL=mysql://{{ kimai.services.mysql.user }}:{{ kimai.services.mysql.password }}@{{ kimai.services.mysql.service_name }}/{{ kimai.services.mysql.db }} + - TRUSTED_HOSTS={{ kimai.services.nginx.service_name }},localhost,127.0.0.1,{{ kimai.main.trusted_hosts}} + volumes: + {% if kimai.general.volume.disk %} + - {{ volume_path + kimai.general.volume.data }}/:/opt/kimai/public + {% else %} + - {{ kimai.general.volume.data }}:/opt/kimai/public + {% endif -%} + depends_on: + - {{ kimai.services.mysql.service_name }} + # - var:/opt/kimai/var + # - ./ldap.conf:/etc/openldap/ldap.conf:z + # - ./ROOT-CA.pem:/etc/ssl/certs/ROOT-CA.pem:z + restart: unless-stopped + + +volumes: + {% if not kimai.general.volume.disk -%} + {{ kimai.general.volume.data }}: + {% endif -%} + {% if not kimai.volumes.mysql.disk -%} + {{ kimai.volumes.mysql.data }}: + {% endif -%} \ No newline at end of file