Skip to content

Commit

Permalink
Merge pull request #2 from yetu/idempotency&enhancement
Browse files Browse the repository at this point in the history
Supports supervisor "service resilience" and various enhancment
  • Loading branch information
jschaul committed May 27, 2015
2 parents 43ecdc5 + e239a0d commit 7d5ef85
Show file tree
Hide file tree
Showing 14 changed files with 258 additions and 125 deletions.
41 changes: 26 additions & 15 deletions defaults/main.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
---

deployment_install_initd : true

# Directory structure for app
deployment_user : "myUnixUsername"
deployment_group : "{{ deployment_user }}"
deployment_work_dir : "/home/{{deployment_user}}/{{deployment_artifact_name}}"
deployment_current_dir : "{{deployment_work_dir}}/current"
deployment_work_dir : "/home/{{ deployment_user }}/{{ deployment_artifact_name }}"
deployment_current_dir : "{{ deployment_work_dir }}/current"

# What daemon system to use
deployment_install_daemon : "initd"

# Resolver what services to get the artifacts from
deployment_artifact_resolver : "nexus"

# artifact settings
deployment_artifact_name : "myApp"
Expand All @@ -15,17 +19,11 @@ deployment_artifact_version : "LATEST"
deployment_artifact_extension : "tgz"
deployment_artifact_destination_name : "{{ deployment_artifact_name }}.{{ deployment_artifact_extension }}"

# Used within the init.d script
deployment_exec_cmd : "{{ deployment_current_dir }}/bin/{{ deployment_artifact_name }}"
deployment_exec_arg : ""

# Used for getting PID ID, it should be a unique value
deployment_grep_name : "{{ deployment_exec_cmd }}"

# How long to probe for an active process upon service start / restart
deployment_timeout_start_sec : 5
# Pause time to wait for application to bootstrap. Needed for e.g. apps including the new relic agent.
deployment_bootstrap_wait_sec : 0
# How long to wait before starting to probe for service
deployment_delay_sec : 5
# How long to wait before failing if an application is not up
deployment_timeout_wait_sec : 45

# HTTP GET request to check that the server is up and running
# the content of the result is then checked and compared with _expected_content
Expand All @@ -37,7 +35,20 @@ deployment_http_check_port : "80"
deployment_http_check_domain : "{{ansible_default_ipv4.address}}"
deployment_http_check_url : "{{deployment_http_check_protocol}}{{deployment_http_check_domain}}:{{deployment_http_check_port}}{{deployment_http_check_url_suffix}}"

# By default you deploy once and to override you must pass true to force
deployment_force : false
deployment_guard_file : "/var/local/deployment_first_boot_file"


# nexus settings
deployment_nexus_url_base : ""
deployment_nexus_url_resolver : "{{ deployment_nexus_url_base }}/nexus/service/local/artifact/maven/resolve?r=releases&g={{ deployment_artifact_organization }}&a={{ deployment_artifact_name }}&e={{ deployment_artifact_extension }}&v={{ deployment_artifact_version }}"
deployment_nexus_url_download : "{{ deployment_nexus_url_base }}/nexus/content/repositories/releases/{{ nexus_artifact_info.json.data.repositoryPath }}"
deployment_nexus_url_download : "{{ deployment_nexus_url_base }}/nexus/content/repositories/releases/{{ nexus_artifact_info.json.data.repositoryPath }}"


## *** Initd setting (if using initd) ***
# Used within the init.d script
deployment_exec_cmd : "{{ deployment_current_dir }}/bin/{{ deployment_artifact_name }}"
deployment_exec_arg : ""
# Used for getting PID ID, it should be a unique value
deployment_grep_name : "{{ deployment_exec_cmd }}"
55 changes: 41 additions & 14 deletions handlers/main.yml
Original file line number Diff line number Diff line change
@@ -1,27 +1,54 @@
---
- name: "restartApp"
service:

- name: restart app
debug:
msg="Restart app"
changed_when: true
notify:
- restart app initd
- restart app supervisor

