Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Zarf files archive path support #1962

Merged
merged 54 commits into from
Aug 27, 2023
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
d6e6976
WIP
cmwylie19 Aug 8, 2023
0ec1054
base work done
cmwylie19 Aug 9, 2023
5789d36
archive-path
cmwylie19 Aug 9, 2023
a46cc78
draft PR for review
cmwylie19 Aug 9, 2023
7cac2e5
fix(deps): update all non-major dependencies (#1866)
renovate[bot] Aug 9, 2023
9ea87d7
removed commented code
cmwylie19 Aug 9, 2023
b1e182e
Merge branch 'main' into 1928
cmwylie19 Aug 9, 2023
edcf11e
clean up code - prepare tests
cmwylie19 Aug 10, 2023
deb842b
implemented test
cmwylie19 Aug 10, 2023
56e069a
Merge branch 'main' into 1928
cmwylie19 Aug 10, 2023
67fc9fc
Error handling
cmwylie19 Aug 10, 2023
1baafd9
removed unneeded files and lint
cmwylie19 Aug 10, 2023
b7fe012
make docs-and-schema
cmwylie19 Aug 10, 2023
4e5299e
remove comments
cmwylie19 Aug 10, 2023
25d0f25
consistent error messaging
cmwylie19 Aug 10, 2023
7adacdc
Merge branch 'main' into 1928
cmwylie19 Aug 10, 2023
97000e6
update test for windows binary
cmwylie19 Aug 10, 2023
037d981
move order of tests
cmwylie19 Aug 10, 2023
5ece671
Update src/types/component.go
cmwylie19 Aug 10, 2023
04d51ff
Update src/pkg/utils/helpers/url.go
cmwylie19 Aug 10, 2023
4b5767c
Merge branch 'main' into 1928
cmwylie19 Aug 11, 2023
367f9c3
update e2e test
cmwylie19 Aug 10, 2023
42d168e
renamed function
cmwylie19 Aug 10, 2023
84f5add
refactor more efficiently
cmwylie19 Aug 11, 2023
8c5d0ae
refactor more efficiently
cmwylie19 Aug 11, 2023
1932629
updates for folders and files in folders
cmwylie19 Aug 12, 2023
23cc087
update e2e tests
cmwylie19 Aug 12, 2023
de9665a
remove archive-path example
cmwylie19 Aug 12, 2023
84f295c
resolve lang conflict
cmwylie19 Aug 12, 2023
ae3fe59
Merge branch 'main' into 1928
cmwylie19 Aug 12, 2023
d98e257
remove eks.yaml
cmwylie19 Aug 12, 2023
deb4ee8
update e2e test
cmwylie19 Aug 12, 2023
2473f5e
rm eks.yaml
cmwylie19 Aug 12, 2023
75dfa5e
logic for files
cmwylie19 Aug 12, 2023
bdecac3
rerun docs-and-schema
cmwylie19 Aug 12, 2023
ad951de
remove zarf-cache from test-archive e2e test
cmwylie19 Aug 12, 2023
ac14d5b
remove extra underscore in distros
cmwylie19 Aug 12, 2023
f277275
update path for zarf-package-distro-eks-multi-0.0.2.tar.zst
cmwylie19 Aug 14, 2023
3e4c4b0
lint and recursive delete
cmwylie19 Aug 14, 2023
6d51ee9
Merge branch 'main' into 1928
cmwylie19 Aug 15, 2023
6a330e6
remove duplicated code
cmwylie19 Aug 15, 2023
284c703
disable bb
cmwylie19 Aug 15, 2023
98128cf
address updates from team
cmwylie19 Aug 21, 2023
29b3c94
Merge branch 'main' into 1928
cmwylie19 Aug 21, 2023
d66232d
make docs-and-schema
cmwylie19 Aug 21, 2023
6bba743
update test
cmwylie19 Aug 22, 2023
8866d44
clean up duplicated code, address comments
cmwylie19 Aug 22, 2023
9f9b71e
Update src/test/e2e/00_use_cli_test.go
cmwylie19 Aug 24, 2023
e048b71
Update src/pkg/packager/create.go
cmwylie19 Aug 24, 2023
1f7ae70
Update src/pkg/packager/create.go
cmwylie19 Aug 24, 2023
17e965a
Update src/pkg/packager/create.go
cmwylie19 Aug 24, 2023
1545a53
Merge branch 'main' into 1928
cmwylie19 Aug 25, 2023
d657a79
Merge branch 'main' into 1928
Racer159 Aug 25, 2023
cf561b5
Merge branch 'main' into 1928
Racer159 Aug 26, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions docs/3-create-a-zarf-package/4-zarf-schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -980,6 +980,22 @@ Must be one of:
</blockquote>
</details>

<details>
<summary>
<strong> <a name="components_items_files_items_archivePath"></a>archivePath</strong>
</summary>
&nbsp;
<blockquote>

**Description:** Local folder or file to be extracted into the package

| | |
| -------- | -------- |
| **Type** | `string` |

</blockquote>
</details>

</blockquote>
</details>

Expand Down
10 changes: 10 additions & 0 deletions packages/distros/eks/zarf.yaml
cmwylie19 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ variables:
default: "1.23"

components:
- name: test-archive
description: Test archive
required: true
files:
- source: https://github.com/weaveworks/eksctl/releases/download/v0.147.0/eksctl_Darwin_arm64.tar.gz
target: eksctl
shasum: 1d7dd5b9907de1cb3fa7832659db29f50530444d10e77b4a8eb27aa648da6fab
archivePath: eksctl
executable: true
cmwylie19 marked this conversation as resolved.
Show resolved Hide resolved

- name: load-eksctl
required: true
actions:
Expand Down
1 change: 1 addition & 0 deletions src/config/lang/english.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const (
ErrCreatingDir = "failed to create directory %s: %s"
ErrRemoveFile = "failed to remove file %s: %s"
ErrUnarchive = "failed to unarchive %s: %s"
ErrFileExtract = "failed to extract filename %s from archive %s: %s"
)

// Zarf CLI commands.
Expand Down
15 changes: 15 additions & 0 deletions src/pkg/packager/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,9 +380,24 @@ func (p *Packager) addComponent(index int, component types.ZarfComponent, isSkel
if isSkeleton {
continue
}

if err := utils.DownloadToFile(file.Source, dst, component.CosignKeyPath); err != nil {
return fmt.Errorf(lang.ErrDownloading, file.Source, err.Error())
}

if file.ArchivePath != "" {
compressFileName, _ := helpers.ExtractFilenameFromURL(file.Source)
archiveFile := filepath.Dir(dst) + "/" + compressFileName
targetFile := filepath.Dir(dst) + "/" + file.Target
err = os.Rename(targetFile, archiveFile)
if err != nil {
return fmt.Errorf(lang.ErrWritingFile, dst, err)
}
err = helpers.FindAndCopyFileFromArchive(archiveFile, file.ArchivePath, filepath.Dir(dst))
if err != nil {
return fmt.Errorf(lang.ErrFileExtract, file.ArchivePath, archiveFile, err)
}
}
} else {
if err := utils.CreatePathAndCopy(file.Source, dst); err != nil {
return fmt.Errorf("unable to copy file %s: %w", file.Source, err)
Expand Down
50 changes: 50 additions & 0 deletions src/pkg/utils/helpers/files.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2021-Present The Zarf Authors

// Package helpers provides generic helper functions with no external imports
package helpers

import (
"io"
"os"
"path/filepath"
"strings"

"github.com/mholt/archiver/v3"
)

// FindAndCopyFileFromArchive inspects an archive file, and copies the target file to the destination
func FindAndCopyFileFromArchive(archivePath, targetFile, destinationDir string) error {
err := archiver.Walk(archivePath, func(f archiver.File) error {
if strings.HasSuffix(f.Name(), targetFile) {
cmwylie19 marked this conversation as resolved.
Show resolved Hide resolved
// read the file in the compressed file
data, err := io.ReadAll(f)
if err != nil {
return err
}

// Create or open the destination file for writing
destinationPath := filepath.Join(destinationDir, filepath.Base(f.Name()))
destinationFile, err := os.Create(destinationPath)
if err != nil {
return err
}
defer destinationFile.Close()

// Write the data to the destination file
_, err = destinationFile.Write(data)
if err != nil {
return err
}

cmwylie19 marked this conversation as resolved.
Show resolved Hide resolved
}
// Remove the compressed file
err := os.Remove(archivePath)
if err != nil {
return err
}
return nil
Racer159 marked this conversation as resolved.
Show resolved Hide resolved
})

return err
}
12 changes: 12 additions & 0 deletions src/pkg/utils/helpers/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package helpers
import (
"fmt"
"net/url"
"path"
)

// Nonstandard URL schemes or prefixes
Expand Down Expand Up @@ -40,3 +41,14 @@ func DoHostnamesMatch(url1 string, url2 string) (bool, error) {

return parsedURL1.Hostname() == parsedURL2.Hostname(), nil
}

// ExtractFilenameFromURL returns filename from URL string
func ExtractFilenameFromURL(urlStr string) (string, error) {
cmwylie19 marked this conversation as resolved.
Show resolved Hide resolved
parsedURL, err := url.Parse(urlStr)
if err != nil {
return "", err
}

filename := path.Base(parsedURL.Path)
return filename, nil
}
23 changes: 23 additions & 0 deletions src/pkg/utils/helpers/url_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,29 @@ func (suite *TestURLSuite) Test_2_DoHostnamesMatch() {
suite.False(b)
}

func (suite *TestURLSuite) Test_3_ExtractFilenameFromURL() {
urls := []string{
cmwylie19 marked this conversation as resolved.
Show resolved Hide resolved
"https://zarf.dev/file.txt",
"https://docs.zarf.dev/file.txt",
"https://zarf.dev/docs/file.tar.gz",
"https://defenseunicorns.com/file.yaml",
"https://google.com/file.md",
}
expectations := []string{
"file.txt",
"file.txt",
"file.tar.gz",
"file.yaml",
"file.md",
}

for idx, url := range urls {
actualURL, err := ExtractFilenameFromURL(url)
suite.NoError(err)
suite.Equal(actualURL, expectations[idx])
}

}
func TestURL(t *testing.T) {
suite.Run(t, new(TestURLSuite))
}
20 changes: 20 additions & 0 deletions src/test/e2e/00_use_cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,26 @@ func TestUseCLI(t *testing.T) {
require.NoError(t, err, stdOut, stdErr)
})

t.Run("zarf package create for archive path", func(t *testing.T) {
t.Parallel()
stdOut, stdErr, err := e2e.Zarf("package", "create", "packages/distros/eks", "--zarf-cache", "--confirm")
require.NoError(t, err, stdOut, stdErr)
})

t.Run("zarf package inspect for archive path", func(t *testing.T) {
t.Parallel()
path := "build/zarf-package-distro-eks-multi-0.0.2.tar.zst"
stdOut, stdErr, err := e2e.Zarf("package", "inspect", path)
require.NoError(t, err, stdOut, stdErr)
})

t.Run("zarf package deploy for archive path", func(t *testing.T) {
t.Parallel()
path := "build/zarf-package-distro-eks-multi-0.0.2.tar.zst"
stdOut, stdErr, err := e2e.Zarf("package", "deploy", path, "--confirm")
require.NoError(t, err, stdOut, stdErr)
})

t.Run("remove cache", func(t *testing.T) {
t.Parallel()
tmpdir := t.TempDir()
Expand Down
11 changes: 6 additions & 5 deletions src/types/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,12 @@ type ZarfComponentOnlyCluster struct {

// ZarfFile defines a file to deploy.
type ZarfFile struct {
Source string `json:"source" jsonschema:"description=Local folder or file path or remote URL to pull into the package"`
Shasum string `json:"shasum,omitempty" jsonschema:"description=(files only) Optional SHA256 checksum of the file"`
Target string `json:"target" jsonschema:"description=The absolute or relative path where the file or folder should be copied to during package deploy"`
Executable bool `json:"executable,omitempty" jsonschema:"description=(files only) Determines if the file should be made executable during package deploy"`
Symlinks []string `json:"symlinks,omitempty" jsonschema:"description=List of symlinks to create during package deploy"`
Source string `json:"source" jsonschema:"description=Local folder or file path or remote URL to pull into the package"`
Shasum string `json:"shasum,omitempty" jsonschema:"description=(files only) Optional SHA256 checksum of the file"`
Target string `json:"target" jsonschema:"description=The absolute or relative path where the file or folder should be copied to during package deploy"`
Executable bool `json:"executable,omitempty" jsonschema:"description=(files only) Determines if the file should be made executable during package deploy"`
Symlinks []string `json:"symlinks,omitempty" jsonschema:"description=List of symlinks to create during package deploy"`
ArchivePath string `json:"archivePath,omitempty" jsonschema:"description=Local folder or file to be extracted into the package"`
cmwylie19 marked this conversation as resolved.
Show resolved Hide resolved
}

// ZarfChart defines a helm chart to be deployed.
Expand Down
5 changes: 5 additions & 0 deletions src/ui/lib/api-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,10 @@ export interface BigBang {
}

export interface ZarfFile {
/**
* Local folder or file to be extracted into the package
*/
archivePath?: string;
/**
* (files only) Determines if the file should be made executable during package deploy
*/
Expand Down Expand Up @@ -1593,6 +1597,7 @@ const typeMap: any = {
{ json: "version", js: "version", typ: "" },
], false),
"ZarfFile": o([
{ json: "archivePath", js: "archivePath", typ: u(undefined, "") },
{ json: "executable", js: "executable", typ: u(undefined, true) },
{ json: "shasum", js: "shasum", typ: u(undefined, "") },
{ json: "source", js: "source", typ: "" },
Expand Down
4 changes: 4 additions & 0 deletions zarf.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -794,6 +794,10 @@
},
"type": "array",
"description": "List of symlinks to create during package deploy"
},
"archivePath": {
"type": "string",
"description": "Local folder or file to be extracted into the package"
}
},
"additionalProperties": false,
Expand Down