[lxc-devel] [lxd/master] rework `lxc profile apply*` cli

tych0 on Github lxc-bot at linuxcontainers.org
Fri May 20 23:00:47 UTC 2016


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 493 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20160520/2da941b4/attachment.bin>
-------------- next part --------------
From 3a6d6a56d214f7b212d283f6c7ba992ef99e618f Mon Sep 17 00:00:00 2001
From: Tycho Andersen <tycho.andersen at canonical.com>
Date: Fri, 20 May 2016 13:58:54 -0700
Subject: [PATCH] rework `lxc profile apply*` cli

* s/apply-add/add
* s/apply-remove/remove
* s/apply/assign
* allow `lxc profile apply ''` again

Closes #1920
Closes #2037

Signed-off-by: Tycho Andersen <tycho.andersen at canonical.com>
---
 client.go             |  8 ++++++--
 lxc/profile.go        | 32 +++++++++++++++++---------------
 po/lxd.pot            | 32 +++++++++++++++++---------------
 test/suites/config.sh | 12 ++++++++++++
 4 files changed, 52 insertions(+), 32 deletions(-)

diff --git a/client.go b/client.go
index a951af8..d1e08cb 100644
--- a/client.go
+++ b/client.go
@@ -2067,7 +2067,7 @@ func (c *Client) ListProfiles() ([]string, error) {
 	return names, nil
 }
 
