|
107 | 107 | name: "{{ postgres_service }}" |
108 | 108 | state: restarted |
109 | 109 |
|
110 | | - |
111 | | -- name: Deploy Application Tier |
| 110 | +- name: Deploy, configure, and populate flask repo and virtualenv |
112 | 111 | hosts: app_servers |
113 | | - gather_facts: false |
114 | 112 | become: true |
115 | | - vars: |
116 | | - postgres_rhel7_repo: "https://download.postgresql.org/pub/repos/yum/10/redhat/rhel-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm" |
117 | | - app_server_packages: |
118 | | - - python-psycopg2 |
119 | | - app_server_port: 8080 |
120 | | - app_server_service: tomcat |
121 | | - |
| 113 | + gather_facts: false |
122 | 114 | tags: |
123 | | - - app_servers |
| 115 | + - application_servers |
| 116 | + vars: |
| 117 | + flask_app_name: resource_hub |
| 118 | + flask_user: flask |
| 119 | + flask_home: /opt/ |
| 120 | + flask_repo: https://github.com/tonykay/resource_hub.git |
| 121 | + flask_scm_ref: main |
| 122 | + flask_app_lauch_script: /usr/local/bin/launch_resource_hub |
| 123 | + virtualenv_name: "venv-{{ flask_app_name }}" |
| 124 | + virtualenv_home: /opt/virtual_envs |
| 125 | + virtualenv_python: /usr/bin/python3 |
| 126 | + |
| 127 | + |
| 128 | + app_yum_packages: |
| 129 | + - autoconf |
| 130 | + - automake |
| 131 | + - git |
| 132 | + - gcc |
| 133 | + - libtool |
| 134 | + - python3 |
| 135 | + - python3-devel |
| 136 | + - python3-pip |
| 137 | + |
| 138 | + pip_dependencies: |
| 139 | + - virtualenv |
| 140 | + - pip |
| 141 | + - psycopg2-binary |
124 | 142 |
|
125 | 143 | tasks: |
126 | 144 |
|
127 | | - - name: Install Tomcat package(s) |
| 145 | + - name: Create flask user |
| 146 | + user: |
| 147 | + name: "{{ flask_user }}" |
| 148 | + state: present |
| 149 | + |
| 150 | + - name: Install flask packages |
128 | 151 | package: |
129 | | - name: "{{ app_server_packages }}" |
| 152 | + name: "{{ __package }}" |
130 | 153 | state: present |
| 154 | + loop: "{{ app_yum_packages }}" |
| 155 | + loop_control: |
| 156 | + loop_var: __package |
131 | 157 |
|
132 | | - - name: Enable Tomcat at boot |
133 | | - service: |
134 | | - name: tomcat |
135 | | - enabled: yes |
| 158 | + - name: "Install flask repo {{ flask_repo }}" |
| 159 | + git: |
| 160 | + repo: "{{ flask_repo }}" |
| 161 | + dest: "{{ flask_home }}/{{ flask_app_name }}" |
| 162 | + update: yes |
| 163 | + force: yes |
| 164 | + tags: |
| 165 | + - git |
136 | 166 |
|
137 | | - - name: Create Tomcat content directory |
138 | | - file: |
139 | | - path: /usr/share/tomcat/webapps/ROOT |
140 | | - state: directory |
| 167 | + - name: "Change flash repo owner to {{ flask_user }} recursively" |
| 168 | + file: |
| 169 | + dest: "{{ flask_home }}/{{ flask_app_name }}" |
| 170 | + owner: "{{ flask_user }}" |
141 | 171 |
|
142 | | - - name: Copy static index.html to tomcat webapps/ansible/index.html |
143 | | - template: |
144 | | - src: index.html.j2 |
145 | | - dest: /usr/share/tomcat/webapps/ROOT/index.html |
146 | | - mode: 0644 |
| 172 | + - name: virtualenv setup |
| 173 | + block: |
147 | 174 |
|
148 | | - - name: Start Tomcat |
149 | | - service: |
150 | | - name: "{{ app_server_service }}" |
151 | | - state: started |
| 175 | + - name: Setup pre-requisite pip3 packages |
| 176 | + pip: |
| 177 | + name: "{{ pip_dependencies }}" |
| 178 | + state: latest |
| 179 | + executable: /usr/bin/pip3 |
| 180 | + |
| 181 | + - name: "Create virtualenv {{ virtualenv_name }} for Flask" |
| 182 | + pip: |
| 183 | + requirements: "{{ flask_home }}/{{ flask_app_name }}/requirements.txt" |
| 184 | + virtualenv: "{{ virtualenv_home }}/{{ flask_app_name }}" |
| 185 | + virtualenv_site_packages: no |
| 186 | + virtualenv_command: /usr/local/bin/virtualenv |
| 187 | + tags: |
| 188 | + - venv |
| 189 | + - virtualenv |
| 190 | + |
| 191 | + - name: Initialize flask app database resources |
| 192 | + command: "{{ virtualenv_home }}/{{ flask_app_name }}/bin/python3 {{ flask_home }}/{{ flask_app_name }}/config.py" |
| 193 | + run_once: true |
152 | 194 |
|
| 195 | + - name: "Generate {{ flask_app_name }} startup script" |
| 196 | + template: |
| 197 | + src: launch_resource_hub.j2 |
| 198 | + dest: /usr/local/bin/launch_resource_hub |
| 199 | + mode: '0555' |
| 200 | + |
| 201 | + - name: template systemd service config |
| 202 | + template: |
| 203 | + src: flask_service.j2 |
| 204 | + dest: /etc/systemd/system/{{ flask_app_name }}.service |
| 205 | + mode: '0755' |
| 206 | + |
| 207 | + - name: start systemd app service |
| 208 | + systemd: |
| 209 | + name: "{{ flask_app_name }}.service" |
| 210 | + state: restarted |
| 211 | + enabled: yes |
153 | 212 |
|
154 | 213 | - name: Deploy haproxy load balancer |
155 | 214 | hosts: load_balancers |
|
189 | 248 | name: haproxy |
190 | 249 | state: restarted |
191 | 250 |
|
192 | | -- name: End to end smoke tests |
193 | | - hosts: app_servers:database_servers |
194 | | - gather_facts: true |
195 | | - become: true |
196 | | - tags: |
197 | | - - smoketest |
198 | | - |
199 | | - tasks: |
200 | | - |
201 | | - - debug: |
202 | | - msg: |
203 | | - - "IP is {{ hostvars['appdb1']['ansible_default_ipv4']['address'] }}" |
204 | | - - "First App server {{ groups.app_servers[0] }}" |
205 | | - verbosity: 2 |
206 | | - |
207 | | - - name: Smoketest Postgres database |
208 | | - postgresql_ping: |
209 | | - db: flask_db |
210 | | - login_host: "{{ hostvars['appdb1']['ansible_default_ipv4']['address'] }}" |
211 | | - login_user: flask |
212 | | - login_password: redhat |
213 | | - ssl_mode: disable |
214 | | - delegate_to: "{{ groups.app_servers[0] }}" |
215 | | - run_once: true |
216 | | - tags: |
217 | | - - smoketest |
218 | | - |
219 | | -- name: End to end smoke tests |
220 | | - hosts: app_servers |
221 | | - become: false |
222 | | - gather_facts: false |
223 | | - tags: |
224 | | - - smoketest |
225 | | - tasks: |
226 | | - |
227 | | - - name: Check webserver for correct response |
228 | | - uri: |
229 | | - url: "http://frontend1.{{ GUID }}.example.opentlc.com" |
230 | | - return_content: yes |
231 | | - until: '"Welcome to Tomcat" in result.content' |
232 | | - retries: 10 |
233 | | - delay: 1 |
234 | | - register: result |
235 | | - delegate_to: localhost |
236 | | - |
237 | | -... |
| 251 | +#- name: End to end smoke tests |
| 252 | +# hosts: app_servers:database_servers |
| 253 | +# gather_facts: true |
| 254 | +# become: true |
| 255 | +# tags: |
| 256 | +# - smoketest |
| 257 | +# |
| 258 | +# tasks: |
| 259 | +# |
| 260 | +# - debug: |
| 261 | +# msg: |
| 262 | +# - "IP is {{ hostvars['appdb1']['ansible_default_ipv4']['address'] }}" |
| 263 | +# - "First App server {{ groups.app_servers[0] }}" |
| 264 | +# verbosity: 2 |
| 265 | +# |
| 266 | +# - name: Smoketest Postgres database |
| 267 | +# postgresql_ping: |
| 268 | +# db: flask_db |
| 269 | +# login_host: "{{ hostvars['appdb1']['ansible_default_ipv4']['address'] }}" |
| 270 | +# login_user: flask |
| 271 | +# login_password: redhat |
| 272 | +# ssl_mode: disable |
| 273 | +# delegate_to: "{{ groups.app_servers[0] }}" |
| 274 | +# run_once: true |
| 275 | +# tags: |
| 276 | +# - smoketest |
| 277 | +# |
| 278 | +#- name: End to end smoke tests |
| 279 | +# hosts: app_servers |
| 280 | +# become: false |
| 281 | +# gather_facts: false |
| 282 | +# tags: |
| 283 | +# - smoketest |
| 284 | +# tasks: |
| 285 | +# |
| 286 | +# - name: Check webserver for correct response |
| 287 | +# uri: |
| 288 | +# url: "http://frontend1.{{ GUID }}.example.opentlc.com" |
| 289 | +# return_content: yes |
| 290 | +# until: '"Welcome to Tomcat" in result.content' |
| 291 | +# retries: 10 |
| 292 | +# delay: 1 |
| 293 | +# register: result |
| 294 | +# delegate_to: localhost |
| 295 | +# |
| 296 | +#... |
0 commit comments