-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentrypoint.sh
51 lines (45 loc) · 1.69 KB
/
entrypoint.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/bin/sh
# Exit immediately if a command exits with a non-zero status
set -e
if [ -z "${CRON_SCHEDULE}" ] && [ -z "${CRONTAB}" ]; then
echo "Error: Either CRON_SCHEDULE or CRONTAB must be set" >&2
exit 1
fi
if [ -z "${PLAYBOOK}" ]; then
echo "Error: PLAYBOOK environment variable cannot be empty" >&2
exit 1
fi
# Default command
DEFAULT_CRON_COMMAND="cd /ansible && ansible-playbook ${HOSTS:+-i $(printf '%s' "$HOSTS" | sed -e 's/[[:space:]]//g' -e 's/[^,]$/&,/')} ${PLAYBOOK}"
# Use CRON_COMMAND if set, otherwise use default
CRON_COMMAND=${CRON_COMMAND:-$DEFAULT_CRON_COMMAND}
# Generate SSH private key
ssh_key_file=~/.ssh/id_ed25519
if [ "$(echo "${SKIP_SSH_KEYGEN}" | tr '[:upper:]' '[:lower:]')" != "true" ] && [ ! -f "${ssh_key_file}" ]; then
echo "Generating SSH key..."
ssh-keygen -t ed25519 -f $ssh_key_file -N "" -C "ansible-scheduled-key-$(date +%Y%m%d%H%M%S)"
echo "SSH key generated successfully."
echo "Public key: $(cat "${ssh_key_file}.pub")"
fi
# Start scheduler
if [ $# -eq 0 ]; then
if [ -z "${CRONTAB}" ]; then
# Create a new crontab file with the provided schedule
CRONTAB=/ansible/crontab.generated
echo "${CRON_SCHEDULE} ${CRON_COMMAND}" > ${CRONTAB}
echo "Created crontab file: ${CRONTAB}"
else
# Check if the crontab file exists
if [ ! -f "${CRONTAB}" ]; then
echo "Error: Crontab file not found at ${CRONTAB}" >&2
exit 1
fi
fi
# Start supercronic
echo "Starting supercronic with crontab file: ${CRONTAB}"
exec supercronic -no-reap -inotify -passthrough-logs "${CRONTAB}"
else
# Execute specified command
echo "Executing command: $*"
exec "$@"
fi