24 import rocon_python_redis
as redis
27 sys.exit(
"\n[ERROR] No python-redis found - 'rosdep install rocon_hub'\n")
28 import rocon_semantic_version
as semantic_version
41 self.
_home_dir = os.path.join(rospkg.get_ros_home(),
'redis', self.
_parameters[
'name'].lower().replace(
" ",
"_"))
49 self.
_files[
'redis_server_log'] = os.path.join(self.
_home_dir,
'redis-server.log')
55 Sniff the version in major.minor format for decision making elsewhere (patch we disregard since our 56 decisions don't depend on it). 58 @return version extension in 'major.minor' format. 61 process = subprocess.Popen([
"redis-server",
"--version"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
62 output, unused_error = process.communicate()
64 version_string = re.search(
'v=([0-9.]+)', output).group(1)
65 except AttributeError:
66 version_string = re.search(
'version ([0-9.]+)', output).group(1)
67 version = semantic_version.Version(version_string)
68 rospy.loginfo(
"Hub : version %s" % (version_string))
69 return str(version.major) +
"." + str(version.minor)
73 Clear and configure redis conf, log files in the ros home 74 directories under a subdirectory styled by the name of this hub. 76 Also check that we have support for the redis server - i.e. check if we 77 have a .conf file for that version and exit this script if not found. 82 rospack = rospkg.RosPack()
83 package_redis_conf_file = os.path.join(rospack.get_path(
'rocon_hub'),
'redis',
'redis-%s.conf' % self.
_version_extension)
84 package_redis_conf_local_file = os.path.join(rospack.get_path(
'rocon_hub'),
'redis',
'redis-%s.conf.local' % self.
_version_extension)
87 if not os.path.isfile(package_redis_conf_file):
88 utils.logfatal(
"Hub : the version of the redis server you have installed is not supported by rocon.")
89 sys.exit(utils.logfatal(
"Hub : please submit a ticket at https://github.com/robotics-in-concert/rocon_multimaster"))
91 redis_conf_template = utils.read_template(package_redis_conf_file)
93 redis_local_template = utils.read_template(package_redis_conf_local_file)
97 self.
_files[
'redis_server_log'],
100 f = open(self.
_files[
'redis_conf'],
'w')
101 f.write(redis_conf_template.encode(
'utf-8'))
105 f = open(self.
_files[
'redis_conf_local'],
'w')
106 f.write(redis_local_template.encode(
'utf-8'))
112 Start the server. Also connect, delete all rocon:xxx 113 variables and reinitialise with specified values. 115 Aborts the program if the connection fails. 118 self.
_process = subprocess.Popen([
"redis-server", self.
_files[
'redis_conf']], preexec_fn=os.setpgrp)
119 pool = redis.ConnectionPool(host=
'localhost', port=int(self.
_parameters[
'port']), db=0)
122 while count < no_attempts:
124 self.
_server = redis.Redis(connection_pool=pool)
125 rocon_keys = self._server.keys(
"rocon:*")
126 pattern = re.compile(
"rocon:*")
128 for key
in rocon_keys:
129 if pattern.match(key):
130 keys_to_delete.append(key)
131 pipe = self._server.pipeline()
132 if len(keys_to_delete) != 0:
133 pipe.delete(*keys_to_delete)
134 pipe.set(
"rocon:hub:name", self.
_parameters[
'name'])
136 rospy.loginfo(
"Hub : reset hub variables on the redis server.")
138 except redis.ConnectionError:
140 if count == no_attempts:
142 sys.exit(utils.logfatal(
"Hub : could not connect to the redis server - is it running?"))
144 rospy.rostime.wallsleep(0.1)
148 Clears rocon: keys on the server. 151 rocon_keys = self._server.keys(
"rocon:*")
152 pattern = re.compile(
"rocon:*")
154 for key
in rocon_keys:
155 if pattern.match(key):
156 keys_to_delete.append(key)
157 pipe = self._server.pipeline()
158 if len(keys_to_delete) != 0:
159 pipe.delete(*keys_to_delete)
162 except redis.ConnectionError:
167 self._process.send_signal(signal.SIGINT)
180 Variable substitution in a template file. 182 @param local_conf_filename : where to find the local redis configuration file 185 return template % locals()
190 Variable substitution in a template file. 192 @param port : port on which the server will run 194 @param pid_file : pathname to where the pid file will be stored 196 @param max_memory: how much memory to allocate to the redis server in bytes 197 @type string (e.g. 10mb) 200 @param working_dir : filesystem which redis uses to dump (can we turn this off in 2.6?) 203 return template % locals()
205 if __name__ ==
"__main__":
206 pool = redis.ConnectionPool(host=
'localhost', port=
'6380', db=0)
209 except redis.exceptions.ConnectionError:
def instantiate_redis_conf_template(template, local_conf_filename)
def instantiate_local_conf_template(template, port, max_memory, logfile, working_dir)
def _introspect_redis_server_version(self)
def __init__(self, parameters)