From f6504736af49eefcf4bf31e5d09c7b7c81f42f31 Mon Sep 17 00:00:00 2001 From: Frank Neff Date: Tue, 3 Jul 2018 17:07:27 +0200 Subject: [PATCH] Enable config file deployment Fixes #2 --- README.md | 76 +++++++++++++++++++ defaults/main.yml | 17 +---- tasks/additional.yml | 12 +++ tasks/download.yml | 8 +- tasks/main.yml | 3 + tasks/service.yml | 3 +- tasks/start.yml | 3 +- tasks/stop.yml | 3 +- tests/playbook/deploy.yml | 7 +- .../playbook/templates/application.conf.j2 | 0 .../playbook/templates/logback.xml.j2 | 0 11 files changed, 108 insertions(+), 24 deletions(-) create mode 100644 tasks/additional.yml rename templates/additional/application.conf => tests/playbook/templates/application.conf.j2 (100%) rename templates/additional/logback.xml => tests/playbook/templates/logback.xml.j2 (100%) diff --git a/README.md b/README.md index 4086f5e..07b5706 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,82 @@ following example to your playbook (e.g. `deploy.yml`): - acmesoftware.java-deployment ``` +### Destination directory structure + +On the destination server, the application(s) will be deployed into the `/opt/{{ deploy_app_name }}/` directory. Unless +configured differently, all directories under that "app root" will be owned by the configured app user and group and +follow a multi-instance structure. For the basic example above, the following deirectories will be generated: + +``` +/opt/my-application/ +├── downloads +└── instances + └── 1 + ├── app + ├── conf + └── logs +``` + +The structure above is fully configurable. See [defaults](defaults/main.yml) for config reference. + +### Service / Systemloader + +This role will, by default, install a service on the target system, which is then used to start & stop each application +instance. The service wrapper to use can be configured using the `d +eploy_service_type` variable. If not configured, we'll +chose the default service wrapper for the target system. Set `deploy_service_type: "none"` to do nothing and start the +deployed app on your own. See [defaults](defaults/main.yml) for config reference. + + +### Deploy Additional Files + +Additional files like an `application.conf`, `logback.xml` or any other file / template can be deployed during the +process. To do so, see the following config example: + +```yml +- hosts: centos6 + vars: + ... + deploy_additional_templates: + - { + src: "templates/application.conf.j2", + dest: "{{ deploy_dir_config }}/application.conf" + } + - { + src: "templates/logback.xml.j2", + dest: "{{ deploy_dir_config }}/logback.xml", + mode: 0600 + } +``` + +Add one item for every file to be deployed and use the following configuration scheme: + +| Property | Mandatory | Description | Default | +| --------- | --------- | --------------------------------------------- | ------------------------ | +| src | yes | Template to render on local machine | - | +| dest | yes | Destination of the file on the local machine | - | +| mode | no | Mode (`chmod`) for the destination file | 0644 | +| user | no | Owner (`chown`) of the destination file | `{{ deploy_app_user }}` | +| group | no | Owner group (`chown`) of the destination file | `{{ deploy_app_group }}` | + +***Note:** These files are deployed per instance, so use the instance directory variables (e.g. +`{{ deploy_dir_config }}`) in destination path to prevent override.* + +All role variables are accessible within those templates, which is especially beneficial when configuring http ports, +paths, etc. The following example shows a hocon configuration, where those variables are used: + +```jinja2 +example { + http { + # Example to automatically set the application's http port + # This will output port '9001' for instance 1, '9002' for instance 2 and '9108' for instance 108 + port: 9{{ '%03d'|format(deploy_instance_nr|int) }} + + config-file-ref: {{ deploy_dir_config }}/some_file.txt + } +} +``` + Ideas / Todo ------------ diff --git a/defaults/main.yml b/defaults/main.yml index d58ff19..02943a4 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -119,7 +119,7 @@ deploy_service_pidfile: "/var/run/{{ deploy_service_name }}.pid" # Which service wrapper / systemloader you want to use # -# This can be default, upstart or systemd +# This can be default, upstart, systemd or none # # If you keep the "default" value, the default systemloader of the used target OS will be used. This will be: # upstart for: Ubuntu <=14.4, RHEL @@ -139,16 +139,7 @@ deploy_service_systemd_location: "..." # Additional files ############################################### -# Generate a logback configuration file -deploy_generate_logback_config: True +# Additionaly template files to deploy. See readme for usage instructions +deploy_additional_templates: [] -deploy_generate_logback_name: logback.xml - -deploy_generate_logback_template: "templates/additional/logback.xml" - -# Generate an application.conf file -deploy_generate_conf_file: True - -deploy_generate_conf_name: application.conf - -deploy_generate_conf_template: "templates/additional/application.conf.j2" \ No newline at end of file +deploy_additional_copy: [] \ No newline at end of file diff --git a/tasks/additional.yml b/tasks/additional.yml new file mode 100644 index 0000000..1b6bebb --- /dev/null +++ b/tasks/additional.yml @@ -0,0 +1,12 @@ +--- + +- name: "Additional - Deploy templates" + become: yes + become_user: "{{ deploy_app_user }}" + template: + src: "{{ item.src }}" + dest: "{{ item.dest }}" + owner: "{{ item.user | deploy_app_user }}" + group: "{{ item.group | deploy_app_group }}" + mode: "{{ item.mode | default('0644') }}" + with_items: "{{ deploy_additional_templates }}" \ No newline at end of file diff --git a/tasks/download.yml b/tasks/download.yml index d9f5a9f..b900456 100644 --- a/tasks/download.yml +++ b/tasks/download.yml @@ -17,11 +17,7 @@ owner: "{{ deploy_app_user }}" group: "{{ deploy_app_group }}" when: deploy_artifact_downloaded.stat.exists == False or deploy_setting_keep_downloads == False - -- name: "Download - Check artifact already extracted" - stat: - path: "{{ deploy_download_extract_dir }}" - register: deploy_artifact_extracted + register: artifact_download - name: "Download - Extract artifact {{ deploy_artifact_filename }} to {{ deploy_download_extract_dir }}" become: yes @@ -32,4 +28,4 @@ owner: "{{ deploy_app_user }}" group: "{{ deploy_app_group }}" remote_src: yes - when: deploy_artifact_extracted.stat.exists == False or deploy_setting_keep_downloads == False \ No newline at end of file + when: artifact_download is not skipped \ No newline at end of file diff --git a/tasks/main.yml b/tasks/main.yml index 52a7247..234e789 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -16,6 +16,9 @@ - name: "Copy application" include_tasks: copy.yml +- name: "Deploy additional files" + include_tasks: additional.yml + - name: "Install service" include_tasks: service.yml diff --git a/tasks/service.yml b/tasks/service.yml index 539eb79..0552f1f 100644 --- a/tasks/service.yml +++ b/tasks/service.yml @@ -9,4 +9,5 @@ - name: "Service - Ensure {{ deploy_service_name }} starts on boot" service: name: "{{ deploy_service_name }}" - enabled: yes \ No newline at end of file + enabled: yes + when: deploy_service_type != "none" \ No newline at end of file diff --git a/tasks/start.yml b/tasks/start.yml index 132525f..cafffe9 100644 --- a/tasks/start.yml +++ b/tasks/start.yml @@ -4,4 +4,5 @@ become: true service: name: "{{ deploy_service_name }}" - state: started \ No newline at end of file + state: started + when: deploy_service_type != "none" \ No newline at end of file diff --git a/tasks/stop.yml b/tasks/stop.yml index 7d5480c..2520718 100644 --- a/tasks/stop.yml +++ b/tasks/stop.yml @@ -4,10 +4,11 @@ stat: path: "{{ deploy_dir_app }}" register: deploy_instance_exists + when: deploy_service_type != "none" - name: "Stop - Service {{ deploy_service_name }}" become: true service: name: "{{ deploy_service_name }}" state: stopped - when: deploy_instance_exists.stat.exists == True \ No newline at end of file + when: deploy_instance_exists.stat.exists == True and deploy_service_type != "none" \ No newline at end of file diff --git a/tests/playbook/deploy.yml b/tests/playbook/deploy.yml index 507e094..3a39129 100644 --- a/tests/playbook/deploy.yml +++ b/tests/playbook/deploy.yml @@ -2,12 +2,15 @@ - hosts: centos6 vars: - deploy_artifact_url: "http://192.168.177.1:8000/play-java-seed-1.0-SNAPSHOT.zip" + deploy_artifact_url: "https://github.com/acme-software/ansible-java-deployment/raw/master/tests/testapp/play-java-seed-1.0-SNAPSHOT.zip" deploy_setting_artifact_contains_subdir: True deploy_setting_keep_downloads: True deploy_app_name: "play-java-seed" deploy_service_start_command: "bin/{{ deploy_app_name }} -Dplay.http.secret.key='foo'" deploy_app_user: "bot-user" deploy_app_group: "bot-group" + deploy_additional_templates: + - { src: "templates/application.conf.j2", dest: "{{ deploy_dir_config }}/application.conf" } + - { src: "templates/logback.xml.j2", dest: "{{ deploy_dir_config }}/logback.xml", mode: 600 } roles: - - ../../roles/deploy \ No newline at end of file + - { role: ../../../ansible-java-deployment } diff --git a/templates/additional/application.conf b/tests/playbook/templates/application.conf.j2 similarity index 100% rename from templates/additional/application.conf rename to tests/playbook/templates/application.conf.j2 diff --git a/templates/additional/logback.xml b/tests/playbook/templates/logback.xml.j2 similarity index 100% rename from templates/additional/logback.xml rename to tests/playbook/templates/logback.xml.j2