Skip to content

Commit

Permalink
Enable config file deployment
Browse files Browse the repository at this point in the history
Fixes #2
  • Loading branch information
frne committed Jul 3, 2018
1 parent 5911f65 commit f650473
Show file tree
Hide file tree
Showing 11 changed files with 108 additions and 24 deletions.
76 changes: 76 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
------------

Expand Down
17 changes: 4 additions & 13 deletions defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
deploy_additional_copy: []
12 changes: 12 additions & 0 deletions tasks/additional.yml
Original file line number Diff line number Diff line change
@@ -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 }}"
8 changes: 2 additions & 6 deletions tasks/download.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
when: artifact_download is not skipped
3 changes: 3 additions & 0 deletions tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 2 additions & 1 deletion tasks/service.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@
- name: "Service - Ensure {{ deploy_service_name }} starts on boot"
service:
name: "{{ deploy_service_name }}"
enabled: yes
enabled: yes
when: deploy_service_type != "none"
3 changes: 2 additions & 1 deletion tasks/start.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
become: true
service:
name: "{{ deploy_service_name }}"
state: started
state: started
when: deploy_service_type != "none"
3 changes: 2 additions & 1 deletion tasks/stop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
when: deploy_instance_exists.stat.exists == True and deploy_service_type != "none"
7 changes: 5 additions & 2 deletions tests/playbook/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
- { role: ../../../ansible-java-deployment }
File renamed without changes.
File renamed without changes.

0 comments on commit f650473

Please sign in to comment.