Skip to content
This repository has been archived by the owner on May 12, 2022. It is now read-only.

Commit

Permalink
Merge pull request #12 from esmet/config
Browse files Browse the repository at this point in the history
Config updates
  • Loading branch information
esmet authored Feb 1, 2018
2 parents 726e9f4 + 1382fce commit 5411b96
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 34 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ ankh:

.PHONY: install
install: ankh
sudo cp -f $(REPOROOT)/bin/ankh /usr/local/bin/ankh-go
sudo cp -f $(REPOROOT)/bin/ankh /usr/local/bin/ankh
10 changes: 5 additions & 5 deletions src/ankh/ankh.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ type ExecutionContext struct {

Verbose, DryRun, Apply bool

ConfigPath string
AnkhConfigPath string
KubeConfigPath string
DataDir string
KubeConfig string

Logger *logrus.Logger
}
Expand Down Expand Up @@ -124,9 +124,9 @@ func ParseAnkhFile(filename string) (AnkhFile, error) {
func GetAnkhConfig(ctx *ExecutionContext) (AnkhConfig, error) {
ankhConfig := AnkhConfig{}

ankhRcFile, err := ioutil.ReadFile(ctx.ConfigPath)
ankhRcFile, err := ioutil.ReadFile(ctx.AnkhConfigPath)
if err != nil {
return ankhConfig, fmt.Errorf("unable to read ankh config '%s': %v", ctx.ConfigPath, err)
return ankhConfig, fmt.Errorf("unable to read ankh config '%s': %v", ctx.AnkhConfigPath, err)
}

if err := os.MkdirAll(ctx.DataDir, 0755); err != nil {
Expand All @@ -135,7 +135,7 @@ func GetAnkhConfig(ctx *ExecutionContext) (AnkhConfig, error) {

err = yaml.Unmarshal(ankhRcFile, &ankhConfig)
if err != nil {
return ankhConfig, fmt.Errorf("unable to process ankh config '%s': %v", ctx.ConfigPath, err)
return ankhConfig, fmt.Errorf("unable to process ankh config '%s': %v", ctx.AnkhConfigPath, err)
}

errs := ankhConfig.ValidateAndInit()
Expand Down
37 changes: 27 additions & 10 deletions src/ankh/cmd/ankh/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"path"

"github.com/jawher/mow.cli"
"github.com/sirupsen/logrus"
"github.com/mattn/go-isatty"
"github.com/sirupsen/logrus"

"gopkg.in/yaml.v2"

Expand Down Expand Up @@ -101,9 +101,24 @@ func main() {

var (
verbose = app.BoolOpt("v verbose", false, "Verbose debug mode")
ankhconfig = app.StringOpt("ankhconfig", path.Join(os.Getenv("HOME"), ".ankh/config"), "The ankhconfig to use.")
kubeconfig = app.StringOpt("kubeconfig", "", "The kubeconfig to use when invoking kubectl.")
datadir = app.StringOpt("datadir", path.Join(os.Getenv("HOME"), ".ankh"), "The data directory for ankh template history.")
ankhconfig = app.String(cli.StringOpt{
Name: "ankhconfig",
Value: path.Join(os.Getenv("HOME"), ".ankh/config"),
Desc: "The ankh config to use",
EnvVar: "ANKHCONFIG",
})
kubeconfig = app.String(cli.StringOpt{
Name: "kubeconfig",
Value: path.Join(os.Getenv("HOME"), ".kube/config"),
Desc: "The kube config to use when invoking kubectl",
EnvVar: "KUBECONFIG",
})
datadir = app.String(cli.StringOpt{
Name: "datadir",
Value: path.Join(os.Getenv("HOME"), ".ankh"),
Desc: "The data directory for ankh template history",
EnvVar: "ANKHDATADIR",
})
)

log.Out = os.Stdout
Expand All @@ -121,18 +136,20 @@ func main() {
}

ctx = &ankh.ExecutionContext{
Verbose: *verbose,
ConfigPath: *ankhconfig,
DataDir: *datadir,
KubeConfig: *kubeconfig,
Logger: log,
Verbose: *verbose,
AnkhConfigPath: *ankhconfig,
KubeConfigPath: *kubeconfig,
DataDir: *datadir,
Logger: log,
}

ankhConfig, err := ankh.GetAnkhConfig(ctx)
check(err)

ctx.AnkhConfig = ankhConfig

log.Debugf("Using KubeConfigPath %v (KUBECONFIG = '%v')", ctx.KubeConfigPath, os.Getenv("KUBECONFIG"))
log.Debugf("Using AnkhConfigPath %v (ANKHCONFIG = '%v')", ctx.AnkhConfigPath, os.Getenv("ANKHCONFIG"))
}

app.Command("apply", "Deploy an ankh file to a kubernetes cluster", func(cmd *cli.Cmd) {
Expand Down Expand Up @@ -210,7 +227,7 @@ func main() {
out, err := yaml.Marshal(ctx.AnkhConfig)
check(err)

err = ioutil.WriteFile(ctx.ConfigPath, out, 0644)
err = ioutil.WriteFile(ctx.AnkhConfigPath, out, 0644)
check(err)

fmt.Printf("Switched to context \"%v\".\n", context)
Expand Down
46 changes: 30 additions & 16 deletions src/ankh/helm/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"os/exec"
"path/filepath"
"strings"
"time"

"ankh"
"ankh/util"
Expand Down Expand Up @@ -70,23 +71,36 @@ func templateChart(ctx *ankh.ExecutionContext, chart ankh.Chart, ankhFile ankh.A
defer f.Close()

// TODO: this code should be modified to properly fetch charts
ctx.Logger.Debugf("downloading chart from %s", tarballURL)
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{Transport: tr}
resp, err := client.Get(tarballURL)
if err != nil {
return "", err
}
if resp.StatusCode != 200 {
return "", fmt.Errorf("got a status code %v when trying to call %s", resp.StatusCode, tarballURL)
ok := false
for attempt := 1; attempt <= 5; attempt++ {
ctx.Logger.Debugf("downloading chart from %s (attempt %v)", tarballURL, attempt)
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{
Transport: tr,
Timeout: time.Duration(5 * time.Second),
}
resp, err := client.Get(tarballURL)
if err != nil {
return "", err
}
defer resp.Body.Close()

if resp.StatusCode == 200 {
ctx.Logger.Debugf("untarring chart to %s", tmpDir)
if err = util.Untar(tmpDir, resp.Body); err != nil {
return "", err
}

ok = true
break
} else {
ctx.Logger.Warningf("got a status code %v when trying to call %s (attempt %v)", resp.StatusCode, tarballURL, attempt)
}
}
// defer resp.Body.Close()

ctx.Logger.Debugf("untarring chart to %s", tmpDir)
if err = util.Untar(tmpDir, resp.Body); err != nil {
return "", err
if !ok {
return "", fmt.Errorf("failed to fetch helm chart from URL: %v", tarballURL)
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/ankh/kubectl/kubectl.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ func Execute(ctx *ankh.ExecutionContext, act action, input string, ankhFile ankh
"--namespace", ankhFile.Namespace,
}

if ctx.KubeConfig != "" {
kubectlArgs = append(kubectlArgs, []string{"--kubeconfig", ctx.KubeConfig}...)
if ctx.KubeConfigPath != "" {
kubectlArgs = append(kubectlArgs, []string{"--kubeconfig", ctx.KubeConfigPath}...)
}

if ctx.DryRun {
Expand Down

0 comments on commit 5411b96

Please sign in to comment.