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

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) ===
Signed-off-by: Free Ekanayaka <free.ekanay...@canonical.com>
From 2702cc7765a22fe2e90c2fcb0892f0776a2941bc Mon Sep 17 00:00:00 2001
From: Free Ekanayaka <free.ekanay...@canonical.com>
Date: Fri, 17 Apr 2020 11:07:24 +0100
Subject: [PATCH] lxd/init: Try to bind LXD network address when running
 interactively

Signed-off-by: Free Ekanayaka <free.ekanay...@canonical.com>
---
 lxd/main_init_interactive.go | 15 ++++++++++++++-
 shared/cmd/ask.go            | 21 +++++++++++++++++----
 2 files changed, 31 insertions(+), 5 deletions(-)

diff --git a/lxd/main_init_interactive.go b/lxd/main_init_interactive.go
index 171e91ae72..38ff31b08e 100644
--- a/lxd/main_init_interactive.go
+++ b/lxd/main_init_interactive.go
@@ -122,6 +122,11 @@ func (c *cmdInit) askClustering(config *cmdInitData, d 
lxd.InstanceServer) error
                        if shared.StringInSlice(host, []string{"", "[::]", 
"0.0.0.0"}) {
                                return fmt.Errorf("Invalid IP address or DNS 
name")
                        }
+                       listener, err := net.Listen("tcp", address)
+                       if err != nil {
+                               return fmt.Errorf("Can't bind address %q", 
address)
+                       }
+                       listener.Close()
                        return nil
                }
                serverAddress := util.CanonicalNetworkAddress(cli.AskString(
@@ -647,7 +652,15 @@ they otherwise would.
                        netAddr = fmt.Sprintf("[%s]", netAddr)
                }
 
-               netPort := cli.AskInt("Port to bind LXD to [default=8443]: ", 
1, 65535, "8443")
+               netPort := cli.AskInt("Port to bind LXD to [default=8443]: ", 
1, 65535, "8443", func(netPort int64) error {
+                       address := fmt.Sprintf("%s:%d", netAddr, netPort)
+                       listener, err := net.Listen("tcp", address)
+                       if err != nil {
+                               return fmt.Errorf("Can't bind address %q", 
address)
+                       }
+                       listener.Close()
+                       return nil
+               })
                config.Node.Config["core.https_address"] = fmt.Sprintf("%s:%d", 
netAddr, netPort)
                config.Node.Config["core.trust_password"] = 
cli.AskPassword("Trust password for new clients: ")
                if config.Node.Config["core.trust_password"] == "" {
diff --git a/shared/cmd/ask.go b/shared/cmd/ask.go
index 62fe715d4b..a8eace979e 100644
--- a/shared/cmd/ask.go
+++ b/shared/cmd/ask.go
@@ -43,17 +43,30 @@ func AskChoice(question string, choices []string, 
defaultAnswer string) string {
 }
 
 // AskInt asks the user to enter an integer between a min and max value
-func AskInt(question string, min int64, max int64, defaultAnswer string) int64 
{
+func AskInt(question string, min int64, max int64, defaultAnswer string, 
validate func(int64) error) int64 {
        for {
                answer := askQuestion(question, defaultAnswer)
 
                result, err := strconv.ParseInt(answer, 10, 64)
+               if err != nil {
+                       fmt.Fprintf(os.Stderr, "Invalid input: %v\n\n", err)
+                       continue
+               }
 
-               if err == nil && (min == -1 || result >= min) && (max == -1 || 
result <= max) {
-                       return result
+               if !((min == -1 || result >= min) && (max == -1 || result <= 
max)) {
+                       fmt.Fprintf(os.Stderr, "Invalid input: out of 
range\n\n")
+                       continue
                }
 
-               invalidInput()
+               if validate != nil {
+                       err = validate(result)
+                       if err != nil {
+                               fmt.Fprintf(os.Stderr, "Invalid input: %v\n\n", 
err)
+                               continue
+                       }
+               }
+
+               return result
        }
 }
 
_______________________________________________
lxc-devel mailing list
lxc-devel@lists.linuxcontainers.org
http://lists.linuxcontainers.org/listinfo/lxc-devel

Reply via email to