-func (c *Client) ApplyProfile(container, profile string) (*Response, error) {
+func (c *Client) AssignProfile(container, profile string) (*Response, error) {
 	if c.Remote.Public {
 		return nil, fmt.Errorf("This function isn't supported by public remotes.")
 	}
@@ -2077,7 +2077,11 @@ func (c *Client) ApplyProfile(container, profile string) (*Response, error) {
 		return nil, err
 	}
 
-	st.Profiles = strings.Split(profile, ",")
+	if profile != "" {
+		st.Profiles = strings.Split(profile, ",")
+	} else {
+		st.Profiles = nil
+	}
 
 	return c.put(fmt.Sprintf("containers/%s", container), st, Async)
 }
diff --git a/lxc/profile.go b/lxc/profile.go
index 2322d4e..0153281 100644
--- a/lxc/profile.go
+++ b/lxc/profile.go
@@ -60,16 +60,18 @@ lxc profile edit <profile>
     Example: lxc profile edit <profile> # launch editor
              cat profile.yml | lxc profile edit <profile> # read from profile.yml
 
-lxc profile apply <container> <profiles>
-    Apply a comma-separated list of profiles to a container, in order.
+lxc profile assign <container> <profiles>
+    Assign a comma-separated list of profiles to a container, in order.
     All profiles passed in this call (and only those) will be applied
-    to the specified container.
+    to the specified container, i.e. it sets the list of profiles exactly to
+    those specified in this command. To add/remove a particular profile from a
+    container, use {add|remove} below.
     Example: lxc profile apply foo default,bar # Apply default and bar
              lxc profile apply foo default # Only default is active
              lxc profile apply '' # no profiles are applied anymore
              lxc profile apply bar,default # Apply default second now
-lxc profile apply-add <container> <profile>
-lxc profile apply-remove <container> <profile>
+lxc profile add <container> <profile> # add a profile to a container
+lxc profile remove <container> <profile> # remove the profile from a container
 
 Devices:
 lxc profile device list <profile>                                   List devices in the given profile.
@@ -113,7 +115,7 @@ func (c *profileCmd) run(config *lxd.Config, args []string) error {
 		return c.doProfileDevice(config, args)
 	case "edit":
 		return c.doProfileEdit(client, profile)
-	case "apply":
+	case "apply", "assign":
 		container := profile
 		switch len(args) {
 		case 2:
@@ -123,8 +125,8 @@ func (c *profileCmd) run(config *lxd.Config, args []string) error {
 		default:
 			return errArgs
 		}
-		return c.doProfileApply(client, container, profile)
-	case "apply-add":
+		return c.doProfileAssign(client, container, profile)
+	case "add":
 		container := profile
 		switch len(args) {
 		case 2:
@@ -134,8 +136,8 @@ func (c *profileCmd) run(config *lxd.Config, args []string) error {
 		default:
 			return errArgs
 		}
-		return c.doProfileApplyAdd(client, container, profile)
-	case "apply-remove":
+		return c.doProfileAdd(client, container, profile)
+	case "remove":
 		container := profile
 		switch len(args) {
 		case 2:
@@ -145,7 +147,7 @@ func (c *profileCmd) run(config *lxd.Config, args []string) error {
 		default:
 			return errArgs
 		}
-		return c.doProfileApplyRemove(client, container, profile)
+		return c.doProfileRemove(client, container, profile)
 	case "get":
 		return c.doProfileGet(client, profile, args[2:])
 	case "set":
@@ -239,8 +241,8 @@ func (c *profileCmd) doProfileDelete(client *lxd.Client, p string) error {
 	return err
 }
 
-func (c *profileCmd) doProfileApply(client *lxd.Client, d string, p string) error {
-	resp, err := client.ApplyProfile(d, p)
+func (c *profileCmd) doProfileAssign(client *lxd.Client, d string, p string) error {
+	resp, err := client.AssignProfile(d, p)
 	if err != nil {
 		return err
 	}
@@ -256,7 +258,7 @@ func (c *profileCmd) doProfileApply(client *lxd.Client, d string, p string) erro
 	return err
 }
 
-func (c *profileCmd) doProfileApplyAdd(client *lxd.Client, d string, p string) error {
+func (c *profileCmd) doProfileAdd(client *lxd.Client, d string, p string) error {
 	ct, err := client.ContainerInfo(d)
 	if err != nil {
 		return err
@@ -274,7 +276,7 @@ func (c *profileCmd) doProfileApplyAdd(client *lxd.Client, d string, p string) e
 	return err
 }
 
-func (c *profileCmd) doProfileApplyRemove(client *lxd.Client, d string, p string) error {
+func (c *profileCmd) doProfileRemove(client *lxd.Client, d string, p string) error {
 	ct, err := client.ContainerInfo(d)
 	if err != nil {
 		return err
diff --git a/po/lxd.pot b/po/lxd.pot
index 8f3339c..373df2e 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-05-17 17:02-0400\n"
+        "POT-Creation-Date: 2016-05-20 13:58-0700\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"
@@ -86,7 +86,7 @@ msgstr  ""
 msgid   "'/' not allowed in snapshot name"
 msgstr  ""
 
-#: lxc/profile.go:251
+#: lxc/profile.go:253
 msgid   "(none)"
 msgstr  ""
 
@@ -159,7 +159,7 @@ msgstr  ""
 msgid   "Can't unset key '%s', it's not currently set."
 msgstr  ""
 
-#: lxc/profile.go:417
+#: lxc/profile.go:419
 msgid   "Cannot provide container name to list"
 msgstr  ""
 
@@ -187,7 +187,7 @@ msgstr  ""
 msgid   "Config key/value to apply to the new container"
 msgstr  ""
 
-#: lxc/config.go:527 lxc/config.go:592 lxc/image.go:714 lxc/profile.go:215
+#: lxc/config.go:527 lxc/config.go:592 lxc/image.go:714 lxc/profile.go:217
 #, c-format
 msgid   "Config parsing error: %s"
 msgstr  ""
@@ -525,16 +525,18 @@ msgid   "Manage configuration profiles.\n"
         "    Example: lxc profile edit <profile> # launch editor\n"
         "             cat profile.yml | lxc profile edit <profile> # read from profile.yml\n"
         "\n"
-        "lxc profile apply <container> <profiles>\n"
-        "    Apply a comma-separated list of profiles to a container, in order.\n"
+        "lxc profile assign <container> <profiles>\n"
+        "    Assign a comma-separated list of profiles to a container, in order.\n"
         "    All profiles passed in this call (and only those) will be applied\n"
-        "    to the specified container.\n"
+        "    to the specified container, i.e. it sets the list of profiles exactly to\n"
+        "    those specified in this command. To add/remove a particular profile from a\n"
+        "    container, use {add|remove} below.\n"
         "    Example: lxc profile apply foo default,bar # Apply default and bar\n"
         "             lxc profile apply foo default # Only default is active\n"
         "             lxc profile apply '' # no profiles are applied anymore\n"
         "             lxc profile apply bar,default # Apply default second now\n"
-        "lxc profile apply-add <container> <profile>\n"
-        "lxc profile apply-remove <container> <profile>\n"
+        "lxc profile add <container> <profile> # add a profile to a container\n"
+        "lxc profile remove <container> <profile> # remove the profile from a container\n"
         "\n"
         "Devices:\n"
         "lxc profile device list <profile>                                   List devices in the given profile.\n"
@@ -809,7 +811,7 @@ msgid   "Presents details on how to use LXD.\n"
         "lxd help [--all]"
 msgstr  ""
 
-#: lxc/profile.go:216
+#: lxc/profile.go:218
 msgid   "Press enter to open the editor again"
 msgstr  ""
 
@@ -840,22 +842,22 @@ msgstr  ""
 msgid   "Processes: %d"
 msgstr  ""
 
-#: lxc/profile.go:272
+#: lxc/profile.go:274
 #, c-format
 msgid   "Profile %s added to %s"
 msgstr  ""
 
-#: lxc/profile.go:167
+#: lxc/profile.go:169
 #, c-format
 msgid   "Profile %s created"
 msgstr  ""
 
-#: lxc/profile.go:237
+#: lxc/profile.go:239
 #, c-format
 msgid   "Profile %s deleted"
 msgstr  ""
 
-#: lxc/profile.go:303
+#: lxc/profile.go:305
 #, c-format
 msgid   "Profile %s removed from %s"
 msgstr  ""
@@ -864,7 +866,7 @@ msgstr  ""
 msgid   "Profile to apply to the new container"
 msgstr  ""
 
-#: lxc/profile.go:253
+#: lxc/profile.go:255
 #, c-format
 msgid   "Profiles %s applied to %s"
 msgstr  ""
diff --git a/test/suites/config.sh b/test/suites/config.sh
index 6613727..732e11e 100644
--- a/test/suites/config.sh
+++ b/test/suites/config.sh
@@ -92,6 +92,18 @@ test_config_profiles() {
   # into the database and never let the user edit the container again.
   ! lxc config set foo raw.lxc "lxc.notaconfigkey = invalid"
 
+  # check that various profile application mechanisms work
+  lxc profile create one
+  lxc profile create two
+  lxc profile assign foo one,two
+  [ "$(lxc info foo | grep Profiles)" = "Profiles: one, two" ]
+  lxc profile assign foo ""
+  [ "$(lxc info foo | grep Profiles)" = "Profiles: " ]
+  lxc profile add foo one
+  [ "$(lxc info foo | grep Profiles)" = "Profiles: one" ]
+  lxc profile remove foo one
+  [ "$(lxc info foo | grep Profiles)" = "Profiles: " ]
+
   lxc profile create stdintest
   echo "BADCONF" | lxc profile set stdintest user.user_data -
   lxc profile show stdintest | grep BADCONF


More information about the lxc-devel mailing list