-
Notifications
You must be signed in to change notification settings - Fork 156
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
Update lifecycle.DownloadBinary to work with local file #5398
Merged
Merged
Changes from all commits
Commits
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 |
---|---|---|
|
@@ -19,6 +19,7 @@ | |
"fmt" | ||
"io" | ||
"net/http" | ||
"net/url" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
|
@@ -104,7 +105,7 @@ | |
|
||
// DownloadBinary downloads a file from the given URL into the specified path | ||
// this also marks it executable and returns its full path. | ||
func DownloadBinary(url, destDir, destFile string, logger *zap.Logger) (string, error) { | ||
func DownloadBinary(sourceURL, destDir, destFile string, logger *zap.Logger) (string, error) { | ||
if err := os.MkdirAll(destDir, 0755); err != nil { | ||
return "", fmt.Errorf("could not create directory %s (%w)", destDir, err) | ||
} | ||
|
@@ -132,32 +133,52 @@ | |
} | ||
}() | ||
|
||
logger.Info("downloading binary", zap.String("url", url)) | ||
logger.Info("downloading binary", zap.String("url", sourceURL)) | ||
|
||
req, err := http.NewRequest("GET", url, nil) | ||
u, err := url.Parse(sourceURL) | ||
if err != nil { | ||
return "", fmt.Errorf("could not create request (%w)", err) | ||
return "", fmt.Errorf("could not parse URL %s (%w)", sourceURL, err) | ||
} | ||
client := &http.Client{} | ||
resp, err := client.Do(req) | ||
if err != nil { | ||
return "", fmt.Errorf("HTTP GET %s failed (%w)", url, err) | ||
} | ||
defer resp.Body.Close() | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
return "", fmt.Errorf("HTTP GET %s failed with error %d", url, resp.StatusCode) | ||
} | ||
switch u.Scheme { | ||
case "http", "https": | ||
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. [ASK] Are we gonna support 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. No we don't, just place http there to make testing process simpler 🙏 |
||
req, err := http.NewRequest("GET", sourceURL, nil) | ||
if err != nil { | ||
return "", fmt.Errorf("could not create request (%w)", err) | ||
} | ||
client := &http.Client{} | ||
resp, err := client.Do(req) | ||
if err != nil { | ||
return "", fmt.Errorf("HTTP GET %s failed (%w)", sourceURL, err) | ||
} | ||
defer resp.Body.Close() | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
return "", fmt.Errorf("HTTP GET %s failed with error %d", sourceURL, resp.StatusCode) | ||
} | ||
|
||
if _, err = io.Copy(tmpFile, resp.Body); err != nil { | ||
return "", fmt.Errorf("could not copy from %s to %s (%w)", url, tmpName, err) | ||
if _, err = io.Copy(tmpFile, resp.Body); err != nil { | ||
return "", fmt.Errorf("could not copy from %s to %s (%w)", sourceURL, tmpName, err) | ||
} | ||
|
||
case "file": | ||
data, err := os.ReadFile(u.Path) | ||
if err != nil { | ||
return "", fmt.Errorf("could not read file %s (%w)", u.Path, err) | ||
} | ||
|
||
if _, err = tmpFile.Write(data); err != nil { | ||
return "", fmt.Errorf("could not write to %s (%w)", tmpName, err) | ||
} | ||
|
||
default: | ||
return "", fmt.Errorf("unsupported file scheme %s", u.Scheme) | ||
} | ||
|
||
if err := os.Chmod(tmpName, 0755); err != nil { | ||
return "", fmt.Errorf("could not chmod file %s (%w)", tmpName, err) | ||
} | ||
|
||
tmpFile.Close() | ||
if err := os.Rename(tmpName, destPath); err != nil { | ||
return "", fmt.Errorf("could not move %s to %s (%w)", tmpName, destPath, err) | ||
} | ||
|
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
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.
This makes the test easier since I don't want to mock the HTTPS server. The validation logic in the piped configuration remains to only allow the "https" and "file" schemes.
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 is difficult to test with https server because
DownloadBinary
uses http.Client{} internally and can't inject for now. So we can't use httptest.NewTLSServer.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.
@t-kikuc For the question you asked 🙏
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.
@khanhtc1202
I got it.
binary.go allows http and https,
PipedPlugin
disallows http.pipecd/pkg/configv1/piped.go
Lines 1328 to 1330 in dcecc2d