From 00631f7fa27f9020b2eee171aff22a33a2557010 Mon Sep 17 00:00:00 2001 From: Daniel J Walsh Date: Tue, 15 Sep 2020 11:03:06 -0400 Subject: [PATCH] Add support for CONTAINER_CONNECTION environment variable Users on the MAC have pointed out that they could have multiple connections to different servers, and they asked to be able to specify the connection name via environmnet variable rather then to always have to specify the connection on the command line if they did not want to use the default setting. Signed-off-by: Daniel J Walsh --- common/pkg/config/config.go | 9 +++- common/pkg/config/config_test.go | 89 ++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+), 1 deletion(-) diff --git a/common/pkg/config/config.go b/common/pkg/config/config.go index a59f664433..6028855c66 100644 --- a/common/pkg/config/config.go +++ b/common/pkg/config/config.go @@ -980,8 +980,15 @@ func (c *Config) ActiveDestination() (uri, identity string, err error) { } return uri, identity, nil } - + connEnv := os.Getenv("CONTAINER_CONNECTION") switch { + case connEnv != "": + d, found := c.Engine.ServiceDestinations[connEnv] + if !found { + return "", "", errors.Errorf("environment variable CONTAINER_CONNECTION=%q service destination not found", connEnv) + } + return d.URI, d.Identity, nil + case c.Engine.ActiveService != "": d, found := c.Engine.ServiceDestinations[c.Engine.ActiveService] if !found { diff --git a/common/pkg/config/config_test.go b/common/pkg/config/config_test.go index a55a2e41d9..2183a4e548 100644 --- a/common/pkg/config/config_test.go +++ b/common/pkg/config/config_test.go @@ -421,6 +421,10 @@ var _ = Describe("Config", func() { cfg.Engine.ActiveService = "QA" cfg.Engine.ServiceDestinations = map[string]Destination{ + "QB": { + URI: "https://qb/run/podman/podman.sock", + Identity: "/.ssh/qb_id_rsa", + }, "QA": { URI: "https://qa/run/podman/podman.sock", Identity: "/.ssh/id_rsa", @@ -439,6 +443,91 @@ var _ = Describe("Config", func() { gomega.Expect(i).To(gomega.Equal("/.ssh/id_rsa")) }) + It("succeed ActiveDestinations() CONTAINER_CONNECTION environment", func() { + cfg, err := ReadCustomConfig() + gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) + + cfg.Engine.ActiveService = "QA" + cfg.Engine.ServiceDestinations = map[string]Destination{ + "QA": { + URI: "https://qa/run/podman/podman.sock", + Identity: "/.ssh/id_rsa", + }, + "QB": { + URI: "https://qb/run/podman/podman.sock", + Identity: "/.ssh/qb_id_rsa", + }, + } + err = cfg.Write() + gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) + + cfg, err = ReadCustomConfig() + gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) + + // Given we do + oldContainerConnection, hostEnvSet := os.LookupEnv("CONTAINER_CONNECTION") + os.Setenv("CONTAINER_CONNECTION", "QB") + + u, i, err := cfg.ActiveDestination() + // Undo that + if hostEnvSet { + os.Setenv("CONTAINER_CONNECTION", oldContainerConnection) + } else { + os.Unsetenv("CONTAINER_CONNECTION") + } + gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) + + gomega.Expect(u).To(gomega.Equal("https://qb/run/podman/podman.sock")) + gomega.Expect(i).To(gomega.Equal("/.ssh/qb_id_rsa")) + }) + + It("succeed ActiveDestinations CONTAINER_HOST ()", func() { + cfg, err := ReadCustomConfig() + gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) + + cfg.Engine.ActiveService = "QA" + cfg.Engine.ServiceDestinations = map[string]Destination{ + "QB": { + URI: "https://qb/run/podman/podman.sock", + Identity: "/.ssh/qb_id_rsa", + }, + "QA": { + URI: "https://qa/run/podman/podman.sock", + Identity: "/.ssh/id_rsa", + }, + } + err = cfg.Write() + gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) + + cfg, err = ReadCustomConfig() + gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) + + // Given we do + oldContainerHost, hostEnvSet := os.LookupEnv("CONTAINER_HOST") + oldContainerSSH, sshEnvSet := os.LookupEnv("CONTAINER_SSHKEY") + os.Setenv("CONTAINER_HOST", "foo.bar") + os.Setenv("CONTAINER_SSHKEY", "/.ssh/newid_rsa") + + u, i, err := cfg.ActiveDestination() + // Undo that + if hostEnvSet { + os.Setenv("CONTAINER_HOST", oldContainerHost) + } else { + os.Unsetenv("CONTAINER_HOST") + } + // Undo that + if sshEnvSet { + os.Setenv("CONTAINER_SSHKEY", oldContainerSSH) + } else { + os.Unsetenv("CONTAINER_SSHKEY") + } + + gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) + + gomega.Expect(u).To(gomega.Equal("foo.bar")) + gomega.Expect(i).To(gomega.Equal("/.ssh/newid_rsa")) + }) + It("fail ActiveDestination() no configuration", func() { cfg, err := ReadCustomConfig() gomega.Expect(err).ShouldNot(gomega.HaveOccurred())