Added fucntions to add/delete environment variables

This commit is contained in:
Yoichi Hariguchi
2019-06-06 17:01:24 -07:00
parent 619609a464
commit c98a92c5c1

View File

@@ -30,6 +30,7 @@ type Process struct {
Name string Name string
Vrf string Vrf string
Args []string Args []string
Env map[string]string
File string File string
ErrLookup string ErrLookup string
ErrStart string ErrStart string
@@ -73,6 +74,7 @@ func NewProcess(name string, args ...string) *Process {
proc := &Process{ proc := &Process{
Name: name, Name: name,
Args: []string(args), Args: []string(args),
Env: make(map[string]string),
RetryTimer: 1, RetryTimer: 1,
} }
return proc return proc
@@ -177,6 +179,23 @@ func (proc *Process) Debug(funcName string, message string) {
fmt.Printf("[proc]%s(%s:%d): %s\n", funcName, proc.Name, proc.Index, message) fmt.Printf("[proc]%s(%s:%d): %s\n", funcName, proc.Name, proc.Index, message)
} }
func (proc *Process) setEnv() {
if proc.Cmd == nil {
proc.Debug("setEnv", "proc.Cmd == nil")
return
}
env := os.Environ()
if proc.Vrf != "" {
env = append(env, fmt.Sprintf("VRF=%s", proc.Vrf))
env = append(env, "LD_PRELOAD=/usr/bin/vrf_socket.so")
}
for key, val := range proc.Env {
proc.Debug("Start", fmt.Sprintf("environment: %s=%s", key, val))
env = append(env, fmt.Sprintf("%s=%s", key, val))
}
proc.Cmd.Env = env
}
func (proc *Process) Start() { func (proc *Process) Start() {
proc.Debug("Start", "function is called") proc.Debug("Start", "function is called")
@@ -210,15 +229,8 @@ func (proc *Process) Start() {
} }
cmd := exec.CommandContext(ctx, binary, proc.Args...) cmd := exec.CommandContext(ctx, binary, proc.Args...)
env := os.Environ()
if proc.Vrf != "" {
env = append(env, fmt.Sprintf("VRF=%s", proc.Vrf))
env = append(env, "LD_PRELOAD=/usr/bin/vrf_socket.so")
}
cmd.Env = env
proc.Cmd = cmd proc.Cmd = cmd
proc.setEnv()
if proc.StartTimer != 0 { if proc.StartTimer != 0 {
proc.Debug("Start", fmt.Sprintf("StartTimer %d", proc.StartTimer)) proc.Debug("Start", fmt.Sprintf("StartTimer %d", proc.StartTimer))
startTimer := time.NewTimer(time.Duration(proc.StartTimer) * time.Second) startTimer := time.NewTimer(time.Duration(proc.StartTimer) * time.Second)
@@ -290,6 +302,14 @@ func (proc *Process) Stop() {
} }
} }
func (proc *Process) AddEnv(key, val string) {
proc.Env[key] = val
}
func (proc *Process) DelEnv(key string) {
delete(proc.Env, key)
}
func ProcessListShow() string { func ProcessListShow() string {
str := "" str := ""
for pos, proc := range ProcessList { for pos, proc := range ProcessList {