Skip to content

Commit

Permalink
Allow using local packs and skip not found var files
Browse files Browse the repository at this point in the history
  • Loading branch information
magec committed Aug 14, 2024
1 parent 21c0407 commit 198431f
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 51 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,9 @@ that you can use templates inside these values.
Releases can have:

- **name**: The name of the release.
- **pack**: The reference of the pack in the form of (`registry/pack`).
- **var-files**: An array of varfiles to be added to
- **pack**: The reference of the pack in the form of. By default it will treat it as a path, if you want to reference a registry, you need to use
`registry://registry_name/pack`.
- **var-files**: An array of varfiles to be added to command invocation. If files are not found it will show a warning and skip it.
- **vars**: An array of vars to be added to `nomad-pack` command invocation.
- **environments**: This permits filtering out environments in case you don't want a given release to be deployed to every environment.
- **nomad-addr**: Nomad addr to be used to deploy. This is usually set in the environment configuration.
Expand Down Expand Up @@ -163,4 +164,3 @@ Use "nomad-packfile [command] --help" for more information about a command.
- **plan**: This will execute a `nomad-pack plan` for every release in the desired state.
- **render**: This will execute a `nomad-pack render` for every release in the desired state.
- **run**: This will execute a `nomad-pack run` for every release in the desired state.

