From a484411272ce641bf826cb687cc8fcead1277d55 Mon Sep 17 00:00:00 2001 From: Andrea Dell'Amico Date: Tue, 29 Dec 2020 15:11:48 +0100 Subject: [PATCH] Role that installs a springboot artifact as a service --- README.md | 31 +++---- defaults/main.yml | 6 +- handlers/main.yml | 8 +- meta/main.yml | 55 +++++-------- tasks/main.yml | 103 +++++++++++++++++++++++- templates/springboot-logrotate.j2 | 8 ++ templates/springboot-upstart.conf.j2 | 17 ++++ templates/springboot.service.systemd.j2 | 30 +++++++ 8 files changed, 203 insertions(+), 55 deletions(-) create mode 100644 templates/springboot-logrotate.j2 create mode 100644 templates/springboot-upstart.conf.j2 create mode 100644 templates/springboot.service.systemd.j2 diff --git a/README.md b/README.md index 3637db8..1d75e3d 100644 --- a/README.md +++ b/README.md @@ -1,31 +1,26 @@ 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. +A role that sets up a springboot service 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. +The most important variables are listed below: + +``` yaml +springboot_config: True +springboot_install_artifact: True +springboot_apps: + - { name: 'foo', log_dir: '/var/log/foo', install_dir: '/usr/lib/foo', state: 'present', app_conf_file: '', remote_conf_url: '', logback_url: '', logback_file: '', user: 'foo', java_opts: '', maven_repo_url: '', maven_id: '', maven_group_id: '', maven_extension: '', maven_version: '' } +``` + +Not all the elements are mandatory. 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 } +None License ------- @@ -35,4 +30,4 @@ EUPL-1.2 Author Information ------------------ -An optional section for the role authors to include contact information, or a website (HTML is not allowed). +Andrea Dell'Amico, diff --git a/defaults/main.yml b/defaults/main.yml index 95d3c70..dd110b5 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -1,2 +1,6 @@ --- -# defaults file for ansible-role-template \ No newline at end of file +springboot_config: True +springboot_install_artifact: True +springboot_apps: [] +# - { name: 'foo', log_dir: '/var/log/foo', install_dir: '/usr/lib/foo', state: 'present', app_conf_file: '', remote_conf_url: '', logback_url: '', logback_file: '', user: 'foo', java_opts: '', maven_repo_url: '', maven_id: '', maven_group_id: '', maven_extension: '', maven_version: '' } + diff --git a/handlers/main.yml b/handlers/main.yml index 27474e0..09f73d3 100644 --- a/handlers/main.yml +++ b/handlers/main.yml @@ -1,2 +1,8 @@ --- -# handlers file for ansible-role-template \ No newline at end of file +- name: systemd reload + command: systemctl daemon-reload + +- name: Restart the springboot webservice + service: name={{ item.name }} state=restarted + with_items: '{{ springboot_apps }}' + when: restart_{{ item.name }} is defined diff --git a/meta/main.yml b/meta/main.yml index 81bda14..55566b9 100644 --- a/meta/main.yml +++ b/meta/main.yml @@ -1,46 +1,33 @@ galaxy_info: - author: your name - description: your description - company: your company (optional) + author: Andrea Dell'Amico + description: Systems Architect + company: ISTI-CNR - # If the issue tracker for your role is not on github, uncomment the - # next line and provide a value - issue_tracker_url: https://support.d4science.org/projects/automatic-provisioning/issues + issue_tracker_url: https://redmine-s2i2s.isti.cnr.it/projects/provisioning - license: EUPL-1.2 + license: EUPL 1.2+ min_ansible_version: 2.8 - # If this a Container Enabled role, provide the minimum Ansible Container version. - # min_ansible_container_version: - - # Optionally specify the branch Galaxy will use when accessing the GitHub - # repo for this role. During role install, if no tags are available, - # Galaxy will use this branch. During import Galaxy will access files on - # this branch. If Travis integration is configured, only notifications for this - # branch will be accepted. Otherwise, in all cases, the repo's default branch - # (usually master) will be used. - #github_branch: - - # - # 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: Ubuntu - versions: - - bionic + - name: Ubuntu + versions: + - bionic + - name: EL + versions: + - 7 + - 8 - 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. + galaxy_tags: + - springboot + - java + +dependencies: + - src: git+https://gitea-s2i2s.isti.cnr.it/ISTI-ansible-roles/ansible-role-openjdk.git + version: master + name: openjdk + state: latest -dependencies: [] - # List your role dependencies here, one per line. Be sure to remove the '[]' above, - # if you add dependencies to this list. \ No newline at end of file diff --git a/tasks/main.yml b/tasks/main.yml index 53c6cae..4196bae 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -1,2 +1,103 @@ --- -# tasks file for ansible-role-template \ No newline at end of file +- name: User under with the service will run + block: + - name: Create the user that will run the springboot app {{ item.name }} + user: name={{ item.user }} home={{ item.install_dir }} createhome=no shell=/usr/sbin/nologin system=yes + with_items: '{{ springboot_apps }}' + when: item.user is defined + + - name: Create the install directory of the springboot app {{ item.name }} + file: dest={{ item.install_dir }} state=directory owner={{ item.user }} group={{ item.user }} recurse=yes + with_items: '{{ springboot_apps }}' + when: item.user is defined + + - name: Create the log directory of the springboot app {{ item.name }} + file: dest={{ item.log_dir }} state=directory owner={{ item.user }} group={{ item.user }} recurse=yes + with_items: '{{ springboot_apps }}' + when: item.user is defined + + tags: [ 'springboot', 'springboot_user' ] + +- name: Service artifact + block: + - name: Download the {{ item.name }} artifact from {{ maven_baseurl }} + maven_artifact: artifact_id={{ item.maven_id }} version={{ item.maven_version | default('latest') }} group_id={{ item.maven_group_id }} extension={{ item.maven_extension | default('war') }} repository_url={{ item.maven_repo_url }} dest={{ item.install_dir }}/{{ item.name }}.{{ item.maven_extension | default('war') }} verify_checksum=always mode='0644' + with_items: '{{ springboot_apps }}' + register: 'restart_{{ item.name }}' + when: item.maven_repo_url is defined + notify: Restart the springboot webservice + + tags: [ 'springboot', 'springboot_artifact' ] + +- name: Service application configuration + block: + - name: Download the {{ item.name }} configuration from {{ item.remote_conf_url }} to use it as a template + get_url: url={{ item.remote_conf_url }} dest=/var/tmp/.{{ item.app_conf_file }}.j2 owner=root group=root mode=0400 + with_items: '{{ springboot_apps }}' + when: item.app_conf_file is defined + delegate_to: 'localhost' + + - name: Install the {{ item.name }} configuration starting from the template + template: src=/var/tmp/.{{ item.app_conf_file }}.j2 dest={{ item.install_dir }}/{{ item.app_conf_file }} owner=root group={{ item.user }} mode=0440 + with_items: '{{ springboot_apps }}' + register: 'restart_{{ item.name }}' + when: item.app_conf_file is defined + notify: Restart the springboot webservice + + - name: Remove the local template file + file: dest=/var/tmp/.{{ item.app_conf_file }}.j2 state=absent + with_items: '{{ springboot_apps }}' + when: item.app_conf_file is defined + delegate_to: 'localhost' + + tags: [ 'springboot', 'springboot_configuration' ] + +- name: Service logback configuration + block: + - name: Download the {{ item.name }} configuration from {{ item.logback_url }} to use it as a template + get_url: url={{ item.logback_url }} dest=/var/tmp/.{{ item.logback_file }}.j2 owner=root group=root mode=0400 + with_items: '{{ springboot_apps }}' + when: item.logback_file is defined + delegate_to: 'localhost' + + - name: Install the {{ item.name }} configuration starting from the template + template: src=/var/tmp/.{{ item.logback_file }}.j2 dest={{ item.install_dir }}/{{ item.logback_file }} owner=root group={{ item.user }} mode=0440 + with_items: '{{ springboot_apps }}' + register: 'restart_{{ item.name }}' + when: item.logback_file is defined + notify: Restart the springboot webservice + + - name: Remove the local template file + file: dest=/var/tmp/.{{ item.logback_file }}.j2 state=absent + with_items: '{{ springboot_apps }}' + when: item.logback_file is defined + delegate_to: 'localhost' + + tags: [ 'springboot', 'springboot_configuration' ] + +- name: Springboot startup files management + block: + - name: Install the springboot app upstart init file + template: src=springboot-upstart.conf.j2 dest=/etc/init/{{ item.name }}.conf owner=root group=root mode=0644 + with_items: '{{ springboot_apps }}' + when: ansible_service_mgr != 'systemd' + + - name: Install the springboot systemd service unit + template: src=springboot.service.systemd.j2 dest=/etc/systemd/system/{{ item.name }}.service mode=0644 owner=root group=root + with_items: '{{ springboot_apps }}' + when: ansible_service_mgr == 'systemd' + notify: systemd reload + + - name: Force all notified handlers to run at this point, not waiting for normal sync points + meta: flush_handlers + + - name: Install the springboot logrotate configuration + template: src=springboot-logrotate.j2 dest=/etc/logrotate.d/{{ item.name }} owner=root group=root mode=0444 + with_items: '{{ springboot_apps }}' + + - name: Ensure that the springboot service is running and enabled + service: name={{ item.name }} state=started enabled=yes + with_items: '{{ springboot_apps }}' + + when: springboot_config + tags: [ 'springboot', 'springboot_service' ] diff --git a/templates/springboot-logrotate.j2 b/templates/springboot-logrotate.j2 new file mode 100644 index 0000000..4e12929 --- /dev/null +++ b/templates/springboot-logrotate.j2 @@ -0,0 +1,8 @@ +{{ item.log_dir }}/{{ item.name }}.log { + copytruncate + daily + rotate 10 + compress + missingok + create 640 {{ item.user }} adm +} diff --git a/templates/springboot-upstart.conf.j2 b/templates/springboot-upstart.conf.j2 new file mode 100644 index 0000000..6d00335 --- /dev/null +++ b/templates/springboot-upstart.conf.j2 @@ -0,0 +1,17 @@ +# {{ item.name }} + +description "{{ item.name }}" + +start on runlevel [2345] +stop on runlevel [!2345] +respawn +respawn limit 10 5 + +setuid {{ item.user }} +setgid {{ item.user }} + +script + cd {{ item.install_dir }} + exec java {{ item.java_opts }} -jar ./{{ item.name }}.jar +end script + diff --git a/templates/springboot.service.systemd.j2 b/templates/springboot.service.systemd.j2 new file mode 100644 index 0000000..cb2b01e --- /dev/null +++ b/templates/springboot.service.systemd.j2 @@ -0,0 +1,30 @@ +[Unit] +Description={{ item.name }} +After=network.target + +[Service] +{% if item.envfile is defined %} +EnvironmentFile=/etc/default/{{ item.envfile }} +{% endif %} +Type=simple +User={{ item.user }} +Group={{ item.user }} + +WorkingDirectory={{ item.install_dir }} +{% if item.envfile is defined %} +ExecStart=/usr/bin/java $JAVA_OPTS -jar $EXEC_JAR +{% else %} +ExecStart=/usr/bin/java {{ item.java_opts }} -jar {{ item.install_dir }}/{{ item.name }}.jar +{% endif %} + +StandardOutput=journal +StandardError=journal +SyslogIdentifier={{ item.name }} + +SuccessExitStatus=143 +TimeoutStopSec=10 +Restart=on-failure +RestartSec=60 + +[Install] +WantedBy=multi-user.target