Skip to content

Commit

Permalink
Allow specifying a docker-image-build-folder to allow access to local…
Browse files Browse the repository at this point in the history
… files on docker-image-build

If --name is added to docker options, no --rm is added
  • Loading branch information
jocgir committed Feb 8, 2018
1 parent e804b13 commit b0ffc04
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 50 deletions.
10 changes: 5 additions & 5 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ builds:
- linux
goarch:
- amd64
- 386
ignore:
- goos: darwin
goarch: 386

# Archive customization
archive:
format: zip

replacements:
amd64: 64-bits
386: 32-bits
darwin: macOS

files:
- nothing.*

# GitHub release customization
release:
draft: true
prerelease: true
2 changes: 0 additions & 2 deletions .prerelease.yml

This file was deleted.

4 changes: 0 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ script:
fi
- if [ ${TRAVIS_TAG::1} = v ];
then
if [[ $TRAVIS_TAG =~ .*-.* ]];
then
cat .prerelease.yml >> .goreleaser.yml && set -- --skip-validate;
fi;
curl -sL https://git.io/goreleaser | bash /dev/stdin $@;
fi

Expand Down
28 changes: 12 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,19 @@ or install it through command line:
On `OSX`:

```bash
curl -sL https://github.com/coveo/tgf/releases/download/v1.16.0/tgf_1.16.0_macOS_64-bits.zip | bsdtar -xf- -C /usr/local/bin
curl -sL https://github.com/coveo/tgf/releases/download/v1.16.1/tgf_1.16.1_macOS_64-bits.zip | bsdtar -xf- -C /usr/local/bin
```

On `Linux`:

```bash
curl -sL https://github.com/coveo/tgf/releases/download/v1.16.0/tgf_1.16.0_linux_64-bits.zip | gzip -d > /usr/local/bin/tgf && chmod +x /usr/local/bin/tgf
curl -sL https://github.com/coveo/tgf/releases/download/v1.16.1/tgf_1.16.1_linux_64-bits.zip | gzip -d > /usr/local/bin/tgf && chmod +x /usr/local/bin/tgf
```

On `Windows` with Powershell:

```powershell
Invoke-WebRequest https://github.com/coveo/tgf/releases/download/v1.16.0/tgf_1.16.0_windows_64-bits.zip -OutFile tgf.zip
Invoke-WebRequest https://github.com/coveo/tgf/releases/download/v1.16.1/tgf_1.16.1_windows_64-bits.zip -OutFile tgf.zip
```

