mongo_wrapper_ros.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 from __future__ import print_function
4 import rospy
5 from subprocess import check_call, CalledProcessError
6 import sys
7 import os
8 import shutil
9 
10 
11 default_db_path = "/tmp/db"
12 
13 
15  print(
16  """
17 Usage: rosrun warehouse_ros_mongo mongo_wrapper_ros.py
18 Start the mongodb database server, configured using the following ROS parameters:
19 
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.
23 
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.
27 """.format(
28  default_db_path
29  )
30  )
31 
32 
33 if "--help" in sys.argv:
35  sys.exit()
36 
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)
41 else:
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)
45 
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")
51  os.remove(lock_file)
52  check_call(["mongodb", "mongod", "--repair", "--dbpath", dbpath])
53  rospy.loginfo(" Successfully repaired.")
54  sys.exit(0)
55 
56 # The defaults here should match the ones used by each client library
57 port = rospy.get_param("warehouse_port", 27017)
58 host = rospy.get_param("warehouse_host", "localhost")
59 
60 if overwrite and os.path.exists(dbpath):
61  rospy.loginfo("Removed existing db at %s", dbpath)
62  shutil.rmtree(dbpath)
63 
64 while not os.path.exists(dbpath):
65  rospy.loginfo("{0} does not exist; creating it.".format(dbpath))
66  try:
67  os.makedirs(dbpath)
68  break
69  except OSError as e:
70  if dbpath != default_db_path:
71  rospy.logerr("{0}. Trying {1} instead.".format(str(e), default_db_path))
72  dbpath = default_db_path
73  else:
74  rospy.logfatal(e)
75  raise e
76 
77 rospy.loginfo(
78  "Starting mongodb with db location {0} listening on {2}:{1}".format(
79  dbpath, port, host
80  )
81 )
82 
83 try:
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")
88  else:
89  rospy.logerr("Mongo process exited with error code {0}".format(e.returncode))
90 except OSError as e:
91  rospy.logerr("Execution failed: %s", e)


warehouse_ros_mongo
Author(s): Bhaskara Marthi , Connor Brew
autogenerated on Sat Apr 2 2022 02:29:13