The following pull request was submitted through Github.
It can be accessed and reviewed at: https://github.com/lxc/lxd/pull/6138

This e-mail was sent by the LXC bot, direct replies will not reach the author
unless they happen to be subscribed to this list.

=== Description (from pull-request) ===
It's now possible to parse configuration options from stdin in boh `lxc init` and `lxc launch` commands.

Example:
```
$# $ cat /tmp/template.yml 
config:
  raw.lxc: |
    lxc.net.0.ipv4.address = 10.10.10.1/24
ephemeral: True
profiles:
- sdpool
- net-lxdbr0

$# lxc launch 64362190ee7c c1 < /tmp/config.yml
```
From a254c122701a7cd8af8c603a037743f8a6a3949e Mon Sep 17 00:00:00 2001
From: Daniele Rondina <gea...@sabayonlinux.org>
Date: Sat, 31 Aug 2019 23:07:39 +0200
Subject: [PATCH] lxc/init: Add parse of options from stdin

Signed-off-by: Daniele Rondina <gea...@sabayonlinux.org>
---
 lxc/init.go | 39 ++++++++++++++++++++++++++++++++++++---
 1 file changed, 36 insertions(+), 3 deletions(-)

diff --git a/lxc/init.go b/lxc/init.go
index 9472e68e95..ad07485d67 100644
--- a/lxc/init.go
+++ b/lxc/init.go
@@ -2,10 +2,12 @@ package main
 
 import (
        "fmt"
+       "io/ioutil"
        "os"
        "strings"
 
        "github.com/spf13/cobra"
+       "gopkg.in/yaml.v2"
 
        "github.com/lxc/lxd/client"
        "github.com/lxc/lxd/lxc/config"
@@ -13,6 +15,7 @@ import (
        "github.com/lxc/lxd/shared/api"
        cli "github.com/lxc/lxd/shared/cmd"
        "github.com/lxc/lxd/shared/i18n"
+       "github.com/lxc/lxd/shared/termios"
 )
 
 type cmdInit struct {
@@ -68,6 +71,22 @@ func (c *cmdInit) create(conf *config.Config, args []string) 
(lxd.ContainerServe
        var remote string
        var iremote string
        var err error
+       var stdinData api.ContainerPut
+       var devicesMap map[string]map[string]string
+       var configMap map[string]string
+
+       // If stdin isn't a terminal, read text from it
+       if !termios.IsTerminal(getStdinFd()) {
+               contents, err := ioutil.ReadAll(os.Stdin)
+               if err != nil {
+                       return nil, "", err
+               }
+
+               err = yaml.Unmarshal(contents, &stdinData)
+               if err != nil {
+                       return nil, "", err
+               }
+       }
 
        if len(args) > 0 {
                iremote, image, err = conf.ParseRemote(args[0])
@@ -129,7 +148,12 @@ func (c *cmdInit) create(conf *config.Config, args 
[]string) (lxd.ContainerServe
                }
        }
 
-       devicesMap := map[string]map[string]string{}
+       if len(stdinData.Devices) > 0 {
+               devicesMap = stdinData.Devices
+       } else {
+               devicesMap = map[string]map[string]string{}
+       }
+
        if c.flagNetwork != "" {
                network, _, err := d.GetNetwork(c.flagNetwork)
                if err != nil {
@@ -143,7 +167,11 @@ func (c *cmdInit) create(conf *config.Config, args 
[]string) (lxd.ContainerServe
                }
        }
 
-       configMap := map[string]string{}
+       if len(stdinData.Config) > 0 {
+               configMap = stdinData.Config
+       } else {
+               configMap = map[string]string{}
+       }
        for _, entry := range c.flagConfig {
                if !strings.Contains(entry, "=") {
                        return nil, "", fmt.Errorf(i18n.G("Bad key=value pair: 
%s"), entry)
@@ -174,8 +202,13 @@ func (c *cmdInit) create(conf *config.Config, args 
[]string) (lxd.ContainerServe
        }
        req.Config = configMap
        req.Devices = devicesMap
+
        if !c.flagNoProfiles && len(profiles) == 0 {
-               req.Profiles = nil
+               if len(stdinData.Profiles) > 0 {
+                       req.Profiles = stdinData.Profiles
+               } else {
+                       req.Profiles = nil
+               }
        } else {
                req.Profiles = profiles
        }
_______________________________________________
lxc-devel mailing list
lxc-devel@lists.linuxcontainers.org
http://lists.linuxcontainers.org/listinfo/lxc-devel

Reply via email to