Skip to content

Commit

Permalink
testing
Browse files Browse the repository at this point in the history
  • Loading branch information
ayshvm authored Jul 6, 2024
1 parent 262af1f commit cd4eff0
Showing 1 changed file with 22 additions and 16 deletions.
38 changes: 22 additions & 16 deletions infrastructure/nomad/playbooks/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -585,21 +585,26 @@

- name: Deploy Jobs
ansible.builtin.shell: |
# nomad job plan {{ ansible_env.HOME }}/{{ env }}/{{ job.name }}.nomad
# nomad run {{ ansible_env.HOME }}/{{ env }}/{{ job.name }}.nomad
LOGFILE="/var/log/nomad_deploy_{{ job.name }}.log"
# Plan the job
echo "Planning job {{ job.name }}..." | tee -a $LOGFILE
nomad job plan {{ ansible_env.HOME }}/{{ env }}/{{ job.name }}.nomad > /tmp/plan_result-{{ job.name }}.txt 2>&1
cat /tmp/plan_result-{{ job.name }}.txt | tee -a $LOGFILE
# Check for differences in the job plan
nomad job plan {{ ansible_env.HOME }}/{{ env }}/{{ job.name }}.nomad > /tmp/plan_result.txt
cat /tmp/plan_result.txt
if grep -q "No changes" /tmp/plan_result.txt; then
echo "No changes in the job plan for {{ job.name }}. Skipping deployment."
echo "Checking for differences in the job plan..." | tee -a $LOGFILE
if grep -q "+/-" /tmp/plan_result-{{ job.name }}.txt; then
echo "No changes in the job plan for {{ job.name }}. Skipping deployment." | tee -a $LOGFILE
else
echo "Differences found in the job plan for {{ job.name }}. Redeploying job."
echo "Differences found in the job plan for {{ job.name }}. Redeploying job." | tee -a $LOGFILE
# Stop the existing job
nomad job stop {{ job.name }}
echo "Stopping existing job {{ job.name }}..." | tee -a $LOGFILE
nomad job stop {{ job.name }} | tee -a $LOGFILE
# Run the updated job
nomad run {{ ansible_env.HOME }}/{{ env }}/{{ job.name }}.nomad
echo "Running updated job {{ job.name }}..." | tee -a $LOGFILE
nomad run {{ ansible_env.HOME }}/{{ env }}/{{ job.name }}.nomad | tee -a $LOGFILE
fi
TIMEOUT={% if profile == 'ci' %}600{% else %}300{% endif %}
Expand All @@ -608,21 +613,21 @@
while true; do
RESULT=$(nomad job status -json "{{ job.name }}")
if [ $? -ne 0 ]; then
echo "Failed to get job status for {{ job.name }}:"
echo "${RESULT}"
echo "Failed to get job status for {{ job.name }}:" | tee -a $LOGFILE
echo "${RESULT}" | tee -a $LOGFILE
exit 1
fi
ALLOC_ID=$(echo "${RESULT}" | jq -r '.[0].Allocations[0].ID')
JOB_TYPE=$(echo "${RESULT}" | jq -r '.[0].Allocations[0].JobType')
STATUS=$(echo "${RESULT}" | jq -r '.[0].Allocations[0].ClientStatus')
echo "Current status of job {{ job.name }}: ${STATUS}"
echo "Current status of job {{ job.name }}: ${STATUS}" | tee -a $LOGFILE
# Fetch and display logs
LOGS=$(nomad alloc logs ${ALLOC_ID} 2>&1)
echo "Logs for allocation ${ALLOC_ID}:"
echo "${LOGS}"
echo "Logs for allocation ${ALLOC_ID}:" | tee -a $LOGFILE
echo "${LOGS}" | tee -a $LOGFILE
case "${JOB_TYPE}" in
service)
Expand All @@ -639,7 +644,7 @@
{% if env != 'devenv' %}
break
{% else %}
echo "Unknown job type: ${JOB_TYPE}"
echo "Unknown job type: ${JOB_TYPE}" | tee -a $LOGFILE
exit 1
{% endif %}
;;
Expand All @@ -648,7 +653,7 @@
CURRENT_TIME="$(date +%s)"
ELAPSED_TIME="$(( CURRENT_TIME - START_TIME ))"
if [ ${ELAPSED_TIME} -ge ${TIMEOUT} ]; then
echo "Deploy timed out for {{ job.name }}, current status: ${STATUS}"
echo "Deploy timed out for {{ job.name }}, current status: ${STATUS}" | tee -a $LOGFILE
exit 1
fi
Expand All @@ -668,6 +673,7 @@
no_log: false



- name: Post Deployment Info
ansible.builtin.debug:
msg: |
Expand Down

0 comments on commit cd4eff0

Please sign in to comment.