Skip to content

Commit

Permalink
Initial variables display code (#1682)
Browse files Browse the repository at this point in the history
## Description

This adds set variables to the package preview prompt

## Related Issue

Fixes #1124 

## Type of change

- [ ] Bug fix (non-breaking change which fixes an issue)
- [X] New feature (non-breaking change which adds functionality)
- [ ] Other (security config, docs update, etc)

## Checklist before merging

- [ ] Test, docs, adr added or updated as needed
- [X] [Contributor Guide
Steps](https://github.com/defenseunicorns/zarf/blob/main/CONTRIBUTING.md#developer-workflow)
followed
  • Loading branch information
Racer159 committed Jun 22, 2023
1 parent a6cf75f commit 32d13a5
Show file tree
Hide file tree
Showing 25 changed files with 972 additions and 917 deletions.
1,518 changes: 759 additions & 759 deletions docs/3-create-a-zarf-package/4-zarf-schema.md

Large diffs are not rendered by default.

30 changes: 0 additions & 30 deletions src/cmd/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@ package cmd

import (
"fmt"
"os"
"path/filepath"
"regexp"
"strings"

"github.com/defenseunicorns/zarf/src/config/lang"
"github.com/defenseunicorns/zarf/src/pkg/message"
"github.com/defenseunicorns/zarf/src/types"
"github.com/pterm/pterm"
"oras.land/oras-go/v2/registry"

Expand All @@ -22,7 +20,6 @@ import (
"github.com/defenseunicorns/zarf/src/internal/cluster"
"github.com/defenseunicorns/zarf/src/pkg/packager"
"github.com/defenseunicorns/zarf/src/pkg/utils"
"github.com/mholt/archiver/v3"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -168,33 +165,6 @@ var packageRemoveCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
pkgName := args[0]

// If the user input is a path to a package, extract the name from the package
isTarball := regexp.MustCompile(`.*zarf-package-.*\.tar(\.zst)?$`).MatchString
if isTarball(pkgName) {
if utils.InvalidPath(pkgName) {
message.Fatalf(nil, lang.CmdPackageRemoveTarballErr)
}

tempPath, err := utils.MakeTempDir(config.CommonOptions.TempDirectory)
if err != nil {
message.Fatalf(err, lang.CmdPackageRemoteTmpDirErr, config.CommonOptions.TempDirectory, err.Error())
}
defer os.RemoveAll(tempPath)

if err := archiver.Extract(pkgName, config.ZarfYAML, tempPath); err != nil {
message.Fatalf(err, lang.CmdPackageRemoveExtractErr)
}

var pkg types.ZarfPackage
configPath := filepath.Join(tempPath, config.ZarfYAML)
if err := utils.ReadYaml(configPath, &pkg); err != nil {
message.Fatalf(err, lang.CmdPackageRemoveReadZarfErr)
}

pkgName = pkg.Metadata.Name
pkgConfig.Pkg = pkg
}

// Configure the packager
pkgClient := packager.NewOrDie(&pkgConfig)
defer pkgClient.ClearTempPaths()
Expand Down
3 changes: 3 additions & 0 deletions src/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ const (

ZarfInClusterGitServiceURL = "http://zarf-gitea-http.zarf.svc.cluster.local:3000"
ZarfInClusterArtifactServiceURL = ZarfInClusterGitServiceURL + "/api/packages/" + ZarfGitPushUser

ZarfDeployStage = "Deploy"
ZarfCreateStage = "Create"
)

// Zarf Global Configuration Variables.
Expand Down
9 changes: 3 additions & 6 deletions src/config/lang/english.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,11 +220,6 @@ const (
CmdPackageListNoPackageWarn = "Unable to get the packages deployed to the cluster"
CmdPackageListUnmarshalErr = "Unable to read all of the packages deployed to the cluster"

CmdPackageRemoveShort = "Removes a Zarf package that has been deployed already (runs offline)"
CmdPackageRemoveTarballErr = "Invalid tarball path provided"
CmdPackageRemoveExtractErr = "Unable to extract the package contents"
CmdPackageRemoveReadZarfErr = "Unable to read zarf.yaml"

CmdPackageCreateFlagConfirm = "Confirm package creation without prompting"
CmdPackageCreateFlagSet = "Specify package variables to set on the command line (KEY=value)"
CmdPackageCreateFlagOutput = "Specify the output (either a directory or an oci:// URL) for the created Zarf package"
Expand Down Expand Up @@ -255,9 +250,11 @@ const (
CmdPackageInspectFlagPublicKey = "Path to a public key file that will be used to validate a signed package"
CmdPackageInspectErr = "Failed to inspect package: %s"

CmdPackageRemoveShort = "Removes a Zarf package that has been deployed already (runs offline)"
CmdPackageRemoveFlagConfirm = "REQUIRED. Confirm the removal action to prevent accidental deletions"
CmdPackageRemoveFlagComponents = "Comma-separated list of components to uninstall"
CmdPackageRemoteTmpDirErr = "Unable to create temporary directory at %s: %s"
CmdPackageRemoveTarballErr = "Invalid tarball path provided"
CmdPackageRemoveExtractErr = "Unable to extract the package contents"
CmdPackageRemoveErr = "Unable to remove the package with an error of: %s"

CmdPackageRegistryPrefixErr = "Registry must be prefixed with 'oci://'"
Expand Down
18 changes: 6 additions & 12 deletions src/internal/api/packages/find.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,6 @@ import (
"github.com/defenseunicorns/zarf/src/pkg/utils"
)

// Find zarf-packages on the local system (https://regex101.com/r/TUUftK/1)
var packagePattern = regexp.MustCompile(`zarf-package[^\s\\\/]*\.tar(\.zst)?$`)

// Find zarf-init packages on the local system
var currentInitPattern = regexp.MustCompile(packager.GetInitPackageName("") + "$")

// FindInHomeStream returns all packages in the user's home directory.
// If the init query parameter is true, only init packages will be returned.
func FindInHomeStream(w http.ResponseWriter, r *http.Request) {
Expand All @@ -32,9 +26,9 @@ func FindInHomeStream(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Connection", "keep-alive")

init := r.URL.Query().Get("init")
regexp := packagePattern
regexp := packager.ZarfPackagePattern
if init == "true" {
regexp = currentInitPattern
regexp = packager.ZarfInitPattern
}

done := make(chan bool)
Expand Down Expand Up @@ -63,7 +57,7 @@ func FindInitStream(w http.ResponseWriter, _ *http.Request) {
go func() {
// stream init packages in the execution directory
if execDir, err := utils.GetFinalExecutablePath(); err == nil {
streamDirPackages(execDir, currentInitPattern, w)
streamDirPackages(execDir, packager.ZarfInitPattern, w)
} else {
streamError(err, w)
}
Expand All @@ -76,11 +70,11 @@ func FindInitStream(w http.ResponseWriter, _ *http.Request) {
streamError(err, w)
}
}
streamDirPackages(cachePath, currentInitPattern, w)
streamDirPackages(cachePath, packager.ZarfInitPattern, w)

// Find init packages in the current working directory
if cwd, err := os.Getwd(); err == nil {
streamDirPackages(cwd, currentInitPattern, w)
streamDirPackages(cwd, packager.ZarfInitPattern, w)
} else {
streamError(err, w)
}
Expand All @@ -98,7 +92,7 @@ func FindPackageStream(w http.ResponseWriter, _ *http.Request) {

go func() {
if cwd, err := os.Getwd(); err == nil {
streamDirPackages(cwd, packagePattern, w)
streamDirPackages(cwd, packager.ZarfPackagePattern, w)
} else {
streamError(err, w)
}
Expand Down
5 changes: 4 additions & 1 deletion src/pkg/message/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ const (
// NoProgress tracks whether spinner/progress bars show updates.
var NoProgress bool

// RuleLine creates a line of ━ as wide as the terminal
var RuleLine = strings.Repeat("━", TermWidth)

// LogWriter is the stream to write logs to.
var LogWriter io.Writer = os.Stderr

Expand Down Expand Up @@ -254,7 +257,7 @@ func HeaderInfof(format string, a ...any) {
// HorizontalRule prints a white horizontal rule to separate the terminal
func HorizontalRule() {
pterm.Println()
pterm.Println(strings.Repeat("━", TermWidth))
pterm.Println(RuleLine)
}

// JSONValue prints any value as JSON.
Expand Down
24 changes: 10 additions & 14 deletions src/pkg/oci/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,16 +120,17 @@ func (o *OrasRemote) LayersFromRequestedComponents(requestedComponents []string)
// - zarf.yaml
// - checksums.txt
// - zarf.yaml.sig
func (o *OrasRemote) PullPackage(destinationDir string, concurrency int, layersToPull ...ocispec.Descriptor) error {
func (o *OrasRemote) PullPackage(destinationDir string, concurrency int, layersToPull ...ocispec.Descriptor) (partialPaths []string, err error) {
isPartialPull := len(layersToPull) > 0
ref := o.Reference

pterm.Println()
message.Debugf("Pulling %s", ref.String())
message.Infof("Pulling Zarf package from %s", ref)

manifest, err := o.FetchRoot()
if err != nil {
return err
return partialPaths, err
}

estimatedBytes := int64(0)
Expand All @@ -148,7 +149,7 @@ func (o *OrasRemote) PullPackage(destinationDir string, concurrency int, layersT

dst, err := file.New(destinationDir)
if err != nil {
return err
return partialPaths, err
}
defer dst.Close()

Expand Down Expand Up @@ -181,7 +182,7 @@ func (o *OrasRemote) PullPackage(destinationDir string, concurrency int, layersT
go utils.RenderProgressBarForLocalDirWrite(destinationDir, estimatedBytes, &wg, doneSaving, "Pulling Zarf package data")
_, err = oras.Copy(o.Context, o.Repository, ref.String(), dst, ref.String(), copyOpts)
if err != nil {
return err
return partialPaths, err
}

// Send a signal to the progress bar that we're done and wait for it to finish
Expand All @@ -191,15 +192,15 @@ func (o *OrasRemote) PullPackage(destinationDir string, concurrency int, layersT
message.Debugf("Pulled %s", ref.String())
message.Successf("Pulled %s", ref.String())

layersToCheck := []string{}
if isPartialPull {
for _, layer := range layersToPull {
if layer.Annotations[ocispec.AnnotationTitle] != "" {
layersToCheck = append(layersToCheck, layer.Annotations[ocispec.AnnotationTitle])
partialPaths = append(partialPaths, layer.Annotations[ocispec.AnnotationTitle])
}
}
}
return utils.ValidatePackageChecksums(destinationDir, layersToCheck)

return partialPaths, nil
}

// PullLayer pulls a layer from the remote repository and saves it to `destinationDir/annotationTitle`.
Expand All @@ -215,7 +216,7 @@ func (o *OrasRemote) PullLayer(desc ocispec.Descriptor, destinationDir string) e
}

// PullPackageMetadata pulls the package metadata from the remote repository and saves it to `destinationDir`.
func (o *OrasRemote) PullPackageMetadata(destinationDir string) error {
func (o *OrasRemote) PullPackageMetadata(destinationDir string) (err error) {
root, err := o.FetchRoot()
if err != nil {
return err
Expand All @@ -229,10 +230,5 @@ func (o *OrasRemote) PullPackageMetadata(destinationDir string) error {
}
}
}
var pkg types.ZarfPackage
err = utils.ReadYaml(filepath.Join(destinationDir, config.ZarfYAML), &pkg)
if err != nil {
return err
}
return utils.ValidatePackageChecksums(destinationDir, AlwaysPull)
return nil
}
2 changes: 1 addition & 1 deletion src/pkg/oci/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ func (o *OrasRemote) PublishPackage(pkg *types.ZarfPackage, sourceDir string, co
},
})
}
utils.ColorPrintYAML(ex)
utils.ColorPrintYAML(ex, nil, true)
} else {
flags := ""
if config.CommonOptions.Insecure {
Expand Down
54 changes: 25 additions & 29 deletions src/pkg/utils/checksum.go → src/pkg/packager/checksum.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2021-Present The Zarf Authors

// Package utils provides generic helper functions.
package utils
// Package packager contains functions for interacting with, managing and deploying zarf packages.
package packager

import (
"bufio"
Expand All @@ -14,48 +14,44 @@ import (

"github.com/defenseunicorns/zarf/src/config"
"github.com/defenseunicorns/zarf/src/pkg/message"
"github.com/defenseunicorns/zarf/src/types"
"github.com/defenseunicorns/zarf/src/pkg/utils"
)

// ValidatePackageChecksums validates the checksums of a Zarf package.
func ValidatePackageChecksums(baseDir string, pathsToCheck []string) error {
// validatePackageChecksums validates the checksums of a Zarf package.
func (p *Packager) validatePackageChecksums(baseDir string, aggregateChecksum string, pathsToCheck []string) error {
spinner := message.NewProgressSpinner("Validating package checksums")
defer spinner.Stop()

// Run pre-checks to make sure we have what we need to validate the checksums
if InvalidPath(baseDir) {
if utils.InvalidPath(baseDir) {
return fmt.Errorf("invalid base directory: %s", baseDir)
}
var pkg types.ZarfPackage
err := ReadYaml(filepath.Join(baseDir, config.ZarfYAML), &pkg)
if err != nil {
return err
}
aggregateChecksum := pkg.Metadata.AggregateChecksum

if aggregateChecksum == "" {
spinner.Updatef("Package does not have an aggregate checksum, skipping checksums validation")
return nil
}
if len(aggregateChecksum) != 64 {
return fmt.Errorf("invalid aggregate checksum: %s", aggregateChecksum)
}

isPartial := false
if len(pathsToCheck) > 0 {
pathsToCheck = Unique(pathsToCheck)
pathsToCheck = utils.Unique(pathsToCheck)
isPartial = true
message.Debugf("Validating checksums for a subset of files in the package - %v", pathsToCheck)
for idx, path := range pathsToCheck {
pathsToCheck[idx] = filepath.Join(baseDir, path)
}
}

checkedMap, err := PathCheckMap(baseDir)
checkedMap, err := pathCheckMap(baseDir)
if err != nil {
return err
}

checksumPath := filepath.Join(baseDir, config.ZarfChecksumsTxt)
actualAggregateChecksum, err := GetSHA256OfFile(checksumPath)
actualAggregateChecksum, err := utils.GetSHA256OfFile(checksumPath)
if err != nil {
return fmt.Errorf("unable to get checksum of: %s", err.Error())
}
Expand All @@ -67,7 +63,7 @@ func ValidatePackageChecksums(baseDir string, pathsToCheck []string) error {
checkedMap[filepath.Join(baseDir, config.ZarfYAML)] = true
checkedMap[filepath.Join(baseDir, config.ZarfYAMLSignature)] = true

err = LineByLine(checksumPath, func(line string) error {
err = lineByLine(checksumPath, func(line string) error {
split := strings.Split(line, " ")
sha := split[0]
rel := split[1]
Expand All @@ -79,17 +75,17 @@ func ValidatePackageChecksums(baseDir string, pathsToCheck []string) error {
status := fmt.Sprintf("Validating checksum of %s", rel)
spinner.Updatef(message.Truncate(status, message.TermWidth, false))

if InvalidPath(path) {
if utils.InvalidPath(path) {
if !isPartial && !checkedMap[path] {
return fmt.Errorf("unable to validate checksums - missing file: %s", rel)
} else if SliceContains(pathsToCheck, path) {
} else if utils.SliceContains(pathsToCheck, path) {
return fmt.Errorf("unable to validate partial checksums - missing file: %s", rel)
}
// it's okay if we're doing a partial check and the file isn't there as long as the path isn't in the list of paths to check
return nil
}

actualSHA, err := GetSHA256OfFile(path)
actualSHA, err := utils.GetSHA256OfFile(path)
if err != nil {
return fmt.Errorf("unable to get checksum of: %s", err.Error())
}
Expand All @@ -113,21 +109,21 @@ func ValidatePackageChecksums(baseDir string, pathsToCheck []string) error {
return fmt.Errorf("unable to validate partial checksums, %s did not get checked", path)
}
}
} else {
// Otherwise, make sure we've checked all the files in the package
for path, checked := range checkedMap {
if !checked {
return fmt.Errorf("unable to validate checksums, %s did not get checked", path)
}
}

for path, checked := range checkedMap {
if !checked {
return fmt.Errorf("unable to validate checksums, %s did not get checked", path)
}
}

spinner.Successf("Checksums validated!")

return nil
}

// PathCheckMap returns a map of all the files in a directory and a boolean to use for checking status.
func PathCheckMap(dir string) (map[string]bool, error) {
// pathCheckMap returns a map of all the files in a directory and a boolean to use for checking status.
func pathCheckMap(dir string) (map[string]bool, error) {
filepathMap := make(map[string]bool)
err := filepath.Walk(dir, func(path string, info fs.FileInfo, err error) error {
if info.IsDir() {
Expand All @@ -139,8 +135,8 @@ func PathCheckMap(dir string) (map[string]bool, error) {
return filepathMap, err
}

// LineByLine reads a file line by line and calls a callback function for each line.
func LineByLine(path string, cb func(line string) error) error {
// lineByLine reads a file line by line and calls a callback function for each line.
func lineByLine(path string, cb func(line string) error) error {
file, err := os.Open(path)
if err != nil {
return err
Expand Down
Loading

0 comments on commit 32d13a5

Please sign in to comment.