Post not yet marked as solved
Post marked as unsolved with 0 replies, 550 views
Hello, here is what I'm doing:
I creating AWS macOS instance
I then set up a /Library/LaunchDaemon plist file that runs a bash script:
&#9;<?xml version="1.0" encoding="UTF-8"?>
&#9;<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "XXXXX/DTDs/PropertyList-1.0.dtd">
&#9;<plist version="1.0">
&#9;<dict>
&#9;&#9;<key>Label</key>
&#9;&#9;<string>aws-ec2-mac-amis.cloud-connect</string>
&#9;&#9;<key>ProgramArguments</key>
&#9;&#9;<array>
&#9;&#9;&#9;<string>/usr/bin/env</string>
&#9;&#9;&#9;<string>/Users/ec2-user/aws-ec2-mac-amis/cloud-connect.bash</string>
&#9;&#9;</array>
&#9;&#9;<key>RunAtLoad</key>
&#9;&#9;<true/>
&#9;&#9;<key>WorkingDirectory</key>
&#9;&#9;<string>/Users/ec2-user</string>
&#9;&#9;<key>StandardErrorPath</key>
&#9;&#9;<string>/var/log/cloud-connect.log</string>
&#9;&#9;<key>StandardOutPath</key>
&#9;&#9;<string>/var/log/cloud-connect.log</string>
&#9;&#9;<key>EnableTransactions</key>
&#9;&#9;<true/>
&#9;&#9;<key>ExitTimeOut</key>
&#9;&#9;<string>300</string>
&#9;</dict>
&#9;</plist>
I've tried this same list without EnableTransactions and there is no difference.
This works and my bash script runs just fine:
#!/bin/bash
set -exo pipefail
[[ ! $EUID -eq 0 ]] && echo "RUN AS ROOT!" && exit 1
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
cd $SCRIPT_DIR
echo "Waiting for networking..."
while ! ping -c 1 -n github.com &> /dev/null; do sleep 1; done
git pull
. ./_helpers.bash
disjoin() {
&#9;set -x
&#9;/usr/local/bin/ankacluster disjoin &
&#9;CERTS=""
&#9;[[ ! -z "$CLOUD_CONNECT_CERT" ]] && CERTS="--cert $CLOUD_CONNECT_CERT"
&#9;[[ ! -z "$CLOUD_CONNECT_KEY" ]] && CERTS="$CERTS --cert-key $CLOUD_CONNECT_KEY"
&#9;[[ ! -z "$CLOUD_CONNECT_CA" ]] && CERTS="$CERTS --cacert $CLOUD_CONNECT_CA"
&#9;NODE_ID="$(curl -s $CERTS "${ANKA_CONTROLLER_ADDRESS}/api/v1/node" | jq -r ".body | .[] | select(.node_name==\"$(hostname)\") | .node_id")"
&#9;curl -s $CERTS -X DELETE "${ANKA_CONTROLLER_ADDRESS}/api/v1/node" -H "Content-Type: application/json" -d "{\"node_id\": \"$NODE_ID\"}"
}
Grab the ENVS the user sets in user-data
if [[ ! -e $CLOUD_CONNECT_PLIST_PATH ]]; then
&#9;mkdir -p $LAUNCH_LOCATION
cat > $CLOUD_CONNECT_PLIST_PATH <<EOD
&#9;<?xml version="1.0" encoding="UTF-8"?>
&#9;<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
&#9;<plist version="1.0">
&#9;<dict>
&#9;&#9;<key>Label</key>
&#9;&#9;<string>aws-ec2-mac-amis.cloud-connect</string>
&#9;&#9;<key>ProgramArguments</key>
&#9;&#9;<array>
&#9;&#9;&#9;<string>/usr/bin/env</string>
&#9;&#9;&#9;<string>/Users/ec2-user/aws-ec2-mac-amis/cloud-connect.bash</string>
&#9;&#9;</array>
&#9;&#9;<key>RunAtLoad</key>
&#9;&#9;<true/>
&#9;&#9;<key>WorkingDirectory</key>
&#9;&#9;<string>/Users/ec2-user</string>
&#9;&#9;<key>StandardErrorPath</key>
&#9;&#9;<string>/var/log/cloud-connect.log</string>
&#9;&#9;<key>StandardOutPath</key>
&#9;&#9;<string>/var/log/cloud-connect.log</string>
&#9;&#9;<key>EnableTransactions</key>
&#9;&#9;<true/>
&#9;&#9;<key>ExitTimeOut</key>
&#9;&#9;<string>300</string>
&#9;</dict>
&#9;</plist>
EOD
&#9;launchctl load -w $CLOUD_CONNECT_PLIST_PATH
else
&#9;echo "$(date) ($(whoami)): Attempting join..."
&#9;Check if user-data exists
&#9;[[ ! -z "$(curl -s XXXX/latest/user-data | grep 404)" ]] && echo "Could not find required ANKA_CONTROLLER_ADDRESS in instance user-data!" && exit 1
&#9;create user ENVs for this session
&#9;$(curl -s XXXX/latest/user-data | sed 's/\"//g')
&#9;IF the user wants to change the IP address for the registry domain name (if they want to use a second EC2 registry for better speed), handle setting the /etc/hosts
&#9;if [[ ! -z "$ANKA_REGISTRY_OVERRIDE_IP" && ! -z "$ANKA_REGISTRY_OVERRIDE_DOMAIN" ]]; then
&#9;&#9;&#9;modify_hosts $ANKA_REGISTRY_OVERRIDE_DOMAIN $ANKA_REGISTRY_OVERRIDE_IP
&#9;fi
&#9;Ensure that anytime the script stops, we disjoin first
&#9;/usr/local/bin/ankacluster join $ANKA_CONTROLLER_ADDRESS $ANKA_JOIN_ARGS
&#9;trap disjoin 0 Disjoin after we joined properly to avoid unloading prematurely
&#9;set +x
&#9;while true; do
&#9;&#9;sleep 1 &
&#9;&#9;wait $!
&#9;done
fi
I see the process running, and the host has connected to the remote server's controller:
root&#9;&#9;&#9;&#9;&#9;&#9;46851&#9; 0.0&#9;0.0&#9;4283172&#9; 1120&#9; ??&#9;Ss&#9;&#9;8:49PM&#9; 0:00.09 /bin/bash /Users/ec2-user/aws-ec2-mac-amis/cloudconnect.bash
However, when I terminate the AWS instance, the process stays running and the bash script's trap is never attempted (at least according to the logs).
This could very well be an AWS specific issue, however, I wanted to check here and see if I was potentially missing something important.
Some things that do work:
I can sudo shutdown -r now inside of the host and it disjoins properly before the host shuts down.
I can sudo launchctl -w unload inside of the host and it disjoins properly, too.