Skip to content

Commit

Permalink
add exit_if_unreachable source config setting for #66
Browse files Browse the repository at this point in the history
  • Loading branch information
xorpaul committed Aug 4, 2017
1 parent 58958e9 commit f26bfa6
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 8 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,27 @@ go get
BUILDTIME=$(date -u '+%Y-%m-%d_%H:%M:%S') && go build -ldflags "-s -w -X main.buildtime=$BUILDTIME"
```

- abort g10k run if source repository is unreachable

```
---
:cachedir: '/tmp/g10k'
sources:
example:
remote: 'git://github.com/xorpaul/g10k-environment-unavailable.git'
basedir: '/tmp/example/'
exit_if_unreachable: true
```

If you then call g10k with that config file. You should get:

```
WARN: git repository git://github.com/xorpaul/g10k-environment-unavailable.git does not exist or is unreachable at this moment!
WARNING: Could not resolve git repository in source 'example' (git://github.com/xorpaul/g10k-environment-unavailable.git)
```
with an exit code 1

# execute example with debug output
```
./g10k -debug -config test.yaml
Expand Down
4 changes: 3 additions & 1 deletion g10k.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ var (
moduleDirParam string
cacheDirParam string
branchParam string
configFile string
config ConfigSettings
wg sync.WaitGroup
mutex sync.Mutex
Expand Down Expand Up @@ -81,6 +82,7 @@ type Source struct {
PrivateKey string `yaml:"private_key"`
ForceForgeVersions bool `yaml:"force_forge_versions"`
WarnMissingBranch bool `yaml:"warn_if_branch_is_missing"`
ExitIfUnreachable bool `yaml:"exit_if_unreachable"`
}

// Puppetfile contains the key value pairs from the Puppetfile
Expand Down Expand Up @@ -156,7 +158,7 @@ func main() {
flag.BoolVar(&quiet, "quiet", false, "no output, defaults to false")
flag.Parse()

configFile := *configFileFlag
configFile = *configFileFlag
version := *versionFlag

if version {
Expand Down
53 changes: 53 additions & 0 deletions g10k_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,3 +369,56 @@ func TestModuleDirOverride(t *testing.T) {
t.Error("Expected '", moduleDirParam, "' for module dir, but got", got.moduleDir)
}
}

func TestResolvConfigExitIfUnreachable(t *testing.T) {
funcName := strings.Split(funcName(), ".")[len(strings.Split(funcName(), "."))-1]
config = readConfigfile("tests/TestConfigExitIfUnreachable.yaml")
purgeDir(config.CacheDir, "TestResolvConfigExitIfUnreachable()")
if os.Getenv("TEST_FOR_CRASH_"+funcName) == "1" {
resolvePuppetEnvironment("single")
return
}

cmd := exec.Command(os.Args[0], "-test.run="+funcName+"$")
cmd.Env = append(os.Environ(), "TEST_FOR_CRASH_"+funcName+"=1")
out, err := cmd.CombinedOutput()

exitCode := 0
if msg, ok := err.(*exec.ExitError); ok { // there is error code
exitCode = msg.Sys().(syscall.WaitStatus).ExitStatus()
}

if 1 != exitCode {
t.Errorf("resolvePuppetEnvironment() terminated with %v, but we expected exit status %v", exitCode, 1)
}
//fmt.Println(string(out))
if !strings.Contains(string(out), "WARN: git repository git://github.com/xorpaul/g10k-environment-unavailable.git does not exist or is unreachable at this moment!\nWARNING: Could not resolve git repository in source 'example' (git://github.com/xorpaul/g10k-environment-unavailable.git)") {
t.Errorf("resolvePuppetEnvironment() terminated with the correct exit code, but the expected output was missing")
}
}

func TestResolvConfigExitIfUnreachableFalse(t *testing.T) {
funcName := strings.Split(funcName(), ".")[len(strings.Split(funcName(), "."))-1]
config = readConfigfile("tests/TestConfigExitIfUnreachableFalse.yaml")
purgeDir(config.CacheDir, "TestResolvConfigExitIfUnreachableFalse()")
if os.Getenv("TEST_FOR_CRASH_"+funcName) == "1" {
resolvePuppetEnvironment("single")
return
}

cmd := exec.Command(os.Args[0], "-test.run="+funcName+"$")
cmd.Env = append(os.Environ(), "TEST_FOR_CRASH_"+funcName+"=1")
out, err := cmd.CombinedOutput()

exitCode := 0
if msg, ok := err.(*exec.ExitError); ok { // there is error code
exitCode = msg.Sys().(syscall.WaitStatus).ExitStatus()
}

if 0 != exitCode {
t.Errorf("resolvePuppetEnvironment() terminated with %v, but we expected exit status %v", exitCode, 1)
}
if !strings.Contains(string(out), "WARN: git repository git://github.com/xorpaul/g10k-environment-unavailable.git does not exist or is unreachable at this moment!\nWARNING: Could not resolve git repository in source 'example' (git://github.com/xorpaul/g10k-environment-unavailable.git)") {
t.Errorf("resolvePuppetEnvironment() terminated with the correct exit code, but the expected output was missing")
}
}
2 changes: 1 addition & 1 deletion git.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func doMirrorOrUpdate(url string, workDir string, sshPrivateKey string, allowFai
}

if er.returnCode != 0 {
fmt.Println("WARN: git repository " + url + " does not exist or is unreachable at this moment!")
Warnf("WARN: git repository " + url + " does not exist or is unreachable at this moment!")
return false
}
return true
Expand Down
31 changes: 25 additions & 6 deletions puppetfile.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"fmt"
"io/ioutil"
"os"
"strings"
Expand All @@ -9,6 +10,21 @@ import (
"github.com/henvic/uiprogress"
)

// sourceSanityCheck is a validation function that checks if the given source has all neccessary attributes (basedir, remote, SSH key exists if given)
func sourceSanityCheck(source string, sa Source) {
if len(sa.PrivateKey) > 0 {
if _, err := os.Stat(sa.PrivateKey); err != nil {
Fatalf("resolvePuppetEnvironment(): could not find SSH private key " + sa.PrivateKey + " for source " + source + " in config file " + configFile + " Error: " + err.Error())
}
}
if len(sa.Basedir) <= 0 {
Fatalf("resolvePuppetEnvironment(): config setting basedir is not set for source " + source + " in config file " + configFile)
}
if len(sa.Remote) <= 0 {
Fatalf("resolvePuppetEnvironment(): config setting remote is not set for source " + source + " in config file " + configFile)
}
}

func resolvePuppetEnvironment(envBranch string) {
allPuppetfiles := make(map[string]Puppetfile)
for source, sa := range config.Sources {
Expand All @@ -20,12 +36,10 @@ func resolvePuppetEnvironment(envBranch string) {
}

sa.Basedir = checkDirAndCreate(sa.Basedir, "basedir for source "+source)
Debugf("Puppet environment: " + source + " (remote=" + sa.Remote + ", basedir=" + sa.Basedir + ", private_key=" + sa.PrivateKey + ", prefix=" + sa.Prefix + ")")
if len(sa.PrivateKey) > 0 {
if _, err := os.Stat(sa.PrivateKey); err != nil {
Fatalf("resolvePuppetEnvironment(): could not find SSH private key " + sa.PrivateKey + "error: " + err.Error())
}
}
Debugf("Puppet environment: " + source + " (" + fmt.Sprintf("%+v", sa) + ")")

// check for a valid source that has all neccessary attributes (basedir, remote, SSH key exists if given)
sourceSanityCheck(source, sa)

workDir := config.EnvCacheDir + source + ".git"
// check if sa.Basedir exists
Expand Down Expand Up @@ -83,6 +97,11 @@ func resolvePuppetEnvironment(envBranch string) {
if sa.WarnMissingBranch && !foundBranch {
Warnf("WARNING: Couldn't find specified branch '" + envBranch + "' anywhere in source '" + source + "' (" + sa.Remote + ")")
}
} else {
Warnf("WARNING: Could not resolve git repository in source '" + source + "' (" + sa.Remote + ")")
if sa.ExitIfUnreachable == true {
os.Exit(1)
}
}
}(source, sa)
}
Expand Down
8 changes: 8 additions & 0 deletions tests/TestConfigExitIfUnreachable.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
:cachedir: '/tmp/g10k'

sources:
example:
remote: 'git://github.com/xorpaul/g10k-environment-unavailable.git'
basedir: '/tmp/example/'
exit_if_unreachable: true
8 changes: 8 additions & 0 deletions tests/TestConfigExitIfUnreachableFalse.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
:cachedir: '/tmp/g10k'

sources:
example:
remote: 'git://github.com/xorpaul/g10k-environment-unavailable.git'
basedir: '/tmp/example/'
exit_if_unreachable: false

0 comments on commit f26bfa6

Please sign in to comment.