grim/convey
Clone
Summary
Browse
Changes
Graph
Missed a bunch of updates on unit tests for the options update
2018-01-21, Gary Kramlich
2909e17deafe
Missed a bunch of updates on unit tests for the options update
// Convey
// Copyright 2016-2018 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"
"path/filepath"
"strings"
"time"
"github.com/aphistic/gomol"
"bitbucket.org/rw_grim/convey/environment"
"bitbucket.org/rw_grim/convey/normalize"
"bitbucket.org/rw_grim/convey/state"
"bitbucket.org/rw_grim/convey/tasks"
"bitbucket.org/rw_grim/convey/util"
"bitbucket.org/rw_grim/convey/yaml"
)
// Run represents a docker run task which will run a container.
type
Run
struct
{
Command
string
`yaml:"command"`
Detach
bool
`yaml:"detach"`
Hostname
string
`yaml:"hostname"`
EntryPoint
string
`yaml:"entrypoint"`
Environment
yaml
.
StringOrSlice
`yaml:"environment"`
Image
string
`yaml:"image"`
WorkDir
string
`yaml:"workdir"`
WorkSpace
string
`yaml:"workspace"`
Script
yaml
.
StringOrSlice
`yaml:"script"`
Shell
string
`yaml:"shell"`
Labels
yaml
.
StringOrSlice
`yaml:"labels"`
User
string
`yaml:"user"`
HealthCheck
HealthCheck
`yaml:"healthcheck"`
}
// UnmarshalYAML is a custom yaml unmarshaller for run tasks.
func
(
r
*
Run
)
UnmarshalYAML
(
unmarshal
func
(
interface
{})
error
)
error
{
type
rawRun
Run
raw
:=
rawRun
{
Shell
:
"/bin/sh"
,
HealthCheck
:
HealthCheck
{}}
if
err
:=
unmarshal
(
&
raw
);
err
!=
nil
{
return
err
}
*
r
=
Run
(
raw
)
return
nil
}
const
runTemplate
=
`run --rm
{{if .Name}} --name {{.Name}}{{end}}
{{if .UID}} -e UID={{.UID}}{{end}}
{{if .GID}} -e GID={{.GID}}{{end}}
{{if .Detach}} -d{{end}}
{{if .Hostname}} --network-alias {{.Hostname}}{{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}}
{{if .Network}} --network {{.Network}}{{end}}
{{range .Labels}} -l '{{.}}'{{end}}
-l {{.TaskLabel}}
{{range .Environment}} -e {{.}}{{end}}
{{if .Username}} --user {{.Username}}{{end}}
{{if .HealthCheck}}
{{if .HealthCheck.Command}} --health-cmd "{{.HealthCheck.Command}}"{{end}}
{{if .HealthCheck.Interval}} --health-interval {{.HealthCheck.Interval}}{{end}}
{{if .HealthCheck.Retries}} --health-retries {{.HealthCheck.Retries}}{{end}}
{{if .HealthCheck.StartPeriod}} --health-start-period {{.HealthCheck.StartPeriod}}{{end}}
{{if .HealthCheck.Timeout}} --health-timeout {{.HealthCheck.Timeout}}{{end}}
{{end}}
{{.Image}}{{if .Command}} {{.Command}}{{end}}`
// writeScript will write a shell script to the given tempfile for the given commands
func
(
r
*
Run
)
writeScript
(
name
string
,
st
*
state
.
State
,
fullEnv
[]
string
)
(
string
,
string
,
string
,
error
)
{
// make sure the scripts directory exist in our state directory
scriptsDir
:=
filepath
.
Join
(
st
.
Directory
,
name
)
err
:=
os
.
MkdirAll
(
scriptsDir
,
0700
)
if
err
!=
nil
{
return
""
,
""
,
""
,
err
}
// create the temp file to write the script to
scriptFile
:=
filepath
.
Join
(
scriptsDir
,
"script"
)
entryPoint
:=
r
.
Shell
if
entryPoint
==
""
{
entryPoint
=
"/bin/sh"
}
// Scripts must retain order, so don't use st.MapSlice to
// expand things (which results in a non-deterministically
// ordered slice of the expanded input). It also doesn't
// make sense to expand things here anyway - use a loop in
// bash if you need that kind of control.
scripts
,
err
:=
environment
.
SliceMapper
(
r
.
Script
,
fullEnv
)
if
err
!=
nil
{
os
.
Remove
(
scriptFile
)
return
""
,
""
,
""
,
err
}
// set the run command argument to the script file
commandArg
:=
scriptFile
// write the script to the file
ioutil
.
WriteFile
(
scriptFile
,
[]
byte
(
strings
.
Join
(
scripts
,
"\n"
)),
0700
,
)
// return it all
return
scriptFile
,
entryPoint
,
commandArg
,
nil
}
// detach will run the container detached
func
(
r
*
Run
)
detach
(
name
,
runTemplate
string
,
params
map
[
string
]
interface
{},
st
*
state
.
State
,
logger
*
gomol
.
LogAdapter
)
error
{
stdout
,
stderr
,
err
:=
DockerOutput
(
name
,
runTemplate
,
params
,
st
)
if
err
!=
nil
{
logger
.
Errorf
(
"%s"
,
stderr
)
return
err
}
cid
:=
strings
.
TrimSpace
(
stdout
)
st
.
Cleanup
(
func
(
logger
*
gomol
.
LogAdapter
)
{
logger
.
Debugf
(
"stopping container %s"
,
cid
)
err
:=
StopContainer
(
cid
,
logger
,
st
)
if
err
!=
nil
{
logger
.
Warnf
(
"failed to stop container %s: %s"
,
cid
,
err
.
Error
())
}
else
{
logger
.
Infof
(
"stopped container %s"
,
cid
)
}
})
logger
.
Infof
(
"started detached container %s"
,
cid
)
logger
.
Infof
(
"checking for healthcheck"
)
// check if the container has a health check
hasHealth
,
err
:=
containerHasHealthCheck
(
name
,
cid
,
st
,
logger
)
if
err
!=
nil
{
return
err
}
if
hasHealth
{
healthChan
:=
make
(
chan
error
)
logger
.
Infof
(
"waiting for container to go healthy"
)
go
func
()
{
duration
:=
5
*
time
.
Second
for
{
// check the health
healthy
,
err
:=
containerIsHealthy
(
name
,
cid
,
st
,
logger
)
if
err
!=
nil
{
healthChan
<-
err
}
if
healthy
{
healthChan
<-
nil
}
logger
.
Infof
(
"container still not healthy, waiting %v"
,
duration
)
time
.
Sleep
(
duration
)
}
}()
err
:=
<-
healthChan
if
err
!=
nil
{
return
err
}
logger
.
Infof
(
"container is ready"
)
}
else
{
logger
.
Infof
(
"no healthcheck found"
)
}
return
nil
}
func
(
r
*
Run
)
mapVariables
(
fullEnv
[]
string
,
st
*
state
.
State
,
workSpace
string
)
(
map
[
string
]
interface
{},
error
)
{
dockerWorkspace
:=
st
.
Workspace
.(
*
Workspace
)
username
,
err
:=
environment
.
Mapper
(
r
.
User
,
fullEnv
)
if
err
!=
nil
{
return
nil
,
err
}
image
,
err
:=
environment
.
Mapper
(
r
.
Image
,
fullEnv
)
if
err
!=
nil
{
return
nil
,
err
}
hostname
,
err
:=
environment
.
Mapper
(
r
.
Hostname
,
fullEnv
)
if
err
!=
nil
{
return
nil
,
err
}
labels
,
err
:=
st
.
MapSlice
(
r
.
Labels
,
fullEnv
)
if
err
!=
nil
{
return
nil
,
err
}
workspacePath
,
err
:=
environment
.
Mapper
(
dockerWorkspace
.
mountPoint
,
fullEnv
)
if
err
!=
nil
{
return
nil
,
err
}
workspaceMount
,
err
:=
environment
.
Mapper
(
workSpace
,
fullEnv
)
if
err
!=
nil
{
return
nil
,
err
}
// we do workDir after workspaceMount so we can put workspaceMount into an
// environment that workDir will be mapped against.
wsEnv
:=
environment
.
Merge
(
fullEnv
,
[]
string
{
"CONVEY_WORKSPACE="
+
workspaceMount
})
workdir
,
err
:=
environment
.
Mapper
(
r
.
WorkDir
,
wsEnv
)
if
err
!=
nil
{
return
nil
,
err
}
return
map
[
string
]
interface
{}{
"username"
:
username
,
"image"
:
image
,
"hostname"
:
hostname
,
"labels"
:
labels
,
"workdir"
:
workdir
,
"workspacePath"
:
workspacePath
,
"workspaceMount"
:
workspaceMount
,
},
nil
}
// Execute runs the run task.
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
.
GetEnv
())
user
,
err
:=
user
.
Current
()
if
err
!=
nil
{
return
err
}
// now expand the environment
for
idx
,
v
:=
range
fullEnv
{
v
,
err
:=
environment
.
Mapper
(
v
,
fullEnv
)
if
err
!=
nil
{
return
err
}
fullEnv
[
idx
]
=
v
}
// assign a default workspace location
workSpace
:=
r
.
WorkSpace
if
workSpace
==
""
{
workSpace
=
"/workspace"
}
// initialize some variables
scriptFile
:=
""
entryPoint
:=
r
.
EntryPoint
commandArg
,
err
:=
environment
.
Mapper
(
r
.
Command
,
fullEnv
)
if
err
!=
nil
{
return
err
}
// if we're using a script defined in the yaml, create it and override
// some variables
if
len
(
r
.
Script
)
>
0
{
scriptFile
,
entryPoint
,
commandArg
,
err
=
r
.
writeScript
(
name
,
st
,
fullEnv
)
if
err
!=
nil
{
return
err
}
}
taskLabel
:=
normalize
.
Normalize
(
fmt
.
Sprintf
(
"convey-%d-task=%s"
,
os
.
Getpid
(),
name
),
)
//
// Map variables
vars
,
err
:=
r
.
mapVariables
(
fullEnv
,
st
,
workSpace
)
if
err
!=
nil
{
return
err
}
runID
:=
util
.
ShortID
()
logger
.
Infof
(
"running container with id %s"
,
runID
)
// build the dict for the template
params
:=
map
[
string
]
interface
{}{
"Command"
:
commandArg
,
"CPUShares"
:
st
.
CPUShares
,
"Detach"
:
r
.
Detach
,
"Hostname"
:
vars
[
"hostname"
],
"Environment"
:
environment
.
Prune
(
fullEnv
),
"EntryPoint"
:
entryPoint
,
"Username"
:
vars
[
"username"
],
"UID"
:
user
.
Uid
,
"GID"
:
user
.
Gid
,
"HealthCheck"
:
r
.
HealthCheck
,
"Image"
:
vars
[
"image"
],
"Labels"
:
vars
[
"labels"
],
"Memory"
:
st
.
Memory
,
"Network"
:
st
.
Network
.
Name
(),
"ScriptFile"
:
scriptFile
,
"SSHAgent"
:
st
.
EnableSSHAgent
,
"SSHAuthSock"
:
os
.
Getenv
(
"SSH_AUTH_SOCK"
),
"WorkDir"
:
vars
[
"workdir"
],
"WorkspacePath"
:
vars
[
"workspacePath"
],
"WorkspaceMount"
:
vars
[
"workspaceMount"
],
"TaskLabel"
:
taskLabel
,
"Name"
:
runID
,
}
if
r
.
Detach
{
return
r
.
detach
(
name
,
runTemplate
,
params
,
st
,
logger
)
}
// Mark running so we can detach this container form the network
// when we've got a signal to shutdown. An active network cannot
// be removed, so we need to track things to kill along with it.
// This is no longer a problem once the subprocess returns.
st
.
MarkRunning
(
runID
)
defer
st
.
UnmarkRunning
(
runID
)
// run the command
return
Docker
(
name
,
runTemplate
,
params
,
st
)
}
// New creates a new run task.
func
(
r
*
Run
)
New
()
tasks
.
Task
{
return
&
Run
{}
}
// Valid validates the run task.
func
(
r
*
Run
)
Valid
()
error
{
if
r
.
Image
==
""
{
return
errNoImage
}
return
nil
}