grim/convey

closing closed branch again
multiple-images
2018-01-26, Gary Kramlich
8e45b1f8ccff
closing closed branch again
/*
* Convey
* Copyright 2016-2017 Gary Kramlich <grim@reaperworld.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package docker
import (
"fmt"
"io/ioutil"
"os"
"os/user"
"strings"
"github.com/aphistic/gomol"
"bitbucket.org/rw_grim/convey/command"
"bitbucket.org/rw_grim/convey/environment"
"bitbucket.org/rw_grim/convey/options"
"bitbucket.org/rw_grim/convey/tasks"
"bitbucket.org/rw_grim/convey/workspace"
)
type Run struct {
Command string `yaml:"command"`
EntryPoint string `yaml:"entrypoint"`
Environment []string `yaml:"environment"`
Image string `yaml:"image"`
WorkDir string `yaml:"workdir"`
WorkSpace string `yaml:"workspace"`
Script []string `yaml:"script"`
Shell string `yaml:"shell" default:"/bin/sh"`
}
const runTemplate = `docker run --rm
{{if .UID}} -e UID={{.UID}}{{end}}
{{if .GID}} -e GID={{.GID}}{{end}}
-v {{.WorkspacePath}}:{{.WorkspaceMount}}
-e CONVEY_WORKSPACE={{.WorkspaceMount}}
{{if .CPUShares }} --cpu-shares {{.CPUShares}}{{end}}
{{if .Memory }} --memory {{.Memory}}{{end}}
{{if .ScriptFile }} -v {{.ScriptFile}}:{{.ScriptFile}}{{end}}
{{if .SSHAgent }} -e SSH_AUTH_SOCK -v {{.SSHAuthSock}}:{{.SSHAuthSock}}{{end}}
{{if .EntryPoint}} --entrypoint {{.EntryPoint}}{{end}}
{{if .WorkDir}} -w {{.WorkDir}}{{end}}
{{range .Environment}} -e {{.}}{{end}}
{{.Image}}{{if .Command}} {{.Command}}{{end}}`
func (r *Run) Execute(name string, ws *workspace.Workspace, logger *gomol.LogAdapter, env []string, opts *options.Options) error {
fullEnv := environment.Merge(env, r.Environment)
fullEnv = environment.Merge(fullEnv, opts.Environment)
user, err := user.Current()
if err != nil {
return err
}
// now expand the environment
for idx, v := range fullEnv {
fullEnv[idx] = environment.Mapper(v, fullEnv)
}
// assign a default workspace location
workSpace := r.WorkSpace
if workSpace == "" {
workSpace = "/workspace"
}
// initialize some variables
entryPoint := r.EntryPoint
commandArg := environment.Mapper(r.Command, fullEnv)
scriptFile := ""
// 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"
}
// create the temp file to write the script to
script, err := ioutil.TempFile("", "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
params := map[string]interface{}{
"Command": commandArg,
"CPUShares": opts.CPUShares,
"Environment": fullEnv,
"EntryPoint": entryPoint,
"GID": user.Gid,
"Image": environment.Mapper(r.Image, fullEnv),
"Memory": opts.Memory,
"ScriptFile": scriptFile,
"SSHAgent": opts.EnableSSHAgent,
"SSHAuthSock": os.Getenv("SSH_AUTH_SOCK"),
"UID": user.Uid,
"WorkDir": environment.Mapper(r.WorkDir, fullEnv),
"WorkspacePath": environment.Mapper(ws.MountPoint, fullEnv),
"WorkspaceMount": environment.Mapper(workSpace, fullEnv),
}
// run the command
return command.Run(name, runTemplate, params, opts.TaskTimeout)
}
func (r *Run) New() tasks.Task {
return &Run{}
}
func (r *Run) Valid() error {
if r.Image == "" {
return fmt.Errorf("no image specified")
}
return nil
}