mirror of https://github.com/docker/docs.git
Move code where it's used
Signed-off-by: David Gageot <david@gageot.net>
This commit is contained in:
parent
8bbfc6ef2a
commit
eb39f098d5
|
@ -3,12 +3,9 @@ package commands
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/docker/machine/libmachine/host"
|
"github.com/docker/machine/libmachine/host"
|
||||||
"github.com/docker/machine/libmachine/persist"
|
"github.com/docker/machine/libmachine/persist"
|
||||||
"github.com/docker/machine/libmachine/state"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -50,17 +47,3 @@ func getActiveHost(store persist.Store) (*host.Host, error) {
|
||||||
|
|
||||||
return nil, errors.New("Active host not found")
|
return nil, errors.New("Active host not found")
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsActive provides a single function for determining if a host is active
|
|
||||||
// based on both the url and if the host is stopped.
|
|
||||||
func isActive(h *host.Host, currentState state.State, url string) (bool, error) {
|
|
||||||
dockerHost := os.Getenv("DOCKER_HOST")
|
|
||||||
|
|
||||||
// TODO: hard-coding the swarm port is a travesty...
|
|
||||||
deSwarmedHost := strings.Replace(dockerHost, ":3376", ":2376", 1)
|
|
||||||
if dockerHost == url || deSwarmedHost == url {
|
|
||||||
return currentState == state.Running, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return false, nil
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,45 +0,0 @@
|
||||||
package commands
|
|
||||||
|
|
||||||
import (
|
|
||||||
"os"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/docker/machine/drivers/fakedriver"
|
|
||||||
"github.com/docker/machine/libmachine/host"
|
|
||||||
"github.com/docker/machine/libmachine/state"
|
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
h = host.Host{
|
|
||||||
Name: "foo",
|
|
||||||
DriverName: "fakedriver",
|
|
||||||
Driver: &fakedriver.Driver{
|
|
||||||
MockState: state.Running,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestIsActive(t *testing.T) {
|
|
||||||
cases := []struct {
|
|
||||||
dockerHost string
|
|
||||||
state state.State
|
|
||||||
expected bool
|
|
||||||
}{
|
|
||||||
{"", state.Running, false},
|
|
||||||
{"tcp://5.6.7.8:2376", state.Running, false},
|
|
||||||
{"tcp://1.2.3.4:2376", state.Stopped, false},
|
|
||||||
{"tcp://1.2.3.4:2376", state.Running, true},
|
|
||||||
{"tcp://1.2.3.4:3376", state.Running, true},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, c := range cases {
|
|
||||||
os.Unsetenv("DOCKER_HOST")
|
|
||||||
if c.dockerHost != "" {
|
|
||||||
os.Setenv("DOCKER_HOST", c.dockerHost)
|
|
||||||
}
|
|
||||||
actual, err := isActive(&h, c.state, "tcp://1.2.3.4:2376")
|
|
||||||
assert.Equal(t, c.expected, actual, "IsActive(%s, \"%s\") should return %v, but didn't", c.state, c.dockerHost, c.expected)
|
|
||||||
assert.NoError(t, err)
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -5,7 +5,6 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/docker/machine/cli"
|
"github.com/docker/machine/cli"
|
||||||
|
@ -447,22 +446,21 @@ func runActionWithContext(actionName string, c CommandLine) error {
|
||||||
// sigh.
|
// sigh.
|
||||||
func getCertPathInfoFromContext(c CommandLine) cert.PathInfo {
|
func getCertPathInfoFromContext(c CommandLine) cert.PathInfo {
|
||||||
caCertPath := c.GlobalString("tls-ca-cert")
|
caCertPath := c.GlobalString("tls-ca-cert")
|
||||||
caKeyPath := c.GlobalString("tls-ca-key")
|
|
||||||
clientCertPath := c.GlobalString("tls-client-cert")
|
|
||||||
clientKeyPath := c.GlobalString("tls-client-key")
|
|
||||||
|
|
||||||
if caCertPath == "" {
|
if caCertPath == "" {
|
||||||
caCertPath = filepath.Join(mcndirs.GetMachineCertDir(), "ca.pem")
|
caCertPath = filepath.Join(mcndirs.GetMachineCertDir(), "ca.pem")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
caKeyPath := c.GlobalString("tls-ca-key")
|
||||||
if caKeyPath == "" {
|
if caKeyPath == "" {
|
||||||
caKeyPath = filepath.Join(mcndirs.GetMachineCertDir(), "ca-key.pem")
|
caKeyPath = filepath.Join(mcndirs.GetMachineCertDir(), "ca-key.pem")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
clientCertPath := c.GlobalString("tls-client-cert")
|
||||||
if clientCertPath == "" {
|
if clientCertPath == "" {
|
||||||
clientCertPath = filepath.Join(mcndirs.GetMachineCertDir(), "cert.pem")
|
clientCertPath = filepath.Join(mcndirs.GetMachineCertDir(), "cert.pem")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
clientKeyPath := c.GlobalString("tls-client-key")
|
||||||
if clientKeyPath == "" {
|
if clientKeyPath == "" {
|
||||||
clientKeyPath = filepath.Join(mcndirs.GetMachineCertDir(), "key.pem")
|
clientKeyPath = filepath.Join(mcndirs.GetMachineCertDir(), "key.pem")
|
||||||
}
|
}
|
||||||
|
@ -474,20 +472,3 @@ func getCertPathInfoFromContext(c CommandLine) cert.PathInfo {
|
||||||
ClientKeyPath: clientKeyPath,
|
ClientKeyPath: clientKeyPath,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func detectShell() (string, error) {
|
|
||||||
// attempt to get the SHELL env var
|
|
||||||
shell := filepath.Base(os.Getenv("SHELL"))
|
|
||||||
|
|
||||||
log.Debugf("shell: %s", shell)
|
|
||||||
if shell == "" {
|
|
||||||
// check for windows env and not bash (i.e. msysgit, etc)
|
|
||||||
if runtime.GOOS == "windows" {
|
|
||||||
log.Printf("On Windows, please specify either 'cmd' or 'powershell' with the --shell flag.\n\n")
|
|
||||||
}
|
|
||||||
|
|
||||||
return "", ErrUnknownShell
|
|
||||||
}
|
|
||||||
|
|
||||||
return shell, nil
|
|
||||||
}
|
|
||||||
|
|
|
@ -2,6 +2,7 @@ package commands
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
@ -12,6 +13,18 @@ import (
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
stdout *os.File
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
stdout = os.Stdout
|
||||||
|
}
|
||||||
|
|
||||||
|
func cleanup() {
|
||||||
|
os.Stdout = stdout
|
||||||
|
}
|
||||||
|
|
||||||
func TestRunActionForeachMachine(t *testing.T) {
|
func TestRunActionForeachMachine(t *testing.T) {
|
||||||
// Assume a bunch of machines in randomly started or
|
// Assume a bunch of machines in randomly started or
|
||||||
// stopped states.
|
// stopped states.
|
||||||
|
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
"text/template"
|
"text/template"
|
||||||
|
|
||||||
|
@ -208,3 +209,20 @@ func generateUsageHint(userShell string, args []string) string {
|
||||||
|
|
||||||
return fmt.Sprintf("%s Run this command to configure your shell: \n%s %s\n", comment, comment, cmd)
|
return fmt.Sprintf("%s Run this command to configure your shell: \n%s %s\n", comment, comment, cmd)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func detectShell() (string, error) {
|
||||||
|
// attempt to get the SHELL env var
|
||||||
|
shell := filepath.Base(os.Getenv("SHELL"))
|
||||||
|
|
||||||
|
log.Debugf("shell: %s", shell)
|
||||||
|
if shell == "" {
|
||||||
|
// check for windows env and not bash (i.e. msysgit, etc)
|
||||||
|
if runtime.GOOS == "windows" {
|
||||||
|
log.Printf("On Windows, please specify either 'cmd' or 'powershell' with the --shell flag.\n\n")
|
||||||
|
}
|
||||||
|
|
||||||
|
return "", ErrUnknownShell
|
||||||
|
}
|
||||||
|
|
||||||
|
return shell, nil
|
||||||
|
}
|
||||||
|
|
|
@ -261,7 +261,7 @@ func attemptGetHostState(h *host.Host, stateQueryChan chan<- HostListItem) {
|
||||||
close(stateCh)
|
close(stateCh)
|
||||||
close(urlCh)
|
close(urlCh)
|
||||||
|
|
||||||
active, err := isActive(h, currentState, url)
|
active, err := isActive(currentState, url)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("error determining if host is active for host %s: %s",
|
log.Errorf("error determining if host is active for host %s: %s",
|
||||||
h.Name, err)
|
h.Name, err)
|
||||||
|
@ -329,3 +329,17 @@ func sortHostListItemsByName(items []HostListItem) {
|
||||||
items[i] = m[v]
|
items[i] = m[v]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IsActive provides a single function for determining if a host is active
|
||||||
|
// based on both the url and if the host is stopped.
|
||||||
|
func isActive(currentState state.State, url string) (bool, error) {
|
||||||
|
if currentState != state.Running {
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: hard-coding the swarm port is a travesty...
|
||||||
|
dockerHost := os.Getenv("DOCKER_HOST")
|
||||||
|
deSwarmedHost := strings.Replace(dockerHost, ":3376", ":2376", 1)
|
||||||
|
|
||||||
|
return dockerHost == url || deSwarmedHost == url, nil
|
||||||
|
}
|
||||||
|
|
|
@ -13,20 +13,6 @@ import (
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
|
||||||
hostTestStorePath string
|
|
||||||
stdout *os.File
|
|
||||||
)
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
stdout = os.Stdout
|
|
||||||
}
|
|
||||||
|
|
||||||
func cleanup() {
|
|
||||||
os.Stdout = stdout
|
|
||||||
os.RemoveAll(hostTestStorePath)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestParseFiltersErrorsGivenInvalidFilter(t *testing.T) {
|
func TestParseFiltersErrorsGivenInvalidFilter(t *testing.T) {
|
||||||
_, err := parseFilters([]string{"foo=bar"})
|
_, err := parseFilters([]string{"foo=bar"})
|
||||||
assert.EqualError(t, err, "Unsupported filter key 'foo'")
|
assert.EqualError(t, err, "Unsupported filter key 'foo'")
|
||||||
|
@ -306,8 +292,6 @@ func captureStdout() (chan string, *os.File) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGetHostListItems(t *testing.T) {
|
func TestGetHostListItems(t *testing.T) {
|
||||||
defer cleanup()
|
|
||||||
|
|
||||||
hostListItemsChan := make(chan HostListItem)
|
hostListItemsChan := make(chan HostListItem)
|
||||||
|
|
||||||
hosts := []*host.Host{
|
hosts := []*host.Host{
|
||||||
|
@ -381,8 +365,6 @@ func TestGetHostListItems(t *testing.T) {
|
||||||
func TestGetHostListItemsEnvDockerHostUnset(t *testing.T) {
|
func TestGetHostListItemsEnvDockerHostUnset(t *testing.T) {
|
||||||
orgDockerHost := os.Getenv("DOCKER_HOST")
|
orgDockerHost := os.Getenv("DOCKER_HOST")
|
||||||
defer func() {
|
defer func() {
|
||||||
cleanup()
|
|
||||||
|
|
||||||
// revert DOCKER_HOST
|
// revert DOCKER_HOST
|
||||||
os.Setenv("DOCKER_HOST", orgDockerHost)
|
os.Setenv("DOCKER_HOST", orgDockerHost)
|
||||||
}()
|
}()
|
||||||
|
@ -465,3 +447,29 @@ func TestGetHostListItemsEnvDockerHostUnset(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestIsActive(t *testing.T) {
|
||||||
|
cases := []struct {
|
||||||
|
dockerHost string
|
||||||
|
state state.State
|
||||||
|
expected bool
|
||||||
|
}{
|
||||||
|
{"", state.Running, false},
|
||||||
|
{"tcp://5.6.7.8:2376", state.Running, false},
|
||||||
|
{"tcp://1.2.3.4:2376", state.Stopped, false},
|
||||||
|
{"tcp://1.2.3.4:2376", state.Running, true},
|
||||||
|
{"tcp://1.2.3.4:3376", state.Running, true},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, c := range cases {
|
||||||
|
os.Unsetenv("DOCKER_HOST")
|
||||||
|
if c.dockerHost != "" {
|
||||||
|
os.Setenv("DOCKER_HOST", c.dockerHost)
|
||||||
|
}
|
||||||
|
|
||||||
|
actual, err := isActive(c.state, "tcp://1.2.3.4:2376")
|
||||||
|
|
||||||
|
assert.Equal(t, c.expected, actual, "IsActive(%s, \"%s\") should return %v, but didn't", c.state, c.dockerHost, c.expected)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue