-
Notifications
You must be signed in to change notification settings - Fork 312
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
Feature/download in stream mode #755
Merged
lonng
merged 15 commits into
pingcap:master
from
jsvisa:feature/download-in-stream-mode
Sep 22, 2020
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
c4de5b4
repository,cluster: download target in stream mode
jsvisa 651878a
repository: UpdateManifests use Download to repalce Fetch
jsvisa 4dfb1eb
pkg/repository: implement MockMirror.Download
jsvisa 1117b2c
gitignore: ignore failpoint files
jsvisa 1d94ec4
pkg/repository: fix comment by `make check`
jsvisa 2cb62d9
repository: rm component if hash checksum no equal
jsvisa 46e91bb
cluster: ensure mirror.Open after NewMirror
jsvisa a4ac339
repository: move file if name not match
jsvisa c16fa2d
docker/control: killall is from package psmisc
jsvisa 4e2138c
Fix: add mirror.Open after NewMirror
jsvisa 0b6cf1f
Chore(make): indent
jsvisa b23f865
localdata: add EnvNameKeepSourceTarget to keep source target or not
jsvisa ac0bcff
repository: rm source target if expand
jsvisa 18b9970
Merge branch 'master' into feature/download-in-stream-mode
lonng 0f329de
Merge branch 'master' into feature/download-in-stream-mode
lonng 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 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 |
---|---|---|
|
@@ -31,3 +31,5 @@ bin/ | |
/tests/tiup_mirrors/ | ||
/logs | ||
docker/secret/ | ||
*__failpoint_binding__.go | ||
*__failpoint_stash__ |
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
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 |
---|---|---|
|
@@ -20,6 +20,8 @@ import ( | |
stderrors "errors" | ||
"fmt" | ||
"io" | ||
"os" | ||
"path" | ||
"path/filepath" | ||
"runtime" | ||
"strconv" | ||
|
@@ -29,6 +31,7 @@ import ( | |
"github.com/fatih/color" | ||
cjson "github.com/gibson042/canonicaljson-go" | ||
"github.com/pingcap/errors" | ||
"github.com/pingcap/tiup/pkg/localdata" | ||
"github.com/pingcap/tiup/pkg/repository/v0manifest" | ||
"github.com/pingcap/tiup/pkg/repository/v1manifest" | ||
"github.com/pingcap/tiup/pkg/utils" | ||
|
@@ -101,6 +104,10 @@ func (r *V1Repository) UpdateComponents(specs []ComponentSpec) error { | |
return errors.Trace(err) | ||
} | ||
|
||
keepSource := false | ||
if v := os.Getenv(localdata.EnvNameKeepSourceTarget); v == "enable" || v == "true" { | ||
keepSource = true | ||
} | ||
var errs []string | ||
for _, spec := range specs { | ||
manifest, err := r.updateComponentManifest(spec.ID, false) | ||
|
@@ -153,20 +160,35 @@ func (r *V1Repository) UpdateComponents(specs []ComponentSpec) error { | |
} | ||
} | ||
|
||
reader, err := r.FetchComponent(versionItem) | ||
if spec.Version == "" { | ||
spec.Version = version | ||
} | ||
|
||
targetDir := filepath.Join(r.local.TargetRootDir(), localdata.ComponentParentDir, spec.ID, spec.Version) | ||
target := filepath.Join(targetDir, versionItem.URL) | ||
err = r.DownloadComponent(versionItem, target) | ||
if err != nil { | ||
errs = append(errs, err.Error()) | ||
continue | ||
} | ||
|
||
if spec.Version == "" { | ||
spec.Version = version | ||
} | ||
err = r.local.InstallComponent(reader, spec.TargetDir, spec.ID, spec.Version, versionItem.URL, r.DisableDecompress) | ||
reader, err := os.Open(target) | ||
if err != nil { | ||
errs = append(errs, err.Error()) | ||
continue | ||
} | ||
|
||
err = r.local.InstallComponent(reader, targetDir, spec.ID, spec.Version, versionItem.URL, r.DisableDecompress) | ||
reader.Close() | ||
|
||
if err != nil { | ||
errs = append(errs, err.Error()) | ||
} | ||
|
||
// remove the source gzip target if expand is on | ||
if !r.DisableDecompress && !keepSource { | ||
_ = os.Remove(target) | ||
} | ||
} | ||
|
||
if len(errs) > 0 { | ||
|
@@ -486,6 +508,38 @@ func (r *V1Repository) FetchComponent(item *v1manifest.VersionItem) (io.Reader, | |
return checkHash(reader, item.Hashes[v1manifest.SHA256]) | ||
} | ||
|
||
// DownloadComponent downloads the component specified by item into local file, | ||
// the component will be removed if hash is not correct | ||
func (r *V1Repository) DownloadComponent(item *v1manifest.VersionItem, target string) error { | ||
targetDir := filepath.Dir(target) | ||
err := r.mirror.Download(item.URL, targetDir) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. seems we assume |
||
if err != nil { | ||
return errors.Trace(err) | ||
} | ||
|
||
// the downloaded file is named by item.URL, which maybe differ to target name | ||
if downloaded := path.Join(targetDir, item.URL); downloaded != target { | ||
err := os.Rename(downloaded, target) | ||
if err != nil { | ||
return errors.Trace(err) | ||
} | ||
} | ||
|
||
reader, err := os.Open(target) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
_, err = checkHash(reader, item.Hashes[v1manifest.SHA256]) | ||
9547 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
reader.Close() | ||
|
||
// remove the target compoonent to avoid attacking | ||
if err != nil { | ||
_ = os.Remove(target) | ||
} | ||
return err | ||
} | ||
|
||
// FetchTimestamp downloads the timestamp file, validates it, and checks if the snapshot hash in it | ||
// has the same value of our local one. (not hashing the snapshot file itself) | ||
// Return weather the manifest is changed compared to the one in local ts and the FileHash of snapshot. | ||
|
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
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.
I think we should delete the target file when the component installed successfully.
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.
Thanks for the advice, rm the target by default, and add an env named
TIUP_KEEP_SOURCE_TARGET
to keep the source target(maybe used for debugging propose).