[lxc-devel] [lxd/master] Some patches in prep for clustering
tych0 on Github
lxc-bot at linuxcontainers.org
Wed Dec 14 15:11:51 UTC 2016
A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 385 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20161214/8ee1c602/attachment.bin>
-------------- next part --------------
From c9e723f295621645124ff3978c028b782de3f6aa Mon Sep 17 00:00:00 2001
From: Tycho Andersen <tycho.andersen at canonical.com>
Date: Thu, 1 Dec 2016 09:03:26 -0700
Subject: [PATCH 1/5] client: commonize update methods and add PATCH
Signed-off-by: Tycho Andersen <tycho.andersen at canonical.com>
---
client.go | 64 ++++++++++++++-------------------------------------------------
1 file changed, 14 insertions(+), 50 deletions(-)
diff --git a/client.go b/client.go
index 67ce9c2..a4265aa 100644
--- a/client.go
+++ b/client.go
@@ -403,7 +403,7 @@ func (c *Client) baseGet(getUrl string) (*Response, error) {
return HoistResponse(resp, Sync)
}
-func (c *Client) put(base string, args interface{}, rtype ResponseType) (*Response, error) {
+func (c *Client) doUpdateMethod(method string, base string, args interface{}, rtype ResponseType) (*Response, error) {
uri := c.url(shared.APIVersion, base)
buf := bytes.Buffer{}
@@ -412,9 +412,9 @@ func (c *Client) put(base string, args interface{}, rtype ResponseType) (*Respon
return nil, err
}
- shared.LogDebugf("Putting %s to %s", buf.String(), uri)
+ shared.LogDebugf("%s %s to %s", method, buf.String(), uri)
- req, err := http.NewRequest("PUT", uri, &buf)
+ req, err := http.NewRequest(method, uri, &buf)
if err != nil {
return nil, err
}
@@ -429,30 +429,20 @@ func (c *Client) put(base string, args interface{}, rtype ResponseType) (*Respon
return HoistResponse(resp, rtype)
}
-func (c *Client) post(base string, args interface{}, rtype ResponseType) (*Response, error) {
- uri := c.url(shared.APIVersion, base)
-
- buf := bytes.Buffer{}
- err := json.NewEncoder(&buf).Encode(args)
- if err != nil {
- return nil, err
- }
-
- shared.LogDebugf("Posting %s to %s", buf.String(), uri)
+func (c *Client) put(base string, args interface{}, rtype ResponseType) (*Response, error) {
+ return c.doUpdateMethod("PUT", base, args, rtype)
+}
- req, err := http.NewRequest("POST", uri, &buf)
- if err != nil {
- return nil, err
- }
- req.Header.Set("User-Agent", shared.UserAgent)
- req.Header.Set("Content-Type", "application/json")
+func (c *Client) patch(base string, args interface{}, rtype ResponseType) (*Response, error) {
+ return c.doUpdateMethod("PATCH", base, args, rtype)
+}
- resp, err := c.Http.Do(req)
- if err != nil {
- return nil, err
- }
+func (c *Client) post(base string, args interface{}, rtype ResponseType) (*Response, error) {
+ return c.doUpdateMethod("POST", base, args, rtype)
+}
- return HoistResponse(resp, rtype)
+func (c *Client) delete(base string, args interface{}, rtype ResponseType) (*Response, error) {
+ return c.doUpdateMethod("DELETE", base, args, rtype)
}
func (c *Client) getRaw(uri string) (*http.Response, error) {
@@ -479,32 +469,6 @@ func (c *Client) getRaw(uri string) (*http.Response, error) {
return raw, nil
}
-func (c *Client) delete(base string, args interface{}, rtype ResponseType) (*Response, error) {
- uri := c.url(shared.APIVersion, base)
-
- buf := bytes.Buffer{}
- err := json.NewEncoder(&buf).Encode(args)
- if err != nil {
- return nil, err
- }
-
- shared.LogDebugf("Deleting %s to %s", buf.String(), uri)
-
- req, err := http.NewRequest("DELETE", uri, &buf)
- if err != nil {
- return nil, err
- }
- req.Header.Set("User-Agent", shared.UserAgent)
- req.Header.Set("Content-Type", "application/json")
-
- resp, err := c.Http.Do(req)
- if err != nil {
- return nil, err
- }
-
- return HoistResponse(resp, rtype)
-}
-
func (c *Client) Websocket(operation string, secret string) (*websocket.Conn, error) {
query := url.Values{"secret": []string{secret}}
url := c.BaseWSURL + path.Join(operation, "websocket") + "?" + query.Encode()
From c07feceae7b28c386c1e8393c6f40f6062a05cd5 Mon Sep 17 00:00:00 2001
From: Tycho Andersen <tycho.andersen at canonical.com>
Date: Fri, 2 Dec 2016 12:24:40 -0700
Subject: [PATCH 2/5] add some extra response types
Signed-off-by: Tycho Andersen <tycho.andersen at canonical.com>
---
lxd/response.go | 23 +++++++++++++++++++++++
1 file changed, 23 insertions(+)
diff --git a/lxd/response.go b/lxd/response.go
index 1116857..235dd3b 100644
--- a/lxd/response.go
+++ b/lxd/response.go
@@ -292,6 +292,29 @@ func PreconditionFailed(err error) Response {
return &errorResponse{http.StatusPreconditionFailed, err.Error()}
}
+func ServiceUnavailable(err error) Response {
+ return &errorResponse{http.StatusServiceUnavailable, err.Error()}
+}
+
+type redirectResponse struct {
+ req *http.Request
+ type_ int
+ url string
+}
+
+func (rr *redirectResponse) Render(w http.ResponseWriter) error {
+ http.Redirect(w, rr.req, rr.url, rr.type_)
+ return nil
+}
+
+func (rr *redirectResponse) String() string {
+ return fmt.Sprintf("redirect (%d) to %s", rr.type_, rr.url)
+}
+
+func Redirect(r *http.Request, url string) Response {
+ return &redirectResponse{r, http.StatusMovedPermanently, url}
+}
+
/*
* SmartError returns the right error message based on err.
*/
From b05b02fb3294f223e16e7469d2927af11ce1a4c5 Mon Sep 17 00:00:00 2001
From: Tycho Andersen <tycho.andersen at canonical.com>
Date: Wed, 7 Dec 2016 16:15:24 +0100
Subject: [PATCH 3/5] tests: only attach lxdbr0 if it is present on the host
Signed-off-by: Tycho Andersen <tycho.andersen at canonical.com>
---
test/main.sh | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)
diff --git a/test/main.sh b/test/main.sh
index f94e85c..e5a88a1 100755
--- a/test/main.sh
+++ b/test/main.sh
@@ -103,7 +103,11 @@ spawn_lxd() {
fi
echo "==> Setting up networking"
- LXD_DIR="${lxddir}" lxc network attach-profile lxdbr0 default eth0
+ bad=0
+ ip link show lxdbr0 || bad=1
+ if [ "${bad}" -eq 0 ]; then
+ LXD_DIR="${lxddir}" lxc network attach-profile lxdbr0 default eth0
+ fi
echo "==> Configuring storage backend"
"$LXD_BACKEND"_configure "${lxddir}"
From 65a3ae2f66e16302733aedbcf65142645d5ab681 Mon Sep 17 00:00:00 2001
From: Tycho Andersen <tycho.andersen at canonical.com>
Date: Wed, 7 Dec 2016 16:15:53 +0100
Subject: [PATCH 4/5] shared: make PrintStack print at the Error level
This is used for debugging, and --debug is very verbose and sometimes I
don't want to have to wade through it. So let's use Error instead :)
Signed-off-by: Tycho Andersen <tycho.andersen at canonical.com>
---
shared/log.go | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/shared/log.go b/shared/log.go
index 792ccf6..f696feb 100644
--- a/shared/log.go
+++ b/shared/log.go
@@ -93,5 +93,5 @@ func LogCritf(format string, args ...interface{}) {
func PrintStack() {
buf := make([]byte, 1<<16)
runtime.Stack(buf, true)
- LogDebugf("%s", buf)
+ LogErrorf("%s", buf)
}
From 9b2bb51fcc72f190727fd32742d63301cfd1e0a8 Mon Sep 17 00:00:00 2001
From: Tycho Andersen <tycho.andersen at canonical.com>
Date: Thu, 8 Dec 2016 17:26:24 +0100
Subject: [PATCH 5/5] make a helper to compute cert fingerprint
Signed-off-by: Tycho Andersen <tycho.andersen at canonical.com>
---
client.go | 9 ++++-----
shared/cert.go | 11 +++++++++++
2 files changed, 15 insertions(+), 5 deletions(-)
diff --git a/client.go b/client.go
index a4265aa..0c45ca5 100644
--- a/client.go
+++ b/client.go
@@ -2,7 +2,6 @@ package lxd
import (
"bytes"
- "crypto/sha256"
"crypto/x509"
"encoding/base64"
"encoding/json"
@@ -1676,10 +1675,10 @@ func (c *Client) ServerStatus() (*shared.ServerState, error) {
// Fill in certificate fingerprint if not provided
if ss.Environment.CertificateFingerprint == "" && ss.Environment.Certificate != "" {
- pemCertificate, _ := pem.Decode([]byte(ss.Environment.Certificate))
- if pemCertificate != nil {
- digest := sha256.Sum256(pemCertificate.Bytes)
- ss.Environment.CertificateFingerprint = fmt.Sprintf("%x", digest)
+ var err error
+ ss.Environment.CertificateFingerprint, err = shared.CertFingerprint(ss.Environment.Certificate)
+ if err != nil {
+ return nil, err
}
}
diff --git a/shared/cert.go b/shared/cert.go
index ebd5604..91be153 100644
--- a/shared/cert.go
+++ b/shared/cert.go
@@ -8,6 +8,7 @@ package shared
import (
"crypto/rand"
"crypto/rsa"
+ "crypto/sha256"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
@@ -211,3 +212,13 @@ func ReadCert(fpath string) (*x509.Certificate, error) {
return x509.ParseCertificate(certBlock.Bytes)
}
+
+func CertFingerprint(c string) (string, error) {
+ pemCertificate, _ := pem.Decode([]byte(c))
+ if pemCertificate != nil {
+ digest := sha256.Sum256(pemCertificate.Bytes)
+ return fmt.Sprintf("%x", digest), nil
+ }
+
+ return "", fmt.Errorf("invalid certificate")
+}
More information about the lxc-devel
mailing list