Skip to content

Commit f650473

Browse files
committed
Enable config file deployment
Fixes #2
1 parent 5911f65 commit f650473

File tree

11 files changed

+108
-24
lines changed

11 files changed

+108
-24
lines changed

README.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,82 @@ following example to your playbook (e.g. `deploy.yml`):
5555
- acmesoftware.java-deployment
5656
```
5757
58+
### Destination directory structure
59+
60+
On the destination server, the application(s) will be deployed into the `/opt/{{ deploy_app_name }}/` directory. Unless
61+
configured differently, all directories under that "app root" will be owned by the configured app user and group and
62+
follow a multi-instance structure. For the basic example above, the following deirectories will be generated:
63+
64+
```
65+
/opt/my-application/
66+
├── downloads
67+
└── instances
68+
└── 1
69+
├── app
70+
├── conf
71+
└── logs
72+
```
73+
74+
The structure above is fully configurable. See [defaults](defaults/main.yml) for config reference.
75+
76+
### Service / Systemloader
77+
78+
This role will, by default, install a service on the target system, which is then used to start & stop each application
79+
instance. The service wrapper to use can be configured using the `d
80+
eploy_service_type` variable. If not configured, we'll
81+
chose the default service wrapper for the target system. Set `deploy_service_type: "none"` to do nothing and start the
82+
deployed app on your own. See [defaults](defaults/main.yml) for config reference.
83+
84+
85+
### Deploy Additional Files
86+
87+
Additional files like an `application.conf`, `logback.xml` or any other file / template can be deployed during the
88+
process. To do so, see the following config example:
89+
90+
```yml
91+
- hosts: centos6
92+
vars:
93+
...
94+
deploy_additional_templates:
95+
- {
96+
src: "templates/application.conf.j2",
97+
dest: "{{ deploy_dir_config }}/application.conf"
98+
}
99+
- {
100+
src: "templates/logback.xml.j2",
101+
dest: "{{ deploy_dir_config }}/logback.xml",
102+
mode: 0600
103+
}
104+
```
105+
106+
Add one item for every file to be deployed and use the following configuration scheme:
107+
108+
| Property | Mandatory | Description | Default |
109+
| --------- | --------- | --------------------------------------------- | ------------------------ |
110+
| src | yes | Template to render on local machine | - |
111+
| dest | yes | Destination of the file on the local machine | - |
112+
| mode | no | Mode (`chmod`) for the destination file | 0644 |
113+
| user | no | Owner (`chown`) of the destination file | `{{ deploy_app_user }}` |
114+
| group | no | Owner group (`chown`) of the destination file | `{{ deploy_app_group }}` |
115+
116+
***Note:** These files are deployed per instance, so use the instance directory variables (e.g.
117+
`{{ deploy_dir_config }}`) in destination path to prevent override.*
118+
119+
All role variables are accessible within those templates, which is especially beneficial when configuring http ports,
120+
paths, etc. The following example shows a hocon configuration, where those variables are used:
121+
122+
```jinja2
123+
example {
124+
http {
125+
# Example to automatically set the application's http port
126+
# This will output port '9001' for instance 1, '9002' for instance 2 and '9108' for instance 108
127+
port: 9{{ '%03d'|format(deploy_instance_nr|int) }}
128+
129+
config-file-ref: {{ deploy_dir_config }}/some_file.txt
130+
}
131+
}
132+
```
133+
58134
Ideas / Todo
59135
------------
60136

defaults/main.yml

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ deploy_service_pidfile: "/var/run/{{ deploy_service_name }}.pid"
119119

120120
# Which service wrapper / systemloader you want to use
121121
#
122-
# This can be default, upstart or systemd
122+
# This can be default, upstart, systemd or none
123123
#
124124
# If you keep the "default" value, the default systemloader of the used target OS will be used. This will be:
125125
# upstart for: Ubuntu <=14.4, RHEL
@@ -139,16 +139,7 @@ deploy_service_systemd_location: "..."
139139
# Additional files
140140
###############################################
141141

142-
# Generate a logback configuration file
143-
deploy_generate_logback_config: True
142+
# Additionaly template files to deploy. See readme for usage instructions
143+
deploy_additional_templates: []
144144

145-
deploy_generate_logback_name: logback.xml
146-
147-
deploy_generate_logback_template: "templates/additional/logback.xml"
148-
149-
# Generate an application.conf file
150-
deploy_generate_conf_file: True
151-
152-
deploy_generate_conf_name: application.conf
153-
154-
deploy_generate_conf_template: "templates/additional/application.conf.j2"
145+
deploy_additional_copy: []

tasks/additional.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
3+
- name: "Additional - Deploy templates"
4+
become: yes
5+
become_user: "{{ deploy_app_user }}"
6+
template:
7+
src: "{{ item.src }}"
8+
dest: "{{ item.dest }}"
9+
owner: "{{ item.user | deploy_app_user }}"
10+
group: "{{ item.group | deploy_app_group }}"
11+
mode: "{{ item.mode | default('0644') }}"
12+
with_items: "{{ deploy_additional_templates }}"

tasks/download.yml

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,7 @@
1717
owner: "{{ deploy_app_user }}"
1818
group: "{{ deploy_app_group }}"
1919
when: deploy_artifact_downloaded.stat.exists == False or deploy_setting_keep_downloads == False
20-
21-
- name: "Download - Check artifact already extracted"
22-
stat:
23-
path: "{{ deploy_download_extract_dir }}"
24-
register: deploy_artifact_extracted
20+
register: artifact_download
2521

2622
- name: "Download - Extract artifact {{ deploy_artifact_filename }} to {{ deploy_download_extract_dir }}"
2723
become: yes
@@ -32,4 +28,4 @@
3228
owner: "{{ deploy_app_user }}"
3329
group: "{{ deploy_app_group }}"
3430
remote_src: yes
35-
when: deploy_artifact_extracted.stat.exists == False or deploy_setting_keep_downloads == False
31+
when: artifact_download is not skipped

tasks/main.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
- name: "Copy application"
1717
include_tasks: copy.yml
1818

19+
- name: "Deploy additional files"
20+
include_tasks: additional.yml
21+
1922
- name: "Install service"
2023
include_tasks: service.yml
2124

tasks/service.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@
99
- name: "Service - Ensure {{ deploy_service_name }} starts on boot"
1010
service:
1111
name: "{{ deploy_service_name }}"
12-
enabled: yes
12+
enabled: yes
13+
when: deploy_service_type != "none"

tasks/start.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44
become: true
55
service:
66
name: "{{ deploy_service_name }}"
7-
state: started
7+
state: started
8+
when: deploy_service_type != "none"

tasks/stop.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
stat:
55
path: "{{ deploy_dir_app }}"
66
register: deploy_instance_exists
7+
when: deploy_service_type != "none"
78

89
- name: "Stop - Service {{ deploy_service_name }}"
910
become: true
1011
service:
1112
name: "{{ deploy_service_name }}"
1213
state: stopped
13-
when: deploy_instance_exists.stat.exists == True
14+
when: deploy_instance_exists.stat.exists == True and deploy_service_type != "none"

tests/playbook/deploy.yml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22

33
- hosts: centos6
44
vars:
5-
deploy_artifact_url: "http://192.168.177.1:8000/play-java-seed-1.0-SNAPSHOT.zip"
5+
deploy_artifact_url: "https://github.com/acme-software/ansible-java-deployment/raw/master/tests/testapp/play-java-seed-1.0-SNAPSHOT.zip"
66
deploy_setting_artifact_contains_subdir: True
77
deploy_setting_keep_downloads: True
88
deploy_app_name: "play-java-seed"
99
deploy_service_start_command: "bin/{{ deploy_app_name }} -Dplay.http.secret.key='foo'"
1010
deploy_app_user: "bot-user"
1111
deploy_app_group: "bot-group"
12+
deploy_additional_templates:
13+
- { src: "templates/application.conf.j2", dest: "{{ deploy_dir_config }}/application.conf" }
14+
- { src: "templates/logback.xml.j2", dest: "{{ deploy_dir_config }}/logback.xml", mode: 600 }
1215
roles:
13-
- ../../roles/deploy
16+
- { role: ../../../ansible-java-deployment }

0 commit comments

Comments
 (0)