-
Notifications
You must be signed in to change notification settings - Fork 213
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
Publish bundles and Allow Install From Tag #379
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
9d8ca53
WIP pull on install
jeremyrickard e8c2586
Push, Pull with a little bit of cache thrown in
jeremyrickard f5b9692
Updated docs
jeremyrickard d11567f
Fix dep
jeremyrickard 6c66118
Dep again
jeremyrickard 8671c52
Fixing test, didn't re-run unit tests after rebasing, hash changed since
jeremyrickard ab50525
Resolve code review comments
jeremyrickard 2bb702e
make verify-vendor is now happy
jeremyrickard a374ef1
Change the flag/help text for insecure-registry on publish
jeremyrickard 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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 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 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,88 @@ | ||
package cache | ||
|
||
import ( | ||
"crypto/md5" | ||
"encoding/hex" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/deislabs/cnab-go/bundle" | ||
"github.com/deislabs/porter/pkg/config" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
type Cache struct { | ||
*config.Config | ||
} | ||
|
||
func New(cfg *config.Config) *Cache { | ||
return &Cache{ | ||
Config: cfg, | ||
} | ||
} | ||
|
||
// FindBundle looks for a given bundle tag in the Porter bundle cache and | ||
// returns the path to the bundle if it exists. If it is not found, an | ||
// empty string and the boolean false value are returned. | ||
func (c *Cache) FindBundle(bundleTag string) (string, bool, error) { | ||
bid := getBundleID(bundleTag) | ||
bundleCnabDir, err := c.getCachedBundleCNABDir(bid) | ||
cachedBundlePath := filepath.Join(bundleCnabDir, "bundle.json") | ||
bExists, err := c.FileSystem.Exists(cachedBundlePath) | ||
if err != nil { | ||
return "", false, errors.Wrapf(err, "unable to read bundle %s at %s", bundleTag, cachedBundlePath) | ||
} | ||
if !bExists { | ||
return "", false, nil | ||
} | ||
return cachedBundlePath, true, nil | ||
|
||
} | ||
|
||
// StoreBundle will write a given bundle to the bundle cache, in a location derived | ||
// from the bundleTag. | ||
func (c *Cache) StoreBundle(bundleTag string, bun *bundle.Bundle) (string, error) { | ||
bid := getBundleID(bundleTag) | ||
bundleCnabDir, err := c.getCachedBundleCNABDir(bid) | ||
cachedBundlePath := filepath.Join(bundleCnabDir, "bundle.json") | ||
err = c.FileSystem.MkdirAll(bundleCnabDir, os.ModePerm) | ||
if err != nil { | ||
return "", errors.Wrap(err, "unable to create cache directory") | ||
} | ||
f, err := c.FileSystem.OpenFile(cachedBundlePath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644) | ||
defer f.Close() | ||
if err != nil { | ||
return "", errors.Wrapf(err, "error creating cnab/bundle.json") | ||
} | ||
_, err = bun.WriteTo(f) | ||
if err != nil { | ||
return "", errors.Wrap(err, "error writing to cnab/bundle.json") | ||
} | ||
return cachedBundlePath, nil | ||
} | ||
|
||
func (c *Cache) getCacheDir() (string, error) { | ||
home, err := c.GetHomeDir() | ||
if err != nil { | ||
return "", err | ||
} | ||
return filepath.Join(home, "cache"), nil | ||
} | ||
|
||
func (c *Cache) getCachedBundleCNABDir(bid string) (string, error) { | ||
cacheDir, err := c.getCacheDir() | ||
if err != nil { | ||
return "", err | ||
} | ||
bundleDir := filepath.Join(cacheDir, string(bid)) | ||
bundleCNABPath := filepath.Join(bundleDir, "cnab") | ||
return bundleCNABPath, nil | ||
|
||
} | ||
|
||
func getBundleID(bundleTag string) string { | ||
// hash the tag, tags have characters that won't work as part of a path | ||
// so hashing here to get a path friendly name | ||
bid := md5.Sum([]byte(bundleTag)) | ||
return hex.EncodeToString(bid[:]) | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would help for the documentation to use example names that have some meaning or hint at what people should use instead. I suggest using something either obviously fake like "foo" or better yet something like "--cred mycluster" or something like that for examples.
Also, I recommend using the full tag names,
--cred
and not the short flags in the examples, for consistency.