-
Notifications
You must be signed in to change notification settings - Fork 84
Update workflow to embed app assets #1039
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
Merged
+374
−47
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
e409889
Update workflow to embed app assets
jlewi b8ba416
Add workflow dispatch
jlewi fe9c19a
Allow no APIKeyFile to allow oauth.
jlewi 46d5906
Add agent assets download command
jlewi f4ccfe1
Clean index files before updating assets
jlewi d8a0695
Configure a default directory for the static assets.
jlewi ac53cb4
Always use CORS to protect static assets; don't allow * for static as…
jlewi 3b3772b
Add assets-dir flag for agent assets download
jlewi 394a591
Extract agent assets directly into output dir
jlewi 3cf0057
Fix the workflow.
jlewi 9c18112
Fix.
jlewi f043b29
Fix lint.
jlewi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,175 @@ | ||
| package assets | ||
|
|
||
| import ( | ||
| "archive/tar" | ||
| "compress/gzip" | ||
| "context" | ||
| "io" | ||
| "net/http" | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
|
|
||
| "github.com/pkg/errors" | ||
| "oras.land/oras-go/v2" | ||
| "oras.land/oras-go/v2/content/file" | ||
| "oras.land/oras-go/v2/registry" | ||
| "oras.land/oras-go/v2/registry/remote" | ||
| "oras.land/oras-go/v2/registry/remote/auth" | ||
| ) | ||
|
|
||
| const ( | ||
| defaultArchiveName = "app-assets.tgz" | ||
| ) | ||
|
|
||
| // DownloadFromImage pulls assets from an OCI image and unpacks them into outputDir. | ||
| func DownloadFromImage(ctx context.Context, imageRef, outputDir string) error { | ||
| if imageRef == "" { | ||
| return errors.New("image reference is required") | ||
| } | ||
| if outputDir == "" { | ||
| return errors.New("assets output directory is required") | ||
| } | ||
|
|
||
| if err := os.MkdirAll(outputDir, 0o755); err != nil { | ||
| return errors.Wrapf(err, "failed to create assets output directory %s", outputDir) | ||
| } | ||
|
|
||
| tempDir, err := os.MkdirTemp("", "runme-agent-assets-*") | ||
| if err != nil { | ||
| return errors.Wrap(err, "failed to create temporary assets directory") | ||
| } | ||
| defer func() { | ||
| _ = os.RemoveAll(tempDir) | ||
| }() | ||
|
|
||
| if err := pullImage(ctx, imageRef, tempDir); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| archivePath := filepath.Join(tempDir, defaultArchiveName) | ||
| if _, err := os.Stat(archivePath); err != nil { | ||
| return errors.Wrapf(err, "expected assets archive not found: %s", archivePath) | ||
| } | ||
|
|
||
| if err := removeIndexFiles(outputDir); err != nil { | ||
| return err | ||
| } | ||
| if err := extractTarGz(archivePath, outputDir); err != nil { | ||
| return errors.Wrapf(err, "failed to extract assets archive %s", archivePath) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func removeIndexFiles(outputDir string) error { | ||
| matches, err := filepath.Glob(filepath.Join(outputDir, "index.*")) | ||
| if err != nil { | ||
| return errors.Wrapf(err, "failed to glob index files in %s", outputDir) | ||
| } | ||
| for _, match := range matches { | ||
| if err := os.RemoveAll(match); err != nil { | ||
| return errors.Wrapf(err, "failed to remove %s", match) | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func pullImage(ctx context.Context, imageRef, outputDir string) error { | ||
| ref, err := registry.ParseReference(imageRef) | ||
| if err != nil { | ||
| return errors.Wrapf(err, "invalid image reference %q", imageRef) | ||
| } | ||
|
|
||
| repo, err := remote.NewRepository(ref.Registry + "/" + ref.Repository) | ||
| if err != nil { | ||
| return errors.Wrapf(err, "failed to create repository for %q", imageRef) | ||
| } | ||
|
|
||
| repo.Client = &auth.Client{ | ||
| Client: http.DefaultClient, | ||
| Cache: auth.NewCache(), | ||
| } | ||
|
|
||
| if ref.Reference == "" { | ||
| ref.Reference = "latest" | ||
| } | ||
|
|
||
| store, err := file.New(outputDir) | ||
| if err != nil { | ||
| return errors.Wrapf(err, "failed to create output file store %s", outputDir) | ||
| } | ||
| defer store.Close() | ||
|
|
||
| if _, err := oras.Copy(ctx, repo, ref.Reference, store, "", oras.DefaultCopyOptions); err != nil { | ||
| return errors.Wrapf(err, "failed to pull image %s", imageRef) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func extractTarGz(archivePath, destDir string) error { | ||
| fileHandle, err := os.Open(archivePath) | ||
| if err != nil { | ||
| return errors.Wrapf(err, "failed to open archive %s", archivePath) | ||
| } | ||
| defer fileHandle.Close() | ||
|
|
||
| gzipReader, err := gzip.NewReader(fileHandle) | ||
| if err != nil { | ||
| return errors.Wrap(err, "failed to create gzip reader") | ||
| } | ||
| defer gzipReader.Close() | ||
|
|
||
| tarReader := tar.NewReader(gzipReader) | ||
| destDirClean := filepath.Clean(destDir) | ||
| if !strings.HasSuffix(destDirClean, string(os.PathSeparator)) { | ||
| destDirClean += string(os.PathSeparator) | ||
| } | ||
|
|
||
| for { | ||
| header, err := tarReader.Next() | ||
| if err == io.EOF { | ||
| break | ||
| } | ||
| if err != nil { | ||
| return errors.Wrap(err, "failed to read tar entry") | ||
| } | ||
|
|
||
| if header == nil { | ||
| continue | ||
| } | ||
|
|
||
| targetPath := filepath.Join(destDir, header.Name) | ||
| cleanTarget := filepath.Clean(targetPath) | ||
| if !strings.HasPrefix(cleanTarget, destDirClean) { | ||
| return errors.Errorf("invalid tar entry path: %s", header.Name) | ||
| } | ||
|
|
||
| switch header.Typeflag { | ||
| case tar.TypeDir: | ||
| if err := os.MkdirAll(cleanTarget, os.FileMode(header.Mode)); err != nil { | ||
| return errors.Wrapf(err, "failed to create directory %s", cleanTarget) | ||
| } | ||
| case tar.TypeReg, tar.TypeRegA: | ||
| if err := os.MkdirAll(filepath.Dir(cleanTarget), 0o755); err != nil { | ||
| return errors.Wrapf(err, "failed to create parent directory for %s", cleanTarget) | ||
| } | ||
| outFile, err := os.OpenFile(cleanTarget, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, os.FileMode(header.Mode)) | ||
| if err != nil { | ||
| return errors.Wrapf(err, "failed to create file %s", cleanTarget) | ||
| } | ||
| if _, err := io.Copy(outFile, tarReader); err != nil { | ||
| _ = outFile.Close() | ||
| return errors.Wrapf(err, "failed to write file %s", cleanTarget) | ||
| } | ||
| if err := outFile.Close(); err != nil { | ||
| return errors.Wrapf(err, "failed to close file %s", cleanTarget) | ||
| } | ||
| default: | ||
| return errors.Errorf("unsupported tar entry type %v for %s", header.Typeflag, header.Name) | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
jlewi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| package cmd | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
| "path/filepath" | ||
|
|
||
| "github.com/go-logr/zapr" | ||
| "github.com/pkg/errors" | ||
| "github.com/spf13/cobra" | ||
| "go.uber.org/zap" | ||
|
|
||
| "github.com/runmedev/runme/v3/pkg/agent/application" | ||
| "github.com/runmedev/runme/v3/pkg/agent/assets" | ||
| ) | ||
|
|
||
| // NewDownloadAssetsCmd downloads and unpacks the web app assets from an OCI image. | ||
| func NewDownloadAssetsCmd(appName string) *cobra.Command { | ||
| var imageRef string | ||
| var assetsDirFlag string | ||
|
|
||
| cmd := &cobra.Command{ | ||
| Use: "download-assets", | ||
| Short: "Download and unpack web app assets", | ||
| Run: func(cmd *cobra.Command, args []string) { | ||
| err := func() error { | ||
| if imageRef == "" { | ||
| return errors.New("image reference is required; set --image") | ||
| } | ||
|
|
||
| var assetsDir string | ||
| if assetsDirFlag != "" { | ||
| assetsDir = assetsDirFlag | ||
| } else { | ||
| app := application.NewApp(appName) | ||
| if err := app.LoadConfig(cmd); err != nil { | ||
| return err | ||
| } | ||
| if err := app.SetupLogging(); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| cfg := app.AppConfig.GetConfig() | ||
| if cfg.AssistantServer == nil { | ||
| return errors.New("assistantServer config must be set to download assets") | ||
| } | ||
|
|
||
| assetsDir = cfg.AssistantServer.StaticAssets | ||
| if assetsDir == "" { | ||
| homeDir, err := os.UserHomeDir() | ||
| if err != nil { | ||
| return errors.Wrap(err, "failed to resolve home directory for assets") | ||
| } | ||
| assetsDir = filepath.Join(homeDir, "."+appName, "assets") | ||
| } else if !filepath.IsAbs(assetsDir) { | ||
| homeDir, err := os.UserHomeDir() | ||
| if err != nil { | ||
| return errors.Wrap(err, "failed to resolve home directory for assets") | ||
| } | ||
| assetsDir = filepath.Join(homeDir, assetsDir) | ||
| } | ||
| } | ||
|
|
||
| log := zapr.NewLogger(zap.L()) | ||
| log.Info("Downloading assets image", "image", imageRef, "dir", assetsDir) | ||
|
|
||
| if err := assets.DownloadFromImage(cmd.Context(), imageRef, assetsDir); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| log.Info("Assets download complete", "dir", assetsDir) | ||
| return nil | ||
| }() | ||
| if err != nil { | ||
| fmt.Printf("Failed to download assets;\n%+v\n", err) | ||
| os.Exit(1) | ||
| } | ||
| }, | ||
| } | ||
|
|
||
| cmd.Flags().StringVar(&imageRef, "image", "ghcr.io/runmedev/app-assets:latest", "OCI image reference to download (e.g. ghcr.io/runmedev/app-assets:latest)") | ||
| cmd.Flags().StringVar(&assetsDirFlag, "assets-dir", "", "Directory to download and unpack assets (skips config)") | ||
|
|
||
| return cmd | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.