39 changes: 6 additions & 33 deletions internal/nomadpack/nomadpack.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,21 +75,13 @@ func (nomadPack *NomadPack) AddRegistry(name, source string, ref, target *string
// pack: the pack to run.
// varFiles: an array of var files to use.
// vars: a map of vars to use.
func (nomadPack *NomadPack) Plan(workDir string, diff bool, registry string, ref *string, pack string, varFiles []string, vars map[string]string) error {
func (nomadPack *NomadPack) Plan(workDir string, diff bool, varFiles []string, vars map[string]string, extraParams []string) error {
err := nomadPack.ensureValidAuth()
if err != nil {
pterm.Error.Printf("Could not connect to Nomad Server: %v\n", err)
return err
}
params := []string{"plan", "--diff", "--exit-code-makes-changes=0"}
if registry != "" {
params = append(params, "--registry")
params = append(params, registry)
}
if ref != nil {
params = append(params, "--ref")
params = append(params, *ref)
}

for _, varFile := range varFiles {
params = append(params, "-var-file")
Expand All @@ -100,7 +92,7 @@ func (nomadPack *NomadPack) Plan(workDir string, diff bool, registry string, ref
params = append(params, "-var")
params = append(params, key+"="+value)
}
params = append(params, pack)
params = append(params, extraParams...)

cmd := exec.Command(nomadPack.binaryPath, params...)
cmd.Dir = workDir
Expand All @@ -114,22 +106,13 @@ func (nomadPack *NomadPack) Plan(workDir string, diff bool, registry string, ref
return err
}

func (nomadPack *NomadPack) Run(workDir string, diff bool, registry string, ref *string, pack string, varFiles []string, vars map[string]string) error {
func (nomadPack *NomadPack) Run(workDir string, diff bool, varFiles []string, vars map[string]string, extraParams []string) error {
err := nomadPack.ensureValidAuth()
if err != nil {
pterm.Error.Printf("Could not connect to Nomad Server: %v\n", err)
return err
}
params := []string{"run"}
if registry != "" {
params = append(params, "--registry")
params = append(params, registry)
}
if ref != nil {
params = append(params, "--ref")
params = append(params, *ref)
}

for _, varFile := range varFiles {
params = append(params, "-var-file")
params = append(params, varFile)
Expand All @@ -139,8 +122,7 @@ func (nomadPack *NomadPack) Run(workDir string, diff bool, registry string, ref
params = append(params, "-var")
params = append(params, key+"="+value)
}
params = append(params, pack)

params = append(params, extraParams...)
cmd := exec.Command(nomadPack.binaryPath, params...)
cmd.Dir = workDir

Expand All @@ -152,17 +134,8 @@ func (nomadPack *NomadPack) Run(workDir string, diff bool, registry string, ref
return err
}

func (nomadPack *NomadPack) Render(workDir string, diff bool, registry string, ref *string, pack string, varFiles []string, vars map[string]string) error {
func (nomadPack *NomadPack) Render(workDir string, diff bool, varFiles []string, vars map[string]string, extraParams []string) error {
params := []string{"render"}
if registry != "" {
params = append(params, "--registry")
params = append(params, registry)
}
if ref != nil {
params = append(params, "--ref")
params = append(params, *ref)
}

for _, varFile := range varFiles {
params = append(params, "-var-file")
params = append(params, varFile)
Expand All @@ -172,8 +145,8 @@ func (nomadPack *NomadPack) Render(workDir string, diff bool, registry string, r
params = append(params, "-var")
params = append(params, key+"="+value)
}
params = append(params, pack)

params = append(params, extraParams...)
cmd := exec.Command(nomadPack.binaryPath, params...)
cmd.Dir = workDir

Expand Down
64 changes: 49 additions & 15 deletions internal/nomadpackfile/nomadpackfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,28 @@ type RegistryNode struct {
NomadPackFile *NomadPackFile
}

type Pack struct {
Name string
Registry *RegistryNode
}

func (p Pack) NomadPackCmdOpts() (params []string) {
if p.Registry != nil {
params = append(params, "--registry")
params = append(params, p.Registry.Name)
if p.Registry.Ref != nil {
params = append(params, "--ref")
params = append(params, *p.Registry.Ref)
}
}
params = append(params, p.Name)

return params
}

type ReleaseNode struct {
Name string
Pack string
Registry RegistryNode
Pack Pack
VarFiles []string
Vars map[string]string
workDir string
Expand All @@ -62,7 +80,7 @@ func (release ReleaseNode) Plan() error {
log.Fatalf("Error getting initializing nomad-pack: %s", err)
}

return nomadPack.Plan(release.workDir, true, release.Registry.Name, release.Registry.Ref, release.Pack, release.VarFiles, release.Vars)
return nomadPack.Plan(release.workDir, true, release.VarFiles, release.Vars, release.Pack.NomadPackCmdOpts())
}

func (release ReleaseNode) Run() error {
Expand All @@ -71,7 +89,7 @@ func (release ReleaseNode) Run() error {
if err != nil {
log.Fatalf("Error getting initializing nomad-pack: %s", err)
}
return nomadPack.Run(release.workDir, true, release.Registry.Name, release.Registry.Ref, release.Pack, release.VarFiles, release.Vars)
return nomadPack.Run(release.workDir, true, release.VarFiles, release.Vars, release.Pack.NomadPackCmdOpts())
}

func (release ReleaseNode) Render() error {
Expand All @@ -80,7 +98,7 @@ func (release ReleaseNode) Render() error {
log.Fatalf("Error getting initializing nomad-pack: %s", err)
}

return nomadPack.Render(release.workDir, true, release.Registry.Name, release.Registry.Ref, release.Pack, release.VarFiles, release.Vars)
return nomadPack.Render(release.workDir, true, release.VarFiles, release.Vars, release.Pack.NomadPackCmdOpts())
}

func (release ReleaseNode) nomadPack() (nomadPack *nomadpack.NomadPack, err error) {
Expand Down Expand Up @@ -192,18 +210,30 @@ func (n *NomadPackFile) Compile() error {
},
Env: environmentToHash(),
}
var pack Pack

splitPack := strings.Split(release.Pack, "/")
if len(splitPack) != 2 {
log.Fatalf("Invalid pack name: %s", release.Pack)
}
if strings.HasPrefix(release.Pack, "registry://") {
release.Pack = strings.TrimPrefix(release.Pack, "registry://")
splitPack := strings.Split(release.Pack, "/")
if len(splitPack) != 2 {
log.Fatalf("Invalid pack name: %s", release.Pack)
}

if n.registries[splitPack[0]].Name == "" {
log.Fatalf("Registry %s not found", splitPack[0])
}

if n.registries[splitPack[0]].Name == "" {
log.Fatalf("Registry %s not found", splitPack[0])
registry := n.registries[splitPack[0]]
pack = Pack{
Registry: &registry,
Name: splitPack[1],
}
} else {
pack = Pack{
Name: release.Pack,
}
}

registry := n.registries[splitPack[0]]
pack := splitPack[1]
workDir := n.config.WorkDir()

var err error
Expand All @@ -225,7 +255,12 @@ func (n *NomadPackFile) Compile() error {
if err != nil {
panic(err)
}
newVarFiles = append(newVarFiles, newVarFile)
filePath := workDir + "/" + newVarFile
if _, err := os.Stat(filePath); err == nil {
newVarFiles = append(newVarFiles, newVarFile)
} else {
pterm.Warning.Printf("Var file %s not found, skipping", filePath)
}
}

newVars := map[string]string{}
Expand All @@ -240,7 +275,6 @@ func (n *NomadPackFile) Compile() error {
releaseNode := ReleaseNode{
Name: release.Name,
Pack: pack,
Registry: registry,
VarFiles: newVarFiles,
workDir: workDir,
NomadPackFile: n,
Expand Down

0 comments on commit 198431f

Please sign in to comment.