1
// Copyright 2012-2014 Canonical Ltd.
2
// Licensed under the AGPLv3, see LICENCE file for details.
13
"github.com/juju/juju/worker/uniter/runner/context"
14
jujuos "github.com/juju/utils/os"
17
var windowsSuffixOrder = []string{
24
func lookPath(hook string) (string, error) {
25
hookFile, err := exec.LookPath(hook)
27
if ee, ok := err.(*exec.Error); ok && os.IsNotExist(ee.Err) {
28
return "", context.NewMissingHookError(hook)
35
// searchHook will search, in order, hooks suffixed with extensions
36
// in windowsSuffixOrder. As windows cares about extensions to determine
37
// how to execute a file, we will allow several suffixes, with powershell
39
func searchHook(charmDir, hook string) (string, error) {
40
hookFile := filepath.Join(charmDir, hook)
41
if jujuos.HostOS() != jujuos.Windows {
42
// we are not running on windows,
43
// there is no need to look for suffixed hooks
44
return lookPath(hookFile)
46
for _, suffix := range windowsSuffixOrder {
47
file := fmt.Sprintf("%s%s", hookFile, suffix)
48
foundHook, err := lookPath(file)
50
if context.IsMissingHookError(err) {
51
// look for next suffix
58
return "", context.NewMissingHookError(hook)
61
// hookCommand constructs an appropriate command to be passed to
62
// exec.Command(). The exec package uses cmd.exe as default on windows.
63
// cmd.exe does not know how to execute ps1 files by default, and
64
// powershell needs a few flags to allow execution (-ExecutionPolicy)
65
// and propagate error levels (-File). .cmd and .bat files can be run
67
func hookCommand(hook string) []string {
68
if jujuos.HostOS() != jujuos.Windows {
69
// we are not running on windows,
70
// just return the hook name
73
if strings.HasSuffix(hook, ".ps1") {