grim/convey

3bc8d82a158e
Parents 02b81cdf72bf
Children 84254a76aa89
Move the run commands script generation to it's own file
  • +48 -37
    docker/run.go
  • --- a/docker/run.go Tue Sep 05 20:15:28 2017 -0500
    +++ b/docker/run.go Tue Sep 05 20:41:11 2017 -0500
    @@ -73,6 +73,53 @@
    {{range .Environment}} -e {{.}}{{end}}
    {{.Image}}{{if .Command}} {{.Command}}{{end}}`
    +// buildScript will create a shell script for the given commands
    +func (r *Run) buildScript(fullEnv []string) (string, string, string, error) {
    + entryPoint := r.Shell
    + if entryPoint == "" {
    + entryPoint = "/bin/sh"
    + }
    +
    + // figure out the current working directory
    + pwd, err := os.Getwd()
    + if err != nil {
    + return "", "", "", err
    + }
    +
    + // now get the absolute path
    + absPwd, err := filepath.Abs(pwd)
    + if err != nil {
    + return "", "", "", err
    + }
    +
    + // create the temp file to write the script to
    + script, err := ioutil.TempFile(absPwd, "convey-script-")
    + if err != nil {
    + return "", "", "", err
    + }
    +
    + // set scriptFile to the name of the temp file
    + scriptFile := script.Name()
    +
    + // set the run command argument to the script file
    + commandArg := scriptFile
    +
    + // iterate the script and run the environment variable exapansion on each line
    + for idx, item := range r.Script {
    + r.Script[idx] = environment.Mapper(item, fullEnv)
    + }
    +
    + // write the script to the file
    + script.WriteString(strings.Join(r.Script, "\n"))
    + script.Close()
    +
    + // make the script executable to the user
    + os.Chmod(scriptFile, 0700)
    +
    + // return it all
    + return scriptFile, entryPoint, commandArg, nil
    +}
    +
    func (r *Run) Execute(name string, logger *gomol.LogAdapter, env []string, st *state.State) error {
    fullEnv := environment.Merge(env, r.Environment)
    fullEnv = environment.Merge(fullEnv, st.Environment)
    @@ -101,49 +148,13 @@
    // if we're using a script defined in the yaml, create it and override
    // some variables
    if len(r.Script) > 0 {
    - entryPoint = r.Shell
    - if entryPoint == "" {
    - entryPoint = "/bin/sh"
    - }
    -
    - // figure out the current working directory
    - pwd, err := os.Getwd()
    - if err != nil {
    - return err
    - }
    -
    - // now get the absolute path
    - absPwd, err := filepath.Abs(pwd)
    + scriptFile, entryPoint, commandArg, err = r.buildScript(fullEnv)
    if err != nil {
    return err
    }
    - // create the temp file to write the script to
    - script, err := ioutil.TempFile(absPwd, "convey-script-")
    - if err != nil {
    - return err
    - }
    -
    - // set scriptFile to the name of the temp file
    - scriptFile = script.Name()
    -
    - // set the run command argument to the script file
    - commandArg = scriptFile
    -
    // remove the file when the function exits
    defer os.Remove(scriptFile)
    -
    - // iterate the script and run the environment variable exapansion on each line
    - for idx, item := range r.Script {
    - r.Script[idx] = environment.Mapper(item, fullEnv)
    - }
    -
    - // write the script to the file
    - script.WriteString(strings.Join(r.Script, "\n"))
    - script.Close()
    -
    - // make the script executable to the user
    - os.Chmod(scriptFile, 0700)
    }
    // build the dict for the template