Making the client use tls if the node from which migration has to be made has enabled tls

This commit is contained in:
Diptanu Choudhury
2016-10-28 17:16:56 -07:00
parent ea68fe13f8
commit afa7e5dca4
2 changed files with 31 additions and 5 deletions

View File

@@ -1,5 +1,7 @@
package api
import "io"
// Raw can be used to do raw queries against custom endpoints
type Raw struct {
c *Client
@@ -17,6 +19,12 @@ func (raw *Raw) Query(endpoint string, out interface{}, q *QueryOptions) (*Query
return raw.c.query(endpoint, out, q)
}
// Response is used to make a GET request against an endpoint and returns the
// response body
func (raw *Raw) Response(endpoint string, q *QueryOptions) (io.ReadCloser, error) {
return raw.c.rawQuery(endpoint, q)
}
// Write is used to do a PUT request against an endpoint
// and serialize/deserialized using the standard Nomad conventions.
func (raw *Raw) Write(endpoint string, in, out interface{}, q *WriteOptions) (*WriteMeta, error) {

View File

@@ -8,7 +8,6 @@ import (
"io/ioutil"
"log"
"net"
"net/http"
"os"
"path/filepath"
"strconv"
@@ -20,6 +19,7 @@ import (
consulapi "github.com/hashicorp/consul/api"
"github.com/hashicorp/consul/lib"
"github.com/hashicorp/go-multierror"
nomadapi "github.com/hashicorp/nomad/api"
"github.com/hashicorp/nomad/client/allocdir"
"github.com/hashicorp/nomad/client/config"
"github.com/hashicorp/nomad/client/driver"
@@ -1519,15 +1519,33 @@ func (c *Client) migrateRemoteAllocDir(alloc *structs.Allocation, allocID string
}
// Get the snapshot
url := fmt.Sprintf("http://%v/v1/client/allocation/%v/snapshot", node.HTTPAddr, alloc.ID)
resp, err := http.Get(url)
scheme := "http"
if node.TLSEnabled {
scheme = "https"
}
// Create an API client
apiConfig := nomadapi.DefaultConfig()
apiConfig.Address = fmt.Sprintf("%s://%s", scheme, node.HTTPAddr)
apiConfig.TLSConfig = &nomadapi.TLSConfig{
CACert: c.config.TLSConfig.CAFile,
ClientCert: c.config.TLSConfig.CertFile,
ClientKey: c.config.TLSConfig.KeyFile,
}
apiClient, err := nomadapi.NewClient(apiConfig)
if err != nil {
return nil, err
}
url := fmt.Sprintf("/v1/client/allocation/%v/snapshot", alloc.ID)
resp, err := apiClient.Raw().Response(url, nil)
if err != nil {
os.RemoveAll(pathToAllocDir)
c.logger.Printf("[ERR] client: error getting snapshot: %v", err)
return nil, fmt.Errorf("error getting snapshot for alloc %v: %v", alloc.ID, err)
}
tr := tar.NewReader(resp.Body)
defer resp.Body.Close()
tr := tar.NewReader(resp)
defer resp.Close()
buf := make([]byte, 1024)