$search
00001 #!/usr/bin/env python 00002 00003 # This is a ros node wrapper for the mongod database server that 00004 # sets various things using ROS parameters: 00005 # - Parent namespace 00006 # - warehouse_host: hostname used by db 00007 # - warehouse_port: port used by db 00008 # - Private namespace 00009 # - ~database location: where the db is stored. Defaults to /tmp/db. 00010 # - ~overwrite: whether to overwrite existing db. Defaults to false. 00011 00012 import roslib; roslib.load_manifest('mongodb') 00013 import rospy 00014 import subprocess as sp 00015 import sys 00016 import os 00017 from os import path 00018 import shutil 00019 00020 def is_oneiric(): 00021 if path.exists('/etc/issue'): 00022 rospy.logdebug('/etc/issue exists') 00023 with open('/etc/issue') as f: 00024 contents = f.readline() 00025 rospy.logdebug('contents are {0}'.format(contents)) 00026 return '11.10' in contents 00027 else: 00028 rospy.logdebug('/etc/issue does not exist') 00029 00030 00031 00032 def is_lucid_or_maverick(): 00033 if path.exists('/etc/issue'): 00034 rospy.logdebug('/etc/issue exists') 00035 with open('/etc/issue') as f: 00036 contents = f.readline() 00037 rospy.logdebug('contents are {0}'.format(contents)) 00038 return '10.04' in contents or '10.10' in contents 00039 else: 00040 rospy.logdebug('/etc/issue does not exist') 00041 00042 def print_help_message(): 00043 print """ 00044 Usage: rosrun mongodb wrapper.py. 00045 Start the mongodb database server. Configured using the following ROS parameters. 00046 00047 Parameters in parent namespace 00048 - warehouse_port: port used by db. Defaults to 27017. 00049 - warehouse_host: doesn't directly affect server, but used by clients to know where the db is. 00050 00051 Parameters in parent namespace 00052 - database_location: where the db is stored on the filesystem. Defaults to /tmp/db. 00053 - overwrite: whether to overwrite existing database if it exists. Defaults to false. 00054 """ 00055 00056 if __name__ == '__main__': 00057 00058 if '--help' in sys.argv: 00059 print_help_message() 00060 sys.exit() 00061 00062 rospy.init_node('mongodb') 00063 dbpath = path.expanduser(rospy.get_param('~database_path' , '/tmp/db')) 00064 overwrite = rospy.get_param('~overwrite', False) 00065 00066 if '--repair' in sys.argv: 00067 rospy.loginfo("Repairing database") 00068 lock_file = '{0}/mongod.lock'.format(dbpath) 00069 if path.exists(lock_file): 00070 rospy.loginfo(" Removing lock file") 00071 os.remove(lock_file) 00072 sp.check_call(['rosrun', 'mongodb', 'mongod', '--repair', '--dbpath', dbpath.format(dbpath)]) 00073 rospy.loginfo(" Successfully repaired.") 00074 sys.exit(0) 00075 00076 # The defaults here should match the ones used by each client library 00077 port = rospy.get_param('warehouse_port', 27017) 00078 host = rospy.get_param('warehouse_host', 'localhost') 00079 00080 rospy.loginfo('Starting mongodb with db location {0} listening on {2}:{1}'.\ 00081 format(dbpath, port, host)) 00082 00083 if overwrite and path.exists(dbpath): 00084 shutil.rmtree(dbpath) 00085 rospy.loginfo('Removed existing db at %s', dbpath) 00086 00087 if not path.exists(dbpath): 00088 rospy.loginfo('{0} did not exist; creating it.'.format(dbpath)) 00089 os.makedirs(dbpath) 00090 00091 00092 00093 00094 # OK, now actually run mongod 00095 try: 00096 #cmd = "mongod" if is_oneiric() else "rosrun mongodb mongod" 00097 cmd = "rosrun mongodb mongod" 00098 sp.check_call("{2} --dbpath {0} --port {1}".\ 00099 format(dbpath, port, cmd).split()) 00100 except sp.CalledProcessError as e: 00101 if e.returncode==12: 00102 rospy.loginfo("Ignoring mongod's nonstandard return code of 12") 00103 else: 00104 rospy.logerr("Mongo process exited with error code {0}".format(e.returncode)) 00105 except OSError, e: 00106 rospy.logerr("Execution failed: %s", e) 00107 00108