harness-drone/yaml/transform/command.go

84 lines
1.6 KiB
Go
Raw Normal View History

2016-05-09 18:28:49 +00:00
package transform
import (
"bytes"
"encoding/base64"
"fmt"
"strings"
"github.com/drone/drone/yaml"
)
// CommandTransform transforms the custom shell commands in the Yaml pipeline
// into a container ENTRYPOINT and and CMD for execution.
func CommandTransform(c *yaml.Config) error {
for _, p := range c.Pipeline {
2016-05-10 05:57:57 +00:00
if isPlugin(p) {
2016-05-09 18:28:49 +00:00
continue
}
p.Entrypoint = []string{
"/bin/sh", "-c",
}
p.Command = []string{
"echo $DRONE_SCRIPT | base64 -d | /bin/sh -e",
}
if p.Environment == nil {
p.Environment = map[string]string{}
}
p.Environment["HOME"] = "/root"
p.Environment["SHELL"] = "/bin/sh"
p.Environment["DRONE_SCRIPT"] = toScript(
p.Commands,
)
}
return nil
}
func toScript(commands []string) string {
var buf bytes.Buffer
for _, command := range commands {
escaped := fmt.Sprintf("%q", command)
2016-08-25 19:02:23 +00:00
escaped = strings.Replace(escaped, "$", `\$`, -1)
2016-05-09 18:28:49 +00:00
buf.WriteString(fmt.Sprintf(
traceScript,
escaped,
command,
))
}
script := fmt.Sprintf(
setupScript,
buf.String(),
)
return base64.StdEncoding.EncodeToString([]byte(script))
}
// setupScript is a helper script this is added to the build to ensure
// a minimum set of environment variables are set correctly.
const setupScript = `
if [ -n "$DRONE_NETRC_MACHINE" ]; then
cat <<EOF > $HOME/.netrc
machine $DRONE_NETRC_MACHINE
login $DRONE_NETRC_USERNAME
password $DRONE_NETRC_PASSWORD
EOF
2016-07-21 19:18:39 +00:00
chmod 0600 $HOME/.netrc
2016-05-09 18:28:49 +00:00
fi
unset DRONE_NETRC_USERNAME
unset DRONE_NETRC_PASSWORD
unset DRONE_SCRIPT
%s
`
// traceScript is a helper script that is added to the build script
// to trace a command.
const traceScript = `
echo + %s
%s
`