Ejemplo n.º 1
0
def start_container(cont_name, e):
    if type(cont_name) is list:
        for n in cont_name:
            start_container(n, e)
        return

    if is_container_running(cont_name):
        return

    run("docker start " + cont_name + " > /dev/null")

    cmds = e.bashrc + e.start + ["sleep 1\n exit\n"]
    exec(cont_name, cmds)
Ejemplo n.º 2
0
def exists_image(img_name):
    status, rows = run("docker image ls \"" + img_name + "\"", read=True)

    if status != 0:
        error("Command error")

    return len(rows) >= 2
Ejemplo n.º 3
0
def is_container_running(cont_name):
    cmd = "docker inspect -f '{{.State.Running}}' " + cont_name
    status, rows = run(cmd, read=True)

    if status != 0 or not rows:
        error("Command error")

    return rows[0] == "true\n"
Ejemplo n.º 4
0
def get_container_ip(cont_name):
    if type(cont_name) is list:
        return [(x, get_container_ip(x)) for x in cont_name]

    cmd = "docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' " + cont_name
    status, rows = run(cmd, read=True)

    if status != 0 or not rows:
        error("Command error")

    return rows[0].strip()
Ejemplo n.º 5
0
def build_single(cont_name, e):
    createCmd = "docker run -i --name tmp"

    if e.volumes:
        createCmd += " -v " + " -v ".join(parse_volumes(e.volumes))

    if e.expose:
        createCmd += " --expose=" + " --expose=".join(e.expose)

    if e.map_ports:
        createCmd += " -p " + " -p".join(e.map_ports)

    createCmd += " " + e.base

    rm_container("tmp")

    cmds = e.bashrc + e.full_build
    run(createCmd, write=cmds)

    rm_container(cont_name)
    rename_container("tmp", cont_name)
Ejemplo n.º 6
0
def ls_commits():
    status, rows = run("docker image ls \"wcommit*\"", read=True)

    if status != 0:
        error("Command error")

    result = []
    for row in rows[1:]:
        cells1 = row.split()
        cells2 = cells1[1].split("__")
        result.append([cells1[0] + ":" + cells1[1]] + cells2)

    return result
Ejemplo n.º 7
0
    def get_containers_in_cluster(self, clustername, abortOnFail=False):
        _, rows = run("docker ps -a", read=True)
        r = re.compile("wclus__" + clustername + "__[a-zA-Z0-9]+__[0-9]+$")
        result = []

        for row in rows:
            m = r.search(row)
            if m:
                name = m.group(0)
                result.append(name)

        if abortOnFail and not result:
            error("Cluster not found")

        return result
Ejemplo n.º 8
0
def new(img_name, cont_name, e):
    if not exists_image(img_name):
        error("Image not found")

    createCmd = "docker run -i --name tmp"

    if e.volumes:
        createCmd += " -v " + " -v ".join(e.volumes)

    if e.expose:
        createCmd += " --expose=" + " --expose=".join(e.expose)

    if e.map_ports:
        createCmd += " -p " + " -p".join(e.map_ports)

    createCmd += " " + img_name

    rm_container("tmp")

    cmds = e.bashrc + e.new
    run(createCmd, write=cmds)

    rm_container(cont_name)
    rename_container("tmp", cont_name)
Ejemplo n.º 9
0
def ls_clusters():
    _, rows = run("docker ps -a -f \"name=wclus__*\"", read=True)
    r = re.compile(r'wclus__([a-zA-Z0-9]+)__([a-zA-Z0-9]+)__([0-9]+)')
    s = [r.search(row) for row in rows]
    res = {}

    for m in s:
        if m:
            cluster = m.group(1)
            env = m.group(2)

            if not cluster in res:
                res[cluster] = [1, env]
            else:
                res[cluster][0] += 1

    for cluster in res:
        print("{} [{}, {}]".format(cluster, *res[cluster]))
