3 from __future__
import print_function
5 from subprocess
import check_call, CalledProcessError
11 default_db_path =
"/tmp/db" 17 Usage: rosrun warehouse_ros_mongo mongo_wrapper_ros.py 18 Start the mongodb database server, configured using the following ROS parameters: 20 * parameters in parent namespace 21 - warehouse_port: port used by db. Defaults to 27017. 22 - warehouse_host: doesn't directly affect server, but used by clients to know where the db is. 24 * parameters in parent namespace 25 - db_path: where the db is stored on the filesystem. Defaults to {0}. 26 - overwrite: whether to overwrite existing database if it exists. Defaults to false. 33 if "--help" in sys.argv:
37 rospy.init_node(
"mongodb")
38 path_param = rospy.get_param(
"~database_path",
None)
39 if path_param
is None:
40 path_param = rospy.get_param(
"~db_path", default_db_path)
42 print(
'Parameter "database_path" is deprecated. Please use "db_path" instead.')
43 dbpath = os.path.expanduser(path_param)
44 overwrite = rospy.get_param(
"~overwrite",
False)
46 if "--repair" in sys.argv:
47 rospy.loginfo(
"Repairing database")
48 lock_file =
"{0}/mongod.lock".format(dbpath)
49 if os.path.exists(lock_file):
50 rospy.loginfo(
" Removing lock file")
52 check_call([
"mongodb",
"mongod",
"--repair",
"--dbpath", dbpath])
53 rospy.loginfo(
" Successfully repaired.")
57 port = rospy.get_param(
"warehouse_port", 27017)
58 host = rospy.get_param(
"warehouse_host",
"localhost")
60 if overwrite
and os.path.exists(dbpath):
61 rospy.loginfo(
"Removed existing db at %s", dbpath)
64 while not os.path.exists(dbpath):
65 rospy.loginfo(
"{0} does not exist; creating it.".format(dbpath))
70 if dbpath != default_db_path:
71 rospy.logerr(
"{0}. Trying {1} instead.".format(str(e), default_db_path))
72 dbpath = default_db_path
78 "Starting mongodb with db location {0} listening on {2}:{1}".format(
84 check_call(
"mongod --dbpath {} --port {}".format(dbpath, port).split())
85 except CalledProcessError
as e:
86 if e.returncode == 12:
87 rospy.loginfo(
"Ignoring mongod's non-standard return code of 12")
89 rospy.logerr(
"Mongo process exited with error code {0}".format(e.returncode))
91 rospy.logerr(
"Execution failed: %s", e)