## Configuration
Expand Down Expand Up @@ -90,6 +90,7 @@ Key | Description | Default value
| docker-image-version | Identify the image version |
| docker-image-tag | Identify the image tag (could specify specialized version such as k8s, full) | latest
| docker-image-build | List of Dockerfile instructions to customize the specified docker image) |
| docker-image-build-folder | Folder where the docker build command should be executed |
| docker-refresh | Delay before checking if a newer version of the docker image is available | 1h (1 hour)
| docker-options | Additional options to supply to the Docker command |
| logging-level | Terragrunt logging level (only apply to Terragrunt entry point).<br>*Critical (0), Error (1), Warning (2), Notice (3), Info (4), Debug (5), Full (6)* | Notice
Expand Down Expand Up @@ -130,31 +131,26 @@ section | Description
> tgf
usage: tgf [<flags>]
DESCRIPTION: TGF (terragrunt frontend) is a Docker frontend for terragrunt/terraform. It automatically maps your current folder, your HOME folder, your TEMP folder as well of most
environment variables to the docker process. You can add -D to your command to get the exact docker command that is generated.
DESCRIPTION: TGF (terragrunt frontend) is a Docker frontend for terragrunt/terraform. It automatically maps your current folder, your HOME folder, your TEMP folder as well of most environment variables to the docker process. You can add -D to your command to get the exact docker command that is generated.
It then looks in your current folder and all its parents to find a file named '.tgf.config' to retrieve the default configuration. If not all configurable values are satisfied and you have an AWS configuration,
it will then try to retrieve the missing elements from the AWS Parameter Store under the key '/default/tgf'.
It then looks in your current folder and all its parents to find a file named '.tgf.config' to retrieve the default configuration. If not all configurable values are satisfied and you have an AWS configuration, it will then try to retrieve the missing elements from the AWS Parameter Store under the key '/default/tgf'.
Configurable values are: docker-image, docker-image-version, docker-image-tag, docker-image-build, docker-refresh, docker-options, recommended-image-version, required-image-version, logging-level,
entry-point, tgf-recommended-version.
Configurable values are: docker-image, docker-image-version, docker-image-tag, docker-image-build, docker-image-build-folder, docker-refresh, docker-options, recommended-image-version, required-image-version, logging-level, entry-point, tgf-recommended-version.
You can get the full documentation at https://github.com/coveo/tgf/blob/master/README.md and check for new version at https://github.com/coveo/tgf/releases/latest.
Any docker image could be used, but TGF specialized images could be found at: https://hub.docker.com/r/coveo/tgf/tags.
Terragrunt documentation could be found at https://github.com/coveo/terragrunt/blob/master/README.md (Coveo fork) or https://github.com/gruntwork-io/terragrunt/blob/master/README.md
(Gruntwork.io original)
Terragrunt documentation could be found at https://github.com/coveo/terragrunt/blob/master/README.md (Coveo fork) or https://github.com/gruntwork-io/terragrunt/blob/master/README.md (Gruntwork.io original)
Terraform documentation could be found at https://www.terraform.io/docs/index.html.
IMPORTANT: Most of the tgf command line arguments are in uppercase to avoid potential conflict with the underlying command. If any of the tgf arguments conflicts with an argument of the desired entry point, you
must place that argument after -- to ensure that they are not interpreted by tgf and are passed to the entry point. Any non conflicting argument will be passed to the entry point wherever it is located on the
invocation arguments.
IMPORTANT: Most of the tgf command line arguments are in uppercase to avoid potential conflict with the underlying command. If any of the tgf arguments conflicts with an argument of the desired entry point, you must place that argument after -- to ensure that they are not interpreted by tgf and are passed to the entry point. Any non
conflicting argument will be passed to the entry point wherever it is located on the invocation arguments.
tgf ls -- -D # Avoid -D to be interpretated by tgf as --debug-docker
VERSION: 1.16.0
VERSION: 1.16.1
AUTHOR: Coveo
Expand Down Expand Up @@ -182,7 +178,7 @@ Example:

```bash
> tgf --current-version
tgf v1.16.0
tgf v1.16.1
```

