Skip to content

Commit

Permalink
Merge pull request #14 from wharfr/ext-image
Browse files Browse the repository at this point in the history
Scriptable crate Image
  • Loading branch information
qur committed Sep 30, 2019
2 parents 61169c8 + 7ecd477 commit ce742ca
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
3 changes: 3 additions & 0 deletions docs/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ default values (if the default is not empty):
+---------------+------------------+-------------------------------------------+
| image | string | name of image to create container from |
+---------------+------------------+-------------------------------------------+
| image-cmd | string | a script to run to determine the image |
| | | name (instead of using image). |
+---------------+------------------+-------------------------------------------+
| mount-home | bool | should /home be mounted into container |
| | | (default: true) |
+---------------+------------------+-------------------------------------------+
Expand Down
29 changes: 29 additions & 0 deletions lib/config/crate.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"io/ioutil"
"log"
"os"
"os/exec"
"os/user"
"path/filepath"
"strings"
Expand All @@ -32,6 +33,7 @@ type Crate struct {
Groups []string `toml:"groups"`
Hostname string `toml:"hostname"`
Image string `toml:"image"`
ImageCmd string `toml:"image-cmd"`
MountHome bool `toml:"mount-home"`
Network string `toml:"network"`
Ports []string `toml:"ports"`
Expand Down Expand Up @@ -100,12 +102,39 @@ func GetCrate(start, name string, ls LabelSource) (*Crate, error) {
return openCrate(project, crateName, branch, ls)
}

func runImageCmd(command string, projectDir string) (string, error) {
shell := []string{"sh"}
if strings.HasPrefix(command, "#!") {
hashBang := strings.Split(command, "\n")[0]
shell = strings.Split(strings.TrimSpace(hashBang[2:]), " ")
}
buf := &bytes.Buffer{}
cmd := exec.Command(shell[0], shell[1:]...)
cmd.Stdin = strings.NewReader(command)
cmd.Stdout = buf
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return "", err
}
return strings.TrimSpace(buf.String()), nil
}

func openCrate(project *Project, crateName, branch string, ls LabelSource) (*Crate, error) {
crate, ok := project.Crates[crateName]
if !ok {
return nil, CrateNotFound
}

if crate.ImageCmd != "" {
image, err := runImageCmd(crate.ImageCmd, filepath.Dir(project.path))
if err != nil {
return nil, fmt.Errorf("image-cmd failed: %s", err)
}
if image != "" {
crate.Image = image
}
}

if crate.Image == "" {
return nil, fmt.Errorf("image is a required parameter")
}
Expand Down

0 comments on commit ce742ca

Please sign in to comment.