forked from buildpacks/pack
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Andrew Meyer <ameyer@pivotal.io>
- Loading branch information
1 parent
84b6c2a
commit 0019490
Showing
27 changed files
with
665 additions
and
683 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,38 +1,46 @@ | ||
package pack | ||
|
||
import ( | ||
"github.com/buildpack/lifecycle/image" | ||
|
||
"github.com/buildpack/pack/config" | ||
"github.com/buildpack/pack/docker" | ||
"github.com/buildpack/pack/logging" | ||
) | ||
|
||
type Client struct { | ||
config *config.Config | ||
logger *logging.Logger | ||
fetcher Fetcher | ||
} | ||
|
||
func NewClient(config *config.Config, fetcher Fetcher) *Client { | ||
func NewClient(config *config.Config, logger *logging.Logger, fetcher Fetcher) *Client { | ||
return &Client{ | ||
config: config, | ||
logger: logger, | ||
fetcher: fetcher, | ||
} | ||
} | ||
|
||
//// TODO : move to build.go | ||
//func (c *Client) Build() { | ||
// | ||
//} | ||
// | ||
//// TODO : move to create_builder.go | ||
//func (c *Client) CreateBuilder() { | ||
// | ||
//} | ||
// | ||
|
||
//// TODO : move to run.go | ||
//func (c *Client) Run() { | ||
// | ||
//} | ||
|
||
// TODO : move to rebase.go | ||
//func (c *Client) Rebase() { | ||
// | ||
//} | ||
func DefaultClient(config *config.Config, logger *logging.Logger) (*Client, error) { | ||
factory, err := image.NewFactory() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
dockerClient, err := docker.New() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
fetcher := &ImageFetcher{ | ||
Factory: factory, | ||
Docker: dockerClient, | ||
} | ||
|
||
return &Client{ | ||
config: config, | ||
logger: logger, | ||
fetcher: fetcher, | ||
}, nil | ||
} |
File renamed without changes.
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,128 @@ | ||
package pack | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
|
||
"github.com/buildpack/lifecycle" | ||
"github.com/buildpack/lifecycle/image" | ||
"github.com/pkg/errors" | ||
|
||
"github.com/buildpack/pack/config" | ||
"github.com/buildpack/pack/style" | ||
) | ||
|
||
type RebaseOptions struct { | ||
RepoName string | ||
Publish bool | ||
SkipPull bool | ||
RunImage string | ||
} | ||
|
||
func (c *Client) Rebase(ctx context.Context, opts RebaseOptions) error { | ||
fetchImage := c.imageFetchFn(ctx, opts) | ||
|
||
appImage, err := fetchImage(opts.RepoName) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
runImageName, err := c.getRunImageName(ctx, opts, appImage) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
baseImage, err := fetchImage(runImageName) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
label, err := appImage.Label(lifecycle.MetadataLabel) | ||
if err != nil { | ||
return err | ||
} | ||
var metadata lifecycle.AppImageMetadata | ||
if err := json.Unmarshal([]byte(label), &metadata); err != nil { | ||
return err | ||
} | ||
c.logger.Info("Rebasing %s on run image %s", style.Symbol(appImage.Name()), style.Symbol(baseImage.Name())) | ||
if err := appImage.Rebase(metadata.RunImage.TopLayer, baseImage); err != nil { | ||
return err | ||
} | ||
|
||
metadata.RunImage.SHA, err = baseImage.Digest() | ||
if err != nil { | ||
return err | ||
} | ||
metadata.RunImage.TopLayer, err = baseImage.TopLayer() | ||
if err != nil { | ||
return err | ||
} | ||
newLabel, err := json.Marshal(metadata) | ||
if err := appImage.SetLabel(lifecycle.MetadataLabel, string(newLabel)); err != nil { | ||
return err | ||
} | ||
|
||
sha, err := appImage.Save() | ||
if err != nil { | ||
return err | ||
} | ||
c.logger.Info("New sha: %s", style.Symbol(sha)) | ||
return nil | ||
} | ||
|
||
func (c *Client) imageFetchFn(ctx context.Context, opts RebaseOptions) func(string) (image.Image, error) { | ||
var newImageFn func(string) (image.Image, error) | ||
if opts.Publish { | ||
newImageFn = c.fetcher.FetchRemoteImage | ||
} else { | ||
newImageFn = func(name string) (image.Image, error) { | ||
if opts.SkipPull { | ||
return c.fetcher.FetchLocalImage(name) | ||
|
||
} else { | ||
return c.fetcher.FetchUpdatedLocalImage(ctx, name, c.logger.RawVerboseWriter()) | ||
} | ||
} | ||
} | ||
return newImageFn | ||
} | ||
|
||
func (c *Client) getRunImageName(ctx context.Context, opts RebaseOptions, appImage image.Image) (string, error) { | ||
var runImageName string | ||
if opts.RunImage != "" { | ||
runImageName = opts.RunImage | ||
} else { | ||
contents, err := appImage.Label(lifecycle.MetadataLabel) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
var appImageMetadata lifecycle.AppImageMetadata | ||
if err := json.Unmarshal([]byte(contents), &appImageMetadata); err != nil { | ||
return "", err | ||
} | ||
|
||
registry, err := config.Registry(opts.RepoName) | ||
if err != nil { | ||
return "", errors.Wrapf(err, "parsing registry from reference '%s'", opts.RepoName) | ||
} | ||
|
||
var mirrors []string | ||
if localRunImage := c.config.GetRunImage(appImageMetadata.Stack.RunImage.Image); localRunImage != nil { | ||
mirrors = localRunImage.Mirrors | ||
} | ||
mirrors = append(mirrors, appImageMetadata.Stack.RunImage.Image) | ||
mirrors = append(mirrors, appImageMetadata.Stack.RunImage.Mirrors...) | ||
runImageName, err = config.ImageByRegistry(registry, mirrors) | ||
if err != nil { | ||
return "", errors.Wrapf(err, "find image by registry") | ||
} | ||
} | ||
|
||
if runImageName == "" { | ||
return "", errors.New("run image must be specified") | ||
} | ||
|
||
return runImageName, nil | ||
} |
Oops, something went wrong.