forked from awslabs/amazon-ebs-autoscale
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.sh
279 lines (240 loc) · 8.4 KB
/
install.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#!/bin/sh
# Copyright Amazon.com, Inc. or its affiliates.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
# BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
# FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
# THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
# INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
# IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
set -e
set -x
USAGE=$(cat <<EOF
Install Amazon EBS Autoscale
$0 [options] [[-m] <mount-point>]
Options
-d, --initial-device DEVICE
Initial device to use for mountpoint - e.g. /dev/xvdba.
(Default: none - automatically create and attaches a volume)
If provided --initial-size is ignored.
-f, --file-system btrfs | lvm.ext4
Filesystem to use (default: btrfs).
Options are btrfs or lvm.ext4
-h, --help
Print help and exit.
-m, --mountpoint MOUNTPOINT
Mount point for autoscale volume (default: /scratch)
-t, --volume-type VOLUMETYPE
Volume type (default: gp3)
--volume-iops VOLUMEIOPS
Volume IOPS for gp3, io1, io2 (default: 3000)
--volume-throughput VOLUMETHOUGHPUT
Volume throughput for gp3 (default: 125)
--min-ebs-volume-size SIZE_GB
Mimimum size in GB of new volumes created by the instance.
(Default: 150)
--max-ebs-volume-size SIZE_GB
Maximum size in GB of new volumes created by the instance.
(Default: 1500)
--max-total-created-size SIZE_GB
Maximum total size in GB of all volumes created by the instance.
(Default: 8000)
--max-attached-volumes N
Maximum number of attached volumes. (Default: 16)
--initial-utilization-threshold N
Initial disk utilization treshold for scale-up. (Default: 50)
-s, --initial-size SIZE_GB
Initial size of the volume in GB. (Default: 200)
Only used if --initial-device is NOT specified.
-i, --imdsv2
Enable imdsv2 for instance metadata API requests.
EOF
)
MOUNTPOINT=/scratch
# defaults to set into ebs-autoscale.json
SIZE=200
VOLUMETYPE=gp3
VOLUMEIOPS=3000
VOLUMETHOUGHPUT=125
MIN_EBS_VOLUME_SIZE=150
MAX_EBS_VOLUME_SIZE=1500
MAX_LOGICAL_VOLUME_SIZE=8000
MAX_ATTACHED_VOLUMES=16
INITIAL_UTILIZATION_THRESHOLD=50
DEVICE=""
FILE_SYSTEM=btrfs
BASEDIR=$(dirname $0)
. ${BASEDIR}/shared/utils.sh
# parse options
PARAMS=""
while (( "$#" )); do
case "$1" in
-s|--initial-size)
SIZE=$2
shift 2
;;
-t|--volume-type)
VOLUMETYPE=$2
shift 2
;;
--volume-iops)
VOLUMEIOPS=$2
shift 2
;;
--volume-throughput)
VOLUMETHOUGHPUT=$2
shift 2
;;
--min-ebs-volume-size)
MIN_EBS_VOLUME_SIZE=$2
shift 2
;;
--max-ebs-volume-size)
MAX_EBS_VOLUME_SIZE=$2
shift 2
;;
--max-total-created-size)
MAX_LOGICAL_VOLUME_SIZE=$2
shift 2
;;
--max-attached-volumes)
MAX_ATTACHED_VOLUMES=$2
shift 2
;;
--initial-utilization-threshold)
INITIAL_UTILIZATION_THRESHOLD=$2
shift 2
;;
-d|--initial-device)
DEVICE=$2
shift 2
;;
-f|--file-system)
FILE_SYSTEM=$2
shift 2
;;
-m|--mountpoint)
MOUNTPOINT=$2
shift 2
;;
-i|--imdsv2)
IMDSV2="true"
shift 1
;;
-h|--help)
echo "$USAGE"
exit
;;
--) # end parsing
shift
break
;;
-*|--*=)
error "unsupported argument $1"
;;
*) # positional arguments
PARAMS="$PARAMS $1"
shift
;;
esac
done
eval set -- "$PARAMS"
initialize
# for backwards compatibility evaluate positional parameters like previous 2.0.x and 2.1.x releases
# this will be removed in the future
if [ ! -z "$PARAMS" ]; then
MOUNTPOINT=$1
if [ ! -z "$2" ]; then
DEVICE=$2
fi
fi
# Install executables
# make executables available on standard PATH
mkdir -p /usr/local/amazon-ebs-autoscale/{bin,shared}
cp ${BASEDIR}/bin/{create-ebs-volume,ebs-autoscale} /usr/local/amazon-ebs-autoscale/bin
chmod +x /usr/local/amazon-ebs-autoscale/bin/*
ln -sf /usr/local/amazon-ebs-autoscale/bin/* /usr/local/bin/
ln -sf /usr/local/amazon-ebs-autoscale/bin/* /usr/bin/
# copy shared assets
cp ${BASEDIR}/shared/utils.sh /usr/local/amazon-ebs-autoscale/shared
## Install configs
# install the logrotate config
cp ${BASEDIR}/config/ebs-autoscale.logrotate /etc/logrotate.d/ebs-autoscale
# install default config
cat ${BASEDIR}/config/ebs-autoscale.json | \
sed -e "s#%%MOUNTPOINT%%#${MOUNTPOINT}#" | \
sed -e "s#%%VOLUMETYPE%%#${VOLUMETYPE}#" | \
sed -e "s#%%VOLUMEIOPS%%#${VOLUMEIOPS}#" | \
sed -e "s#%%VOLUMETHOUGHPUT%%#${VOLUMETHOUGHPUT}#" | \
sed -e "s#%%FILESYSTEM%%#${FILE_SYSTEM}#" | \
sed -e "s#%%MINEBSVOLUMESIZE%%#${MIN_EBS_VOLUME_SIZE}#" | \
sed -e "s#%%MAXEBSVOLUMESIZE%%#${MAX_EBS_VOLUME_SIZE}#" | \
sed -e "s#%%MAXLOGICALVOLUMESIZE%%#${MAX_LOGICAL_VOLUME_SIZE}#" | \
sed -e "s#%%MAXATTACHEDVOLUMES%%#${MAX_ATTACHED_VOLUMES}#" | \
sed -e "s#%%INITIALUTILIZATIONTHRESHOLD%%#${INITIAL_UTILIZATION_THRESHOLD}#" \
> /etc/ebs-autoscale.json
## Create filesystem
if [ -e $MOUNTPOINT ] && ! [ -d $MOUNTPOINT ]; then
echo "ERROR: $MOUNTPOINT exists but is not a directory."
exit 1
elif ! [ -e $MOUNTPOINT ]; then
mkdir -p $MOUNTPOINT
fi
# If a device is not given, or if the device is not valid
if [ -z "${DEVICE}" ] || [ ! -b "${DEVICE}" ]; then
DEVICE=$(create-ebs-volume --size $SIZE --type $VOLUMETYPE)
fi
# create and mount the BTRFS filesystem
if [ "${FILE_SYSTEM}" = "btrfs" ]; then
mkfs.btrfs -f -d single $DEVICE
mount $DEVICE $MOUNTPOINT
# add entry to fstab
# allows non-root users to mount/unmount the filesystem
echo -e "${DEVICE}\t${MOUNTPOINT}\tbtrfs\tdefaults\t0\t0" | tee -a /etc/fstab
elif [ "${FILE_SYSTEM}" = "lvm.ext4" ]; then
VG=$(get_config_value .lvm.volume_group)
LV=$(get_config_value .lvm.logical_volume)
pvcreate $DEVICE
vgcreate $VG $DEVICE
lvcreate $VG -n $LV -l 100%VG
mkfs.ext4 /dev/mapper/${VG}-${LV}
mount /dev/mapper/${VG}-${LV} $MOUNTPOINT
echo -e "/dev/mapper/${VG}-${LV}\t${MOUNTPOINT}\text4\tdefaults\t0\t0" | tee -a /etc/fstab
else
echo "Unknown file system type: ${FILE_SYSTEM}"
exit 1
fi
chmod 1777 ${MOUNTPOINT}
## Install service
INIT_SYSTEM=$(detect_init_system 2>/dev/null)
case $INIT_SYSTEM in
upstart|systemd)
echo "$INIT_SYSTEM detected"
cd ${BASEDIR}/service/$INIT_SYSTEM
. ./install.sh
;;
*)
echo "Could not install EBS Autoscale - unsupported init system"
exit 1
esac
cd ${BASEDIR}