github.com/altoros/juju-vmware@v0.0.0-20150312064031-f19ae857ccca/worker/uniter/runner/env.go (about)

     1  // Copyright 2012-2014 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package runner
     5  
     6  import (
     7  	"os"
     8  	"path/filepath"
     9  	"strings"
    10  
    11  	"github.com/juju/juju/version"
    12  )
    13  
    14  func osDependentEnvVars(paths Paths) []string {
    15  	switch version.Current.OS {
    16  	case version.Windows:
    17  		return windowsEnv(paths)
    18  	case version.Ubuntu:
    19  		return ubuntuEnv(paths)
    20  	case version.CentOS:
    21  		return centosEnv(paths)
    22  	}
    23  	return nil
    24  }
    25  
    26  func appendPath(paths Paths) []string {
    27  	return []string{
    28  		"PATH=" + paths.GetToolsDir() + ":" + os.Getenv("PATH"),
    29  	}
    30  }
    31  
    32  func ubuntuEnv(paths Paths) []string {
    33  	path := appendPath(paths)
    34  	env := []string{
    35  		"APT_LISTCHANGES_FRONTEND=none",
    36  		"DEBIAN_FRONTEND=noninteractive",
    37  	}
    38  	env = append(env, path...)
    39  	return env
    40  }
    41  
    42  func centosEnv(paths Paths) []string {
    43  	return appendPath(paths)
    44  }
    45  
    46  // windowsEnv adds windows specific environment variables. PSModulePath
    47  // helps hooks use normal imports instead of dot sourcing modules
    48  // its a convenience variable. The PATH variable delimiter is
    49  // a semicolon instead of a colon
    50  func windowsEnv(paths Paths) []string {
    51  	charmDir := paths.GetCharmDir()
    52  	charmModules := filepath.Join(charmDir, "lib", "Modules")
    53  	return []string{
    54  		"Path=" + paths.GetToolsDir() + ";" + os.Getenv("Path"),
    55  		"PSModulePath=" + os.Getenv("PSModulePath") + ";" + charmModules,
    56  	}
    57  }
    58  
    59  // mergeEnvironment takes in a string array representing the desired environment
    60  // and merges it with the current environment. On Windows, clearing the environment,
    61  // or having missing environment variables, may lead to standard go packages not working
    62  // (os.TempDir relies on $env:TEMP), and powershell erroring out
    63  // TODO(fwereade, gsamfira): this is copy/pasted from utils/exec.
    64  func mergeEnvironment(env []string) []string {
    65  	if env == nil {
    66  		return nil
    67  	}
    68  	m := map[string]string{}
    69  	var tmpEnv []string
    70  	for _, val := range os.Environ() {
    71  		varSplit := strings.SplitN(val, "=", 2)
    72  		m[varSplit[0]] = varSplit[1]
    73  	}
    74  
    75  	for _, val := range env {
    76  		varSplit := strings.SplitN(val, "=", 2)
    77  		m[varSplit[0]] = varSplit[1]
    78  	}
    79  
    80  	for key, val := range m {
    81  		tmpEnv = append(tmpEnv, key+"="+val)
    82  	}
    83  
    84  	return tmpEnv
    85  }