forked from apache/cloudstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvm_data.sh
More file actions
executable file
·147 lines (126 loc) · 4.8 KB
/
Copy pathvm_data.sh
File metadata and controls
executable file
·147 lines (126 loc) · 4.8 KB
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
#!/bin/bash
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
# $Id: vm_data.sh 9307 2010-06-08 00:43:08Z chiradeep $ $HeadURL: svn://svn.lab.vmops.com/repos/vmdev/java/scripts/vm/hypervisor/xenserver/patch/vm_data.sh $
# @VERSION@
usage() {
printf "Usage: %s: -r <domr-ip> -v <vm ip> -F <vm data folder> -f <vm data file> -d <data to put in file> \n" $(basename $0) >&2
exit 2
}
set -x
cert="/root/.ssh/id_rsa.cloud"
PORT=3922
create_htaccess() {
local domrIp=$1
local vmIp=$2
local folder=$3
local file=$4
local result=0
#rewrite rule in top level /latest folder to redirect
#to vm specific folder based on source ip
entry="RewriteRule ^$file$ ../$folder/%{REMOTE_ADDR}/$file [L,NC,QSA]"
htaccessFolder="/var/www/html/latest"
htaccessFile=$htaccessFolder/.htaccess
ssh -p $PORT -o StrictHostKeyChecking=no -i $cert root@$domrIp "mkdir -p $htaccessFolder; touch $htaccessFile; grep -F \"$entry\" $htaccessFile; if [ \$? -gt 0 ]; then echo -e \"$entry\" >> $htaccessFile; fi" >/dev/null
result=$?
if [ $result -eq 0 ]
then
#ensure that vm specific folder cannot be listed and that only
#the vm that owns the data can access the items in this directory
entry="Options -Indexes\\nOrder Deny,Allow\\nDeny from all\\nAllow from $vmIp"
htaccessFolder="/var/www/html/$folder/$vmIp"
htaccessFile=$htaccessFolder/.htaccess
ssh -p $PORT -o StrictHostKeyChecking=no -i $cert root@$domrIp "mkdir -p $htaccessFolder; echo -e \"$entry\" > $htaccessFile" >/dev/null
result=$?
fi
#support access by http://<dhcp server>/latest/<metadata key> (legacy, see above) also
# http://<dhcp server>/latest/meta-data/<metadata key> (correct)
if [ "$folder" == "metadata" ] || [ "$folder" == "meta-data" ]
then
entry="RewriteRule ^meta-data/(.+)$ ../$folder/%{REMOTE_ADDR}/\\\$1 [L,NC,QSA]"
htaccessFolder="/var/www/html/latest"
htaccessFile=$htaccessFolder/.htaccess
ssh -p $PORT -o StrictHostKeyChecking=no -i $cert root@$domrIp "grep -F \"$entry\" $htaccessFile; if [ \$? -gt 0 ]; then echo -e \"$entry\" >> $htaccessFile; fi" >/dev/null
entry="RewriteRule ^meta-data/$ ../$folder/%{REMOTE_ADDR}/meta-data [L,NC,QSA]"
ssh -p $PORT -o StrictHostKeyChecking=no -i $cert root@$domrIp "grep -F \"$entry\" $htaccessFile; if [ \$? -gt 0 ]; then echo -e \"$entry\" >> $htaccessFile; fi" >/dev/null
result=$?
fi
return $result
}
copy_vm_data_file() {
local domrIp=$1
local vmIp=$2
local folder=$3
local file=$4
local dataFile=$5
dest=/var/www/html/$folder/$vmIp/$file
metamanifest=/var/www/html/$folder/$vmIp/meta-data
scp -P $PORT -o StrictHostKeyChecking=no -i $cert $dataFile root@$domrIp:$dest >/dev/null
ssh -p $PORT -o StrictHostKeyChecking=no -i $cert root@$domrIp "chmod 644 $dest" > /dev/null
ssh -p $PORT -o StrictHostKeyChecking=no -i $cert root@$domrIp "touch $metamanifest; chmod 644 $metamanifest" > /dev/null
if [ "$folder" == "metadata" ] || [ "$folder" == "meta-data" ]
then
ssh -p $PORT -o StrictHostKeyChecking=no -i $cert root@$domrIp "sed -i '/$file/d' $metamanifest; echo $file >> $metamanifest" > /dev/null
fi
return $?
}
delete_vm_data_file() {
local domrIp=$1
local vmIp=$2
local folder=$3
local file=$4
vmDataFilePath="/var/www/html/$folder/$vmIp/$file"
ssh -p $PORT -o StrictHostKeyChecking=no -i $cert root@$domrIp "if [ -f $vmDataFilePath ]; then rm -rf $vmDataFilePath; fi" >/dev/null
return $?
}
domrIp=
vmIp=
folder=
file=
dataFile=
while getopts 'r:v:F:f:d:' OPTION
do
case $OPTION in
r) domrIp="$OPTARG"
;;
v) vmIp="$OPTARG"
;;
F) folder="$OPTARG"
;;
f) file="$OPTARG"
;;
d) dataFile="$OPTARG"
;;
?) usage
exit 1
;;
esac
done
[ "$domrIp" == "" ] || [ "$vmIp" == "" ] || [ "$folder" == "" ] || [ "$file" == "" ] && usage
[ "$folder" != "userdata" ] && [ "$folder" != "metadata" ] && usage
if [ "$dataFile" != "" ]
then
create_htaccess $domrIp $vmIp $folder $file
if [ $? -gt 0 ]
then
exit 1
fi
copy_vm_data_file $domrIp $vmIp $folder $file $dataFile
else
delete_vm_data_file $domrIp $vmIp $folder $file
fi
exit $?