[lxc-devel] [lxd/master] Implement "lxc config device get"

stgraber on Github lxc-bot at linuxcontainers.org
Wed Mar 23 21:02:45 UTC 2016


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 419 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20160323/19dd0de1/attachment.bin>
-------------- next part --------------
From 45cbd3936a46e9ef39a6dc8d555adc924126dd99 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Wed, 23 Mar 2016 17:02:06 -0400
Subject: [PATCH] Implement "lxc config device get"
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

And fix output of existing config get to be vaguely parsable.

Signed-off-by: Stéphane Graber <stgraber at ubuntu.com>
---
 lxc/config.go  | 71 +++++++++++++++++++++++++++++++++++++++++++++++-----------
 lxc/profile.go |  7 ++++--
 po/lxd.pot     | 66 ++++++++++++++++++++++++++---------------------------
 3 files changed, 96 insertions(+), 48 deletions(-)

diff --git a/lxc/config.go b/lxc/config.go
index 54e8452..4bf7cc5 100644
--- a/lxc/config.go
+++ b/lxc/config.go
@@ -59,19 +59,18 @@ func (c *configCmd) usage() string {
 		`Manage configuration.
 
 lxc config device add <[remote:]container> <name> <type> [key=value]...     Add a device to a container.
+lxc config device get <[remote:]container> <name> <key>                     Get a device property.
 lxc config device set <[remote:]container> <name> <key> <value>             Set a device property.
 lxc config device unset <[remote:]container> <name> <key>                   Unset a device property.
-lxc config device list [remote:]<container>                                 List devices for container.
-lxc config device show [remote:]<container>                                 Show full device details for container.
-lxc config device remove [remote:]<container> <name>                        Remove device from container.
-
-lxc config get [remote:]<container> key                                     Get configuration key.
-lxc config set [remote:]<container> key value                               Set container configuration key.
-lxc config unset [remote:]<container> key                                   Unset container configuration key.
-lxc config set key value                                                    Set server configuration key.
-lxc config unset key                                                        Unset server configuration key.
-lxc config show [--expanded] [remote:]<container>                           Show container configuration.
-lxc config edit [remote:][container]                                        Edit container configuration in external editor.
+lxc config device list <[remote:]container>                                 List devices for container.
+lxc config device show <[remote:]container>                                 Show full device details for container.
+lxc config device remove <[remote:]container> <name>                        Remove device from container.
+
+lxc config get [remote:][container] <key>                                   Get container or server configuration key.
+lxc config set [remote:][container] <key> <value>                           Set container or server configuration key.
+lxc config unset [remote:][container] <key>                                 Unset container or server configuration key.
+lxc config show [remote:][container] [--expanded]                           Show container or server configuration.
+lxc config edit [remote:][container]                                        Edit container or server configuration in external editor.
     Edit configuration, either by launching external editor or reading STDIN.
     Example: lxc config edit <container> # launch editor
              cat config.yml | lxc config edit <config> # read from config.yml
@@ -381,7 +380,7 @@ func (c *configCmd) run(config *lxd.Config, args []string) error {
 			if err != nil {
 				return err
 			}
-			fmt.Printf("%s: %s\n", key, resp.Config[key])
+			fmt.Println(resp.Config[key])
 		} else {
 			resp, err := d.ServerStatus()
 			if err != nil {
@@ -397,7 +396,7 @@ func (c *configCmd) run(config *lxd.Config, args []string) error {
 				value = "false"
 			}
 
-			fmt.Printf("%s: %s\n", key, value)
+			fmt.Println(value)
 		}
 		return nil
 
@@ -413,6 +412,8 @@ func (c *configCmd) run(config *lxd.Config, args []string) error {
 			return c.deviceAdd(config, "container", args)
 		case "remove":
 			return c.deviceRm(config, "container", args)
+		case "get":
+			return c.deviceGet(config, "container", args)
 		case "set":
 			return c.deviceSet(config, "container", args)
 		case "unset":
@@ -618,6 +619,50 @@ func (c *configCmd) deviceAdd(config *lxd.Config, which string, args []string) e
 	return err
 }
 
+func (c *configCmd) deviceGet(config *lxd.Config, which string, args []string) error {
+	if len(args) < 5 {
+		return errArgs
+	}
+
+	remote, name := config.ParseRemoteAndContainer(args[2])
+
+	client, err := lxd.NewClient(config, remote)
+	if err != nil {
+		return err
+	}
+
+	devname := args[3]
+	key := args[4]
+
+	if which == "profile" {
+		st, err := client.ProfileConfig(name)
+		if err != nil {
+			return err
+		}
+
+		dev, ok := st.Devices[devname]
+		if !ok {
+			return fmt.Errorf(i18n.G("The device doesn't exist"))
+		}
+
+		fmt.Println(dev[key])
+	} else {
+		st, err := client.ContainerInfo(name)
+		if err != nil {
+			return err
+		}
+
+		dev, ok := st.Devices[devname]
+		if !ok {
+			return fmt.Errorf(i18n.G("The device doesn't exist"))
+		}
+
+		fmt.Println(dev[key])
+	}
+
+	return nil
+}
+
 func (c *configCmd) deviceSet(config *lxd.Config, which string, args []string) error {
 	if len(args) < 6 {
 		return errArgs
diff --git a/lxc/profile.go b/lxc/profile.go
index 1b42ce2..a85c9c4 100644
--- a/lxc/profile.go
+++ b/lxc/profile.go
@@ -72,8 +72,9 @@ Devices:
 lxc profile device list <profile>                                   List devices in the given profile.
 lxc profile device show <profile>                                   Show full device details in the given profile.
 lxc profile device remove <profile> <name>                          Remove a device from a profile.
-lxc profile device set <[remote:]container> <name> <key> <value>    Set a device property.
-lxc profile device unset <[remote:]container> <name> <key>          Unset a device property.
+lxc profile device get <[remote:]profile> <name> <key>              Get a device property.
+lxc profile device set <[remote:]profile> <name> <key> <value>      Set a device property.
+lxc profile device unset <[remote:]profile> <name> <key>            Unset a device property.
 lxc profile device add <profile name> <device name> <device type> [key=value]...
     Add a profile device, such as a disk or a nic, to the containers
     using the specified profile.`)
