github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/worker/uniter/runner/args.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  	"os/exec"
     9  	"path"
    10  	"path/filepath"
    11  
    12  	"github.com/juju/errors"
    13  
    14  	"github.com/juju/juju/worker/common/charmrunner"
    15  )
    16  
    17  func lookPath(hook string) (string, error) {
    18  	hookFile, err := exec.LookPath(hook)
    19  	if err != nil {
    20  		if ee, ok := err.(*exec.Error); ok && os.IsNotExist(ee.Err) {
    21  			return "", charmrunner.NewMissingHookError(hook)
    22  		}
    23  		return "", err
    24  	}
    25  	return hookFile, nil
    26  }
    27  
    28  // discoverHookScript will return the name of the script to run for a hook.
    29  // We verify an executable file exists with the same name as the hook.
    30  func discoverHookScript(charmDir, hook string) (string, error) {
    31  	hookFile := filepath.Join(charmDir, hook)
    32  	return lookPath(hookFile)
    33  }
    34  
    35  func checkCharmExists(charmDir string) error {
    36  	if _, err := os.Stat(path.Join(charmDir, "metadata.yaml")); os.IsNotExist(err) {
    37  		return errors.New("charm missing from disk")
    38  	} else if err != nil {
    39  		return errors.Annotatef(err, "failed to check for metadata.yaml")
    40  	}
    41  	return nil
    42  }