Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: remove InstanceInfo from container env #1738

Merged
merged 1 commit into from
Jul 17, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 4 additions & 51 deletions daemon/mgr/container_envs.go
Original file line number Diff line number Diff line change
@@ -9,7 +9,6 @@ import (

"github.com/alibaba/pouch/pkg/utils"

"github.com/magiconair/properties"
"github.com/sirupsen/logrus"
)

@@ -25,35 +24,27 @@ import (
// /etc/profild.d/*.sh (here is pouchenv.sh). After that a process execed from outside would take env of init process,
// then such action like reload of detailed user application could share the env.
const (
// containerInstanceInfo is file in the container image.
// row in this file has content like `env_appDeployType = taoJavaWeb`
containerInstanceInfo = "/etc/instanceInfo"
// pouchEnvDir =
pouchEnvDir = "/etc/profile.d"
// this filepath is used in PouchContainer to store user input env via persistent file
pouchEnvFile = "/etc/profile.d/pouchenv.sh"
)

// updateContainerEnv update the container's envs in
// /etc/instanceInfo and /etc/profile.d/pouchenv.sh
// Env used by rich container.
// /etc/profile.d/pouchenv.sh used by rich container.
func updateContainerEnv(inputRawEnv []string, baseFs string) error {
var (
envPropertiesPath = path.Join(baseFs, containerInstanceInfo)
envShPath = path.Join(baseFs, pouchEnvFile)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where do we create envShPath and envShDir, are they create in rich-mode with runc?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The file is located in the image's rootfs.
In addition, the file is in the base image.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you mean we not create the file, but only judge the file

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes

envShPath = path.Join(baseFs, pouchEnvFile)
)

// check the existence of related files.
// if dir of pouch.sh is not exist, it's unnecessary to update that files.
if _, err := os.Stat(path.Join(baseFs, pouchEnvDir)); err != nil {
return nil
}
if _, err := os.Stat(envPropertiesPath); err != nil {
//if etc/instanceInfo is not exist, it's unnecessary to update that file.
return nil
}
if _, err := os.Stat(envShPath); err != nil {
return fmt.Errorf("failed to stat container's env file /etc/profile.d/pouchenv.sh: %v", err)
logrus.Warnf("failed to stat container's env file /etc/profile.d/pouchenv.sh: %v", err)
return nil
}

inputEnv := utils.ConvertKVStrToMapWithNoErr(inputRawEnv)
@@ -73,44 +64,6 @@ func updateContainerEnv(inputRawEnv []string, baseFs string) error {
}
ioutil.WriteFile(envShPath, []byte(str), 0755)

p, err := properties.LoadFile(envPropertiesPath, properties.ISO_8859_1)
if err != nil {
return fmt.Errorf("failed to properties load container's environment variable file(/etc/instanceInfo): %v", err)
}

for key, val := range newEnv {
if v, ok := p.Get("env_" + key); ok {
if key == "PATH" {
val = val + ":$PATH"
}
if v == val {
continue
}
_, _, err := p.Set("env_"+key, val)
if err != nil {
return fmt.Errorf("failed to properties set value key=%s, value=%s: %v", "env_"+key, val, err)
}
logrus.Infof("the environment variable exist and the value is not same, key=%s, old value=%s, new value=%s", "env_"+key, v, val)
} else {
_, _, err := p.Set("env_"+key, val)
if err != nil {
return fmt.Errorf("failed to properties set value key=%s, value=%s: %v", "env_"+key, val, err)
}
logrus.Infof("the environment variable not exist and set the new key value pair, key=%s, value=%s", "env_"+key, val)
}
}

f, err := os.Create(envPropertiesPath)
if err != nil {
return fmt.Errorf("failed to create container's environment variable file(properties): %v", err)
}
defer f.Close()

_, err = p.Write(f, properties.ISO_8859_1)
if err != nil {
return fmt.Errorf("failed to write to container's environment variable file(properties): %v", err)
}

return nil
}