203 lines
7.4 KiB
YAML
203 lines
7.4 KiB
YAML
---
|
|
- name: Playbook to deploy Jar
|
|
hosts: all
|
|
gather_facts: false
|
|
become: yes
|
|
become_method: sudo
|
|
|
|
tasks:
|
|
- name: Check if region is configured
|
|
shell: "aws configure get default.region"
|
|
register: aws_cli_region_reponse
|
|
|
|
- name: "ERROR: AWS region missing"
|
|
vars:
|
|
msg: |
|
|
#############################################################################################################################
|
|
####### AWS REGION IS NOT CONFIGURED PROPERLY. CURRENT REGION: {{ aws_cli_region_reponse.stdout | upper }}, SHOULD BE: AP-SOUTHEAST-1 #######
|
|
#############################################################################################################################
|
|
debug:
|
|
msg: "{{ msg.split('\n') }}"
|
|
when: aws_cli_region_reponse.stdout != "ap-southeast-1"
|
|
failed_when:
|
|
- aws_cli_region_reponse.stdout != "ap-southeast-1"
|
|
|
|
- name: Check if CICD profile is configured
|
|
shell: "aws configure get region --profile cicd"
|
|
register: aws_profile_cli_region_reponse
|
|
|
|
- name: "ERROR: AWS region missing in profile"
|
|
vars:
|
|
msg: |
|
|
#############################################################################################################################
|
|
####### AWS REGION IS NOT CONFIGURED PROPERLY. CURRENT REGION: {{ aws_cli_region_reponse.stdout | upper }}, SHOULD BE: AP-SOUTHEAST-1 #######
|
|
#############################################################################################################################
|
|
debug:
|
|
msg: "{{ msg.split('\n') }}"
|
|
when: aws_profile_cli_region_reponse.stdout != "ap-southeast-1"
|
|
failed_when:
|
|
- aws_profile_cli_region_reponse.stdout != "ap-southeast-1"
|
|
|
|
- name: Check if IAM role is attached
|
|
shell: "aws sts get-caller-identity"
|
|
register: aws_cli_gci_reponse
|
|
|
|
- name: "ERROR: IAM role missing"
|
|
vars:
|
|
msg: |
|
|
#############################################################################################################################
|
|
############################################### CICD ROLE IS NOT ATTACHED ###############################################
|
|
#############################################################################################################################
|
|
debug:
|
|
msg: "{{ msg.split('\n') }}"
|
|
when:
|
|
- aws_cli_gci_reponse.stdout is not search("assumed-role/cicd")
|
|
failed_when:
|
|
- aws_cli_gci_reponse.stdout is not search("assumed-role/cicd")
|
|
|
|
- name: Check if systemd service exists
|
|
stat:
|
|
path: "/etc/systemd/system/{{ app_name }}.service"
|
|
register: service_status
|
|
|
|
- name: Systemd service status
|
|
vars:
|
|
msg: |
|
|
#############################################################################################################################
|
|
################################ SYSTEMD SERVICE {{ app_name | upper }} DOES NOT EXIST. EXITING. ##################################
|
|
#############################################################################################################################
|
|
debug:
|
|
msg: "{{ msg.split('\n') }}"
|
|
when: service_status is defined and not service_status.stat.exists
|
|
failed_when:
|
|
- not service_status.stat.exists
|
|
|
|
- name: Check application directory structure
|
|
block:
|
|
|
|
- name: Check application directory structure
|
|
stat:
|
|
path: "/home/ubuntu/{{ app_name }}"
|
|
register: app_dir_status
|
|
|
|
- name: Application directory does not exist
|
|
vars:
|
|
msg: |
|
|
#############################################################################################################################
|
|
###################### APPLICATION DIRECTORY: /home/ubuntu/{{ app_name }} DOES NOT EXIST. EXITING. #######################
|
|
#############################################################################################################################
|
|
debug:
|
|
msg: "{{ msg.split('\n') }}"
|
|
when: app_dir_status is defined and not app_dir_status.stat.exists
|
|
failed_when:
|
|
- not app_dir_status.stat.exists
|
|
|
|
tags:
|
|
- artifact_deployment
|
|
|
|
- name: Ensure jq is installed
|
|
apt:
|
|
name: "jq"
|
|
state: present
|
|
force_apt_get: yes
|
|
|
|
- name: Checking if environment file exists, pre-deployment
|
|
stat:
|
|
path: "/etc/sysconfig/{{ app_name }}"
|
|
register: env_file
|
|
|
|
- name: Backing up current properties
|
|
copy:
|
|
src: "/etc/sysconfig/{{ app_name }}"
|
|
dest: "/etc/sysconfig/rollback-{{ app_name }}"
|
|
owner: ubuntu
|
|
group: ubuntu
|
|
mode: '0664'
|
|
remote_src: yes
|
|
force: yes
|
|
when: env_file.stat.exists
|
|
|
|
- name: Checking if current symlink exists, pre-deployment
|
|
stat:
|
|
path: "/home/ubuntu/{{ app_name }}/{{ app_name }}-current.jar"
|
|
register: current_symlink
|
|
|
|
- name: Show output
|
|
debug: msg= "{{ current_symlink }}"
|
|
|
|
- name: Checking if current artifact exists, pre-deployment
|
|
stat:
|
|
path: "{{ current_symlink.stat.lnk_source }}"
|
|
when:
|
|
- current_symlink.stat.exists
|
|
- current_symlink.stat.islnk
|
|
register: current_artifact
|
|
|
|
- name: Show output
|
|
debug: msg= "{{ current_artifact }}"
|
|
|
|
- name: Stopping systemd service
|
|
systemd:
|
|
name: '{{ app_name }}'
|
|
state: stopped
|
|
|
|
- name: Download latest properties
|
|
shell: "pull-env '{{ env }}' '{{ app_name }}'"
|
|
ignore_errors: yes
|
|
|
|
- name: Download latest artifact
|
|
copy:
|
|
src: "{{ item }}"
|
|
dest: "/home/ubuntu/{{ app_name }}/"
|
|
owner: ubuntu
|
|
group: ubuntu
|
|
with_fileglob:
|
|
- "{{ repo_name }}/target/*.jar"
|
|
|
|
- name: Setting latest_artifact_name
|
|
set_fact:
|
|
latest_artifact_name: '{{ item.split("/")[-1] }}'
|
|
with_fileglob:
|
|
- "{{ repo_name }}/target/*.jar"
|
|
|
|
- name: Creating rollback symlink
|
|
file:
|
|
src: "{{ current_artifact.stat.path }}"
|
|
dest: "/home/ubuntu/{{ app_name }}/rollback"
|
|
state: link
|
|
|
|
- name: Removing current symlink
|
|
file:
|
|
path: "/home/ubuntu/{{ app_name }}/{{ app_name }}-current.jar"
|
|
state: absent
|
|
|
|
- name: Creating current symlink to latest artifact
|
|
file:
|
|
src: "/home/ubuntu/{{ app_name }}/{{ latest_artifact_name }}"
|
|
dest: "/home/ubuntu/{{ app_name }}/{{ app_name }}-current.jar"
|
|
state: link
|
|
|
|
- name: Starting systemd service
|
|
systemd:
|
|
name: '{{ app_name }}'
|
|
state: started
|
|
daemon_reload: yes
|
|
|
|
- name: Trying healthcheck
|
|
uri:
|
|
url: "http://localhost:{{ app_port }}{{ healthcheck_api }}"
|
|
method: GET
|
|
register: healthcheck_response
|
|
until: healthcheck_response.status == 200
|
|
retries: 60
|
|
delay: 1
|
|
ignore_errors: False
|
|
|
|
- name: Healthcheck response
|
|
vars:
|
|
msg: |
|
|
##############################################################################################################################
|
|
HEALTH-CHECK ({{ healthcheck_api }}) RESPONSE: {{ healthcheck_response.status }}
|
|
##############################################################################################################################
|
|
debug:
|
|
msg: "{{ msg.split('\n') }}" |