From 808694d6b5c45d6c49fba61d13a8710386f79805 Mon Sep 17 00:00:00 2001 From: Anthony Lapenna Date: Fri, 24 Jun 2016 10:11:49 +1200 Subject: [PATCH] feat(global): hide containers with labels using -l flag (#19) --- README.md | 16 +++++ .../containers/containersController.js | 35 +++++++++-- dockerui.go | 63 +++++++++++++++---- 3 files changed, 95 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 829461600..a3c7b1dc8 100644 --- a/README.md +++ b/README.md @@ -44,3 +44,19 @@ UI For Docker listens on port 9000 by default. If you run UI For Docker inside a # Expose UI For Docker on 10.20.30.1:80 $ docker run -d -p 10.20.30.1:80:9000 --privileged -v /var/run/docker.sock:/var/run/docker.sock cloudinovasi/cloudinovasi-ui + +### Hide containers with specifc labels + +You can hide specific containers from the containers view by using the `-hide-label` or `-l` options and specifying a label. + +For example, take a container started with the label `owner=acme`: + +``` +$ docker run -d --label owner=acme nginx +``` + +You can hide it in the view by starting the ui with: + +``` +$ docker run -d -p 9000:9000 --privileged -v /var/run/docker.sock:/var/run/docker.sock cloudinovasi/cloudinovasi-ui -l owner=acme +``` diff --git a/app/components/containers/containersController.js b/app/components/containers/containersController.js index 0672cda7f..62ea46930 100644 --- a/app/components/containers/containersController.js +++ b/app/components/containers/containersController.js @@ -1,6 +1,6 @@ angular.module('containers', []) -.controller('ContainersController', ['$scope', 'Container', 'Settings', 'Messages', 'ViewSpinner', -function ($scope, Container, Settings, Messages, ViewSpinner) { +.controller('ContainersController', ['$scope', 'Container', 'Settings', 'Messages', 'ViewSpinner', 'Config', +function ($scope, Container, Settings, Messages, ViewSpinner, Config) { $scope.state = {}; $scope.state.displayAll = Settings.displayAll; @@ -18,9 +18,11 @@ function ($scope, Container, Settings, Messages, ViewSpinner) { ViewSpinner.spin(); $scope.state.selectedItemCount = 0; Container.query(data, function (d) { - $scope.containers = d.filter(function (container) { - return container.Image !== 'swarm'; - }).map(function (container) { + var containers = d; + if (hiddenLabels) { + containers = hideContainers(d); + } + $scope.containers = containers.map(function (container) { return new ContainerViewModel(container); }); ViewSpinner.stop(); @@ -134,5 +136,26 @@ function ($scope, Container, Settings, Messages, ViewSpinner) { batch($scope.containers, Container.remove, "Removed"); }; - update({all: Settings.displayAll ? 1 : 0}); + var hideContainers = function (containers) { + return containers.filter(function (container) { + var filterContainer = false; + hiddenLabels.forEach(function(label, index) { + if (_.has(container.Labels, label.name) && + container.Labels[label.name] === label.value) { + filterContainer = true; + console.log('Hide: ' + container.Names[0]); + + } + }); + if (!filterContainer) { + return container; + } + }); + }; + + var hiddenLabels; + Config.$promise.then(function (c) { + hiddenLabels = c.hiddenLabels; + update({all: Settings.displayAll ? 1 : 0}); + }); }]); diff --git a/dockerui.go b/dockerui.go index 6d51a16a0..51e9f5102 100644 --- a/dockerui.go +++ b/dockerui.go @@ -1,7 +1,6 @@ package main // import "github.com/cloudinovasi/ui-for-docker" import ( - "flag" "io" "log" "net" @@ -15,13 +14,15 @@ import ( "io/ioutil" "fmt" "github.com/gorilla/securecookie" + "gopkg.in/alecthomas/kingpin.v2" ) var ( - endpoint = flag.String("e", "/var/run/docker.sock", "Dockerd endpoint") - addr = flag.String("p", ":9000", "Address and port to serve UI For Docker") - assets = flag.String("a", ".", "Path to the assets") - swarm = flag.Bool("swarm", false, "Swarm mode") + endpoint = kingpin.Flag("endpoint", "Dockerd endpoint").Default("/var/run/docker.sock").Short('e').String() + addr = kingpin.Flag("bind", "Address and port to serve UI For Docker").Default(":9000").Short('p').String() + assets = kingpin.Flag("assets", "Path to the assets").Default(".").Short('a').String() + swarm = kingpin.Flag("swarm", "Swarm cluster support").Default("false").Short('s').Bool() + labels = LabelParser(kingpin.Flag("hide-label", "Hide containers with a specific label in the UI").Short('l')) authKey []byte authKeyFile = "authKey.dat" ) @@ -32,6 +33,40 @@ type UnixHandler struct { type Config struct { Swarm bool `json:"swarm"` + HiddenLabels Labels `json:"hiddenLabels"` +} + +type Label struct { + Name string `json:"name"` + Value string `json:"value"` +} + +type Labels []Label + +func (l *Labels) Set(value string) error { + parts := strings.SplitN(value, "=", 2) + if len(parts) != 2 { + return fmt.Errorf("expected HEADER=VALUE got '%s'", value) + } + label := new(Label) + label.Name = parts[0] + label.Value = parts[1] + *l = append(*l, *label) + return nil +} + +func (l *Labels) String() string { + return "" +} + +func (l *Labels) IsCumulative() bool { + return true +} + +func LabelParser(s kingpin.Settings) (target *[]Label) { + target = new([]Label) + s.SetValue((*Labels)(target)) + return } func (h *UnixHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { @@ -82,7 +117,7 @@ func createUnixHandler(e string) http.Handler { return &UnixHandler{e} } -func createHandler(dir string, e string, s bool) http.Handler { +func createHandler(dir string, e string, c Config) http.Handler { var ( mux = http.NewServeMux() fileHandler = http.FileServer(http.Dir(dir)) @@ -120,14 +155,10 @@ func createHandler(dir string, e string, s bool) http.Handler { csrf.Secure(false), ) - configuration := Config{ - Swarm: s, - } - mux.Handle("/dockerapi/", http.StripPrefix("/dockerapi", h)) mux.Handle("/", fileHandler) mux.HandleFunc("/config", func(w http.ResponseWriter, r *http.Request) { - configurationHandler(w, r, configuration) + configurationHandler(w, r, c) }) return CSRF(csrfWrapper(mux)) } @@ -140,9 +171,15 @@ func csrfWrapper(h http.Handler) http.Handler { } func main() { - flag.Parse() + kingpin.Version("1.0.3") + kingpin.Parse() - handler := createHandler(*assets, *endpoint, *swarm) + configuration := Config{ + Swarm: *swarm, + HiddenLabels: *labels, + } + + handler := createHandler(*assets, *endpoint, configuration) if err := http.ListenAndServe(*addr, handler); err != nil { log.Fatal(err) }