This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

cli: Allow setting the host socket using an environment variable

This makes it much easier to specify a non-default host, as users can
simply set the environment variable once.
  • Loading branch information...
kklin committed Nov 13, 2017
1 parent e288c14 commit af171a17d93c07fb0beea53e6197f2e6cba7e283
Showing with 74 additions and 3 deletions.
  1. +7 −0 CHANGELOG.md
  2. +9 −1 cli/command/connection.go
  3. +47 −0 cli/command/connection_test.go
  4. +5 −1 integration-tester/exec.go
  5. +6 −1 integration-tester/util/util.go
View
@@ -8,6 +8,13 @@ Up Next
hostname would get its Docker ID, which is meaningless in the Kelda network.
- Only allow a single base infrastructure. We had no good use case for having
multiple base infrastructures, and the feature added unnecessary complexity.
- Allow configuring the Kelda daemon host socket with the `KELDA_HOST`
environment variable. While using the default socket is sufficient for most
scenarios, using a non-default socket path is useful for connecting to a remote
daemon, or running multiple daemons on the same machine. Setting the socket
path using an environment variable allows users to simply set the environment
variable once, and not worry about setting the correct flags in all `kelda`
commands.
Release 0.6.0
-------------
View
@@ -2,6 +2,7 @@ package command
import (
"flag"
"os"
"github.com/kelda/kelda/api"
"github.com/kelda/kelda/api/client"
@@ -15,7 +16,14 @@ type connectionFlags struct {
}
func (cf *connectionFlags) InstallFlags(flags *flag.FlagSet) {
flags.StringVar(&cf.host, "H", api.DefaultSocket, "the host to connect to")
defaultSocket := os.Getenv("KELDA_HOST")
if defaultSocket == "" {
defaultSocket = api.DefaultSocket
}
flags.StringVar(&cf.host, "H", defaultSocket, "the host to connect to. This "+
"flag can also be specified by setting the KELDA_HOST environment "+
"variable. If the flag is set using both the environment variable and a "+
"command line argument, the command line value takes precedence.")
}
type connectionHelper struct {
@@ -1,10 +1,13 @@
package command
import (
"flag"
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/kelda/kelda/api"
"github.com/kelda/kelda/api/client"
"github.com/kelda/kelda/api/client/mocks"
"github.com/kelda/kelda/connection"
@@ -41,3 +44,47 @@ func TestSetupClient(t *testing.T) {
err = cmd.setupClient(newClient)
assert.NotNil(t, err)
}
func TestConnectionFlagsHostEnv(t *testing.T) {
t.Parallel()
tests := []struct {
keldaHostEnv string
args []string
expHost string
}{
// Test setting the host through an environment variable.
{
keldaHostEnv: "envSocket",
expHost: "envSocket",
},
// Test setting the host through command line arguments.
{
args: []string{"-H", "cmdSocket"},
expHost: "cmdSocket",
},
// Test that command line arguments take precedence.
{
keldaHostEnv: "envSocket",
args: []string{"-H", "cmdSocket"},
expHost: "cmdSocket",
},
// Test that when no socket is provided, we use the default.
{
expHost: api.DefaultSocket,
},
}
for _, test := range tests {
os.Setenv("KELDA_HOST", test.keldaHostEnv)
flags := flag.NewFlagSet("test", flag.ExitOnError)
cf := connectionFlags{}
cf.InstallFlags(flags)
flags.Parse(test.args)
assert.Equal(t, test.expHost, cf.host)
os.Setenv("KELDA_HOST", "")
}
}
@@ -71,7 +71,11 @@ func runBlueprint(blueprint string) (string, string, error) {
// runKeldaDaemon starts the daemon.
func runKeldaDaemon() {
os.Remove(api.DefaultSocket[len("unix://"):])
socket := os.Getenv("KELDA_HOST")
if socket == "" {
socket = api.DefaultSocket
}
os.Remove(socket[len("unix://"):])
args := []string{"-l", "debug", "daemon"}
cmd := exec.Command("kelda", args...)
@@ -3,6 +3,7 @@ package util
import (
"fmt"
"net/http"
"os"
"strconv"
"testing"
"time"
@@ -28,7 +29,11 @@ func GetDefaultDaemonClient() (client.Client, error) {
return nil, err
}
return client.New(api.DefaultSocket, creds)
socket := os.Getenv("KELDA_HOST")
if socket == "" {
socket = api.DefaultSocket
}
return client.New(socket, creds)
}
// CheckPublicConnections test that HTTP GETs against all ports that are

0 comments on commit af171a1

Please sign in to comment.