Fixed problem with variables containing \n.
The /.dockerenv file is now a marshalled json array so that environment variables with \n and other weird values are supported.
This commit is contained in:
parent
25c4c87c86
commit
1c3a674444
3 changed files with 13 additions and 9 deletions
|
@ -444,16 +444,11 @@ func (container *Container) SaveHostConfig(hostConfig *HostConfig) (err error) {
|
|||
}
|
||||
|
||||
func (container *Container) generateEnvConfig(env []string) error {
|
||||
fo, err := os.Create(container.EnvConfigPath())
|
||||
data, err := json.Marshal(env)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer fo.Close()
|
||||
for _, item := range env {
|
||||
if _, err := fo.WriteString(item + "\n"); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
ioutil.WriteFile(container.EnvConfigPath(), data, 0600)
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
|
@ -974,9 +974,10 @@ func TestTty(t *testing.T) {
|
|||
|
||||
func TestEnv(t *testing.T) {
|
||||
os.Setenv("TRUE", "false")
|
||||
os.Setenv("TRICKY", "tri\ncky\n")
|
||||
runtime := mkRuntime(t)
|
||||
defer nuke(runtime)
|
||||
config, _, _, err := ParseRun([]string{"-e=FALSE=true", "-e=TRUE", GetTestImage(runtime).ID, "env"}, nil)
|
||||
config, _, _, err := ParseRun([]string{"-e=FALSE=true", "-e=TRUE", "-e=TRICKY", GetTestImage(runtime).ID, "env"}, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
@ -1012,6 +1013,9 @@ func TestEnv(t *testing.T) {
|
|||
"HOSTNAME=" + container.ShortID(),
|
||||
"FALSE=true",
|
||||
"TRUE=false",
|
||||
"TRICKY=tri",
|
||||
"cky",
|
||||
"",
|
||||
}
|
||||
sort.Strings(goodEnv)
|
||||
if len(goodEnv) != len(actualEnv) {
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package sysinit
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"github.com/dotcloud/docker/netlink"
|
||||
|
@ -72,11 +73,15 @@ func changeUser(u string) {
|
|||
// Clear environment pollution introduced by lxc-start
|
||||
func cleanupEnv() {
|
||||
os.Clearenv()
|
||||
var lines []string
|
||||
content, err := ioutil.ReadFile("/.dockerenv")
|
||||
if err != nil {
|
||||
log.Fatalf("Unable to load environment variables: %v", err)
|
||||
}
|
||||
lines := strings.Split(string(content), "\n")
|
||||
err = json.Unmarshal(content, &lines)
|
||||
if err != nil {
|
||||
log.Fatalf("Unable to unmarshal environment variables: %v", err)
|
||||
}
|
||||
for _, kv := range lines {
|
||||
parts := strings.SplitN(kv, "=", 2)
|
||||
if len(parts) == 1 {
|
||||
|
|
Loading…
Reference in a new issue