forked from awslabs/amazon-ebs-autoscale
-
Notifications
You must be signed in to change notification settings - Fork 2
/
install.sh
204 lines (174 loc) · 5.94 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
#!/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)
-s, --initial-size SIZE
Initial size of the volume in GB. (Default: 200)
Only used if --initial-device is NOT specified.
EOF
)
MOUNTPOINT=/scratch
SIZE=200
VOLUMETYPE=gp3
DEVICE=""
FILE_SYSTEM=btrfs
BASEDIR=$(dirname $0)
. ${BASEDIR}/shared/utils.sh
initialize
# parse options
PARAMS=""
while (( "$#" )); do
case "$1" in
-s|--initial-size)
SIZE=$2
shift 2
;;
-t|--volume-type)
VOLUMETYPE=$2
shift 2
;;
-d|--initial-device)
DEVICE=$2
shift 2
;;
-f|--file-system)
FILE_SYSTEM=$2
shift 2
;;
-m|--mountpoint)
MOUNTPOINT=$2
shift 2
;;
-h|--help)
echo "$USAGE"
exit
;;
--) # end parsing
shift
break
;;
-*|--*=)
error "unsupported argument $1"
;;
*) # positional arguments
PARAMS="$PARAMS $1"
shift
;;
esac
done
eval set -- "$PARAMS"
# 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#%%FILESYSTEM%%#${FILE_SYSTEM}#" \
> /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}