---
schemaVersion: "2.2"
description: "Run an binary service action, either stop, start or upgrade"
parameters:
Service:
type: "String"
description: "Service to Run on Node"
default: "N/A"
Environment:
type: "String"
description: "Environment to run in"
default: "N/A"
InstanceId:
type: "String"
description: "InstanceId of the executing Node"
default: "N/A"
Action:
type: "String"
description: "Action to execute on the Node"
default: "N/A"
mainSteps:
- action: "aws:runShellScript"
name: "example"
inputs:
runCommand:
- |
case "{{ Action }}" in
"down")
service {{ Service }} stop
echo "{\"instance_id\": \"{{ InstanceId }}\", \"status\": \"success\", \"service\": \"{{ Service }}\", \"state\": \"{{ Action }}\" }"
;;
"up")
service {{ Service }} restart
echo "{\"instance_id\": \"{{ InstanceId }}\", \"status\": \"success\", \"service\": \"{{ Service }}\", \"state\": \"{{ Action }}\" }"
;;
"upgrade")
export SI_SERVICE={{ Service }}
export SI_HOSTENV={{ Environment }}
export SI_VERSION=$(aws ssm get-parameter --query "Parameter.Value" --output text --name "$SI_HOSTENV-si-version-$SI_SERVICE")
export SI_ARTIFACT_TYPE=$(aws ssm get-parameter --query "Parameter.Value" --output text --name "$SI_HOSTENV-binary-type")
docker-compose -f /run/app/docker-compose.yaml --profile $SI_SERVICE down -v &
docker-compose -f /run/app/docker-compose.yaml --profile $SI_SERVICE pull &
wait
docker-compose -f /run/app/docker-compose.yaml --profile $SI_SERVICE up --wait
wget --no-verbose https://artifacts.systeminit.com/{{ Service }}/${SI_VERSION}/${SI_ARTIFACT_TYPE}/linux/$(arch)/{{ Service }}-${SI_VERSION}-${SI_ARTIFACT_TYPE}-linux-$(arch).tar.gz -O - | tar -xzf - -C /
if [ -f "/usr/local/share/{{ Service }}/metadata.json" ]; then
METADATA=$(cat /usr/local/share/{{ Service }}/metadata.json | jq)
else
METADATA=$(sudo find / -wholename '/etc/nix-omnibus/{{ Service }}/**/metadata.json' | tail -n 1 | xargs cat | jq)
fi
COMMIT=$(echo $METADATA | jq -r '.commit')
RUNNING_VERSION=$(echo $METADATA | jq -r '.version')
HONEYCOMB_API_KEY=$(aws secretsmanager get-secret-value --region us-east-1 --secret-id ${SI_HOSTENV}-honeycomb-api-key | jq -r '.SecretString')
curl https://api.honeycomb.io/1/markers/$SI_SERVICE -X POST \
-H "X-Honeycomb-Team: $HONEYCOMB_API_KEY" \
-d '{"message":" '"$SI_SERVICE replica deployed! Commit: $COMMIT Version: $RUNNING_VERSION"' ", "type":"deploy"}'
echo "{\"instance_id\": \"{{ InstanceId }}\", \"status\": \"success\", \"service\": \"{{ Service }}\", \"running\": \"$RUNNING_VERSION\", \"desired_version\": \"$SI_VERSION\" }"
;;
*)
echo "{\"instance_id\": \"{{ InstanceId }}\", \"status\": \"error\", \"message\": \"Failed to execute action {{ Action }}, not supported.\"}"
;;
esac