-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.sh
executable file
·106 lines (84 loc) · 3.51 KB
/
run.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/env bash
set -euo pipefail
# This script should be run as root
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root or with sudo. (apt requires root privileges)"
exit 1
fi
# Function to handle errors
error_exit() {
local error_message="$1"
echo "$error_message"
notify_slack "$error_message" "danger"
exit 1
}
# Function to send a message to Slack
notify_slack() {
local message="$1"
local color="$2"
curl -X POST "$WEBHOOK_URL" \
-H 'Content-type: application/json' \
--data "{
\"attachments\": [
{
\"color\": \"$color\",
\"title\": \"Planetiler job\",
\"text\": \"$message\"
}
]
}" || true
}
# Global variables
AREA="${AREA:-planet}"
SOPS_AGE_KEY="${SOPS_AGE_KEY:-}"
S3_PATH="${S3_PATH:-ovh:kargo/data/MBTiles}"
FILENAME="${AREA}-$(date +%d-%m-%Y).mbtiles"
# Install rclone, curl, wget, and openjdk-21-jdk
apt-get update
apt-get install -y rclone curl wget openjdk-21-jdk
# Install sops if the decrypted files don't exist
if [[ ! -f "./rclone.dec.conf" || ! -f "./SLACK_WEBHOOK.dec.env" ]]; then
curl -LO https://github.com/getsops/sops/releases/download/v3.9.0/sops-v3.9.0.linux.amd64 && chmod +x sops-v3.9.0.linux.amd64 && mv sops-v3.9.0.linux.amd64 /usr/local/bin/sops
# If the SOPS_AGE_KEY is not set, ask for it
echo "SOPS_AGE_KEY: ${SOPS_AGE_KEY}"
if [[ -z "${SOPS_AGE_KEY}" ]]; then
echo "Please enter the SOPS key of a worker to decrypt the rclone configuration file"
echo "Your SOPS key should be in \"\$DEVELOPMENT_DIR/age/keys.txt\" on your local machine"
echo "It begins with 'AGE-SECRET-KEY-XXXXX...'"
read -s -p "Enter a compatible SOPS key: " SOPS_AGE_KEY
echo
fi
# Decrypt the rclone configuration file
sops --decrypt --output "./rclone.dec.conf" "./rclone.enc.conf"
sops --decrypt --output "./SLACK_WEBHOOK.dec.env" "./SLACK_WEBHOOK.enc.env"
fi
# Load the environment variables if WEBHOOK_URL is not set
WEBHOOK_URL="${WEBHOOK_URL:-}"
if [[ -z "${WEBHOOK_URL}" && -f "./SLACK_WEBHOOK.dec.env" ]]; then
source ./SLACK_WEBHOOK.dec.env
fi
# Notify Slack that the job has started
notify_slack "The Planetiler job has started for the region *${AREA}*." "good"
# Install Planetiler
wget https://github.com/onthegomap/planetiler/releases/latest/download/planetiler.jar -O planetiler.jar || error_exit "Error downloading Planetiler."
# Get the amount of RAM in GB
RAM_GB=$(free -g | awk '/^Mem:/{print $2}') || error_exit "Error retrieving RAM amount."
# Run Planetiler with the specified region
java -Xmx${RAM_GB}g -jar planetiler.jar --download --output=${FILENAME} --area=${AREA} --force || error_exit "Error executing Planetiler."
# Copy the MBTiles file to the S3 storage
rclone copy --progress --stats-one-line --stats=5s --config=./rclone.dec.conf ${FILENAME} ${S3_PATH} || error_exit "Error sending MBTiles file to object storage."
# Send a success notification
notify_slack "The Planetiler job has completed for the region *${AREA}*." "good"
#delete the mbtiles file and data folder after the job is done
#can be opt out by setting the KEEP_DATA env variable to true
KEEP_DATA="${KEEP_DATA:-false}"
if [[ "${KEEP_DATA}" == "false" ]]; then
rm -rf ${FILENAME}
rm -rf data
fi
# shutdown the instance after the job is done
# can be opt out by setting the SHUTDOWN env variable to false
SHUTDOWN="${SHUTDOWN:-true}"
if [[ "${SHUTDOWN}" == "true" ]]; then
shutdown -h now
fi