Chapter 6: Secure Delivery and Rollback Preparation #11

Open
turanisl wants to merge 27 commits from chapter6-secure-delivery into main
4 changed files with 105 additions and 0 deletions
Showing only changes of commit f8525aa64d - Show all commits
@@ -0,0 +1,33 @@
pipeline {
agent { label 'chapter6-trusted' }
options {
disableConcurrentBuilds()
disableRestartFromStage()
skipDefaultCheckout()
timeout(time: 10, unit: 'MINUTES')
}
stages {
stage('Exercise staging startup failure') {
steps {
script {
int result = sh(returnStatus: true, script: 'mkdir -p drill && sudo -n /opt/chapter6-tools/staging-drill fail > drill/failure.txt 2>&1')
echo "Staging drill command exited ${result}"
if (result == 42) {
error('Expected failure verified: wrong-port staging container was unhealthy. Restoring staging in post.')
}
error("Unexpected drill result: ${result}. Inspect the failure and recovery logs.")
}
}
}
}
post {
always {
script {
int restored = sh(returnStatus: true, script: 'mkdir -p drill && sudo -n /opt/chapter6-tools/staging-drill restore > drill/recovery.txt 2>&1')
archiveArtifacts artifacts: 'drill/*.txt', allowEmptyArchive: false
if (restored != 0) { error("Staging restoration failed: ${restored}") }
echo 'Staging restored and verified over HTTPS.'
}
}
}
}
+14
View File
@@ -0,0 +1,14 @@
# Jenkins staging failure drill
The live controller runs this Pipeline on the restricted Chapter 6 agent.
Its two sudo entries permit only the literal arguments `fail` and `restore`
to the root-owned wrapper at `/opt/chapter6-tools/staging-drill`.
The root-owned deployment record is fixed to v1 staging.
The failure stage uses the ordinary Ansible playbook with the wrong-port
template from this directory. Exit 42 means Ansible rejected the unhealthy
staging container as expected. The Pipeline records FAILURE and its post
action restores and verifies the original v1 staging deployment.
[Observed Jenkins drill #1](https://ci.165.227.90.215.sslip.io/job/chapter6-staging-drill/1/).
The checkout does not install any sudo rules. Coordinate with the controller
owner for grader access to the archived failure and recovery logs.
+20
View File
@@ -0,0 +1,20 @@
services:
app:
image: "{{ release_image }}"
command: ["gunicorn", "--bind", "0.0.0.0:8001", "app:app"]
restart: unless-stopped
read_only: true
tmpfs:
- /tmp
cap_drop:
- ALL
security_opt:
- no-new-privileges:true
networks:
edge:
aliases:
- "pricing-{{ target_env }}"
networks:
edge:
external: true
name: "{{ proxy_network }}"
+38
View File
@@ -0,0 +1,38 @@
#!/bin/bash
set -euo pipefail
umask 077
export PATH=/usr/sbin:/usr/bin:/sbin:/bin
export ANSIBLE_COLLECTIONS_PATH=/opt/chapter6-tools/collections ANSIBLE_NOCOLOR=1
base=/opt/chapter6-tools
known=$base/v1/secure-delivery/ansible
record=$base/promotions/v1-staging.yml
[[ $# = 1 ]] || exit 64
exec 9>/run/lock/chapter6-promote.lock
flock -n 9
run() {
"$base/ansible/bin/ansible-playbook" -i "$known/inventory.ini" "$1" -e ansible_connection=local -e "@$record"
}
date -u +%FT%TZ
case "$1" in
fail)
[[ $(docker inspect -f '{{.State.Health.Status}}' pricing-staging-app-1) = healthy ]]
output=$(mktemp)
trap 'rm -f "$output"' EXIT
rc=0
run "$base/failure-rehearsal/deploy.yml" >"$output" 2>&1 || rc=$?
cat "$output"
[[ $rc = 2 ]] || exit 65
grep -q 'pricing-staging-app-1 is unhealthy' "$output" || exit 66
docker inspect -f '{{json .Config.Cmd}}' pricing-staging-app-1 | grep -q '0.0.0.0:8001' || exit 67
date -u +%FT%TZ
exit 42
;;
restore)
run "$known/deploy.yml"
for path in / /health '/price?quantity=3'; do
curl -fsS --max-time 10 "https://staging.165.227.90.215.sslip.io$path"
done
date -u +%FT%TZ
;;
*) exit 64 ;;
esac