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 <dwalsh@redhat.com>
This commit is contained in:
Daniel J Walsh 2020-09-15 11:03:06 -04:00
parent d6cb241bed
commit 00631f7fa2
2 changed files with 97 additions and 1 deletions

View File

@ -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 {

View File

@ -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())