bogdanrada
6/7/2017 - 7:32 AM

to flatten dhrp/sshd : sudo python flatten.py 2bbfe079a94259b229ae66962d9d06b97fcdce7a5449775ef738bb619ff8ce73 it'll produce a 42422bbfe0

to flatten dhrp/sshd : sudo python flatten.py 2bbfe079a94259b229ae66962d9d06b97fcdce7a5449775ef738bb619ff8ce73

it'll produce a 42422bbfe079a94259b229ae66962d9d06b97fcdce7a5449775ef738bb619ff8ce73 image

import os
from StringIO import StringIO
import json
import sys
import subprocess
import shutil


def getParent(rootid, id):
    print "--> ", id
    with open("/var/lib/docker/graph/"+id+"/json") as data_file:
        data = json.load(data_file)
        if "parent" in data:
            parent = data["parent"]
            getParent(rootid, parent)
        print "[cp -r /var/lib/docker/graph/"+id+"/* /var/lib/docker/graph/4242"+rootid+"/]"
        os.system("cp -r /var/lib/docker/graph/"+id+"/* /var/lib/docker/graph/4242"+rootid+"/")


rootid = sys.argv[-1]
shutil.rmtree(path="/var/lib/docker/graph/4242"+rootid,ignore_errors=True)
print "[mkdir /var/lib/docker/graph/4242"+rootid+"]"
os.mkdir("/var/lib/docker/graph/4242"+rootid)
getParent(rootid, rootid)
data = []
with open("/var/lib/docker/graph/4242"+rootid+"/json", "r") as data_file:
    data = json.load(data_file)
data["id"] = "4242" +  data["id"]
del data["parent"]
del data["Size"]
with open("/var/lib/docker/graph/4242"+rootid+"/json", "w") as data_file:
    json.dump(data, data_file)
shutil.rmtree(path="/var/lib/docker/graph/4242"+rootid+"/layersize",ignore_errors=True)
print "==> 4242"+rootid