[lxc-devel] [lxd/master] Dump LXD config

monstermunchkin on Github lxc-bot at linuxcontainers.org
Mon Jul 16 10:32:34 UTC 2018


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 321 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20180716/1dca1d64/attachment.bin>
-------------- next part --------------
From d448ca0221b145b7429480c87a3f2894e872868f Mon Sep 17 00:00:00 2001
From: Thomas Hipp <thomas.hipp at canonical.com>
Date: Fri, 13 Jul 2018 12:29:11 +0200
Subject: [PATCH 1/2] lxd: Introduce --dump flag to lxd init

Signed-off-by: Thomas Hipp <thomas.hipp at canonical.com>
---
 lxd/main_init.go      | 20 +++++++++++++
 lxd/main_init_dump.go | 80 +++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 100 insertions(+)
 create mode 100644 lxd/main_init_dump.go

diff --git a/lxd/main_init.go b/lxd/main_init.go
index 7b819eea5e..72c826154c 100644
--- a/lxd/main_init.go
+++ b/lxd/main_init.go
@@ -21,6 +21,7 @@ type cmdInit struct {
 
 	flagAuto    bool
 	flagPreseed bool
+	flagDump    bool
 
 	flagNetworkAddress  string
 	flagNetworkPort     int
@@ -42,10 +43,12 @@ func (c *cmdInit) Command() *cobra.Command {
   init --auto [--network-address=IP] [--network-port=8443] [--storage-backend=dir]
               [--storage-create-device=DEVICE] [--storage-create-loop=SIZE]
               [--storage-pool=POOL] [--trust-password=PASSWORD]
+  init --dump
 `
 	cmd.RunE = c.Run
 	cmd.Flags().BoolVar(&c.flagAuto, "auto", false, "Automatic (non-interactive) mode")
 	cmd.Flags().BoolVar(&c.flagPreseed, "preseed", false, "Pre-seed mode, expects YAML config from stdin")
+	cmd.Flags().BoolVar(&c.flagDump, "dump", false, "Dump YAML config to stdout")
 
 	cmd.Flags().StringVar(&c.flagNetworkAddress, "network-address", "", "Address to bind LXD to (default: none)"+"``")
 	cmd.Flags().IntVar(&c.flagNetworkPort, "network-port", -1, "Port to bind LXD to (default: 8443)"+"``")
@@ -71,12 +74,29 @@ func (c *cmdInit) Run(cmd *cobra.Command, args []string) error {
 		return fmt.Errorf("Configuration flags require --auto")
 	}
 
+	if c.flagDump && (c.flagAuto || c.flagPreseed || c.flagNetworkAddress != "" ||
+		c.flagNetworkPort != -1 || c.flagStorageBackend != "" ||
+		c.flagStorageDevice != "" || c.flagStorageLoopSize != -1 ||
+		c.flagStoragePool != "" || c.flagTrustPassword != "") {
+		return fmt.Errorf("Can't use --dump with other flags")
+	}
+
 	// Connect to LXD
 	d, err := lxd.ConnectLXDUnix("", nil)
 	if err != nil {
 		return errors.Wrap(err, "Failed to connect to local LXD")
 	}
 
+	// Dump mode
+	if c.flagDump {
+		err := c.RunDump(d)
+		if err != nil {
+			return err
+		}
+
+		return nil
+	}
+
 	// Prepare the input data
 	var config *cmdInitData
 
diff --git a/lxd/main_init_dump.go b/lxd/main_init_dump.go
new file mode 100644
index 0000000000..4ce72e3f90
--- /dev/null
+++ b/lxd/main_init_dump.go
@@ -0,0 +1,80 @@
+package main
+
+import (
+	"fmt"
+
+	"github.com/pkg/errors"
+	yaml "gopkg.in/yaml.v2"
+
+	lxd "github.com/lxc/lxd/client"
+	"github.com/lxc/lxd/shared/api"
+)
+
+func (c *cmdInit) RunDump(d lxd.ContainerServer) error {
+	currentServer, _, err := d.GetServer()
+	if err != nil {
+		return errors.Wrap(err, "Failed to retrieve current server configuration")
+	}
+
+	var config initDataNode
+	config.Config = currentServer.Config
+
+	networks, err := d.GetNetworks()
+	if err != nil {
+		return errors.Wrap(err, "Failed to retrieve current server configuration")
+	}
+
+	for _, network := range networks {
+		// Only list managed networks
+		if !network.Managed {
+			continue
+		}
+		networksPost := api.NetworksPost{}
+		networksPost.Config = network.Config
+		networksPost.Description = network.Description
+		networksPost.Managed = network.Managed
+		networksPost.Name = network.Name
+		networksPost.Type = network.Type
+
+		config.Networks = append(config.Networks, networksPost)
+	}
+
+	storagePools, err := d.GetStoragePools()
+	if err != nil {
+		return errors.Wrap(err, "Failed to retrieve current server configuration")
+	}
+
+	for _, storagePool := range storagePools {
+		storagePoolsPost := api.StoragePoolsPost{}
+		storagePoolsPost.Config = storagePool.Config
+		storagePoolsPost.Description = storagePool.Description
+		storagePoolsPost.Name = storagePool.Name
+		storagePoolsPost.Driver = storagePool.Driver
+
+		config.StoragePools = append(config.StoragePools, storagePoolsPost)
+	}
+
+	profiles, err := d.GetProfiles()
+	if err != nil {
+		return errors.Wrap(err, "Failed to retrieve current server configuration")
+	}
+
+	for _, profile := range profiles {
+		profilesPost := api.ProfilesPost{}
+		profilesPost.Config = profile.Config
+		profilesPost.Description = profile.Description
+		profilesPost.Devices = profile.Devices
+		profilesPost.Name = profile.Name
+
+		config.Profiles = append(config.Profiles, profilesPost)
+	}
+
+	out, err := yaml.Marshal(config)
+	if err != nil {
+		return errors.Wrap(err, "Failed to retrieve current server configuration")
+	}
+
+	fmt.Printf("%s\n", out)
+
+	return nil
+}

From 431b2874ae87564cc615bca26bfd686abc329aab Mon Sep 17 00:00:00 2001
From: Thomas Hipp <thomas.hipp at canonical.com>
Date: Mon, 16 Jul 2018 12:14:20 +0200
Subject: [PATCH 2/2] test: Add test for lxd init --dump

Signed-off-by: Thomas Hipp <thomas.hipp at canonical.com>
---
 test/suites/init_dump.sh | 96 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 96 insertions(+)
 create mode 100644 test/suites/init_dump.sh

diff --git a/test/suites/init_dump.sh b/test/suites/init_dump.sh
new file mode 100644
index 0000000000..b1d0b506da
--- /dev/null
+++ b/test/suites/init_dump.sh
@@ -0,0 +1,96 @@
+test_init_dump() {
+  # - lxd init --dump
+  LXD_INIT_DIR=$(mktemp -d -p "${TEST_DIR}" XXX)
+  chmod +x "${LXD_INIT_DIR}"
+  spawn_lxd "${LXD_INIT_DIR}" false
+
+  (
+    set -e
+    # shellcheck disable=SC2034
+    LXD_DIR=${LXD_INIT_DIR}
+
+    storage_pool="lxdtest-$(basename "${LXD_DIR}")-data"
+    driver="dir"
+
+    cat <<EOF | lxd init --preseed
+config:
+  core.https_address: 127.0.0.1:9999
+  images.auto_update_interval: 15
+storage_pools:
+- name: ${storage_pool}
+  driver: $driver
+networks:
+- name: lxdt$$
+  type: bridge
+  config:
+    ipv4.address: none
+    ipv6.address: none
+profiles:
+- name: default
+  devices:
+    root:
+      path: /
+      pool: ${storage_pool}
+      type: disk
+- name: test-profile
+  description: "Test profile"
+  config:
+    limits.memory: 2GB
+  devices:
+    test0:
+      name: test0
+      nictype: bridged
+      parent: lxdt$$
+      type: nic
+EOF
+  lxd init --dump > config.yaml
+cat <<EOF > expected.yaml
+config:
+  core.https_address: 127.0.0.1:9999
+  core.trust_password: true
+  images.auto_update_interval: "15"
+networks:
+- config:
+    ipv4.address: none
+    ipv6.address: none
+  description: ""
+  managed: true
+  name: lxdt$$
+  type: bridge
+storage_pools:
+- config:
+    source: ${LXD_DIR}/storage-pools/${storage_pool}
+  description: ""
+  name: ${storage_pool}
+  driver: ${driver}
+profiles:
+- config: {}
+  description: Default LXD profile
+  devices:
+    eth0:
+      name: eth0
+      nictype: p2p
+      type: nic
+    root:
+      path: /
+      pool: ${storage_pool}
+      type: disk
+  name: default
+- config:
+    limits.memory: 2GB
+  description: Test profile
+  devices:
+    test0:
+      name: test0
+      nictype: bridged
+      parent: lxdt$$
+      type: nic
+  name: test-profile
+
+EOF
+
+  diff -u config.yaml expected.yaml
+)
+  rm -f config.yaml expected.yaml
+  kill_lxd "${LXD_INIT_DIR}"
+}


More information about the lxc-devel mailing list