// writeEnvFile creates an environment file for given app name, the minimum // required environment variables by the appc spec will be set to sensible // defaults here if they're not provided by env. func writeEnvFile(p *stage1commontypes.Pod, env types.Environment, appName types.ACName, uidRange *user.UidRange, separator byte, envFilePath string) error { ef := bytes.Buffer{} for dk, dv := range defaultEnv { if _, exists := env.Get(dk); !exists { fmt.Fprintf(&ef, "%s=%s%c", dk, dv, separator) } } for _, e := range env { fmt.Fprintf(&ef, "%s=%s%c", e.Name, e.Value, separator) } if err := ioutil.WriteFile(envFilePath, ef.Bytes(), 0644); err != nil { return err } if uidRange.Shift != 0 && uidRange.Count != 0 { if err := os.Chown(envFilePath, int(uidRange.Shift), int(uidRange.Shift)); err != nil { return err } } return nil }
// writeEnvFile creates an environment file for given app name, the minimum // required environment variables by the appc spec will be set to sensible // defaults here if they're not provided by env. func writeEnvFile(p *stage1commontypes.Pod, env types.Environment, appName types.ACName, privateUsers string) error { ef := bytes.Buffer{} for dk, dv := range defaultEnv { if _, exists := env.Get(dk); !exists { fmt.Fprintf(&ef, "%s=%s\000", dk, dv) } } for _, e := range env { fmt.Fprintf(&ef, "%s=%s\000", e.Name, e.Value) } uidRange := uid.NewBlankUidRange() if err := uidRange.Deserialize([]byte(privateUsers)); err != nil { return err } envFilePath := EnvFilePath(p.Root, appName) if err := ioutil.WriteFile(envFilePath, ef.Bytes(), 0644); err != nil { return err } if uidRange.Shift != 0 && uidRange.Count != 0 { if err := os.Chown(envFilePath, int(uidRange.Shift), int(uidRange.Shift)); err != nil { return err } } return nil }
// mergeEnvs merges environment variables from env into the current appEnv // if override is set to true, then variables with the same name will be set to the value in env // env is expected to be in the os.Environ() key=value format func mergeEnvs(appEnv *types.Environment, env []string, override bool) { for _, ev := range env { pair := strings.SplitN(ev, "=", 2) if _, exists := appEnv.Get(pair[0]); override || !exists { appEnv.Set(pair[0], pair[1]) } } }
// MergeEnvs amends appEnv setting variables in setEnv before setting anything new from os.Environ if inheritEnv = true // setEnv is expected to be in the os.Environ() key=value format func MergeEnvs(appEnv *types.Environment, inheritEnv bool, setEnv []string) { for _, ev := range setEnv { pair := strings.SplitN(ev, "=", 2) appEnv.Set(pair[0], pair[1]) } if inheritEnv { for _, ev := range os.Environ() { pair := strings.SplitN(ev, "=", 2) if _, exists := appEnv.Get(pair[0]); !exists { appEnv.Set(pair[0], pair[1]) } } } }
// WriteEnvFile creates an environment file for given app name, the minimum // required environment variables by the appc spec will be set to sensible // defaults here if they're not provided by env. func WriteEnvFile(env types.Environment, uidRange *user.UidRange, envFilePath string) error { ef := bytes.Buffer{} for dk, dv := range defaultEnv { if _, exists := env.Get(dk); !exists { fmt.Fprintf(&ef, "%s=%s\n", dk, dv) } } for _, e := range env { fmt.Fprintf(&ef, "%s=%s\n", e.Name, e.Value) } if err := ioutil.WriteFile(envFilePath, ef.Bytes(), 0644); err != nil { return err } if err := user.ShiftFiles([]string{envFilePath}, uidRange); err != nil { return err } return nil }