Skip to content

Instantly share code, notes, and snippets.

Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cezarsa/d2c8b8db611af9a2d67d to your computer and use it in GitHub Desktop.
Save cezarsa/d2c8b8db611af9a2d67d to your computer and use it in GitHub Desktop.
Migrate to docker cluster in Tsuru 0.6.0
import redis
import os
redis_prefix = os.environ.get('REDIS_PREFIX', 'docker-cluster')
redis_host = os.environ.get('REDIS_HOST', 'localhost')
redis_port = int(os.environ.get('REDIS_PORT', '6379'))
mongo_uri = os.environ.get('MONGO_URI')
mongo_db = os.environ.get('MONGO_DB')
def redis_to_redis():
r = redis.Redis(host=redis_host, port=redis_port)
images = r.keys(redis_prefix + ":image:*")
for img_key in images:
if r.type(img_key) == "set":
hosts = r.lrange(img_key, 0, -1)
tmp_key = "tmp_" + img_key
for h in hosts:
r.sadd(tmp_key, h)
r.rename(tmp_key, img_key)
def redis_to_mongo():
import pymongo
client = pymongo.MongoClient(mongo_uri)
db = client[mongo_db]
r = redis.Redis(host=redis_host, port=redis_port)
keys = r.keys(redis_prefix + "*")
for key in keys:
parts = key.split(':')
if len(parts) == 1:
if len(parts) == 2:
if parts[1] == 'nodes':
to_mongo_nodes(db, r, key)
to_mongo_container(db, r, key)
elif parts[1] == 'image':
to_mongo_image(db, r, key)
def to_mongo_image(db, r, key):
image = "".join(key.split(":")[2:])
hosts = list(r.smembers(key))
db.images.update({"_id": image}, {"$addToSet": {"hosts": {"$each": hosts}}}, upsert=True)
def to_mongo_container(db, r, key):
cont = key.split(":")[-1]
host = r.get(key)
db.containers.update({"_id": cont}, {"$set": {"host": host}}, upsert=True)
def to_mongo_nodes(db, r, key):
nodes = r.smembers(key)
for node in nodes:
healing = r.get(redis_prefix + ":node:healing:" + node) == "1"
healing_is_fail = r.get(redis_prefix + ":node:healing:isfailure:" + node) == "1"
metadata = r.hgetall(redis_prefix + ":node:metadata:" + node)
db.nodes.update({"_id": node}, {"$set":{
"metadata": metadata,
"healing": {"locked": healing, "isfailure": healing_is_fail}
}}, upsert=True)
def main():
if mongo_uri:
if __name__ == '__main__':
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment