grim/convey
Clone
Summary
Browse
Changes
Graph
Normalize labels for docker run. Fixes #121
2017-09-12, Gary Kramlich
bd8085fe9a80
Normalize labels for docker run. Fixes #121
/*
* 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
workspace
import
(
"strings"
"time"
"github.com/aphistic/gomol"
"github.com/satori/go.uuid"
"bitbucket.org/rw_grim/convey/command"
"bitbucket.org/rw_grim/convey/logging"
)
type
Workspace
struct
{
Name
string
VolumeName
string
MountPoint
string
logger
*
gomol
.
LogAdapter
}
const
(
createTemplate
=
`docker create --name={{.Name}} convey/workspace`
destroyTemplate
=
`docker rm -v {{.Name}}`
volumeMountTemplate
=
`docker inspect --format "{{.Format}}" {{.Volume}}`
volumeMountFormat
=
`{{range .Mounts}}{{if eq .Destination \"/workspace\" }}{{.Source}}{{end}}{{end}}`
)
func
(
ws
*
Workspace
)
findMountPoint
()
error
{
params
:=
map
[
string
]
interface
{}{
"Volume"
:
ws
.
VolumeName
,
"Format"
:
volumeMountFormat
,
}
out
,
_
,
err
:=
command
.
RunOutput
(
"findMountPoint"
,
volumeMountTemplate
,
params
,
time
.
Minute
)
if
err
!=
nil
{
return
err
}
ws
.
MountPoint
=
strings
.
TrimSpace
(
out
)
return
nil
}
func
Create
()
(
*
Workspace
,
error
)
{
ws
:=
&
Workspace
{
Name
:
uuid
.
NewV4
().
String
(),
logger
:
logging
.
NewAdapter
(
"workspace"
),
}
params
:=
map
[
string
]
interface
{}{
"Name"
:
ws
.
Name
,
}
out
,
_
,
err
:=
command
.
RunOutput
(
"create workspace"
,
createTemplate
,
params
,
time
.
Minute
)
if
err
!=
nil
{
return
nil
,
err
}
ws
.
VolumeName
=
strings
.
TrimSpace
(
out
)
err
=
ws
.
findMountPoint
()
if
err
!=
nil
{
return
nil
,
err
}
ws
.
logger
.
Debugf
(
"created workspace: %#v"
,
ws
)
return
ws
,
nil
}
func
(
ws
*
Workspace
)
Destroy
()
error
{
params
:=
map
[
string
]
interface
{}{
"Name"
:
ws
.
Name
,
}
return
command
.
Run
(
"remove workspace"
,
destroyTemplate
,
params
,
time
.
Minute
)
}