- name: restart app initd
service:
name="{{ deployment_artifact_name }}"
state="restarted"
enabled="yes"
notify: "wait for application to bootstrap"
when: deployment_install_initd
changed_when: true
notify: wait 4 port
when: deployment_install_daemon == "initd"

- name: "wait for application to bootstrap"
pause: seconds={{ deployment_bootstrap_wait_sec }}
- name: restart app supervisor
supervisorctl:
name="{{ deployment_artifact_name }}"
state="restarted"
changed_when: true
notify: "make http request to service"
notify: wait 4 port
when: deployment_install_daemon == "supervisor"

- name: "make http request to service"
uri: url={{deployment_http_check_url}} return_content=yes HEADER_Accept="text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"
- name: wait 4 port
wait_for:
port="{{ deployment_http_check_port }}"
host="{{ ansible_ssh_host | default(inventory_hostname) }}"
delay="{{ deployment_delay_sec }}"
timeout="{{ deployment_timeout_wait_sec }}"
changed_when: true
notify: HTTP request

- name: HTTP request
uri:
url="{{ deployment_http_check_url }}"
return_content="yes"
HEADER_Accept="text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"
changed_when: true
notify: check payload
register: webpage
tags: ["verify-working"]
notify: "fail if http request does not contain expected content"
when: deployment_http_check_url_suffix is defined
tags: ["verify-working"]

- name: "fail if http request does not contain expected content"
fail: msg=" {{deployment_artifact_name}} is not happy, {{deployment_http_check_url}} does not return expected content [{{deployment_http_check_expected_content}}]"
when: "'{{deployment_http_check_expected_content}}' not in webpage.content"
- name: check payload
fail:
msg=" {{deployment_artifact_name}} is not happy, {{deployment_http_check_url}} does not return expected content [{{deployment_http_check_expected_content}}]"
when: deployment_http_check_expected_content not in webpage.content
tags: ["verify-working"]


12 changes: 8 additions & 4 deletions tasks/init-script.yml → tasks/daemon/init-script.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---

- name: "init-script | Create application init.d script"
- name: init-script | Create application init.d script
initd:
name="{{ deployment_artifact_name }}"
path="{{ deployment_work_dir }}/{{ deployment_artifact_name }}.sh"
Expand All @@ -14,14 +14,18 @@
owner="{{ deployment_user }}"
group="{{ deployment_user }}"
mode=0755
notify:
- restart app
tags: create_initscript

- name: "init-script | Create symlink for init script"
- name: init-script | Create symlink for init script
file:
src="{{deployment_work_dir}}/{{deployment_artifact_name}}.sh"
dest="/etc/init.d/{{deployment_artifact_name}}"
src="{{ deployment_work_dir }}/{{ deployment_artifact_name }}.sh"
dest="/etc/init.d/{{ deployment_artifact_name }}"
owner="root"
group="root"
mode="0755"
state="link"
notify:
- restart app
tags: link_initscript
9 changes: 9 additions & 0 deletions tasks/daemon/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---

- include: init-script.yml
when: deployment_install_daemon == "initd"
tags: ["initd"]

- include: supervisor.yml
when: deployment_install_daemon == "supervisor"
tags: ["supervisor"]
10 changes: 10 additions & 0 deletions tasks/daemon/supervisor.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---

- name: daemon | supervisor | Create Service file
template:
src="../../templates/supervisor.j2"
dest="/etc/supervisor/conf.d/{{ deployment_supervisor_name }}.ini.conf"
owner=root
mode=0640
notify:
- restart app
20 changes: 0 additions & 20 deletions tasks/directories.yml

This file was deleted.

36 changes: 36 additions & 0 deletions tasks/directory-structure.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
---

- name: directory structure | Change file owner for all files
file:
path="{{ deployment_artifact_version_dir }}"
owner="{{ deployment_user }}"
group="{{ deployment_group }}"
state="directory"
recurse="yes"

- name: directory structure | Change file owner for all files
file:
path="{{ deployment_artifact_version_dir }}"
owner="{{ deployment_user }}"
group="{{ deployment_group }}"
state="directory"
recurse="yes"

- name: directory structure | Create latest symlink
file:
src="{{ deployment_artifact_version_dir }}"
dest="{{ deployment_current_dir }}"
owner="{{ deployment_user }}"
group="{{ deployment_group }}"
mode="0755"
state="link"
notify:
- restart app

