Skip to content

Commit 9db03bf

Browse files
author
Marcus Sorensen
committed
Summary: Make vmdata_kvm.py concurrency friendly
Detail: Adds flocks, etc for calling this concurrently, since CLOUDSTACK-3484 made userdata, vmdata, start commands concurrent Signed-off-by: Marcus Sorensen <marcus@betterservers.com> 1374773177 -0600
1 parent 2b0a972 commit 9db03bf

1 file changed

Lines changed: 82 additions & 18 deletions

File tree

‎patches/systemvm/debian/config/opt/cloud/bin/vmdata_kvm.py‎

Lines changed: 82 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
# under the License.
1818

1919
import sys, getopt, json, os, base64
20+
from fcntl import flock, LOCK_EX, LOCK_UN
21+
2022

2123
def main(argv):
2224
fpath = ''
@@ -83,20 +85,37 @@ def createfile(ip, folder, file, data):
8385
if data is not None:
8486
data = base64.b64decode(data)
8587

88+
fh = open(dest, "w")
89+
exflock(fh)
8690
if data is not None:
87-
open(dest, "w").write(data)
91+
fh.write(data)
8892
else:
89-
open(dest, "w").write("")
93+
fh.write("")
94+
unflock(fh)
95+
fh.close()
9096
os.chmod(dest, 0644)
9197

9298
if folder == "metadata" or folder == "meta-data":
93-
if not os.path.exists(metamanifestdir):
99+
try:
94100
os.makedirs(metamanifestdir, 0755)
101+
except OSError as e:
102+
# error 17 is already exists, we do it this way for concurrency
103+
if e.errno != 17:
104+
print "failed to make directories " + metamanifestdir + " due to :" +e.strerror
105+
sys.exit(1)
95106
if os.path.exists(metamanifest):
96-
if not file in open(metamanifest).read():
97-
open(metamanifest, "a").write(file + '\n')
107+
fh = open(metamanifest, "r+a")
108+
exflock(fh)
109+
if not file in fh.read():
110+
fh.write(file + '\n')
111+
unflock(fh)
112+
fh.close()
98113
else:
99-
open(metamanifest, "w").write(file + '\n')
114+
fh = open(metamanifest, "w")
115+
exflock(fh)
116+
fh.write(file + '\n')
117+
unflock(fh)
118+
fh.close()
100119

101120
if os.path.exists(metamanifest):
102121
os.chmod(metamanifest, 0644)
@@ -106,35 +125,80 @@ def htaccess(ip, folder, file):
106125
htaccessFolder = "/var/www/html/latest"
107126
htaccessFile = htaccessFolder + "/.htaccess"
108127

109-
if not os.path.exists(htaccessFolder):
128+
try:
110129
os.mkdir(htaccessFolder,0755)
130+
except OSError as e:
131+
# error 17 is already exists, we do it this way for concurrency
132+
if e.errno != 17:
133+
print "failed to make directories " + htaccessFolder + " due to :" +e.strerror
134+
sys.exit(1)
111135

112136
if os.path.exists(htaccessFile):
113-
if not entry in open(htaccessFile).read():
114-
open(htaccessFile, "a").write(entry + '\n')
137+
fh = open(htaccessFile, "r+a")
138+
exflock(fh)
139+
if not entry in fh.read():
140+
fh.write(entry + '\n')
141+
unflock(fh)
142+
fh.close()
143+
else:
144+
fh = open(htaccessFile, "w")
145+
exflock(fh)
146+
fh.write("Options +FollowSymLinks\nRewriteEngine On\n\n")
147+
fh.write(entry + '\n')
148+
unflock(fh)
149+
fh.close()
115150

116151
entry="Options -Indexes\nOrder Deny,Allow\nDeny from all\nAllow from " + ip
117152
htaccessFolder = "/var/www/html/" + folder + "/" + ip
118153
htaccessFile = htaccessFolder+"/.htaccess"
119154

120-
if not os.path.exists(htaccessFolder):
155+
try:
121156
os.makedirs(htaccessFolder,0755)
122-
123-
open(htaccessFile, "w").write(entry + '\n')
157+
except OSError as e:
158+
# error 17 is already exists, we do it this way for sake of concurrency
159+
if e.errno != 17:
160+
print "failed to make directories " + htaccessFolder + " due to :" +e.strerror
161+
sys.exit(1)
162+
163+
fh = open(htaccessFile, "w")
164+
exflock(fh)
165+
fh.write(entry + '\n')
166+
unflock(fh)
167+
fh.close()
124168

125169
if folder == "metadata" or folder == "meta-data":
126-
entry="RewriteRule ^meta-data/(.+)$ ../" + folder + "/%{REMOTE_ADDR}/$1 [L,NC,QSA]"
170+
entry = "RewriteRule ^meta-data/(.+)$ ../" + folder + "/%{REMOTE_ADDR}/$1 [L,NC,QSA]"
127171
htaccessFolder = "/var/www/html/latest"
128172
htaccessFile = htaccessFolder + "/.htaccess"
129173

130-
if not entry in open(htaccessFile).read():
131-
open(htaccessFile, "a").write(entry + '\n')
174+
fh = open(htaccessFile, "r+a")
175+
exflock(fh)
176+
if not entry in fh.read():
177+
fh.write(entry + '\n')
132178

133-
entry="RewriteRule ^meta-data/$ ../" + folder + "/%{REMOTE_ADDR}/meta-data [L,NC,QSA]"
179+
entry = "RewriteRule ^meta-data/$ ../" + folder + "/%{REMOTE_ADDR}/meta-data [L,NC,QSA]"
134180

135-
if not entry in open(htaccessFile).read():
136-
open(htaccessFile, "a").write(entry + '\n')
181+
fh.seek(0)
182+
if not entry in fh.read():
183+
fh.write(entry + '\n')
184+
unflock(fh)
185+
fh.close()
137186

187+
def exflock(file):
188+
try:
189+
flock(file, LOCK_EX)
190+
except IOError as e:
191+
print "failed to lock file" + file.name + " due to : " + e.strerror
192+
sys.exit(1)
193+
return True
194+
195+
def unflock(file):
196+
try:
197+
flock(file, LOCK_UN)
198+
except IOError:
199+
print "failed to unlock file" + file.name + " due to : " + e.strerror
200+
sys.exit(1)
201+
return True
138202

139203
if __name__ == "__main__":
140204
main(sys.argv[1:])

0 commit comments

Comments
 (0)