Ejemplo n.º 10
0
def open_and_init(cont_name, bash_init, tty=True):
    k = quote("".join(["source \"$HOME/.bashrc\"\n"] + bash_init))
    b = quote("bash --init-file <(echo " + k + ")")
    tty = "-it " if tty else "-i "
    c = "docker exec " + tty + cont_name + " bash -c " + b.replace("$", "\\$")
    run(c)
Ejemplo n.º 11
0
def build_with_commits(cont_name, img_name, e):

    # Retrieve existing commits
    commits = ls_commits()
    commits_map = {x[0] for x in commits}

    # Prefix for creating the container
    createCmd = "docker run -i --name tmp"

    if e.volumes:
        createCmd += " -v " + " -v ".join(parse_volumes(e.volumes))

    if e.expose:
        createCmd += " --expose=" + " --expose=".join(e.expose)

    if e.map_ports:
        createCmd += " -p " + " -p".join(e.map_ports)

    # Here
    restore_point = None

    for i in range(len(e.commits) - 1, -1, -1):
        name = e.commits[i].commit_name

        if name in commits_map:
            restore_point = i
            break

    if restore_point is None:
        yprint("Creating from the base image", e.base)
        createCmd += " " + e.base
        restore_point = 0

    else:
        for i in range(restore_point):
            yprint("Skipping migration", e.commits[i].name)

        yprint("Recovering snapshot", e.commits[restore_point].name, "...")
        createCmd += " " + e.commits[restore_point].commit_name
        restore_point += 1

    rm_container("tmp")
    run(createCmd, write=e.bashrc)
    start_container("tmp", e)

    for i in range(restore_point, len(e.commits)):
        c = e.commits[i]
        yprint("Applying migration", c.name, "...")
        exec("tmp", c.lines + ["exit\n"])

        print("Saving snapshot...")
        commit("tmp", c.commit_name)

    yprint("Creating final image:", img_name, "...")
    rm_container(cont_name)
    commit("tmp", img_name)
    rename_container("tmp", cont_name)

    yprint("Cleaning old snapshots ...")
    current_hashs = {x.hashstr for x in e.commits}
    olds = []

    for x in commits:
        commit_img = x[0]
        xhash = x[4]

        if commit_img.startswith(e.commit_prefix):
            if not xhash in current_hashs:
                olds.append(commit_img)

    rm_image(olds)
Ejemplo n.º 12
0
def rm_container(cont_name):
    if type(cont_name) is list:
        cont_name = " ".join(cont_name)

    stop(cont_name)
    run("docker rm " + cont_name + " 1> /dev/null 2> /dev/null")
Ejemplo n.º 13
0
def ls_cluster_nodes(clustername):
    run("docker ps -a -f \"name=wclus__" + clustername + "__*\"")
Ejemplo n.º 14
0
def commit(cont_name, img_name):
    rm_image(img_name)
    run("docker commit " + cont_name + " " + img_name)
Ejemplo n.º 15
0
def stop(cont_name):
    if type(cont_name) is list:
        cont_name = " ".join(cont_name)

    run("docker kill " + cont_name + " 1> /dev/null 2> /dev/null")
Ejemplo n.º 16
0
def rm_image(img_name):
    if type(img_name) is list:
        img_name = " ".join(img_name)

    run("docker rmi " + img_name + " 2> /dev/null")
Ejemplo n.º 17
0
def ls_containers():
    run("docker ps -a -f \"name=wcont__*\"")
Ejemplo n.º 18
0
def rename_container(oldname, newname):
    run("docker rename " + oldname + " " + newname)
Ejemplo n.º 19
0
def load_image(filepath):
    run(["zcat " + filepath, "docker load -q"])
Ejemplo n.º 20
0
def export_image(img_name, filepath):
    run("docker save {} | gzip > {}".format(img_name, filepath))
Ejemplo n.º 21
0
def ls_images():
    run("docker image ls \"wimg*\"")