[lxc-devel] [lxd/master] More refactoring

stgraber on Github lxc-bot at linuxcontainers.org
Fri Dec 16 21:25:11 UTC 2016


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/20161216/5531cdfd/attachment.bin>
-------------- next part --------------
From eaf23c7c04e1c182d752fedf3bd3f0dbeed9cbae Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Fri, 16 Dec 2016 15:27:12 -0500
Subject: [PATCH 1/3] simplestreams: Pass UserAgent as argument
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>
---
 client.go                             |  6 +-----
 lxd/daemon_images.go                  | 11 ++---------
 shared/simplestreams/simplestreams.go | 26 +++++++++++++++++++-------
 3 files changed, 22 insertions(+), 21 deletions(-)

diff --git a/client.go b/client.go
index fbfa9ed..11c0198 100644
--- a/client.go
+++ b/client.go
@@ -352,11 +352,7 @@ func NewClientFromInfo(info ConnectInfo) (*Client, error) {
 		}
 		c.Http.Transport = tr
 
-		ss, err := simplestreams.NewClient(c.Remote.Addr, c.Http)
-		if err != nil {
-			return nil, err
-		}
-
+		ss := simplestreams.NewClient(c.Remote.Addr, c.Http, shared.UserAgent)
 		c.simplestreams = ss
 	}
 
diff --git a/lxd/daemon_images.go b/lxd/daemon_images.go
index c2dd4f6..6afe492 100644
--- a/lxd/daemon_images.go
+++ b/lxd/daemon_images.go
@@ -72,11 +72,7 @@ func imageLoadStreamCache(d *Daemon) error {
 				return err
 			}
 
-			ss, err := simplestreams.NewClient(url, *myhttp)
-			if err != nil {
-				return err
-			}
-
+			ss := simplestreams.NewClient(url, *myhttp, shared.UserAgent)
 			entry.ss = ss
 		}
 	}
@@ -109,10 +105,7 @@ func (d *Daemon) ImageDownload(op *operation, server string, protocol string, ce
 					return nil, err
 				}
 
-				ss, err = simplestreams.NewClient(server, *myhttp)
-				if err != nil {
-					return nil, err
-				}
+				ss = simplestreams.NewClient(server, *myhttp, shared.UserAgent)
 
 				// Get all aliases
 				aliases, err := ss.ListAliases()
diff --git a/shared/simplestreams/simplestreams.go b/shared/simplestreams/simplestreams.go
index fd45e4a..9be22d7 100644
--- a/shared/simplestreams/simplestreams.go
+++ b/shared/simplestreams/simplestreams.go
@@ -262,16 +262,19 @@ type SimpleStreamsIndexStream struct {
 	Products []string `json:"products"`
 }
 