@@ -278,6 +279,8 @@ func (c *profileCmd) doProfileDevice(config *lxd.Config, args []string) error {
 		return cfg.deviceList(config, "profile", args)
 	case "show":
 		return cfg.deviceShow(config, "profile", args)
+	case "get":
+		return cfg.deviceGet(config, "profile", args)
 	case "set":
 		return cfg.deviceSet(config, "profile", args)
 	case "unset":
diff --git a/po/lxd.pot b/po/lxd.pot
index d5a1545..5d98666 100644
--- a/po/lxd.pot
+++ b/po/lxd.pot
@@ -7,7 +7,7 @@
 msgid   ""
 msgstr  "Project-Id-Version: lxd\n"
         "Report-Msgid-Bugs-To: lxc-devel at lists.linuxcontainers.org\n"
-        "POT-Creation-Date: 2016-03-22 21:56-0400\n"
+        "POT-Creation-Date: 2016-03-23 17:02-0400\n"
         "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
         "Last-Translator: FULL NAME <EMAIL at ADDRESS>\n"
         "Language-Team: LANGUAGE <LL at li.org>\n"
@@ -74,7 +74,7 @@ msgstr  ""
 msgid   "'/' not allowed in snapshot name"
 msgstr  ""
 
-#: lxc/profile.go:225
+#: lxc/profile.go:226
 msgid   "(none)"
 msgstr  ""
 
@@ -121,7 +121,7 @@ msgstr  ""
 msgid   "Available commands:"
 msgstr  ""
 
-#: lxc/config.go:271
+#: lxc/config.go:270
 msgid   "COMMON NAME"
 msgstr  ""
 
@@ -129,17 +129,17 @@ msgstr  ""
 msgid   "CREATED AT"
 msgstr  ""
 
-#: lxc/config.go:115
+#: lxc/config.go:114
 #, c-format
 msgid   "Can't read from stdin: %s"
 msgstr  ""
 
-#: lxc/config.go:128 lxc/config.go:161 lxc/config.go:183
+#: lxc/config.go:127 lxc/config.go:160 lxc/config.go:182
 #, c-format
 msgid   "Can't unset key '%s', it's not currently set."
 msgstr  ""
 
-#: lxc/profile.go:340
+#: lxc/profile.go:343
 msgid   "Cannot provide container name to list"
 msgstr  ""
 
@@ -167,7 +167,7 @@ msgstr  ""
 msgid   "Config key/value to apply to the new container"
 msgstr  ""
 
-#: lxc/config.go:499 lxc/config.go:564 lxc/image.go:669 lxc/profile.go:189
+#: lxc/config.go:500 lxc/config.go:565 lxc/image.go:669 lxc/profile.go:190
 #, c-format
 msgid   "Config parsing error: %s"
 msgstr  ""
@@ -251,12 +251,12 @@ msgid   "Delete containers or container snapshots.\n"
         "Destroy containers or snapshots with any attached data (configuration, snapshots, ...)."
 msgstr  ""
 
-#: lxc/config.go:616
+#: lxc/config.go:617
 #, c-format
 msgid   "Device %s added to %s"
 msgstr  ""
 
-#: lxc/config.go:759
+#: lxc/config.go:804
 #, c-format
 msgid   "Device %s removed from %s"
 msgstr  ""
@@ -265,7 +265,7 @@ msgstr  ""
 msgid   "EPHEMERAL"
 msgstr  ""
 
-#: lxc/config.go:273
+#: lxc/config.go:272
 msgid   "EXPIRY DATE"
 msgstr  ""
 
@@ -306,7 +306,7 @@ msgstr  ""
 msgid   "Expires: never"
 msgstr  ""
 
-#: lxc/config.go:270 lxc/image.go:591 lxc/image.go:616
+#: lxc/config.go:269 lxc/image.go:591 lxc/image.go:616
 msgid   "FINGERPRINT"
 msgstr  ""
 
@@ -349,7 +349,7 @@ msgstr  ""
 msgid   "IPV6"
 msgstr  ""
 
-#: lxc/config.go:272
+#: lxc/config.go:271
 msgid   "ISSUE DATE"
 msgstr  ""
 
@@ -505,8 +505,9 @@ msgid   "Manage configuration profiles.\n"
         "lxc profile device list <profile>                                   List devices in the given profile.\n"
         "lxc profile device show <profile>                                   Show full device details in the given profile.\n"
         "lxc profile device remove <profile> <name>                          Remove a device from a profile.\n"
-        "lxc profile device set <[remote:]container> <name> <key> <value>    Set a device property.\n"
-        "lxc profile device unset <[remote:]container> <name> <key>          Unset a device property.\n"
+        "lxc profile device get <[remote:]profile> <name> <key>              Get a device property.\n"
+        "lxc profile device set <[remote:]profile> <name> <key> <value>      Set a device property.\n"
+        "lxc profile device unset <[remote:]profile> <name> <key>            Unset a device property.\n"
         "lxc profile device add <profile name> <device name> <device type> [key=value]...\n"
         "    Add a profile device, such as a disk or a nic, to the containers\n"
         "    using the specified profile."
@@ -516,19 +517,18 @@ msgstr  ""
 msgid   "Manage configuration.\n"
         "\n"
         "lxc config device add <[remote:]container> <name> <type> [key=value]...     Add a device to a container.\n"
+        "lxc config device get <[remote:]container> <name> <key>                     Get a device property.\n"
         "lxc config device set <[remote:]container> <name> <key> <value>             Set a device property.\n"
         "lxc config device unset <[remote:]container> <name> <key>                   Unset a device property.\n"
-        "lxc config device list [remote:]<container>                                 List devices for container.\n"
-        "lxc config device show [remote:]<container>                                 Show full device details for container.\n"
-        "lxc config device remove [remote:]<container> <name>                        Remove device from container.\n"
-        "\n"
-        "lxc config get [remote:]<container> key                                     Get configuration key.\n"
-        "lxc config set [remote:]<container> key value                               Set container configuration key.\n"
-        "lxc config unset [remote:]<container> key                                   Unset container configuration key.\n"
-        "lxc config set key value                                                    Set server configuration key.\n"
-        "lxc config unset key                                                        Unset server configuration key.\n"
-        "lxc config show [--expanded] [remote:]<container>                           Show container configuration.\n"
-        "lxc config edit [remote:][container]                                        Edit container configuration in external editor.\n"
+        "lxc config device list <[remote:]container>                                 List devices for container.\n"
+        "lxc config device show <[remote:]container>                                 Show full device details for container.\n"
+        "lxc config device remove <[remote:]container> <name>                        Remove device from container.\n"
+        "\n"
+        "lxc config get [remote:][container] <key>                                   Get container or server configuration key.\n"
+        "lxc config set [remote:][container] <key> <value>                           Set container or server configuration key.\n"
+        "lxc config unset [remote:][container] <key>                                 Unset container or server configuration key.\n"
+        "lxc config show [remote:][container] [--expanded]                           Show container or server configuration.\n"
+        "lxc config edit [remote:][container]                                        Edit container or server configuration in external editor.\n"
         "    Edit configuration, either by launching external editor or reading STDIN.\n"
         "    Example: lxc config edit <container> # launch editor\n"
         "             cat config.yml | lxc config edit <config> # read from config.yml\n"
@@ -686,11 +686,11 @@ msgstr  ""
 msgid   "New alias to define at target"
 msgstr  ""
 
-#: lxc/config.go:282
+#: lxc/config.go:281
 msgid   "No certificate provided to add"
 msgstr  ""
 
-#: lxc/config.go:305
+#: lxc/config.go:304
 msgid   "No fingerprint specified."
 msgstr  ""
 
@@ -758,11 +758,11 @@ msgid   "Presents details on how to use LXD.\n"
         "lxd help [--all]"
 msgstr  ""
 
-#: lxc/profile.go:190
+#: lxc/profile.go:191
 msgid   "Press enter to open the editor again"
 msgstr  ""
 
-#: lxc/config.go:500 lxc/config.go:565 lxc/image.go:670
+#: lxc/config.go:501 lxc/config.go:566 lxc/image.go:670
 msgid   "Press enter to start the editor again"
 msgstr  ""
 
@@ -789,17 +789,17 @@ msgstr  ""
 msgid   "Processes: %d"
 msgstr  ""
 
-#: lxc/profile.go:227
+#: lxc/profile.go:228
 #, c-format
 msgid   "Profile %s applied to %s"
 msgstr  ""
 
-#: lxc/profile.go:141
+#: lxc/profile.go:142
 #, c-format
 msgid   "Profile %s created"
 msgstr  ""
 
-#: lxc/profile.go:211
+#: lxc/profile.go:212
 #, c-format
 msgid   "Profile %s deleted"
 msgstr  ""
@@ -958,7 +958,7 @@ msgstr  ""
 msgid   "The container is currently running. Use --force to have it stopped and restarted."
 msgstr  ""
 
-#: lxc/config.go:645 lxc/config.go:663 lxc/config.go:701 lxc/config.go:719
+#: lxc/config.go:645 lxc/config.go:657 lxc/config.go:690 lxc/config.go:708 lxc/config.go:746 lxc/config.go:764
 msgid   "The device doesn't exist"
 msgstr  ""
 


More information about the lxc-devel mailing list