Commit 3b179302 authored by Jérome Perrin's avatar Jérome Perrin

recipe/redis: implement promise with redis-cli

This way we don't need to vendor python-redis (so we don't need to
update it for python3 support).

This also change the options in a backward incompatible way:
 - options are now kebab case ( server_bin -> server-bin )
 - a new cli-bin option holding the full path to redis-cli command
 is required to enable promise
parent c2530f4c
Pipeline #21318 passed with stage
This diff is collapsed.
......@@ -33,16 +33,17 @@ class Recipe(GenericBaseRecipe):
def install(self):
path_list = []
if not self.optionIsTrue('use_passwd', False):
if not self.optionIsTrue('use-passwd', False):
master_passwd = "# masterauth <master-password>"
else:
master_passwd = "masterauth %s" % self.options['passwd']
config_file = self.options['config_file'].strip()
configuration = dict(pid_file=self.options['pid_file'],
config_file = self.options['config-file'].strip()
configuration = dict(
pid_file=self.options['pid-file'],
port=self.options['port'],
ipv6=self.options['ipv6'],
server_dir=self.options['server_dir'],
log_file=self.options['log_file'],
server_dir=self.options['server-dir'],
log_file=self.options['log-file'],
master_passwd=master_passwd
)
if self.options.get('unixsocket'):
......@@ -58,28 +59,31 @@ class Recipe(GenericBaseRecipe):
redis = self.createWrapper(
self.options['wrapper'],
(self.options['server_bin'], config_file),
(self.options['server-bin'], config_file),
)
path_list.append(redis)
promise_script = self.options.get('promise_wrapper', '').strip()
promise_script = self.options.get('promise-wrapper', '').strip()
if promise_script:
promise = self.createPythonScript(
args = [
self.options['cli-bin'],
'-h',
self.options['ipv6'],
'-p',
self.options['port'],
]
if self.options.get('unixsocket'):
args.extend(('-s', self.options['unixsocket']))
args.extend((
'publish',
'Promise-Service',
'SlapOS Promise',
))
promise = self.createWrapper(
promise_script,
__name__ + '.promise',
(self.options['ipv6'], int(self.options['port']),
self.options.get('unixsocket'))
args,
)
path_list.append(promise)
return path_list
def promise(host, port, unixsocket):
from .MyRedis2410 import Redis
try:
r = Redis(host=host, port=port, unix_socket_path=unixsocket, db=0)
r.publish("Promise-Service","SlapOS Promise")
r.connection_pool.disconnect()
except Exception as e:
sys.exit(e)
import functools
import os
import shutil
import tempfile
import unittest
import zc.buildout.testing
class TestRedis(unittest.TestCase):
def getConfig(self):
return {
'config-file': self.getTempPath('redis.cfg'),
'pid-file': self.getTempPath('redis.pid'),
'port': 1234,
'ipv6': '::1',
'server-dir': self.getTempPath('srv'),
'log-file': self.getTempPath('redis.log'),
'wrapper': self.getTempPath('wrapper'),
'server-bin': '/path/to/bin/redis-server',
}
def setUp(self):
self.tmp_dir = tempfile.mkdtemp()
self.addCleanup(shutil.rmtree, self.tmp_dir)
self.getTempPath = functools.partial(os.path.join, self.tmp_dir)
self.buildout = buildout = zc.buildout.testing.Buildout()
self.config = self.getConfig()
buildout['redis'] = self.config
from slapos.recipe import redis
self.recipe = redis.Recipe(buildout, "redis", buildout['redis'])
def test_install(self):
self.installed = self.recipe.install()
redis_cfg = self.installed[0]
self.assertEqual(redis_cfg, self.config['config-file'])
with open(redis_cfg) as f:
self.assertIn(self.config['pid-file'], f.read())
wrapper = self.installed[1]
self.assertEqual(wrapper, self.getConfig()['wrapper'])
with open(wrapper) as f:
self.assertIn('/path/to/bin/redis-server', f.read())
class TestRedisWithUnixSocket(TestRedis):
def getConfig(self):
return dict(
super(TestRedisWithUnixSocket, self).getConfig(),
unixsocket=self.getTempPath('redis.sock'))
class TestRedisWithPassword(TestRedis):
def getConfig(self):
return dict(
super(TestRedisWithPassword, self).getConfig(), passwd='secret')
class TestRedisWithPromise(TestRedis):
def getConfig(self):
return dict(
super(TestRedisWithPromise, self).getConfig(), **{
'cli-bin': '/path/to/bin/redis-cli',
'promise-wrapper': self.getTempPath('promise-wrapper')
})
def test_install(self):
super(TestRedisWithPromise, self).test_install()
promise_wrapper = self.installed[2]
self.assertEqual(promise_wrapper, self.getConfig()['promise-wrapper'])
with open(promise_wrapper) as f:
self.assertIn('/path/to/bin/redis-cli', f.read())
class TestRedisWithUnixSocketAndPromise(TestRedisWithPromise):
def getConfig(self):
return dict(
super(TestRedisWithUnixSocketAndPromise, self).getConfig(),
unixsocket=self.getTempPath('redis.sock'))
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment