[lxc-devel] [lxd/stable-2.0] Move state-changing inline functions to own methods
freeekanayaka on Github
lxc-bot at linuxcontainers.org
Wed Aug 30 17:26:40 UTC 2017
A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 582 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20170830/c8f2adf7/attachment.bin>
-------------- next part --------------
From 53a7072f143522a1f18fca8d062d86c180dd7cf3 Mon Sep 17 00:00:00 2001
From: Free Ekanayaka <free at ekanayaka.io>
Date: Tue, 2 May 2017 07:40:32 +0200
Subject: [PATCH] Move state-changing inline functions to own methods
This is a mechanical commit just moving the various inline helpers
functions of CmdInit.Run to separate methods, in order to make Run()
itself a bit slimmer and easier to read with a brief eye look.
There's no logic change.
Signed-off-by: Free Ekanayaka <free at ekanayaka.io>
---
lxd/main_init.go | 117 +++++++++++++++++++++++++++++++++----------------------
1 file changed, 71 insertions(+), 46 deletions(-)
diff --git a/lxd/main_init.go b/lxd/main_init.go
index 4044d2fde..e673f9924 100644
--- a/lxd/main_init.go
+++ b/lxd/main_init.go
@@ -71,46 +71,6 @@ func (cmd *CmdInit) Run() error {
return fmt.Errorf("Unable to talk to LXD: %s", err)
}
- setServerConfig := func(key string, value string) error {
- server, etag, err := c.GetServer()
- if err != nil {
- return err
- }
-
- if server.Config == nil {
- server.Config = map[string]interface{}{}
- }
-
- server.Config[key] = value
-
- err = c.UpdateServer(server.Writable(), etag)
- if err != nil {
- return err
- }
-
- return nil
- }
-
- setProfileConfigItem := func(profileName string, key string, value string) error {
- profile, etag, err := c.GetProfile(profileName)
- if err != nil {
- return err
- }
-
- if profile.Config == nil {
- profile.Config = map[string]string{}
- }
-
- profile.Config[key] = value
-
- err = c.UpdateProfile(profileName, profile.Writable(), etag)
- if err != nil {
- return err
- }
-
- return nil
- }
-
// Check that we have no containers or images in the store
containers, err := c.GetContainerNames()
if err != nil {
@@ -288,7 +248,7 @@ they otherwise would.
// Unset all storage keys, core.https_address and core.trust_password
for _, key := range []string{"storage.zfs_pool_name", "core.https_address", "core.trust_password"} {
- err = setServerConfig(key, "")
+ err = cmd.setServerConfig(c, key, "")
if err != nil {
return err
}
@@ -334,19 +294,19 @@ they otherwise would.
}
// Configure LXD to use the pool
- err = setServerConfig("storage.zfs_pool_name", storagePool)
+ err = cmd.setServerConfig(c, "storage.zfs_pool_name", storagePool)
if err != nil {
return err
}
}
if defaultPrivileged == 0 {
- err = setProfileConfigItem("default", "security.privileged", "")
+ err = cmd.setProfileConfigItem(c, "default", "security.privileged", "")
if err != nil {
return err
}
} else if defaultPrivileged == 1 {
- err = setProfileConfigItem("default", "security.privileged", "true")
+ err = cmd.setProfileConfigItem(c, "default", "security.privileged", "true")
if err != nil {
}
}
@@ -356,13 +316,13 @@ they otherwise would.
networkPort = 8443
}
- err = setServerConfig("core.https_address", fmt.Sprintf("%s:%d", networkAddress, networkPort))
+ err = cmd.setServerConfig(c, "core.https_address", fmt.Sprintf("%s:%d", networkAddress, networkPort))
if err != nil {
return err
}
if trustPassword != "" {
- err = setServerConfig("core.trust_password", trustPassword)
+ err = cmd.setServerConfig(c, "core.trust_password", trustPassword)
if err != nil {
return err
}
@@ -373,6 +333,71 @@ they otherwise would.
return nil
}
+func (cmd *CmdInit) setServerConfig(c lxd.ContainerServer, key string, value string) error {
+ server, etag, err := c.GetServer()
+ if err != nil {
+ return err
+ }
+
+ if server.Config == nil {
+ server.Config = map[string]interface{}{}
+ }
+
+ server.Config[key] = value
+
+ err = c.UpdateServer(server.Writable(), etag)
+ if err != nil {
+ return err
+ }
+
+ return nil
+}
+
+func (cmd *CmdInit) profileDeviceAdd(c lxd.ContainerServer, profileName string, deviceName string, deviceConfig map[string]string) error {
+ profile, etag, err := c.GetProfile(profileName)
+ if err != nil {
+ return err
+ }
+
+ if profile.Devices == nil {
+ profile.Devices = map[string]map[string]string{}
+ }
+
+ _, ok := profile.Devices[deviceName]
+ if ok {
+ return fmt.Errorf("Device already exists: %s", deviceName)
+ }
+
+ profile.Devices[deviceName] = deviceConfig
+
+ err = c.UpdateProfile(profileName, profile.Writable(), etag)
+ if err != nil {
+ return err
+ }
+
+ return nil
+}
+
+func (cmd *CmdInit) setProfileConfigItem(c lxd.ContainerServer, profileName string, key string, value string) error {
+ profile, etag, err := c.GetProfile(profileName)
+ if err != nil {
+ return err
+ }
+
+ if profile.Config == nil {
+ profile.Config = map[string]string{}
+ }
+
+ profile.Config[key] = value
+
+ err = c.UpdateProfile(profileName, profile.Writable(), etag)
+ if err != nil {
+ return err
+ }
+
+ return nil
+}
+
func cmdInit() error {
context := cmd.NewContext(os.Stdin, os.Stdout, os.Stderr)
args := &CmdInitArgs{
More information about the lxc-devel
mailing list