Skip to content

Commit

Permalink
Add var_file support on out
Browse files Browse the repository at this point in the history
- This take into account relative path provided by user
  and prepend the resource directory.

#46
  • Loading branch information
drich10 committed Aug 7, 2018
1 parent 36094e7 commit 7efeb2b
Show file tree
Hide file tree
Showing 9 changed files with 149 additions and 73 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ deployment manifest and then deploy.

* `vars_files`: *Optional.* A collection of vars files to be interpolated into the deployment manifest.

* `var_files`: *Optional.* A collection of variables to be set by the contents of the file configured.

* `ops_files`: *Optional.* A collection of ops files to be applied over the deployment manifest.

* `cleanup`: *Optional* An boolean that specifies if a bosh cleanup should be
Expand Down
19 changes: 19 additions & 0 deletions bosh/director.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (

type DeployParams struct {
Vars map[string]interface{}
VarFiles map[string]string
VarsFiles []string
OpsFiles []string
NoRedact bool
Expand Down Expand Up @@ -74,6 +75,11 @@ func (d BoshDirector) Deploy(manifestBytes []byte, deployParams DeployParams) er
return err
}

boshVarFiles, err := parsedVarFiles(deployParams.VarFiles)
if err != nil {
return err
}

boshOpsFiles, err := parsedOpsFiles(deployParams.OpsFiles)
if err != nil {
return err
Expand All @@ -93,6 +99,7 @@ func (d BoshDirector) Deploy(manifestBytes []byte, deployParams DeployParams) er
VarFlags: boshcmd.VarFlags{
VarKVs: varKVsFromVars(deployParams.Vars),
VarsFiles: boshVarsFiles,
VarFiles: boshVarFiles,
},
OpsFlags: boshcmd.OpsFlags{
OpsFiles: boshOpsFiles,
Expand Down Expand Up @@ -331,6 +338,18 @@ func parsedVarsFiles(varsFiles []string) ([]boshtpl.VarsFileArg, error) {
return varsFileArgs, nil
}

func parsedVarFiles(varFiles map[string]string) ([]boshtpl.VarFileArg, error) {
varFileArgs := []boshtpl.VarFileArg{}
for varKey, varFile := range varFiles {
varFileArg := boshtpl.VarFileArg{FS: boshFileSystem()}
if err := varFileArg.UnmarshalFlag(fmt.Sprintf("%s=%s", varKey, varFile)); err != nil {
return nil, err
}
varFileArgs = append(varFileArgs, varFileArg)
}
return varFileArgs, nil
}

func parsedOpsFiles(opsFiles []string) ([]boshcmd.OpsFileArg, error) {
nullLogger := boshlog.NewWriterLogger(boshlog.LevelInfo, ioutil.Discard)
boshFS := boshsys.NewOsFileSystemWithStrictTempRoot(nullLogger)
Expand Down
17 changes: 13 additions & 4 deletions bosh/director_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,14 @@ var _ = Describe("BoshDirector", func() {
Value: "bar",
},
}
varFileContents := properYaml(`
varsFileContents := properYaml(`
baz: "best-bar"
`)
varFile, _ := ioutil.TempFile("", "var-file-1")
varFile.Write(varFileContents)
varsFile, _ := ioutil.TempFile("", "var-file-1")
varsFile.Write(varsFileContents)

varFile, _ := ioutil.TempFile("", "var-file-key2")
varFile.Write([]byte("val2"))

opsFileContents := properYaml(`
- type: replace
Expand All @@ -71,7 +74,8 @@ var _ = Describe("BoshDirector", func() {
NoRedact: noRedact,
DryRun: dryRun,
Vars: vars,
VarsFiles: []string{varFile.Name()},
VarFiles: map[string]string{"key2": varFile.Name()},
VarsFiles: []string{varsFile.Name()},
OpsFiles: []string{opsFile.Name()},
})
Expect(err).ToNot(HaveOccurred())
Expand All @@ -87,6 +91,11 @@ var _ = Describe("BoshDirector", func() {
Expect(deployOpts.VarsFiles[0].Vars).To(Equal(boshtpl.StaticVariables{
"baz": "best-bar",
}))

Expect(len(deployOpts.VarFiles)).To(Equal(1))
Expect(deployOpts.VarFiles[0].Vars).To(Equal(boshtpl.StaticVariables{
"key2": "val2",
}))
Expect(len(deployOpts.OpsFiles)).To(Equal(1))

pathPointer, _ := patch.NewPointerFromString("/my?/new_key")
Expand Down
21 changes: 21 additions & 0 deletions ci/pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,25 @@ jobs:
passed: [rc]
- get: bosh-deployment-resource-image
passed: [rc]
- task: write-var-file
config:
platform: linux
image_resource:
type: docker-image
source:
repository: ubuntu
tag: "14.04"
run:
path: /bin/sh
args:
- -c
- |
set -e
echo "less-interesting-value" > var-file/file
dir: ""
outputs:
- name: var-file
path: ""
- task: write-vars-files
config:
platform: linux
Expand Down Expand Up @@ -174,6 +193,8 @@ jobs:
- ops-files/*
vars_files:
- vars-files/*
var_files:
interesting_var: var-file/file
vars:
max_in_flight: 10
releases:
Expand Down
1 change: 1 addition & 0 deletions ci/test-deployment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ update:
serial: false
update_watch_time: 5000-1200000
really: ((cool))
var_file_interpolation: ((interesting_var))
instance_groups:
- name: dummy
azs: [z1]
Expand Down
1 change: 1 addition & 0 deletions concourse/out_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type OutParams struct {
Stemcells []string `json:"stemcells,omitempty"`
Vars map[string]interface{} `json:"vars,omitempty"`
VarsFiles []string `json:"vars_files,omitempty"`
VarFiles map[string]string `json:"var_files,omitempty"`
OpsFiles []string `json:"ops_files,omitempty"`
Delete DeleteParams `json:"delete,omitempty"`
}
Expand Down
8 changes: 8 additions & 0 deletions concourse/out_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ var _ = Describe("NewOutRequest", func() {
"path/to/file",
"second/path/to/file"
],
"var_files": {
"var_from_file": "/path/to/file/with/var/content",
"another_var_from_file": "/more/path/to/content"
},
"ops_files": [
"ops-file1",
"path/to/ops-file2"
Expand Down Expand Up @@ -70,6 +74,10 @@ var _ = Describe("NewOutRequest", func() {
"path/to/file",
"second/path/to/file",
},
VarFiles: map[string]string{
"var_from_file": "/path/to/file/with/var/content",
"another_var_from_file": "/more/path/to/content",
},
OpsFiles: []string{
"ops-file1",
"path/to/ops-file2",
Expand Down
12 changes: 11 additions & 1 deletion out/out_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io/ioutil"
"os"
"path"
"path/filepath"

"github.com/cloudfoundry/bosh-deployment-resource/bosh"
"github.com/cloudfoundry/bosh-deployment-resource/concourse"
Expand Down Expand Up @@ -51,7 +52,7 @@ func (c OutCommand) deploy(outRequest concourse.OutRequest) (OutResponse, error)

varsFilePaths, err := tools.UnfurlGlobs(c.resourcesDirectory, outRequest.Params.VarsFiles)
if err != nil {
return OutResponse{}, fmt.Errorf("Invalid var_file name: %s", err)
return OutResponse{}, fmt.Errorf("Invalid vars_file name: %s", err)
}

opsFilePaths, err := tools.UnfurlGlobs(c.resourcesDirectory, outRequest.Params.OpsFiles)
Expand Down Expand Up @@ -91,6 +92,7 @@ func (c OutCommand) deploy(outRequest concourse.OutRequest) (OutResponse, error)
Recreate: outRequest.Params.Recreate,
SkipDrain: outRequest.Params.SkipDrain,
Cleanup: outRequest.Params.Cleanup,
VarFiles: c.prependResourcesDir(outRequest.Params.VarFiles),
}

var varsStoreFile *os.File
Expand Down Expand Up @@ -182,3 +184,11 @@ func (c OutCommand) consumeStemcells(manifest bosh.DeploymentManifest, stemcellG

return metadata, nil
}

func (c OutCommand) prependResourcesDir(varsFiles map[string]string) map[string]string {
varsWithAbsPath := map[string]string{}
for varName, varFilePath := range varsFiles {
varsWithAbsPath[varName] = filepath.Join(c.resourcesDirectory, varFilePath)
}
return varsWithAbsPath
}
Loading

0 comments on commit 7efeb2b

Please sign in to comment.