Created
October 25, 2018 14:37
-
-
Save zulzeen/d3a718f3a90e7a39abb6aebd8b04e6ad to your computer and use it in GitHub Desktop.
A simple containerized redis controlled by Python
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 argparse | |
import docker | |
def run_redis(client): | |
try: | |
container = client.containers.get('redis_cache') | |
except docker.errors.NotFound: | |
container = client.containers.run('redis', ports={'6379/tcp' : 6379}, detach = True, name='redis_cache') | |
else: | |
if container.status == 'exited': | |
container.start() | |
return container | |
def stop_redis(client): | |
container = client.containers.get('redis_cache') | |
container.stop() | |
return container | |
def delete_redis(client): | |
container = stop_redis(client) | |
container.remove() | |
return container | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser() | |
group = parser.add_mutually_exclusive_group(required=True) | |
group.add_argument('-r', '--run', help='Run cache', action='store_true') | |
group.add_argument('-s', '--stop', help='Stop cache', action='store_true') | |
group.add_argument('-d', '--delete', help='Delete cache', action='store_true') | |
client = docker.from_env() | |
args = vars(parser.parse_args()) | |
if args['run']: | |
run_redis(client) | |
elif args['stop']: | |
stop_redis(client) | |
elif args['delete']: | |
delete_redis(client) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment