[lxc-devel] [lxd/master] "lxd sql" improvements

stgraber on Github lxc-bot at linuxcontainers.org
Tue Apr 24 04:04:39 UTC 2018


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 301 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20180424/ce55e361/attachment.bin>
-------------- next part --------------
From 7e7a39ecf25712f30b6c15ea80b1f25d53b4c81c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Mon, 23 Apr 2018 23:48:22 -0400
Subject: [PATCH 1/2] lxd/db: Don't crash on empty queries
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: Stéphane Graber <stgraber at ubuntu.com>
---
 lxd/api_internal.go | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/lxd/api_internal.go b/lxd/api_internal.go
index 0970d8c7a..7a2dbd82f 100644
--- a/lxd/api_internal.go
+++ b/lxd/api_internal.go
@@ -173,6 +173,11 @@ func internalSQLPost(d *Daemon, r *http.Request) Response {
 	batch := internalSQLBatch{}
 	for _, query := range strings.Split(req.Query, ";") {
 		query = strings.TrimLeft(query, " ")
+
+		if query == "" {
+			continue
+		}
+
 		result := internalSQLResult{}
 		if strings.HasPrefix(strings.ToUpper(query), "SELECT") {
 			err = internalSQLSelect(db, query, &result)

From c9da87aa88e1e9bf1fc4c228011a34ca668b2c12 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Tue, 24 Apr 2018 00:03:47 -0400
Subject: [PATCH 2/2] lxd/sql: Drop custom table renderer
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: Stéphane Graber <stgraber at ubuntu.com>
---
 lxd/main_sql.go | 51 +++++++++++----------------------------------------
 lxd/networks.go |  2 +-
 2 files changed, 12 insertions(+), 41 deletions(-)

diff --git a/lxd/main_sql.go b/lxd/main_sql.go
index 2f1fef143..52a7ac870 100644
--- a/lxd/main_sql.go
+++ b/lxd/main_sql.go
@@ -5,10 +5,8 @@ import (
 	"fmt"
 	"io/ioutil"
 	"os"
-	"strconv"
-	"strings"
-	"time"
 
+	"github.com/olekukonko/tablewriter"
 	"github.com/pkg/errors"
 	"github.com/spf13/cobra"
 
@@ -137,44 +135,17 @@ func (c *cmdSql) Run(cmd *cobra.Command, args []string) error {
 }
 
 func sqlPrintSelectResult(result internalSQLResult) {
-	// Print results in tabular format
-	widths := make([]int, len(result.Columns))
-	for i, column := range result.Columns {
-		widths[i] = len(column)
-	}
+	table := tablewriter.NewWriter(os.Stdout)
+	table.SetAlignment(tablewriter.ALIGN_LEFT)
+	table.SetAutoWrapText(false)
+	table.SetAutoFormatHeaders(false)
+	table.SetHeader(result.Columns)
 	for _, row := range result.Rows {
-		for i, v := range row {
-			width := 10
-			switch v := v.(type) {
-			case string:
-				width = len(v)
-			case int:
-				width = 6
-			case int64:
-				width = 6
-			case time.Time:
-				width = 12
-			}
-			if width > widths[i] {
-				widths[i] = width
-			}
+		data := []string{}
+		for _, col := range row {
+			data = append(data, fmt.Sprintf("%v", col))
 		}
+		table.Append(data)
 	}
-	format := "|"
-	separator := "+"
-	columns := make([]interface{}, len(result.Columns))
-	for i, column := range result.Columns {
-		format += " %-" + strconv.Itoa(widths[i]) + "v |"
-		columns[i] = column
-		separator += strings.Repeat("-", widths[i]+2) + "+"
-	}
-	format += "\n"
-	separator += "\n"
-	fmt.Printf(separator)
-	fmt.Printf(fmt.Sprintf(format, columns...))
-	fmt.Printf(separator)
-	for _, row := range result.Rows {
-		fmt.Printf(format, row...)
-	}
-	fmt.Printf(separator)
+	table.Render()
 }
diff --git a/lxd/networks.go b/lxd/networks.go
index f7c751120..19c6d1b25 100644
--- a/lxd/networks.go
+++ b/lxd/networks.go
@@ -1461,7 +1461,7 @@ func (n *network) Start() error {
 		}
 
 		// Configure NAT
-		err = networkIptablesPrepend("ipv4", n.name, "nat", "POSTROUTING", "-s", underlaySubnet.String(), "!", "-d", underlaySubnet.String(), "-j", "MASQUERADE")
+		err = networkIptablesPrepend("ipv4", n.name, "nat", "POSTROUTING", "-s", overlaySubnet.String(), "!", "-d", overlaySubnet.String(), "-j", "MASQUERADE")
 		if err != nil {
 			return err
 		}


More information about the lxc-devel mailing list