[lxc-devel] [lxd/master] lxc/list: add three new columns for "IMAGE ALIAS", "BASE IMAGE" (short and long hash)

TerraTech on Github lxc-bot at linuxcontainers.org
Sun Sep 23 01:37:26 UTC 2018


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 1062 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20180923/fd266c24/attachment.bin>
-------------- next part --------------
From c0c8e9e85f38c28e4d3583825dc8add6d2db8544 Mon Sep 17 00:00:00 2001
From: fqbuild <TerraTech at users.noreply.github.com>
Date: Sat, 22 Sep 2018 18:10:43 -0400
Subject: [PATCH] lxc/list: add three new columns for "IMAGE ALIAS", "BASE
 IMAGE" (short and long hash)

  $ lxc list -c nIfF
  +-------------+---------------+--------------+------------------------------------------------------------------+
  |    NAME     |  IMAGE ALIAS  |  BASE IMAGE  |                            BASE IMAGE                            |
  +-------------+---------------+--------------+------------------------------------------------------------------+
  | gentoo-lc   | gentoo-201809 | 6131070a4f44 | 6131070a4f44b4e48b5f34fc6093b9cb52c7d2c34fad85068a1f404322d02bc6 |
  +-------------+---------------+--------------+------------------------------------------------------------------+

  *NOTE: "IMAGE ALIAS" currently only does lookups on the "local" image server.

Signed-off-by: fqbuild <TerraTech at users.noreply.github.com>
---
 lxc/image.go     |  6 +++++-
 lxc/list.go      | 52 ++++++++++++++++++++++++++++++++++++++++++++++--
 lxc/list_test.go |  2 +-
 3 files changed, 56 insertions(+), 4 deletions(-)

diff --git a/lxc/image.go b/lxc/image.go
index 4d9272ffad..c7978e7c63 100644
--- a/lxc/image.go
+++ b/lxc/image.go
@@ -1005,7 +1005,7 @@ func (c *cmdImageList) uploadDateColumnData(image api.Image) string {
 	return image.UploadedAt.UTC().Format("Jan 2, 2006 at 3:04pm (MST)")
 }
 
-func (c *cmdImageList) shortestAlias(list []api.ImageAlias) string {
+func imageShortestAlias(list []api.ImageAlias) string {
 	shortest := ""
 	for _, l := range list {
 		if shortest == "" {
@@ -1020,6 +1020,10 @@ func (c *cmdImageList) shortestAlias(list []api.ImageAlias) string {
 	return shortest
 }
 
+func (c *cmdImageList) shortestAlias(list []api.ImageAlias) string {
+	return imageShortestAlias(list)
+}
+
 func (c *cmdImageList) findDescription(props map[string]string) string {
 	for k, v := range props {
 		if k == "description" {
diff --git a/lxc/list.go b/lxc/list.go
index 4c73756d13..3313f0617a 100644
--- a/lxc/list.go
+++ b/lxc/list.go
@@ -91,6 +91,9 @@ Pre-defined column shorthand chars:
   S - Number of snapshots
   t - Type (persistent or ephemeral)
   L - Location of the container (e.g. its cluster member)
+  I - Base Image Alias (shortest name, local images only)
+  f - Base Image Fingerprint (short)
+  F - Base Image Fingerprint (long)
 
 Custom columns are defined with "key[:name][:maxWidth]":
   KEY: The (extended) config key to display
@@ -101,8 +104,8 @@ Custom columns are defined with "key[:name][:maxWidth]":
   Defaults to -1 (unlimited). Use 0 to limit to the column header size.`))
 
 	cmd.Example = cli.FormatSection("", i18n.G(
-		`lxc list -c n,volatile.base_image:"BASE IMAGE":0,s46,volatile.eth0.hwaddr:MAC
-  Show containers using the "NAME", "BASE IMAGE", "STATE", "IPV4", "IPV6" and "MAC" columns.
+		`lxc list -c nIFs46,volatile.eth0.hwaddr:MAC
+  Show containers using the "NAME", "IMAGE ALIAS", "BASE IMAGE", "STATE", "IPV4", "IPV6" and "MAC" columns.
   "BASE IMAGE" and "MAC" are custom columns generated from container configuration keys.
 
 lxc list -c ns,user.comment:comment
@@ -118,6 +121,9 @@ lxc list -c ns,user.comment:comment
 
 const defaultColumns = "ns46tSL"
 
+// Cache the GetImageServer lookup
+var localServer lxd.ImageServer
+
 // This seems a little excessive.
 func (c *cmdList) dotPrefixMatch(short string, full string) bool {
 	fullMembs := strings.Split(full, ".")
@@ -464,6 +470,9 @@ func (c *cmdList) parseColumns(clustered bool) ([]column, bool, error) {
 		's': {i18n.G("STATE"), c.statusColumnData, false, false},
 		't': {i18n.G("TYPE"), c.typeColumnData, false, false},
 		'b': {i18n.G("STORAGE POOL"), c.StoragePoolColumnData, false, false},
+		'f': {i18n.G("BASE IMAGE"), c.baseImageColumnData, false, false},
+		'F': {i18n.G("BASE IMAGE"), c.baseImageFullColumnData, false, false},
+		'I': {i18n.G("IMAGE ALIAS"), c.baseImageAliasColumnData, false, false},
 	}
 
 	if c.flagFast {
@@ -570,6 +579,45 @@ func (c *cmdList) parseColumns(clustered bool) ([]column, bool, error) {
 	return columns, needsData, nil
 }
 
+func (c *cmdList) baseImageAliasColumnData(cInfo api.ContainerFull) string {
+	var err error
+
+	if localServer == nil {
+		localServer, err = c.global.conf.GetImageServer("local")
+		if err != nil {
+			return ""
+		}
+	}
+
+	info, _, err := localServer.GetImage(cInfo.Config["volatile.base_image"])
+	if err != nil {
+		return ""
+	}
+
+	return imageShortestAlias(info.Aliases)
+}
+
+func getBaseImage(cInfo api.ContainerFull, long bool) string {
+	v, ok := cInfo.Config["volatile.base_image"]
+	if !ok {
+		return "-Not Found-"
+	}
+
+	if !long && len(v) >= 12 {
+		v = v[:12]
+	}
+
+	return v
+}
+
+func (c *cmdList) baseImageColumnData(cInfo api.ContainerFull) string {
+	return getBaseImage(cInfo, false)
+}
+
+func (c *cmdList) baseImageFullColumnData(cInfo api.ContainerFull) string {
+	return getBaseImage(cInfo, true)
+}
+
 func (c *cmdList) nameColumnData(cInfo api.ContainerFull) string {
 	return cInfo.Name
 }
diff --git a/lxc/list_test.go b/lxc/list_test.go
index a7fe3857a2..5fee540483 100644
--- a/lxc/list_test.go
+++ b/lxc/list_test.go
@@ -52,7 +52,7 @@ func TestShouldShow(t *testing.T) {
 }
 
 // Used by TestColumns and TestInvalidColumns
-const shorthand = "46abcdlnNpPsStL"
+const shorthand = "46abcdfFIlnNpPsStL"
 const alphanum = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
 
 func TestColumns(t *testing.T) {


More information about the lxc-devel mailing list