-
Notifications
You must be signed in to change notification settings - Fork 4
/
emr-install_autoscale.sh
113 lines (90 loc) · 3.11 KB
/
emr-install_autoscale.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
107
108
109
110
111
112
#!/bin/bash
#==============================================================================
#!# emr-install_autoscale.sh - Install Trino Autoscale on an Amazon EMR cluster
#!#
#!# version 1.1
#!# author ripani
#!# license MIT license
#!#
#==============================================================================
#?#
#?# usage: ./emr-install_autoscale.sh <REGIONAL_S3_BUCKET>
#?# ./emr-install_autoscale.sh mybucketname
#?#
#?# REGIONAL_S3_BUCKET Amazon S3 bucket name eg: mybucketname
#?#
#==============================================================================
function usage() {
[ "$*" ] && echo "$0: $*"
sed -n '/^#?#/,/^$/s/^#?# \{0,1\}//p' "$0"
exit 1
}
# make sure to run as root
if [ $(id -u) != "0" ]; then
sudo "$0" "$@"
exit $?
fi
[[ $# -ne 1 ]] && echo "error: missing parameters" && usage
S3_BUCKET="$1"
S3_PREFIX="artifacts/aws-emr-trino-autoscaling"
INSTALL_PATH="/opt/trino-autoscale"
# create folder
mkdir -p $INSTALL_PATH
# download packages
aws s3 cp "s3://$S3_BUCKET/$S3_PREFIX/trino-autoscale.jar" "$INSTALL_PATH/trino-autoscale.jar"
# systemd service
cat << EOF > /etc/systemd/system/trino-autoscale.service
[Unit]
Description=Trino-Autoscale
[Service]
Environment=JAVA_HOME=/etc/alternatives/jre
Type=forking
ExecStart=/usr/bin/trino-autoscale
Restart=always
RestartSec=5
PIDFile=/var/run/trino/trino-autoscale.pid
User=root
WorkingDirectory=$INSTALL_PATH
[Install]
WantedBy=multi-user.target
EOF
# create startup script
cat << 'EOF' > /usr/bin/trino-autoscale
#!/bin/bash -l
#
# Copyright 2008-2009 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
PID_FILE=/var/run/trino/trino-autoscale.pid
TRINO_AUTOSCALE_CP="/opt/trino-autoscale/trino-autoscale.jar"
TRINO_JAVA=$JAVA_HOME
if [ -d "/usr/lib/jvm/java-7-oracle" ]; then
TRINO_JAVA=/usr/lib/jvm/java-7-oracle
elif [ -h "/usr/java/latest" ]; then
TRINO_JAVA=/usr/java/latest
fi
export LANG=en_US.UTF-8
$TRINO_JAVA/bin/java -Xmx1024m -XX:+ExitOnOutOfMemoryError -XX:MinHeapFreeRatio=10 -server -cp $TRINO_AUTOSCALE_CP com.amazonaws.emr.TrinoAutoscaler 2>> /var/log/trino/trino-autoscale.out 2>&1 &
JAVA_PID=$!
echo $JAVA_PID > $PID_FILE
# wait for autoscale to start up, grab the lock file and write itself to it
echo "$(date '+%Y-%m-%d %H:%M:%S') Trino Autoscale Process started with pid $JAVA_PID. Now waiting for process PID file"
for I in {1..10} ; do
PID_IN_FILE=$(cat $PID_FILE 2>/dev/null)
if [ "$PID_IN_FILE" != "$JAVA_PID" ]; then
echo "$(date '+%Y-%m-%d %H:%M:%S') Trino Autoscale PID_FILE $PID_FILE doesn't exist or doesn't contain $JAVA_PID (contains '$PID_IN_FILE')... sleeping to give process a chance to start"
sleep 5
else
echo "$(date '+%Y-%m-%d %H:%M:%S') Trino Autoscale Process Started"
exit 0
fi
done
echo "$(date '+%Y-%m-%d %H:%M:%S') Trino Autoscale PID_FILE contains $PID_IN_FILE != $JAVA_PID. Start failed."
exit 1
EOF
# Fix permissions
chmod +x /usr/bin/trino-autoscale
chmod oug+wrx /emr/metricscollector/isbusy
# Start service
systemctl daemon-reload
systemctl enable trino-autoscale.service
systemctl start trino-autoscale.service