[lxc-devel] [lxd/master] lxd/init: Try to bind LXD network address when running interactively

freeekanayaka on Github lxc-bot at linuxcontainers.org
Fri Apr 17 10:08:51 UTC 2020


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 361 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20200417/fe37b170/attachment.bin>
-------------- next part --------------
From 2702cc7765a22fe2e90c2fcb0892f0776a2941bc Mon Sep 17 00:00:00 2001
From: Free Ekanayaka <free.ekanayaka at 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.ekanayaka at 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
 	}
 }
 


More information about the lxc-devel mailing list