grim/convey

closing merged branch
hostnames
2017-10-13, Gary Kramlich
33eae19fcbbe
closing merged branch
/*
* 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"
"os"
"path/filepath"
"strings"
"github.com/aphistic/gomol"
"bitbucket.org/rw_grim/convey/environment"
"bitbucket.org/rw_grim/convey/state"
"bitbucket.org/rw_grim/convey/tasks"
"bitbucket.org/rw_grim/convey/yaml"
)
type Export struct {
Files yaml.StringOrSlice `yaml:"files"`
}
const exportTemplate = `cp {{.workspace}}:/workspace/{{.source}} {{.destination}}`
const zglobTemplate = `run --rm -v {{.workspace}}:/workspace convey/workspace-tools:latest zglob {{.file}}`
func checkFilePattern(file string) error {
if strings.ContainsRune(file, '*') && strings.ContainsRune(file, ':') {
return errWildcardWithDestination
}
return nil
}
func exportFile(name, workSpace, src, dest string, st *state.State) error {
params := map[string]interface{}{
"source": src,
"destination": dest,
"workspace": workSpace,
}
dir := filepath.Dir(dest)
if err := os.MkdirAll(dir, 0777); err != nil {
return err
}
return Docker(name, exportTemplate, params, st)
}
func exportGlob(name, workSpace, mountPoint, pattern string, st *state.State) error {
params := map[string]interface{}{
"file": pattern,
"workspace": mountPoint,
}
out, _, err := DockerOutput(fmt.Sprintf("%s-zglob", name), zglobTemplate, params, st)
if err != nil {
return err
}
for _, match := range strings.Split(out, "\n") {
if match == "" {
continue
}
exportFile(name, workSpace, match, tasks.DestFromSrc(match), st)
}
return nil
}
func (e *Export) Execute(name string, logger *gomol.LogAdapter, env []string, st *state.State) error {
fullEnv := environment.Merge(env, st.Environment)
files, err := st.MapSlice(e.Files, fullEnv)
if err != nil {
return err
}
for _, file := range files {
if err := checkFilePattern(file); err != nil {
return err
}
dockerWorkspace := st.Workspace.(*Workspace)
if strings.ContainsRune(file, '*') {
mountPoint, err := environment.Mapper(dockerWorkspace.mountPoint, fullEnv)
if err != nil {
return err
}
if err := exportGlob(name, st.Workspace.Name(), mountPoint, file, st); err != nil {
return err
}
} else {
src, dest := tasks.ParseFilePath("", file)
if err := exportFile(name, st.Workspace.Name(), src, dest, st); err != nil {
return err
}
}
}
return nil
}
func (e *Export) New() tasks.Task {
return &Export{}
}
func (e *Export) Valid() error {
if len(e.Files) == 0 {
return errNoFiles
}
for _, file := range e.Files {
if err := checkFilePattern(file); err != nil {
return err
}
}
return nil
}