mongo_wrapper_ros.py
Go to the documentation of this file.
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('warehouse_ros')
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 - db_path: where the db is stored on the filesystem.  Defaults to /tmp/db.
00053 - database_path: same as db_path.  For backward compatibility.
00054 - overwrite: whether to overwrite existing database if it exists.  Defaults to false.
00055 """
00056 
00057 if __name__ == '__main__':
00058 
00059     if '--help' in sys.argv:
00060         print_help_message()
00061         sys.exit()
00062 
00063     rospy.init_node('mongodb')
00064     path_param = rospy.get_param('~database_path' , '/tmp/db')
00065     if path_param=='/tmp/db':
00066         path_param = rospy.get_param('~db_path' , '/tmp/db')
00067     dbpath = path.expanduser(path_param)
00068     overwrite = rospy.get_param('~overwrite', False)
00069 
00070     if '--repair' in sys.argv:
00071         rospy.loginfo("Repairing database")
00072         lock_file = '{0}/mongod.lock'.format(dbpath)
00073         if path.exists(lock_file):
00074             rospy.loginfo("  Removing lock file")
00075             os.remove(lock_file)
00076         sp.check_call(['mongodb', 'mongod', '--repair', '--dbpath', dbpath.format(dbpath)])
00077         rospy.loginfo("  Successfully repaired.")
00078         sys.exit(0)
00079 
00080     # The defaults here should match the ones used by each client library
00081     port = rospy.get_param('warehouse_port', 27017)
00082     host = rospy.get_param('warehouse_host', 'localhost')
00083 
00084     rospy.loginfo('Starting mongodb with db location {0} listening on {2}:{1}'.\
00085                   format(dbpath, port, host))
00086 
00087     if overwrite and path.exists(dbpath):
00088         shutil.rmtree(dbpath)
00089         rospy.loginfo('Removed existing db at %s', dbpath)
00090 
00091     if not path.exists(dbpath):
00092         rospy.loginfo('{0} did not exist; creating it.'.format(dbpath))
00093         os.makedirs(dbpath)
00094 
00095 
00096 
00097 
00098     # OK, now actually run mongod
00099     try:
00100         cmd = "mongod"
00101         sp.check_call("{2} --dbpath {0} --port {1}".\
00102                       format(dbpath, port, cmd).split())
00103     except sp.CalledProcessError as e:
00104         if e.returncode==12:
00105             rospy.loginfo("Ignoring mongod's nonstandard return code of 12")
00106         else:
00107             rospy.logerr("Mongo process exited with error code {0}".format(e.returncode))
00108     except OSError, e:
00109         rospy.logerr("Execution failed: %s", e)
00110   
00111 


warehouse_ros
Author(s): Bhaskara Marthi
autogenerated on Wed Aug 26 2015 16:44:56