-
Notifications
You must be signed in to change notification settings - Fork 1k
/
run.go
111 lines (93 loc) · 3.43 KB
/
run.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Copyright 2017 HootSuite Media Inc.
//
// Licensed under the Apache License, Version 2.0 (the License);
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an AS IS BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Modified hereafter by contributors to runatlantis/atlantis.
//
// Package run handles running commands prior and following the
// regular Atlantis commands.
package run
import (
"bufio"
"fmt"
"io/ioutil"
"os"
"os/exec"
"strings"
"github.com/hashicorp/go-version"
"github.com/pkg/errors"
"github.com/runatlantis/atlantis/server/logging"
)
const inlineShebang = "#!/bin/sh -e"
//go:generate pegomock generate -m --use-experimental-model-gen --package mocks -o mocks/mock_runner.go Runner
type Runner interface {
Execute(log *logging.SimpleLogger, commands []string, path string, workspace string, terraformVersion *version.Version, stage string) (string, error)
}
type Run struct{}
// Execute runs the commands by writing them as a script to disk
// and then executing the script.
func (p *Run) Execute(
log *logging.SimpleLogger,
commands []string,
path string,
workspace string,
terraformVersion *version.Version,
stage string) (string, error) {
// we create a script from the commands provided
if len(commands) == 0 {
return "", errors.Errorf("%s commands cannot be empty", stage)
}
s, err := createScript(commands, stage)
if err != nil {
return "", err
}
defer os.Remove(s) // nolint: errcheck
log.Info("running %s commands: %v", stage, commands)
// set environment variable for the run.
// this is to support scripts to use the WORKSPACE, ATLANTIS_TERRAFORM_VERSION
// and DIR variables in their scripts
os.Setenv("WORKSPACE", workspace) // nolint: errcheck
os.Setenv("ATLANTIS_TERRAFORM_VERSION", terraformVersion.String()) // nolint: errcheck
os.Setenv("DIR", path) // nolint: errcheck
return execute(s)
}
func createScript(cmds []string, stage string) (string, error) {
tmp, err := ioutil.TempFile("/tmp", "atlantis-temp-script")
if err != nil {
return "", errors.Wrapf(err, "preparing %s shell script", stage)
}
scriptName := tmp.Name()
// Write our contents to it
writer := bufio.NewWriter(tmp)
if _, err = writer.WriteString(fmt.Sprintf("%s\n", inlineShebang)); err != nil {
return "", errors.Wrapf(err, "writing to %q", tmp.Name())
}
cmdsJoined := strings.Join(cmds, "\n")
if _, err := writer.WriteString(cmdsJoined); err != nil {
return "", errors.Wrapf(err, "preparing %s", stage)
}
if err := writer.Flush(); err != nil {
return "", errors.Wrap(err, "flushing contents to file")
}
tmp.Close() // nolint: errcheck
if err := os.Chmod(scriptName, 0700); err != nil { // nolint: gas
return "", errors.Wrapf(err, "making %s script executable", stage)
}
return scriptName, nil
}
func execute(script string) (string, error) {
localCmd := exec.Command("sh", "-c", script) // #nosec
out, err := localCmd.CombinedOutput()
output := string(out)
if err != nil {
return output, errors.Wrapf(err, "running script %s: %s", script, output)
}
return output, nil
}