-func NewClient(url string, httpClient http.Client) (*SimpleStreams, error) {
+func NewClient(url string, httpClient http.Client, useragent string) *SimpleStreams {
 	return &SimpleStreams{
 		http:           &httpClient,
 		url:            url,
-		cachedManifest: map[string]*SimpleStreamsManifest{}}, nil
+		cachedManifest: map[string]*SimpleStreamsManifest{},
+		useragent:      useragent,
+	}
 }
 
 type SimpleStreams struct {
-	http *http.Client
-	url  string
+	http      *http.Client
+	url       string
+	useragent string
 
 	cachedIndex    *SimpleStreamsIndex
 	cachedManifest map[string]*SimpleStreamsManifest
@@ -288,7 +291,10 @@ func (s *SimpleStreams) parseIndex() (*SimpleStreamsIndex, error) {
 	if err != nil {
 		return nil, err
 	}
-	req.Header.Set("User-Agent", shared.UserAgent)
+
+	if s.useragent != "" {
+		req.Header.Set("User-Agent", s.useragent)
+	}
 
 	r, err := s.http.Do(req)
 	if err != nil {
@@ -322,7 +328,10 @@ func (s *SimpleStreams) parseManifest(path string) (*SimpleStreamsManifest, erro
 	if err != nil {
 		return nil, err
 	}
-	req.Header.Set("User-Agent", shared.UserAgent)
+
+	if s.useragent != "" {
+		req.Header.Set("User-Agent", s.useragent)
+	}
 
 	r, err := s.http.Do(req)
 	if err != nil {
@@ -510,7 +519,10 @@ func (s *SimpleStreams) downloadFile(path string, hash string, target string, pr
 		if err != nil {
 			return err
 		}
-		req.Header.Set("User-Agent", shared.UserAgent)
+
+		if s.useragent != "" {
+			req.Header.Set("User-Agent", s.useragent)
+		}
 
 		resp, err := s.http.Do(req)
 		if err != nil {

From 1c7104aeccc1dc7a360a16aee4f9f92e9d97c983 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Fri, 16 Dec 2016 15:36:53 -0500
Subject: [PATCH 2/3] shared: Give version handling its own package
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>
---
 Makefile                  |  2 +-
 client.go                 | 33 +++++++++++++++++----------------
 lxc/launch.go             |  7 ++++---
 lxc/version.go            |  4 ++--
 lxd/api_1.0.go            |  5 +++--
 lxd/certificates.go       |  5 +++--
 lxd/container_exec.go     |  5 +++--
 lxd/container_logs.go     |  3 ++-
 lxd/container_snapshot.go |  3 ++-
 lxd/containers_get.go     |  3 ++-
 lxd/daemon.go             | 11 ++++++-----
 lxd/daemon_images.go      | 13 +++++++------
 lxd/devlxd.go             |  3 ++-
 lxd/images.go             | 13 +++++++------
 lxd/main.go               |  3 ++-
 lxd/networks.go           |  9 +++++----
 lxd/operations.go         |  5 +++--
 lxd/profiles.go           |  9 +++++----
 lxd/remote.go             |  3 ++-
 shared/flex.go            | 14 --------------
 shared/version/flex.go    | 14 ++++++++++++++
 21 files changed, 92 insertions(+), 75 deletions(-)
 delete mode 100644 shared/flex.go
 create mode 100644 shared/version/flex.go

diff --git a/Makefile b/Makefile
index bfb774d..8424ce9 100644
--- a/Makefile
+++ b/Makefile
@@ -7,7 +7,7 @@ POTFILE=po/$(DOMAIN).pot
 # dist is primarily for use when packaging; for development we still manage
 # dependencies via `go get` explicitly.
 # TODO: use git describe for versioning
-VERSION=$(shell grep "var Version" shared/flex.go | cut -d'"' -f2)
+VERSION=$(shell grep "var Version" shared/version/flex.go | cut -d'"' -f2)
 ARCHIVE=lxd-$(VERSION).tar
 
 .PHONY: default
diff --git a/client.go b/client.go
index 11c0198..f7d1a6f 100644
--- a/client.go
+++ b/client.go
@@ -26,6 +26,7 @@ import (
 	"github.com/lxc/lxd/shared"
 	"github.com/lxc/lxd/shared/ioprogress"
 	"github.com/lxc/lxd/shared/simplestreams"
+	"github.com/lxc/lxd/shared/version"
 )
 
 // Client can talk to a LXD daemon.
@@ -352,7 +353,7 @@ func NewClientFromInfo(info ConnectInfo) (*Client, error) {
 		}
 		c.Http.Transport = tr
 
-		ss := simplestreams.NewClient(c.Remote.Addr, c.Http, shared.UserAgent)
+		ss := simplestreams.NewClient(c.Remote.Addr, c.Http, version.UserAgent)
 		c.simplestreams = ss
 	}
 
@@ -392,7 +393,7 @@ func (c *Client) Addresses() ([]string, error) {
 }
 
 func (c *Client) get(base string) (*Response, error) {
-	uri := c.url(shared.APIVersion, base)
+	uri := c.url(version.APIVersion, base)
 
 	return c.baseGet(uri)
 }
@@ -403,7 +404,7 @@ func (c *Client) baseGet(getUrl string) (*Response, error) {
 		return nil, err
 	}
 
-	req.Header.Set("User-Agent", shared.UserAgent)
+	req.Header.Set("User-Agent", version.UserAgent)
 
 	resp, err := c.Http.Do(req)
 	if err != nil {
@@ -414,7 +415,7 @@ func (c *Client) baseGet(getUrl string) (*Response, error) {
 }
 
 func (c *Client) doUpdateMethod(method string, base string, args interface{}, rtype ResponseType) (*Response, error) {
-	uri := c.url(shared.APIVersion, base)
+	uri := c.url(version.APIVersion, base)
 
 	buf := bytes.Buffer{}
 	err := json.NewEncoder(&buf).Encode(args)
@@ -428,7 +429,7 @@ func (c *Client) doUpdateMethod(method string, base string, args interface{}, rt
 	if err != nil {
 		return nil, err
 	}
-	req.Header.Set("User-Agent", shared.UserAgent)
+	req.Header.Set("User-Agent", version.UserAgent)
 	req.Header.Set("Content-Type", "application/json")
 
 	resp, err := c.Http.Do(req)
@@ -460,7 +461,7 @@ func (c *Client) getRaw(uri string) (*http.Response, error) {
 	if err != nil {
 		return nil, err
 	}
-	req.Header.Set("User-Agent", shared.UserAgent)
+	req.Header.Set("User-Agent", version.UserAgent)
 
 	raw, err := c.Http.Do(req)
 	if err != nil {
@@ -521,7 +522,7 @@ func (c *Client) GetServerConfig() (*Response, error) {
 		return nil, fmt.Errorf("This function isn't supported by simplestreams remote.")
 	}
 
-	return c.baseGet(c.url(shared.APIVersion))
+	return c.baseGet(c.url(version.APIVersion))
 }
 
 // GetLocalLXDErr determines whether or not an error is likely due to a
@@ -769,7 +770,7 @@ func (c *Client) ExportImage(image string, target string) (string, error) {
 		return c.simplestreams.ExportImage(image, target)
 	}
 
-	uri := c.url(shared.APIVersion, "images", image, "export")
+	uri := c.url(version.APIVersion, "images", image, "export")
 	raw, err := c.getRaw(uri)
 	if err != nil {
 		return "", err
@@ -977,7 +978,7 @@ func (c *Client) PostImage(imageFile string, rootfsFile string, properties []str
 		return "", fmt.Errorf("This function isn't supported by public remotes.")
 	}
 
-	uri := c.url(shared.APIVersion, "images")
+	uri := c.url(version.APIVersion, "images")
 
 	var err error
 	var fImage *os.File
@@ -1077,7 +1078,7 @@ func (c *Client) PostImage(imageFile string, rootfsFile string, properties []str
 	if err != nil {
 		return "", err
 	}
-	req.Header.Set("User-Agent", shared.UserAgent)
+	req.Header.Set("User-Agent", version.UserAgent)
 
 	if public {
 		req.Header.Set("X-LXD-public", "1")
@@ -1739,7 +1740,7 @@ func (c *Client) GetLog(container string, log string) (io.Reader, error) {
 		return nil, fmt.Errorf("This function isn't supported by public remotes.")
 	}
 
-	uri := c.url(shared.APIVersion, "containers", container, "logs", log)
+	uri := c.url(version.APIVersion, "containers", container, "logs", log)
 	resp, err := c.getRaw(uri)
 	if err != nil {
 		return nil, err
@@ -1773,13 +1774,13 @@ func (c *Client) PushFile(container string, p string, gid int, uid int, mode str
 	}
 
 	query := url.Values{"path": []string{p}}
-	uri := c.url(shared.APIVersion, "containers", container, "files") + "?" + query.Encode()
+	uri := c.url(version.APIVersion, "containers", container, "files") + "?" + query.Encode()
 
 	req, err := http.NewRequest("POST", uri, buf)
 	if err != nil {
 		return err
 	}
-	req.Header.Set("User-Agent", shared.UserAgent)
+	req.Header.Set("User-Agent", version.UserAgent)
 	req.Header.Set("X-LXD-type", "file")
 
 	if mode != "" {
@@ -1807,14 +1808,14 @@ func (c *Client) Mkdir(container string, p string, mode os.FileMode) error {
 	}
 
 	query := url.Values{"path": []string{p}}
-	uri := c.url(shared.APIVersion, "containers", container, "files") + "?" + query.Encode()
+	uri := c.url(version.APIVersion, "containers", container, "files") + "?" + query.Encode()
 
 	req, err := http.NewRequest("POST", uri, nil)
 	if err != nil {
 		return err
 	}
 
-	req.Header.Set("User-Agent", shared.UserAgent)
+	req.Header.Set("User-Agent", version.UserAgent)
 	req.Header.Set("X-LXD-type", "directory")
 	req.Header.Set("X-LXD-mode", fmt.Sprintf("%04o", mode.Perm()))
 
@@ -1906,7 +1907,7 @@ func (c *Client) PullFile(container string, p string) (int, int, int, string, io
 		return 0, 0, 0, "", nil, nil, fmt.Errorf("This function isn't supported by public remotes.")
 	}
 
-	uri := c.url(shared.APIVersion, "containers", container, "files")
+	uri := c.url(version.APIVersion, "containers", container, "files")
 	query := url.Values{"path": []string{p}}
 
 	r, err := c.getRaw(uri + "?" + query.Encode())
diff --git a/lxc/launch.go b/lxc/launch.go
index 0c7ea84..f570b82 100644
--- a/lxc/launch.go
+++ b/lxc/launch.go
@@ -8,6 +8,7 @@ import (
 	"github.com/lxc/lxd/shared"
 	"github.com/lxc/lxd/shared/gnuflag"
 	"github.com/lxc/lxd/shared/i18n"
+	"github.com/lxc/lxd/shared/version"
 )
 
 type launchCmd struct {
@@ -117,9 +118,9 @@ func (c *launchCmd) run(config *lxd.Config, args []string) error {
 			return fmt.Errorf(i18n.G("didn't get any affected image, container or snapshot from server"))
 		}
 
-		var version string
+		var restVersion string
 		toScan := strings.Replace(containers[0], "/", " ", -1)
-		count, err := fmt.Sscanf(toScan, " %s containers %s", &version, &name)
+		count, err := fmt.Sscanf(toScan, " %s containers %s", &restVersion, &name)
 		if err != nil {
 			return err
 		}
@@ -128,7 +129,7 @@ func (c *launchCmd) run(config *lxd.Config, args []string) error {
 			return fmt.Errorf(i18n.G("bad number of things scanned from image, container or snapshot"))
 		}
 
-		if version != shared.APIVersion {
+		if restVersion != version.APIVersion {
 			return fmt.Errorf(i18n.G("got bad version"))
 		}
 	}
diff --git a/lxc/version.go b/lxc/version.go
index bc25737..973eede 100644
--- a/lxc/version.go
+++ b/lxc/version.go
@@ -4,8 +4,8 @@ import (
 	"fmt"
 
 	"github.com/lxc/lxd"
-	"github.com/lxc/lxd/shared"
 	"github.com/lxc/lxd/shared/i18n"
+	"github.com/lxc/lxd/shared/version"
 )
 
 type versionCmd struct{}
@@ -28,6 +28,6 @@ func (c *versionCmd) run(_ *lxd.Config, args []string) error {
 	if len(args) > 0 {
 		return errArgs
 	}
-	fmt.Println(shared.Version)
+	fmt.Println(version.Version)
 	return nil
 }
diff --git a/lxd/api_1.0.go b/lxd/api_1.0.go
index 3117106..43f692a 100644
--- a/lxd/api_1.0.go
+++ b/lxd/api_1.0.go
@@ -12,6 +12,7 @@ import (
 
 	"github.com/lxc/lxd/shared"
 	"github.com/lxc/lxd/shared/osarch"
+	"github.com/lxc/lxd/shared/version"
 )
 
 var api10 = []Command{
@@ -85,7 +86,7 @@ func api10Get(d *Daemon, r *http.Request) Response {
 		},
 
 		"api_status":  "stable",
-		"api_version": shared.APIVersion,
+		"api_version": version.APIVersion,
 	}
 
 	if d.isTrustedClient(r) {
@@ -160,7 +161,7 @@ func api10Get(d *Daemon, r *http.Request) Response {
 			"storage_version":     d.Storage.GetStorageTypeVersion(),
 			"server":              "lxd",
 			"server_pid":          os.Getpid(),
-			"server_version":      shared.Version}
+			"server_version":      version.Version}
 
 		body["environment"] = env
 		body["public"] = false
diff --git a/lxd/certificates.go b/lxd/certificates.go
index ea75222..e31a0d2 100644
--- a/lxd/certificates.go
+++ b/lxd/certificates.go
@@ -12,6 +12,7 @@ import (
 	"github.com/gorilla/mux"
 
 	"github.com/lxc/lxd/shared"
+	"github.com/lxc/lxd/shared/version"
 )
 
 func certificatesGet(d *Daemon, r *http.Request) Response {
@@ -40,7 +41,7 @@ func certificatesGet(d *Daemon, r *http.Request) Response {
 
 	body := []string{}
 	for _, cert := range d.clientCerts {
-		fingerprint := fmt.Sprintf("/%s/certificates/%s", shared.APIVersion, shared.CertFingerprint(&cert))
+		fingerprint := fmt.Sprintf("/%s/certificates/%s", version.APIVersion, shared.CertFingerprint(&cert))
 		body = append(body, fingerprint)
 	}
 
@@ -151,7 +152,7 @@ func certificatesPost(d *Daemon, r *http.Request) Response {
 
 	d.clientCerts = append(d.clientCerts, *cert)
 
-	return SyncResponseLocation(true, nil, fmt.Sprintf("/%s/certificates/%s", shared.APIVersion, fingerprint))
+	return SyncResponseLocation(true, nil, fmt.Sprintf("/%s/certificates/%s", version.APIVersion, fingerprint))
 }
 
 var certificatesCmd = Command{name: "certificates", untrustedPost: true, get: certificatesGet, post: certificatesPost}
diff --git a/lxd/container_exec.go b/lxd/container_exec.go
index 9dead34..6161dec 100644
--- a/lxd/container_exec.go
+++ b/lxd/container_exec.go
@@ -16,6 +16,7 @@ import (
 	"github.com/gorilla/websocket"
 
 	"github.com/lxc/lxd/shared"
+	"github.com/lxc/lxd/shared/version"
 
 	log "gopkg.in/inconshreveable/log15.v2"
 )
@@ -416,8 +417,8 @@ func containerExecPost(d *Daemon, r *http.Request) Response {
 			// Update metadata with the right URLs
 			metadata["return"] = cmdResult
 			metadata["output"] = shared.Jmap{
-				"1": fmt.Sprintf("/%s/containers/%s/logs/%s", shared.APIVersion, c.Name(), filepath.Base(stdout.Name())),
-				"2": fmt.Sprintf("/%s/containers/%s/logs/%s", shared.APIVersion, c.Name(), filepath.Base(stderr.Name())),
+				"1": fmt.Sprintf("/%s/containers/%s/logs/%s", version.APIVersion, c.Name(), filepath.Base(stdout.Name())),
+				"2": fmt.Sprintf("/%s/containers/%s/logs/%s", version.APIVersion, c.Name(), filepath.Base(stderr.Name())),
 			}
 		} else {
 			cmdResult, _, cmdErr = c.Exec(post.Command, env, nil, nil, nil, true)
diff --git a/lxd/container_logs.go b/lxd/container_logs.go
index e86df94..963283e 100644
--- a/lxd/container_logs.go
+++ b/lxd/container_logs.go
@@ -10,6 +10,7 @@ import (
 	"github.com/gorilla/mux"
 
 	"github.com/lxc/lxd/shared"
+	"github.com/lxc/lxd/shared/version"
 )
 
 func containerLogsGet(d *Daemon, r *http.Request) Response {
@@ -38,7 +39,7 @@ func containerLogsGet(d *Daemon, r *http.Request) Response {
 			continue
 		}
 
-		result = append(result, fmt.Sprintf("/%s/containers/%s/logs/%s", shared.APIVersion, name, f.Name()))
+		result = append(result, fmt.Sprintf("/%s/containers/%s/logs/%s", version.APIVersion, name, f.Name()))
 	}
 
 	return SyncResponse(true, result)
diff --git a/lxd/container_snapshot.go b/lxd/container_snapshot.go
index a2c1ca6..bc75729 100644
--- a/lxd/container_snapshot.go
+++ b/lxd/container_snapshot.go
@@ -10,6 +10,7 @@ import (
 	"github.com/gorilla/mux"
 
 	"github.com/lxc/lxd/shared"
+	"github.com/lxc/lxd/shared/version"
 )
 
 type containerSnapshotPostReq struct {
@@ -41,7 +42,7 @@ func containerSnapshotsGet(d *Daemon, r *http.Request) Response {
 	for _, snap := range snaps {
 		snapName := strings.SplitN(snap.Name(), shared.SnapshotDelimiter, 2)[1]
 		if recursion == 0 {
-			url := fmt.Sprintf("/%s/containers/%s/snapshots/%s", shared.APIVersion, cname, snapName)
+			url := fmt.Sprintf("/%s/containers/%s/snapshots/%s", version.APIVersion, cname, snapName)
 			resultString = append(resultString, url)
 		} else {
 			render, _, err := snap.Render()
diff --git a/lxd/containers_get.go b/lxd/containers_get.go
index 55d9fd5..5c54107 100644
--- a/lxd/containers_get.go
+++ b/lxd/containers_get.go
@@ -6,6 +6,7 @@ import (
 	"time"
 
 	"github.com/lxc/lxd/shared"
+	"github.com/lxc/lxd/shared/version"
 )
 
 func containersGet(d *Daemon, r *http.Request) Response {
@@ -42,7 +43,7 @@ func doContainersGet(d *Daemon, recursion bool) (interface{}, error) {
 
 	for _, container := range result {
 		if !recursion {
-			url := fmt.Sprintf("/%s/containers/%s", shared.APIVersion, container)
+			url := fmt.Sprintf("/%s/containers/%s", version.APIVersion, container)
 			resultString = append(resultString, url)
 		} else {
 			c, err := doContainerGet(d, container)
diff --git a/lxd/daemon.go b/lxd/daemon.go
index d2dcd31..2414cc0 100644
--- a/lxd/daemon.go
+++ b/lxd/daemon.go
@@ -33,6 +33,7 @@ import (
 	"github.com/lxc/lxd/shared"
 	"github.com/lxc/lxd/shared/logging"
 	"github.com/lxc/lxd/shared/osarch"
+	"github.com/lxc/lxd/shared/version"
 
 	log "gopkg.in/inconshreveable/log15.v2"
 )
@@ -151,7 +152,7 @@ func (d *Daemon) httpGetSync(url string, certificate string) (*lxd.Response, err
 		return nil, err
 	}
 
-	req.Header.Set("User-Agent", shared.UserAgent)
+	req.Header.Set("User-Agent", version.UserAgent)
 
 	myhttp, err := d.httpClient(certificate)
 	if err != nil {
@@ -188,7 +189,7 @@ func (d *Daemon) httpGetFile(url string, certificate string) (*http.Response, er
 		return nil, err
 	}
 
-	req.Header.Set("User-Agent", shared.UserAgent)
+	req.Header.Set("User-Agent", version.UserAgent)
 
 	raw, err := myhttp.Do(req)
 	if err != nil {
@@ -565,13 +566,13 @@ func (d *Daemon) Init() error {
 
 	/* Print welcome message */
 	if d.MockMode {
-		shared.LogInfo(fmt.Sprintf("LXD %s is starting in mock mode", shared.Version),
+		shared.LogInfo(fmt.Sprintf("LXD %s is starting in mock mode", version.Version),
 			log.Ctx{"path": shared.VarPath("")})
 	} else if d.SetupMode {
-		shared.LogInfo(fmt.Sprintf("LXD %s is starting in setup mode", shared.Version),
+		shared.LogInfo(fmt.Sprintf("LXD %s is starting in setup mode", version.Version),
 			log.Ctx{"path": shared.VarPath("")})
 	} else {
-		shared.LogInfo(fmt.Sprintf("LXD %s is starting in normal mode", shared.Version),
+		shared.LogInfo(fmt.Sprintf("LXD %s is starting in normal mode", version.Version),
 			log.Ctx{"path": shared.VarPath("")})
 	}
 
diff --git a/lxd/daemon_images.go b/lxd/daemon_images.go
index 6afe492..bcc1ec9 100644
--- a/lxd/daemon_images.go
+++ b/lxd/daemon_images.go
@@ -18,6 +18,7 @@ import (
 	"github.com/lxc/lxd/shared"
 	"github.com/lxc/lxd/shared/ioprogress"
 	"github.com/lxc/lxd/shared/simplestreams"
+	"github.com/lxc/lxd/shared/version"
 
 	log "gopkg.in/inconshreveable/log15.v2"
 )
@@ -72,7 +73,7 @@ func imageLoadStreamCache(d *Daemon) error {
 				return err
 			}
 
-			ss := simplestreams.NewClient(url, *myhttp, shared.UserAgent)
+			ss := simplestreams.NewClient(url, *myhttp, version.UserAgent)
 			entry.ss = ss
 		}
 	}
@@ -105,7 +106,7 @@ func (d *Daemon) ImageDownload(op *operation, server string, protocol string, ce
 					return nil, err
 				}
 
-				ss = simplestreams.NewClient(server, *myhttp, shared.UserAgent)
+				ss = simplestreams.NewClient(server, *myhttp, version.UserAgent)
 
 				// Get all aliases
 				aliases, err := ss.ListAliases()
@@ -276,9 +277,9 @@ func (d *Daemon) ImageDownload(op *operation, server string, protocol string, ce
 		if secret != "" {
 			url = fmt.Sprintf(
 				"%s/%s/images/%s?secret=%s",
-				server, shared.APIVersion, fp, secret)
+				server, version.APIVersion, fp, secret)
 		} else {
-			url = fmt.Sprintf("%s/%s/images/%s", server, shared.APIVersion, fp)
+			url = fmt.Sprintf("%s/%s/images/%s", server, version.APIVersion, fp)
 		}
 
 		resp, err := d.httpGetSync(url, certificate)
@@ -298,12 +299,12 @@ func (d *Daemon) ImageDownload(op *operation, server string, protocol string, ce
 		if secret != "" {
 			exporturl = fmt.Sprintf(
 				"%s/%s/images/%s/export?secret=%s",
-				server, shared.APIVersion, fp, secret)
+				server, version.APIVersion, fp, secret)
 
 		} else {
 			exporturl = fmt.Sprintf(
 				"%s/%s/images/%s/export",
-				server, shared.APIVersion, fp)
+				server, version.APIVersion, fp)
 		}
 	} else if protocol == "simplestreams" {
 		err := ss.Download(fp, "meta", destName, nil)
diff --git a/lxd/devlxd.go b/lxd/devlxd.go
index af59000..bf4edc8 100644
--- a/lxd/devlxd.go
+++ b/lxd/devlxd.go
@@ -16,6 +16,7 @@ import (
 	"github.com/gorilla/mux"
 
 	"github.com/lxc/lxd/shared"
+	"github.com/lxc/lxd/shared/version"
 )
 
 type devLxdResponse struct {
@@ -74,7 +75,7 @@ var handlers = []devLxdHandler{
 		return okResponse([]string{"/1.0"}, "json")
 	}},
 	devLxdHandler{"/1.0", func(c container, r *http.Request) *devLxdResponse {
-		return okResponse(shared.Jmap{"api_version": shared.APIVersion}, "json")
+		return okResponse(shared.Jmap{"api_version": version.APIVersion}, "json")
 	}},
 	configGet,
 	configKeyGet,
diff --git a/lxd/images.go b/lxd/images.go
index c6724e1..12e49a1 100644
--- a/lxd/images.go
+++ b/lxd/images.go
@@ -25,6 +25,7 @@ import (
 	"github.com/lxc/lxd/shared"
 	"github.com/lxc/lxd/shared/logging"
 	"github.com/lxc/lxd/shared/osarch"
+	"github.com/lxc/lxd/shared/version"
 
 	log "gopkg.in/inconshreveable/log15.v2"
 )
@@ -392,9 +393,9 @@ func imgPostURLInfo(d *Daemon, req imagePostReq, op *operation) error {
 		architecturesStr = append(architecturesStr, fmt.Sprintf("%d", arch))
 	}
 
-	head.Header.Set("User-Agent", shared.UserAgent)
+	head.Header.Set("User-Agent", version.UserAgent)
 	head.Header.Set("LXD-Server-Architectures", strings.Join(architecturesStr, ", "))
-	head.Header.Set("LXD-Server-Version", shared.Version)
+	head.Header.Set("LXD-Server-Version", version.Version)
 
 	raw, err := myhttp.Do(head)
 	if err != nil {
@@ -828,7 +829,7 @@ func doImagesGet(d *Daemon, recursion bool, public bool) (interface{}, error) {
 	i := 0
 	for _, name := range results {
 		if !recursion {
-			url := fmt.Sprintf("/%s/images/%s", shared.APIVersion, name)
+			url := fmt.Sprintf("/%s/images/%s", version.APIVersion, name)
 			resultString[i] = url
 		} else {
 			image, response := doImageGet(d, name, public)
@@ -1202,7 +1203,7 @@ func aliasesPost(d *Daemon, r *http.Request) Response {
 		return InternalError(err)
 	}
 
-	return SyncResponseLocation(true, nil, fmt.Sprintf("/%s/images/aliases/%s", shared.APIVersion, req.Name))
+	return SyncResponseLocation(true, nil, fmt.Sprintf("/%s/images/aliases/%s", version.APIVersion, req.Name))
 }
 
 func aliasesGet(d *Daemon, r *http.Request) Response {
@@ -1221,7 +1222,7 @@ func aliasesGet(d *Daemon, r *http.Request) Response {
 	for _, res := range results {
 		name = res[0].(string)
 		if !recursion {
-			url := fmt.Sprintf("/%s/images/aliases/%s", shared.APIVersion, name)
+			url := fmt.Sprintf("/%s/images/aliases/%s", version.APIVersion, name)
 			responseStr = append(responseStr, url)
 
 		} else {
@@ -1378,7 +1379,7 @@ func aliasPost(d *Daemon, r *http.Request) Response {
 		return SmartError(err)
 	}
 
-	return SyncResponseLocation(true, nil, fmt.Sprintf("/%s/images/aliases/%s", shared.APIVersion, req.Name))
+	return SyncResponseLocation(true, nil, fmt.Sprintf("/%s/images/aliases/%s", version.APIVersion, req.Name))
 }
 
 func imageExport(d *Daemon, r *http.Request) Response {
diff --git a/lxd/main.go b/lxd/main.go
index 327df27..52f4972 100644
--- a/lxd/main.go
+++ b/lxd/main.go
@@ -9,6 +9,7 @@ import (
 	"github.com/lxc/lxd/shared"
 	"github.com/lxc/lxd/shared/gnuflag"
 	"github.com/lxc/lxd/shared/logging"
+	"github.com/lxc/lxd/shared/version"
 )
 
 // Global arguments
@@ -168,7 +169,7 @@ func run() error {
 
 	// Deal with --version right here
 	if *argVersion {
-		fmt.Println(shared.Version)
+		fmt.Println(version.Version)
 		return nil
 	}
 
diff --git a/lxd/networks.go b/lxd/networks.go
index 62ed2b9..bb5d3c6 100644
--- a/lxd/networks.go
+++ b/lxd/networks.go
@@ -16,6 +16,7 @@ import (
 	log "gopkg.in/inconshreveable/log15.v2"
 
 	"github.com/lxc/lxd/shared"
+	"github.com/lxc/lxd/shared/version"
 )
 
 // API endpoints
@@ -35,7 +36,7 @@ func networksGet(d *Daemon, r *http.Request) Response {
 	resultMap := []shared.NetworkConfig{}
 	for _, iface := range ifs {
 		if recursion == 0 {
-			resultString = append(resultString, fmt.Sprintf("/%s/networks/%s", shared.APIVersion, iface))
+			resultString = append(resultString, fmt.Sprintf("/%s/networks/%s", version.APIVersion, iface))
 		} else {
 			net, err := doNetworkGet(d, iface)
 			if err != nil {
@@ -142,7 +143,7 @@ func networksPost(d *Daemon, r *http.Request) Response {
 		return InternalError(err)
 	}
 
-	return SyncResponseLocation(true, nil, fmt.Sprintf("/%s/networks/%s", shared.APIVersion, req.Name))
+	return SyncResponseLocation(true, nil, fmt.Sprintf("/%s/networks/%s", version.APIVersion, req.Name))
 }
 
 var networksCmd = Command{name: "networks", get: networksGet, post: networksPost}
@@ -189,7 +190,7 @@ func doNetworkGet(d *Daemon, name string) (shared.NetworkConfig, error) {
 		}
 
 		if networkIsInUse(c, n.Name) {
-			n.UsedBy = append(n.UsedBy, fmt.Sprintf("/%s/containers/%s", shared.APIVersion, ct))
+			n.UsedBy = append(n.UsedBy, fmt.Sprintf("/%s/containers/%s", version.APIVersion, ct))
 		}
 	}
 
@@ -284,7 +285,7 @@ func networkPost(d *Daemon, r *http.Request) Response {
 		return SmartError(err)
 	}
 
-	return SyncResponseLocation(true, nil, fmt.Sprintf("/%s/networks/%s", shared.APIVersion, req.Name))
+	return SyncResponseLocation(true, nil, fmt.Sprintf("/%s/networks/%s", version.APIVersion, req.Name))
 }
 
 func networkPut(d *Daemon, r *http.Request) Response {
diff --git a/lxd/operations.go b/lxd/operations.go
index 8e54263..52df1ec 100644
--- a/lxd/operations.go
+++ b/lxd/operations.go
@@ -12,6 +12,7 @@ import (
 	"github.com/pborman/uuid"
 
 	"github.com/lxc/lxd/shared"
+	"github.com/lxc/lxd/shared/version"
 )
 
 var operationsLock sync.Mutex
@@ -252,7 +253,7 @@ func (op *operation) Render() (string, *shared.Operation, error) {
 		for key, value := range resources {
 			var values []string
 			for _, c := range value {
-				values = append(values, fmt.Sprintf("/%s/%s/%s", shared.APIVersion, key, c))
+				values = append(values, fmt.Sprintf("/%s/%s/%s", version.APIVersion, key, c))
 			}
 			tmpResources[key] = values
 		}
@@ -365,7 +366,7 @@ func operationCreate(opClass operationClass, opResources map[string][]string, op
 	op.createdAt = time.Now()
 	op.updatedAt = op.createdAt
 	op.status = shared.Pending
-	op.url = fmt.Sprintf("/%s/operations/%s", shared.APIVersion, op.id)
+	op.url = fmt.Sprintf("/%s/operations/%s", version.APIVersion, op.id)
 	op.resources = opResources
 	op.chanDone = make(chan error)
 
diff --git a/lxd/profiles.go b/lxd/profiles.go
index 4babe81..3c7d08b 100644
--- a/lxd/profiles.go
+++ b/lxd/profiles.go
@@ -13,6 +13,7 @@ import (
 	_ "github.com/mattn/go-sqlite3"
 
 	"github.com/lxc/lxd/shared"
+	"github.com/lxc/lxd/shared/version"
 
 	log "gopkg.in/inconshreveable/log15.v2"
 )
@@ -38,7 +39,7 @@ func profilesGet(d *Daemon, r *http.Request) Response {
 	i := 0
 	for _, name := range results {
 		if !recursion {
-			url := fmt.Sprintf("/%s/profiles/%s", shared.APIVersion, name)
+			url := fmt.Sprintf("/%s/profiles/%s", version.APIVersion, name)
 			resultString[i] = url
 		} else {
 			profile, err := doProfileGet(d, name)
@@ -99,7 +100,7 @@ func profilesPost(d *Daemon, r *http.Request) Response {
 			fmt.Errorf("Error inserting %s into database: %s", req.Name, err))
 	}
 
-	return SyncResponseLocation(true, nil, fmt.Sprintf("/%s/profiles/%s", shared.APIVersion, req.Name))
+	return SyncResponseLocation(true, nil, fmt.Sprintf("/%s/profiles/%s", version.APIVersion, req.Name))
 }
 
 var profilesCmd = Command{
@@ -120,7 +121,7 @@ func doProfileGet(d *Daemon, name string) (*shared.ProfileConfig, error) {
 
 	usedBy := []string{}
 	for _, ct := range cts {
-		usedBy = append(usedBy, fmt.Sprintf("/%s/containers/%s", shared.APIVersion, ct))
+		usedBy = append(usedBy, fmt.Sprintf("/%s/containers/%s", version.APIVersion, ct))
 	}
 	profile.UsedBy = usedBy
 
@@ -366,7 +367,7 @@ func profilePost(d *Daemon, r *http.Request) Response {
 		return InternalError(err)
 	}
 
-	return SyncResponseLocation(true, nil, fmt.Sprintf("/%s/profiles/%s", shared.APIVersion, req.Name))
+	return SyncResponseLocation(true, nil, fmt.Sprintf("/%s/profiles/%s", version.APIVersion, req.Name))
 }
 
 // The handler for the delete operation.
diff --git a/lxd/remote.go b/lxd/remote.go
index 9469ce2..ca7158a 100644
--- a/lxd/remote.go
+++ b/lxd/remote.go
@@ -5,12 +5,13 @@ import (
 	"fmt"
 
 	"github.com/lxc/lxd/shared"
+	"github.com/lxc/lxd/shared/version"
 )
 
 func remoteGetImageFingerprint(d *Daemon, server string, certificate string, alias string) (string, error) {
 	url := fmt.Sprintf(
 		"%s/%s/images/aliases/%s",
-		server, shared.APIVersion, alias)
+		server, version.APIVersion, alias)
 
 	resp, err := d.httpGetSync(url, certificate)
 	if err != nil {
diff --git a/shared/flex.go b/shared/flex.go
deleted file mode 100644
index 1ac5459..0000000
--- a/shared/flex.go
+++ /dev/null
@@ -1,14 +0,0 @@
-/* This is a FLEXible file which can be used by both client and daemon.
- * Teehee.
- */
-package shared
-
-var Version = "2.6.2"
-var UserAgent = "LXD " + Version
-
-/*
- * Please increment the api compat number every time you change the API.
- *
- * Version 1.0: ping
- */
-var APIVersion = "1.0"
diff --git a/shared/version/flex.go b/shared/version/flex.go
new file mode 100644
index 0000000..8076729
--- /dev/null
+++ b/shared/version/flex.go
@@ -0,0 +1,14 @@
+/* This is a FLEXible file which can be used by both client and daemon.
+ * Teehee.
+ */
+package version
+
+var Version = "2.6.2"
+var UserAgent = "LXD " + Version
+
+/*
+ * Please increment the api compat number every time you change the API.
+ *
+ * Version 1.0: ping
+ */
+var APIVersion = "1.0"

From 77023591b5dd74417adf9ff8be4e56f16bec0f18 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Fri, 16 Dec 2016 16:21:49 -0500
Subject: [PATCH 3/3] Makefile: Rework "make dist"
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

This rework makes everything happen inside the temp directory, always
use the current HEAD of whatever git branch it's called from and doesn't
cause a re-download of the LXD tree.

Signed-off-by: Stéphane Graber <stgraber at ubuntu.com>
---
 Makefile | 35 ++++++++++++++++++++++-------------
 1 file changed, 22 insertions(+), 13 deletions(-)

diff --git a/Makefile b/Makefile
index 8424ce9..09c537e 100644
--- a/Makefile
+++ b/Makefile
@@ -56,20 +56,29 @@ gccgo:
 
 .PHONY: dist
 dist:
+	# Cleanup
+	rm -Rf $(ARCHIVE).gz
+	
+	# Create build dir
 	$(eval TMP := $(shell mktemp -d))
-	rm -Rf lxd-$(VERSION) $(ARCHIVE) $(ARCHIVE).gz
-	mkdir -p lxd-$(VERSION)/
-	-GOPATH=$(TMP) go get -t -v -d ./...
-	-GOPATH=$(TMP) go get -t -v -d ./...
-	-GOPATH=$(TMP) go get -t -v -d ./...
-	GOPATH=$(TMP) go get -t -v -d ./...
-	rm -rf $(TMP)/src/github.com/lxc/lxd
-	ln -s ../../../.. $(TMP)/src/github.com/lxc/lxd
-	mv $(TMP)/ lxd-$(VERSION)/dist
-	git archive --prefix=lxd-$(VERSION)/ --output=$(ARCHIVE) HEAD
-	tar -uf $(ARCHIVE) --exclude-vcs lxd-$(VERSION)/
-	gzip -9 $(ARCHIVE)
-	rm -Rf lxd-$(VERSION) $(ARCHIVE)
+	git archive --prefix=lxd-$(VERSION)/ HEAD | tar -x -C $(TMP)
+	mkdir -p $(TMP)/dist/src/github.com/lxc
+	ln -s ../../../../lxd-$(VERSION) $(TMP)/dist/src/github.com/lxc/lxd
+	
+	# Download dependencies
+	-cd $(TMP)/lxd-$(VERSION) && GOPATH=$(TMP)/dist go get -t -v -d ./...
+	-cd $(TMP)/lxd-$(VERSION) && GOPATH=$(TMP)/dist go get -t -v -d ./...
+	-cd $(TMP)/lxd-$(VERSION) && GOPATH=$(TMP)/dist go get -t -v -d ./...
+	cd $(TMP)/lxd-$(VERSION) && GOPATH=$(TMP)/dist go get -t -v -d ./...
+	
+	# Assemble tarball
+	rm $(TMP)/dist/src/github.com/lxc/lxd
+	ln -s ../../../../ $(TMP)/dist/src/github.com/lxc/lxd
+	mv $(TMP)/dist $(TMP)/lxd-$(VERSION)/
+	tar --exclude-vcs -C $(TMP) -zcf $(ARCHIVE).gz lxd-$(VERSION)/
+	
+	# Cleanup
+	rm -Rf $(TMP)
 
 .PHONY: i18n update-po update-pot build-mo static-analysis
 i18n: update-pot


More information about the lxc-devel mailing list