5 import os, sys, subprocess, argparse, time, psutil, signal, socket
6 from subprocess import call, check_call
9 PIDFILE_PATH="/var/run/archipelago"
11 DEVICE_PREFIX="/dev/xsegbd"
12 XSEGBD_SYSFS="/sys/bus/xsegbd/"
14 CHARDEV_NAME="/dev/segdev"
18 SPEC="segdev:xsegbd:512:1024:12"
56 execfile(os.path.expanduser("/etc/default/archipelago"), globals())
58 print "Cannot open defaults"
61 HOSTNAME=socket.gethostname()
63 if BLOCKER == "pfiled":
64 peer_blockerb = [BLOCKER,
65 ["-p" , str(BPORT), "-g", str(SPEC), "-n", str(NR_OPS_BLOCKERB),
66 str(PITHOS), str(FILED_IMAGES), "-d",
67 "-f", os.path.join(PIDFILE_PATH, "blockerb.pid")],
69 peer_blockerm = [BLOCKER,
70 ["-p" , str(MBPORT), "-g", str(SPEC), "-n", str(NR_OPS_BLOCKERM),
71 str(PITHOSMAPS), str(FILED_MAPS), "-d",
72 "-f", os.path.join(PIDFILE_PATH, "blockerm.pid")],
75 peer_blockerb = [BLOCKER,
76 ["-p" , str(BPORT), "-g", str(SPEC), "-n", str(NR_OPS_BLOCKERB),
77 "--pool", str(RADOS_POOL_BLOCKS), "-v", str(VERBOSITY_BLOCKERB),
78 "-d", "--pidfile", os.path.join(PIDFILE_PATH, "blockerb.pid"),
79 "-l", os.path.join(str(LOGS_PATH), "blockerb-"+HOSTNAME+".log"),
82 peer_blockerm = [BLOCKER,
83 ["-p" , str(MBPORT), "-g", str(SPEC), "-n", str(NR_OPS_BLOCKERM),
84 "--pool", str(RADOS_POOL_MAPS), "-v", str(VERBOSITY_BLOCKERM),
85 "-d", "--pidfile", os.path.join(PIDFILE_PATH, "blockerm.pid"),
86 "-l", os.path.join(str(LOGS_PATH), "blockerm-"+HOSTNAME+".log"),
91 ["-t" , "1", "-sp", str(VPORT_START), "-ep", str(VPORT_END),
92 "-g", str(SPEC), "-n", str(NR_OPS_VLMC), "-bp", str(BPORT),
93 "-mp", str(MPORT), "-d", "-v", str(VERBOSITY_VLMC),
94 "--pidfile", os.path.join(PIDFILE_PATH, "vlmcd.pid"),
95 "-l", os.path.join(str(LOGS_PATH), "vlmc-"+HOSTNAME+".log")
97 peer_mapperd = [MAPPER,
98 ["-t" , "1", "-p", str(MPORT), "-mbp", str(MBPORT),
99 "-g", str(SPEC), "-n", str(NR_OPS_MAPPER), "-bp", str(BPORT), "-d",
100 "--pidfile", os.path.join(PIDFILE_PATH, "mapperd.pid"),
101 "-v", str(VERBOSITY_MAPPER),
102 "-l", os.path.join(str(LOGS_PATH), "mapperd-"+HOSTNAME+".log")
105 peers = [peer_blockerb, peer_blockerm, peer_vlmcd, peer_mapperd]
106 modules = ["xseg", "segdev", "xseg_posix", "xseg_pthread", "xseg_segdev"]
110 def isExec(file_path):
111 return os.path.isfile(file_path) and os.access(file_path, os.X_OK)
113 def validExec(program):
114 for path in os.environ["PATH"].split(os.pathsep):
115 exe_file = os.path.join(path, program)
121 def validPort(port, limit, name):
123 if int(port) >= limit:
124 print str(port) + " >= " + limit
127 print "Invalid port "+name+" : " + str(port)
133 for p in [BLOCKER, MAPPER, VLMC]:
135 print p + "is not a valid executable"
139 print "LOGS_PATH is not set"
142 print "PIDFILE_PATH is not set"
146 if not os.path.isdir(str(LOGS_PATH)):
147 print "LOGS_PATH "+str(LOGS_PATH)+" does not exist"
150 print "LOGS_PATH doesn't exist or is not set"
154 os.makedirs(str(PIDFILE_PATH))
156 if e.errno == errno.EEXIST:
157 if os.path.isdir(str(PIDFILE_PATH)):
160 print str(PIDFILE_PATH) + " is not a directory"
163 print "Cannot create " + str(PIDFILE_PATH)
166 print "PIDFILE_PATH or is not set"
169 splitted_spec = str(SPEC).split(':')
170 if len(splitted_spec) < 5:
174 xseg_type=splitted_spec[0]
175 xseg_name=splitted_spec[1]
176 xseg_ports=int(splitted_spec[2])
177 xseg_heapsize=int(splitted_spec[3])
178 xseg_align=int(splitted_spec[4])
180 if xseg_type != "segdev":
181 print "Segment type not segdev"
183 if xseg_name != "xsegbd":
184 print "Segment name not equal xsegbd"
187 print "Wrong alignemt"
190 for v in [VERBOSITY_BLOCKERB, VERBOSITY_BLOCKERM, VERBOSITY_MAPPER,
193 print "Verbosity missing"
195 if (int(v) > 3 or int(v) < 0):
196 print "Invalid verboisity " + str(v)
199 print "Invalid verboisity " + str(v)
202 for n in [NR_OPS_BLOCKERB, NR_OPS_BLOCKERM, NR_OPS_VLMC, NR_OPS_MAPPER]:
204 print "Nr ops missing"
207 print "Invalid nr_ops " + str(n)
210 print "Invalid nr_ops " + str(n)
213 if not validPort(VTOOL, xseg_ports, "VTOOL"):
215 if not validPort(MPORT, xseg_ports, "MPORT"):
217 if not validPort(BPORT, xseg_ports, "BPORT"):
219 if not validPort(MBPORT, xseg_ports, "MBPORT"):
221 if not validPort(VPORT_START, xseg_ports, "VPORT_START"):
223 if not validPort(VPORT_END, xseg_ports, "VPORT_END"):
226 if STORAGE == "File":
227 if FILED_IMAGES and not os.path.isdir(str(FILED_IMAGES)):
228 print "FILED_IMAGES invalid"
230 if FILED_MAPS and not os.path.isdir(str(FILED_MAPS)):
231 print "FILED_PATH invalid"
233 if PITHOS and not os.path.isdir(str(PITHOS)):
234 print "PITHOS invalid "
236 if PITHOSMAPS and not os.path.isdir(str(PITHOSMAPS)):
237 print "PITHOSMAPS invalid"
247 def exclusive_args(args):
248 file = "/tmp/vlmc_map.lock"
251 fd = os.open(file, os.O_CREAT|os.O_EXCL|os.O_WRONLY)
253 except Exception, reason:
254 print >> sys.stderr, reason
263 return exclusive_args
266 def vlmc_showmapped(args):
268 devices = os.listdir(os.path.join(XSEGBD_SYSFS, "devices/"))
274 print "id\tpool\timage\tsnap\tdevice"
277 d_id = open(XSEGBD_SYSFS + "devices/" + f + "/id").read().strip()
278 target = open(XSEGBD_SYSFS + "devices/"+ f + "/target").read().strip()
280 print "%s\t%s\t%s\t%s\t%s" % (d_id, '-', target, '-', DEVICE_PREFIX +
282 except Exception, reason:
283 print >> sys.stderr, reason
290 execfile(os.path.expanduser("/etc/default/archipelago"), globals())
292 execfile(rc, globals())
294 print "Cannot read config file"
300 def loaded_modules():
301 lines = open("/proc/modules").read().split("\n")
302 modules = [f.split(" ")[0] for f in lines]
305 def loaded_module(name):
306 return name in loaded_modules()
308 def load_module(name):
309 modules = loaded_modules()
312 cmd = ["modprobe -v %s" % name]
315 check_call(cmd, shell=True);
317 sys.stderr.write("Module %s failed to load. \n" % name)
321 def unload_module(name):
322 modules = loaded_modules()
323 if name not in modules:
325 cmd = ["modprobe -rv %s" % name]
328 check_call(cmd, shell=True);
330 sys.stderr.write("Module %s failed to unload. \n" % name)
334 def create_segment():
336 cmd = ["xseg", str(SPEC), "create"]
338 check_call(cmd, shell=False);
340 sys.stderr.write("Cannot create segment. \n")
344 def destroy_segment():
346 cmd = ["xseg", str(SPEC), "destroy"]
348 check_call(cmd, shell=False);
350 sys.stderr.write("Cannot destroy segment. \n")
354 def check_running(name, pid = -1):
355 for p in psutil.process_iter():
364 def check_pidfile(name):
365 pidfile = os.path.join(PIDFILE_PATH, name + ".pid")
368 pf = open(pidfile, "r")
378 def start_peer(peer):
379 cmd = [peer[0]] + peer[1]
381 check_call(cmd, shell=False);
383 sys.stderr.write("Peer %s start failed.\n" % peer[0])
388 pid = check_pidfile(peer[2])
390 print " process not running"
393 os.kill(pid, signal.SIGTERM)
395 while check_running(peer[0], pid) > 0:
399 print "process did not die in 10 secs"
403 def peer_running(peer):
404 pid = check_pidfile(peer[2])
408 r = check_running(peer[0], pid)
410 print "Peer " + peer[2] + " has valid pidfile but does not seem to be active"
416 os.stat(str(CHARDEV_NAME))
420 cmd = ["mknod", str(CHARDEV_NAME), "c", str(CHARDEV_MAJOR), str(CHARDEV_MINOR)]
423 check_call(cmd, shell=False);
425 sys.stderr.write("Segdev device creation failed. \n")
431 os.stat(str(CHARDEV_NAME))
435 os.unlink(str(CHARDEV_NAME))
437 sys.stderr.write("Segdev device removal failed. \n")
446 if load_module(m) < 0:
451 if make_segdev() < 0:
457 if create_segment() < 0:
464 if start_peer(p) < 0:
469 if load_module(xsegbd) < 0:
476 if vlmc_showmapped(args) > 0:
477 print "Cannot stop archipelago. Mapped volumes exist"
479 if unload_module(xsegbd):
482 for p in reversed(peers):
487 for m in reversed(modules):
492 if vlmc_showmapped(args) >= 0:
494 if loaded_module(xsegbd):
495 print "Xsegbd loaded"
498 print "Xsegbd not loaded"
499 for m in reversed(modules):
504 print m + " not loaded"
505 for p in reversed(peers):
506 if peer_running(p) < 0:
507 print p[0] + " not running"
509 print p[0] + " running"
517 if __name__ == "__main__":
518 # parse arguments and discpatch to the correct func
519 parser = argparse.ArgumentParser(description='vlmc tool')
520 parser.add_argument('-c', '--config', type=str, nargs='?', help='config file')
521 subparsers = parser.add_subparsers()
523 start_parser = subparsers.add_parser('start', help='Start archipelago')
524 start_parser.set_defaults(func=start)
526 stop_parser = subparsers.add_parser('stop', help='Stop archipelago')
527 stop_parser.set_defaults(func=stop)
529 status_parser = subparsers.add_parser('status', help='Archipelago status')
530 status_parser.set_defaults(func=status)
532 restart_parser = subparsers.add_parser('restart', help='Restart archipelago')
533 restart_parser.set_defaults(func=restart)
535 args = parser.parse_args()