-
Notifications
You must be signed in to change notification settings - Fork 38
/
pd.sh
210 lines (196 loc) · 6.39 KB
/
pd.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
#!/bin/bash
#
# KuberDock - is a platform that allows users to run applications using Docker
# container images and create SaaS / PaaS based on these applications.
# Copyright (C) 2017 Cloud Linux INC
#
# This file is part of KuberDock.
#
# KuberDock is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# KuberDock is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with KuberDock; if not, see <http://www.gnu.org/licenses/>.
#
#TMS=$(date +"%Y-%m-%d %H:%M:%S")
#echo "$TMS $1 $2 $3 $4 $5 $6 $7" >> /tmp/pd.log
if [ $1 == umount ]; then
ACTION=$1
UUID=$2
else
UUID=$1
ACTION=$2
fi
DEVICE=$3 # device name like my-name__SEP__user
NAME=$4
SIZE=$5
AWS_ACCESS_KEY_ID=$6
AWS_SECRET_ACCESS_KEY=$7
IID=$(curl -s --connect-timeout 1 http://169.254.169.254/latest/meta-data/instance-id)
AV_ZONE=""
REGION=""
FREE_CHAR=""
if [ -n "$IID" ];then
AV_ZONE=$(curl -s connect-timeout 1 http://169.254.169.254/latest/meta-data/placement/availability-zone)
REGION=$(echo $AV_ZONE|sed 's/\([0-9][0-9]*\)[a-z]*$/\1/')
fi
MP="/var/lib/kubelet/pods/$UUID/volumes/kubernetes.io~scriptable-disk/$NAME"
if [ $USER != root ];then
echo "Superuser privileges required"
exit 0
fi
function mkdir_if_missing {
if [ ! -d "$MP" ];then
mkdir -p "$MP"
fi
}
function get_next {
ARRAY=($(ls -1 /dev/xvd*|awk -F '' '/xvd/ {print $9}'|sort))
NEXT=$(($(printf "%d" "'${ARRAY[-1]}")+1))
if [ "$NEXT" -gt 122 ];then
exit 1
fi
FREE_CHAR=$(awk "BEGIN{printf \"%c\", $NEXT}")
}
function remove_volume {
AWS_ACCESS_KEY_ID=$2
AWS_SECRET_ACCESS_KEY=$3
VOLNAME=$4
export AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY
aws ec2 delete-volume --volume-id=$VOLNAME --region=$REGION
unset AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY
}
function create_map_and_mount {
mkdir_if_missing
if [ -z "$IID" ];then # not aws
SIZE_MB=$((1024*$SIZE))
rbd create $DEVICE --size=$SIZE_MB
DEVICE=$(rbd map $DEVICE)
mkfs.ext4 $DEVICE
mount -o context="\"system_u:object_r:cgroup_t:s0\"" "$DEVICE" "$MP"
else # aws
get_next
if [ -z "$FREE_CHAR" ];then
exit 1
fi
NEXT_DEV="xvd$FREE_CHAR"
if [ -n "$AWS_ACCESS_KEY_ID" ] && [ -n "AWS_SECRET_ACCESS_KEY" ];then
export AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY
VOL=$(aws ec2 create-volume --size=$SIZE --region=$REGION --availability-zone=$AV_ZONE | jq -r '.VolumeId')
aws ec2 create-tags --resources=$VOL --region=$REGION --tags Key=Name,Value=$DEVICE
aws ec2 attach-volume --volume-id=$VOL --instance-id=$IID --device=$NEXT_DEV --region=$REGION
while [ $? -ne 0 ];do
sleep 1
aws ec2 attach-volume --volume-id=$VOL --instance-id=$IID --device=$NEXT_DEV --region=$REGION
done
mkfs.ext4 "/dev/$NEXT_DEV"
while [ $? -ne 0 ];do
sleep 2
mkfs.ext4 "/dev/$NEXT_DEV"
done
mount -o context="\"system_u:object_r:cgroup_t:s0\"" "/dev/$NEXT_DEV" "$MP"
unset AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY
fi
fi
}
function map_and_mount {
if [ -z "$IID" ];then
rbd showmapped|awk "NR>1{if(\$3==\"$DEVICE\"){exit 1}}"
if [ $? -ne 0 ];then
exit 1
fi
mkdir_if_missing
DEVICE=$(rbd map $DEVICE)
mount -o context="\"system_u:object_r:cgroup_t:s0\"" "$DEVICE" "$MP"
else
RV=$(aws ec2 describe-volumes --region=$REGION --filters "Name=tag-key,Values=Name" "Name=tag-value,Values=$DEVICE")
RES=$(echo $RV | jq -r ".Volumes[0].State")
if [ $RES != available ];then
exit 1
fi
mkdir_if_missing
get_next
if [ -z "$FREE_CHAR" ];then
exit 1
fi
NEXT_DEV="xvd$FREE_CHAR"
VOL=$(echo $RV | jq -r ".Volumes[0].VolumeId")
aws ec2 attach-volume --volume-id=$VOL --instance-id=$IID --device=$NEXT_DEV --region=$REGION
while [ $? -ne 0 ];do
sleep 1
aws ec2 attach-volume --volume-id=$VOL --instance-id=$IID --device=$NEXT_DEV --region=$REGION
done
mount -o context="\"system_u:object_r:cgroup_t:s0\"" "/dev/$NEXT_DEV" "$MP"
while [ $? -ne 0 ];do
sleep 1
mount -o context="\"system_u:object_r:cgroup_t:s0\"" "/dev/$NEXT_DEV" "$MP"
done
fi
}
function lookup {
if [ -z "$IID" ];then
LIST=$(rbd ls)
if [ -z "$LIST" ];then
return 0
else
while read -r LINE; do
if [ $LINE == $DEVICE ];then
return 1
fi
done <<< "$LIST"
return 0
fi
else
RV=$(aws ec2 describe-volumes --region=$REGION --filters "Name=tag-key,Values=Name" "Name=tag-value,Values=$DEVICE")
RES=$(echo $RV | jq -r ".Volumes[0].VolumeId")
if [ $RES == null ];then
return 0
fi
return 1
fi
}
function make_unmount {
DRIVES=$(mount | awk "/$UUID/ {print \$1}")
if [ -z "$DRIVES" ];then
exit 0
fi
while read -r DRIVE;do
umount $DRIVE
if [ -z "$IID" ];then
rbd unmap $DRIVE
else
STRIPPED_DRIVE=$(echo $DRIVE | sed 's/\/dev\/\(.*\)$/\1/')
RV=$(aws ec2 describe-volumes --region=$REGION --filters "Name=attachment.device,Values=$STRIPPED_DRIVE")
VOL=$(echo $RV | jq -r ".Volumes[0].VolumeId")
aws ec2 detach-volume --region=$REGION --volume-id=$VOL
while [ $? -ne 0 ];do
sleep 1
aws ec2 detach-volume --region=$REGION --volume-id=$VOL
done
fi
done <<< "$DRIVES"
}
if [ $ACTION == "create" ];then
lookup
if [ $? -eq 0 ];then
create_map_and_mount
else
exit 1
fi
elif [ $ACTION == "mount" ];then
lookup
if [ $? -eq 0 ];then
exit 1
else
map_and_mount
fi
elif [ $ACTION == "umount" ];then
make_unmount
fi