graph mainly rebuilt in javascript
This commit is contained in:
		@@ -9,8 +9,122 @@
 | 
			
		||||
// 45678901234567890123456789012345678901234567890123456789012345678901234567890
 | 
			
		||||
 | 
			
		||||
var socket = io.connect();
 | 
			
		||||
var container = [];
 | 
			
		||||
 | 
			
		||||
function DockerContainers() {
 | 
			
		||||
    var Status = Object.freeze({
 | 
			
		||||
        Error:      "red",
 | 
			
		||||
        Terminated: "yellow",
 | 
			
		||||
        Restarting: "lightblue",
 | 
			
		||||
        Paused:     "lightgrey",
 | 
			
		||||
        Running:    "lightgreen"
 | 
			
		||||
    });
 | 
			
		||||
    var containers = [];
 | 
			
		||||
    var nodes = [];
 | 
			
		||||
    this.graph = function() {
 | 
			
		||||
        var res = "";
 | 
			
		||||
        console.log("nodes["+nodes.length+"]=", nodes);
 | 
			
		||||
        for (name in nodes) {
 | 
			
		||||
            var n = nodes[name];
 | 
			
		||||
            var label = n.name+'\\n'+n.image;
 | 
			
		||||
            res += '"'+n.name+'"'
 | 
			
		||||
                +' [label="'+label
 | 
			
		||||
                +'",URL="details('+"'"+n.name+"'"
 | 
			
		||||
                +')",style=filled,fillcolor='+n.status+"];\n";
 | 
			
		||||
        }
 | 
			
		||||
        res += "{rank=same;\n";
 | 
			
		||||
        for (name in nodes) {
 | 
			
		||||
            var n = nodes[name];
 | 
			
		||||
            n.volumes.forEach(function(v) {
 | 
			
		||||
                res += '"'+v.id+'" [label="'+v.inside+'",shape=box];\n';
 | 
			
		||||
            });
 | 
			
		||||
        }
 | 
			
		||||
        res+="}\n";
 | 
			
		||||
        res += "{rank=same;\n";
 | 
			
		||||
        for (name in nodes) {
 | 
			
		||||
            var n = nodes[name];
 | 
			
		||||
            n.volumes.forEach(function(v) {
 | 
			
		||||
                if (v.host)
 | 
			
		||||
                    res += '"'+v.outside+'" [label="'+v.host+'",shape=box];\n';
 | 
			
		||||
            });
 | 
			
		||||
        }
 | 
			
		||||
        res+="}\n";
 | 
			
		||||
        for (name in nodes) {
 | 
			
		||||
            var n = nodes[name];
 | 
			
		||||
            n.volumes.forEach(function(v) {
 | 
			
		||||
                if (v.host)
 | 
			
		||||
                    res += '"'+v.id+'" -> "'+v.outside+'"\n';
 | 
			
		||||
            });
 | 
			
		||||
        }
 | 
			
		||||
        for (name in nodes) {
 | 
			
		||||
            var n = nodes[name];
 | 
			
		||||
            n.volumes.forEach(function(v) {
 | 
			
		||||
                res += '"'+n.name+'" -> "'+v.id+'"\n';
 | 
			
		||||
            });
 | 
			
		||||
        }
 | 
			
		||||
        return res;
 | 
			
		||||
    }
 | 
			
		||||
    function setup() {
 | 
			
		||||
        delete nodes; nodes=[];
 | 
			
		||||
        containers.forEach(function(c) {
 | 
			
		||||
            var name = c.Name.replace(/^\//, "");
 | 
			
		||||
            nodes[name] = {};
 | 
			
		||||
            console.log("container: "+name);
 | 
			
		||||
            nodes[name].name = name;
 | 
			
		||||
            nodes[name].image = c.Config.Image;
 | 
			
		||||
            nodes[name].ports = [];
 | 
			
		||||
            var ports = c.NetworkSettings.Ports || c.NetworkSettings.PortBindings;
 | 
			
		||||
            if (ports)
 | 
			
		||||
                for (var port in ports)
 | 
			
		||||
                    if (ports[port])
 | 
			
		||||
                        for (var expose in ports[port]) {
 | 
			
		||||
                            var ip = ports[port][expose].HostIp;
 | 
			
		||||
                            if (ip==""||ip=="127.0.0.1"||ip=="0.0.0.0") ip=null;
 | 
			
		||||
                            nodes[name].ports.push({
 | 
			
		||||
                                internal: port,
 | 
			
		||||
                                external: ports[port][expose].HostPort,
 | 
			
		||||
                                ip: ip
 | 
			
		||||
                            });
 | 
			
		||||
                        }
 | 
			
		||||
            if (c.State.Running) nodes[name].status = Status.Running;
 | 
			
		||||
            else if (c.State.Paused) nodes[name].status = Status.Paused;
 | 
			
		||||
            else if (c.State.Restarting) nodes[name].status = Status.Restarting;
 | 
			
		||||
            else if (c.State.ExitCode == 0) nodes[name].status = Status.Terminated;
 | 
			
		||||
            else nodes[name].status = Status.Error;
 | 
			
		||||
            nodes[name].volumes = [];
 | 
			
		||||
            var volumes = c.Volumes || c.Config.Volumes;
 | 
			
		||||
            nodes[name].volumes = [];
 | 
			
		||||
            if (volumes)
 | 
			
		||||
                for (var volume in volumes) {
 | 
			
		||||
                    var outside = (typeof volumes[volume]=="string")?volumes[volume]:null;
 | 
			
		||||
                    nodes[name].volumes.push({
 | 
			
		||||
                        id: volume+':'+(outside?outside:name),
 | 
			
		||||
                        inside: volume,
 | 
			
		||||
                        outside: outside,
 | 
			
		||||
                        host: outside && !outside.match(/^\/var\/lib\/docker/)
 | 
			
		||||
                            ? volumes[volume] : null
 | 
			
		||||
                    });
 | 
			
		||||
                }
 | 
			
		||||
            nodes[name].volumesfrom = c.VolumesFrom;
 | 
			
		||||
            nodes[name].links = [];
 | 
			
		||||
            if (c.HostConfig && c.HostConfig.Links)
 | 
			
		||||
                c.HostConfig.Links.forEach(function(l) {
 | 
			
		||||
                    nodes[name].links.push({
 | 
			
		||||
                        to:   l.replace(/^\/?([^:]*).*$/, "$1"),
 | 
			
		||||
                        link: l.replace(new RegExp("^.*:/?"+name+"/"), "")
 | 
			
		||||
                    });
 | 
			
		||||
                });
 | 
			
		||||
            console.log(nodes[name]);
 | 
			
		||||
        });
 | 
			
		||||
    }
 | 
			
		||||
    this.setContainers = function(c) {
 | 
			
		||||
        if (typeof c == "string") c = JSON.parse(c);
 | 
			
		||||
        if (typeof c != "object") throw "wrong format: "+(typeof c);
 | 
			
		||||
        containers = c;
 | 
			
		||||
        setup();
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
var dc = new DockerContainers();
 | 
			
		||||
 | 
			
		||||
/// Show error messsage
 | 
			
		||||
/** Fades in an error message and logs to console.
 | 
			
		||||
@@ -176,6 +290,7 @@ function rotateviz() {
 | 
			
		||||
function showviz(vizpath) {
 | 
			
		||||
    $("#imagetools").show();
 | 
			
		||||
    viz = vizpath;
 | 
			
		||||
    console.log("DRAW: "+viz);
 | 
			
		||||
    res = "digraph {\n"+"  rankdir="+rankdir+";\n"+viz+"\n}";
 | 
			
		||||
    try {
 | 
			
		||||
        zoomlevel = 0;
 | 
			
		||||
@@ -246,7 +361,8 @@ function imgs() {
 | 
			
		||||
 | 
			
		||||
function containers(c) {
 | 
			
		||||
    console.log("->rcv containers");
 | 
			
		||||
    console.log(c);
 | 
			
		||||
    dc.setContainers(c);
 | 
			
		||||
    showviz(dc.graph());
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/// Initial Function: Startup
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user