Returns the current version of the tgf tool
Expand Down
14 changes: 14 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const (
dockerImageVersion = "docker-image-version"
dockerImageTag = "docker-image-tag"
dockerImageBuild = "docker-image-build"
dockerImageBuildFolder = "docker-image-build-folder"
dockerRefresh = "docker-refresh"
dockerOptionsTag = "docker-options"
loggingLevel = "logging-level"
Expand All @@ -48,6 +49,7 @@ type TGFConfig struct {
ImageVersion *string
ImageTag *string
ImageBuild string
ImageBuildFolder string
LogLevel string
EntryPoint string
Refresh time.Duration
Expand Down Expand Up @@ -87,6 +89,7 @@ func (config TGFConfig) String() (result string) {
ifNotZero(dockerImage, config.Image)
ifNotZero(dockerImageVersion, config.ImageVersion)
ifNotZero(dockerImageTag, config.ImageTag)
ifNotZero(dockerImageBuildFolder, config.ImageBuildFolder)
if config.ImageBuild != "" {
lines := strings.Split(strings.TrimSpace(config.ImageBuild), "\n")
buildScript := lines[0]
Expand Down Expand Up @@ -163,6 +166,13 @@ func (config *TGFConfig) SetDefaultValues() {
}
sort.Strings(keys)
for _, key := range keys {
if key == dockerImageBuildFolder {
folder := fmt.Sprint(content[key])
// If the build folder is relative, we make it relative to the config file folder where it is declared
if !filepath.IsAbs(folder) {
content[key] = filepath.Join(filepath.Dir(configFile), folder)
}
}
config.SetValue(key, content[key])
}
default:
Expand Down Expand Up @@ -234,6 +244,10 @@ func (config *TGFConfig) SetValue(key string, value interface{}) {
case dockerImageBuild:
// We concatenate the various levels of docker build instructions
config.ImageBuild = strings.Join([]string{strings.TrimSpace(valueStr), strings.TrimSpace(config.ImageBuild)}, "\n")
case dockerImageBuildFolder:
if config.ImageBuildFolder == "" {
config.ImageBuildFolder = valueStr
}
case recommendedImageVersion:
if config.RecommendedImageVersion == "" {
config.RecommendedImageVersion = valueStr
Expand Down
33 changes: 24 additions & 9 deletions docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ func callDocker(args ...string) int {
"run", "-it",
"-v", fmt.Sprintf("%s%s:%s", convertDrive(currentDrive), rootFolder, filepath.ToSlash(filepath.Join("/", mountPoint, rootFolder))),
"-w", sourceFolder,
"--rm",
}
if !noHome {
currentUser := Must(user.Current()).(*user.User)
Expand Down Expand Up @@ -109,6 +108,12 @@ func callDocker(args ...string) int {
for _, do := range dockerOptions {
dockerArgs = append(dockerArgs, strings.Split(do, " ")...)
}

if !util.ListContainsElement(dockerArgs, "--name") {
// We do not remove the image after execution if a name has been provided
dockerArgs = append(dockerArgs, "--rm")
}

dockerArgs = append(dockerArgs, getEnviron(!noHome)...)
dockerArgs = append(dockerArgs, imageName)
dockerArgs = append(dockerArgs, command...)
Expand Down Expand Up @@ -167,29 +172,39 @@ func runCommands(commands []string) error {
// Returns the image name to use
// If docker-image-build option has been set, an image is dynamically built and the resulting image digest is returned
func getImage() string {
if config.ImageBuild == "" {
if config.ImageBuild == "" && config.ImageBuildFolder == "" {
return config.GetImageName()
}

var dockerFile string
dockerFile += fmt.Sprintln("FROM", config.GetImageName())
dockerFile += fmt.Sprintln(config.ImageBuild)
if config.ImageBuildFolder == "" {
config.ImageBuildFolder = "."
}

tempDir := Must(ioutil.TempDir("", "tgf-docker")).(string)
PanicOnError(ioutil.WriteFile(fmt.Sprintf("%s/Dockerfile", tempDir), []byte(dockerFile), 0644))
defer os.RemoveAll(tempDir)
var dockerFile string
if config.ImageBuild != "" {
out := Must(ioutil.TempFile(config.ImageBuildFolder, "DockerFile")).(*os.File)
Must(fmt.Fprintf(out, "FROM %s \n%s", config.GetImageName(), config.ImageBuild))
Must(out.Close())
defer os.Remove(out.Name())
dockerFile = out.Name()
}

args := []string{"build", tempDir, "--quiet", "--force-rm"}
args := []string{"build", config.ImageBuildFolder, "--quiet", "--force-rm"}
if refresh {
args = append(args, "--pull")
}
if dockerFile != "" {
args = append(args, "--file")
args = append(args, dockerFile)
}
buildCmd := exec.Command("docker", args...)

if debug {
printfDebug(os.Stderr, "%s\n", strings.Join(buildCmd.Args, " "))
printfDebug(os.Stderr, "%s", dockerFile)
}
buildCmd.Stderr = os.Stderr
buildCmd.Dir = config.ImageBuildFolder

return strings.TrimSpace(string(Must(buildCmd.Output()).([]byte)))
}
Expand Down
24 changes: 12 additions & 12 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"html/template"
"os"
"path/filepath"
"strings"

"github.com/fatih/color"
Expand Down Expand Up @@ -80,7 +81,7 @@ func main() {
"parameterStoreKey": parameterFolder,
"config": configFile,
"options": color.GreenString(strings.Join([]string{
dockerImage, dockerImageVersion, dockerImageTag, dockerImageBuild, dockerRefresh, dockerOptionsTag,
dockerImage, dockerImageVersion, dockerImageTag, dockerImageBuild, dockerImageBuildFolder, dockerRefresh, dockerOptionsTag,
recommendedImageVersion, requiredImageVersion, loggingLevel, entryPoint, tgfVersion,
}, ", ")),
"readme": link(gitSource + "/blob/master/README.md"),
Expand Down Expand Up @@ -182,7 +183,7 @@ func main() {
}

if *getAllVersions {
if config.EntryPoint != "terragrunt" {
if filepath.Base(config.EntryPoint) != "terragrunt" {
fmt.Fprintln(os.Stderr, errorString("--all-version works only with terragrunt as the entrypoint"))
os.Exit(1)
}
Expand Down

0 comments on commit b0ffc04

Please sign in to comment.