Last active
June 3, 2023 08:26
-
-
Save uds5501/df499fb528127beb889eb1236718f585 to your computer and use it in GitHub Desktop.
Script for docker compose and redis sentinel config generation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
REDIS_CONFIG_MASTER_NAME = "mymaster" | |
def generate_sentinels(num_pairs, num_sentinels): | |
docker_compose = "" | |
for i in range(1, num_sentinels + 1): | |
docker_compose += f" redis-sentinel-{i}:\n" | |
docker_compose += f" container_name: 'redis-sentinel-{i}'\n" | |
docker_compose += f" image: redis:6.0-alpine\n" | |
docker_compose += f" ports:\n" | |
docker_compose += f" - '{5000 + (i - 1)}:5000'\n" | |
docker_compose += f" command: redis-server /etc/sentinel/sentinel-{i}.conf --sentinel\n" | |
docker_compose += f" volumes:\n" | |
docker_compose += f" - ./config/sentinel:/etc/sentinel\n" | |
if i == 1: | |
docker_compose += f" depends_on:\n" | |
for j in range(1, num_pairs + 1): | |
docker_compose += f" - redis-master-{j}\n" | |
else: | |
docker_compose += f" depends_on:\n" | |
docker_compose += f" - redis-sentinel-1\n" | |
docker_compose += "\n" | |
return docker_compose | |
def generate_sentinel_confs(num_pairs, num_sentinels): | |
for i in range(1, num_sentinels + 1): | |
sentinel_conf = generate_sentinel_conf(num_pairs) | |
with open(f'config/sentinel/sentinel-{i}.conf', 'w') as file: | |
file.write(sentinel_conf) | |
def generate_sentinel_conf(num_pairs): | |
sentinel_conf = f"port 5000\n" | |
for i in range(1, num_pairs + 1): | |
sentinel_conf += f"sentinel monitor {REDIS_CONFIG_MASTER_NAME}-{i} redis-master-{i} 6379 2\n" | |
sentinel_conf += f"sentinel down-after-milliseconds {REDIS_CONFIG_MASTER_NAME}-{i} 5000\n" | |
sentinel_conf += f"sentinel failover-timeout {REDIS_CONFIG_MASTER_NAME}-{i} 60000\n" | |
sentinel_conf += f"sentinel parallel-syncs {REDIS_CONFIG_MASTER_NAME}-{i} 1\n\n" | |
return sentinel_conf | |
def generate_docker_compose(num_pairs, num_sentinels): | |
docker_compose = f"version: '3'\n\nservices:\n" | |
for i in range(1, num_pairs + 1): | |
docker_compose += f" redis-master-{i}:\n" | |
docker_compose += f" container_name: 'redis-master-{i}'\n" | |
docker_compose += f" image: redis:6.0-alpine\n" | |
docker_compose += f" command: redis-server\n" | |
docker_compose += f" ports:\n" | |
docker_compose += f" - '{(i - 1) * 2 + 6379}:6379'\n" | |
docker_compose += f" redis-slave-{i}:\n" | |
docker_compose += f" container_name: 'redis-slave-{i}'\n" | |
docker_compose += f" image: redis:6.0-alpine\n" | |
docker_compose += f" ports:\n" | |
docker_compose += f" - '{(i - 1) * 2 + 6380}:6379'\n" | |
docker_compose += f" command: redis-server --slaveof redis-master-{i} 6379\n" | |
docker_compose += f" depends_on:\n" | |
docker_compose += f" - redis-master-{i}\n\n" | |
docker_compose += generate_sentinels(num_pairs, num_sentinels) | |
return docker_compose | |
if __name__ == "__main__": | |
if len(sys.argv) != 3: | |
print("Usage: python docker-compose-gen.py <num_pairs> <num_sentinels>") | |
sys.exit(1) | |
num_pairs = int(sys.argv[1]) | |
num_sentinels = int(sys.argv[2]) | |
compose_content = generate_docker_compose(num_pairs, num_sentinels) | |
generate_sentinel_confs(num_pairs, num_sentinels) | |
with open('docker-compose.yml', 'w') as file: | |
file.write(compose_content) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment