[lxc-devel] [lxd/master] lxd-p2c: Handle target URL smarter

monstermunchkin on Github lxc-bot at linuxcontainers.org
Wed May 23 10:02:34 UTC 2018


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 370 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20180523/f069661f/attachment.bin>
-------------- next part --------------
From e5e7a2a31ff20842a104edf2d83927a2ab629705 Mon Sep 17 00:00:00 2001
From: Thomas Hipp <thomas.hipp at canonical.com>
Date: Wed, 23 May 2018 11:59:42 +0200
Subject: [PATCH] lxd-p2c: Handle target URL smarter

Fixes #4576

Signed-off-by: Thomas Hipp <thomas.hipp at canonical.com>
---
 lxd-p2c/main_migrate.go |  7 ++++++-
 lxd-p2c/utils.go        | 23 +++++++++++++++++++++++
 2 files changed, 29 insertions(+), 1 deletion(-)

diff --git a/lxd-p2c/main_migrate.go b/lxd-p2c/main_migrate.go
index 39134e3f7..69555d701 100644
--- a/lxd-p2c/main_migrate.go
+++ b/lxd-p2c/main_migrate.go
@@ -109,8 +109,13 @@ func (c *cmdMigrate) Run(cmd *cobra.Command, args []string) error {
 		return fmt.Errorf("Failed to setup the source: %v", err)
 	}
 
+	URL, err := parseURL(args[0])
+	if err != nil {
+		return err
+	}
+
 	// Connect to the target
-	dst, err := connectTarget(args[0])
+	dst, err := connectTarget(URL)
 	if err != nil {
 		return err
 	}
diff --git a/lxd-p2c/utils.go b/lxd-p2c/utils.go
index 6a30bf36e..5268508ab 100644
--- a/lxd-p2c/utils.go
+++ b/lxd-p2c/utils.go
@@ -4,6 +4,7 @@ import (
 	"crypto/x509"
 	"encoding/pem"
 	"fmt"
+	"net/url"
 	"strings"
 	"syscall"
 
@@ -180,3 +181,25 @@ func setupSource(path string, mounts []string) error {
 
 	return nil
 }
+
+func parseURL(URL string) (string, error) {
+	u, err := url.Parse(URL)
+	if err != nil {
+		return "", err
+	}
+
+	// Create a URL with scheme and hostname since it wasn't provided
+	if u.Scheme == "" && u.Host == "" && u.Path != "" {
+		u, err = url.Parse(fmt.Sprintf("https://%s", u.Path))
+		if err != nil {
+			return "", err
+		}
+	}
+
+	// If no port was provided, use port 8443
+	if u.Port() == "" {
+		u.Host = fmt.Sprintf("%s:8443", u.Hostname())
+	}
+
+	return u.String(), nil
+}


More information about the lxc-devel mailing list