- name: directory structure | Create deployment date file
copy:
content=""
dest="{{ deployment_current_dir }}/{{ current_date.stdout }}.deployment.date"
owner="{{ deployment_user }}"
group="{{ deployment_group }}"
when: download_artifact.changed
40 changes: 40 additions & 0 deletions tasks/download.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
---

- name: download | Get current date
shell: date +"%Y-%m-%d_%H_%M"
changed_when: False
register: current_date

- name: download | set new version directory
set_fact:
deployment_artifact_version_dir : "{{ deployment_work_dir }}/{{ nexus_artifact_info.json.data.version }}"

- name: download | Create application base directories
file:
path="{{ item }}"
owner="{{ deployment_user }}"
group="{{ deployment_group }}"
mode="0755"
state="directory"
with_items:
- "{{ deployment_work_dir }}"
- "{{ deployment_artifact_version_dir }}"

- name: download | nexus | download artifact
get_url:
url="{{ deployment_nexus_url_download }}"
dest="{{ deployment_artifact_version_dir }}/{{ deployment_artifact_destination_name }}"
register: download_artifact

- name: download | Unzip application (if .tgz)
command: "tar -xf {{deployment_artifact_version_dir}}/{{deployment_artifact_destination_name}} -C {{deployment_artifact_version_dir}} --strip-components 1"
when: deployment_artifact_destination_name.find('.tgz') >= 1 and download_artifact.changed

- name: download | Unzip application (if .zip)
command: "unzip -o {{deployment_artifact_version_dir}}/{{deployment_artifact_destination_name}} -d {{deployment_artifact_version_dir}}"
when: deployment_artifact_destination_name.find('.zip') >= 1 and download_artifact.changed

- name: download | Unzip config files (if .jar) (Optional)
command: "unzip {{deployment_artifact_version_dir}}/{{deployment_artifact_destination_name}} -d {{deployment_artifact_version_dir}} *.conf"
when: deployment_artifact_destination_name.find('.jar') >= 1 and download_artifact.changed
ignore_errors: yes
20 changes: 0 additions & 20 deletions tasks/get-artifact.yml

This file was deleted.

37 changes: 24 additions & 13 deletions tasks/main.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,29 @@
---

- include: directories.yml
tags: ["deploy-directories"]

- include: init-script.yml
when: deployment_install_initd
tags: ["deploy-initd"]

- include: get-artifact.yml
tags: ["deploy-artifact"]

- include: unpack.yml
tags: ["deploy-unpack"]

- name: main | Check if first boot file exists
stat:
path="{{ deployment_guard_file }}"
register: reg_guard_file

- include: resolver/main.yml
when: not reg_guard_file.stat.exists or deployment_force

- include: download.yml
when: not reg_guard_file.stat.exists or deployment_force
tags: ["download"]

- include: daemon/main.yml
when: not reg_guard_file.stat.exists or deployment_force
tags: ["initd"]

- include: directory-structure.yml
when: not reg_guard_file.stat.exists or deployment_force
tags: ["directory", "directory-structure"]

- name: main | Touch file guard
copy:
content=""
dest="{{ deployment_guard_file }}"



Expand Down
4 changes: 4 additions & 0 deletions tasks/resolver/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---

- include: nexus.yml
when: deployment_artifact_resolver == "nexus"
19 changes: 19 additions & 0 deletions tasks/resolver/nexus.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---

- name: resolve | nexus | Make the http request to find the latest version on nexus
uri:
url="{{ deployment_nexus_url_resolver }}"
method=GET
HEADER_Accept="application/json"
HEADER_Authorization="{{ nexus_credentials | default(omit) }}"
register: nexus_artifact_info
tags: ["artifact-info"]

- name: resolve | nexus | Show nexus url
debug:
var=deployment_nexus_url_resolver

- name: resolve | nexus | Show nexus artifact information
debug:
var=nexus_artifact_info.json.data
tags: ["artifact-info"]
Loading

0 comments on commit 7d5ef85

Please sign in to comment.