Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions http.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package stretcher

type HTTPOptions struct {
Headers map[string]string `yaml:"headers"`
RetryMax int `yaml:"retry_max"`
Headers map[string]string `help:"HTTP request headers(key=value) for download src archives with HTTP or HTTPS"`
}
4 changes: 2 additions & 2 deletions manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,13 @@ func (m *Manifest) Deploy(ctx context.Context, conf *Config) error {

func (m *Manifest) fetchSrc(ctx context.Context, conf *Config, tmp *os.File) error {
begin := time.Now()
src, err := getURL(ctx, m.Src)
src, err := getURL(ctx, m.Src, conf)
if err != nil {
for i := 0; i < conf.Retry; i++ {
log.Printf("Get src failed: %s", err)
log.Printf("Try again. Waiting: %s", conf.RetryWait)
time.Sleep(conf.RetryWait)
src, err = getURL(ctx, m.Src)
src, err = getURL(ctx, m.Src, conf)
if err == nil {
break
}
Expand Down
16 changes: 10 additions & 6 deletions stretcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type Config struct {
RetryWait time.Duration `help:"wait for retry download src archives"`
RsyncVerbose string `help:"rsync verbose option (default: -v)" default:"-v"`
Version kong.VersionFlag `short:"v" help:"Show version and exit."`
HTTP HTTPOptions `embed prefix:"http-"`

maxbw uint64
initSleep time.Duration
Expand Down Expand Up @@ -86,7 +87,7 @@ func Run(ctx context.Context, conf *Config) error {
}

log.Println("Loading manifest:", manifestURL)
m, err := getManifest(ctx, manifestURL)
m, err := getManifest(ctx, manifestURL, conf)
if err != nil {
return fmt.Errorf("load manifest failed: %w", err)
}
Expand Down Expand Up @@ -144,12 +145,15 @@ func getFile(_ context.Context, u *url.URL) (io.ReadCloser, error) {
return os.Open(u.Path)
}

func getHTTP(ctx context.Context, u *url.URL) (io.ReadCloser, error) {
func getHTTP(ctx context.Context, u *url.URL, opt *HTTPOptions) (io.ReadCloser, error) {
req, err := http.NewRequestWithContext(ctx, "GET", u.String(), nil)
if err != nil {
return nil, err
}
req.Header.Add("User-Agent", "Stretcher/"+Version)
for k, v := range opt.Headers {
req.Header.Add(k, v)
}

resp, err := http.DefaultClient.Do(req)
if err != nil {
Expand All @@ -158,7 +162,7 @@ func getHTTP(ctx context.Context, u *url.URL) (io.ReadCloser, error) {
return resp.Body, nil
}

func getURL(ctx context.Context, urlStr string) (io.ReadCloser, error) {
func getURL(ctx context.Context, urlStr string, conf *Config) (io.ReadCloser, error) {
log.Println("Loading URL", urlStr)
u, err := url.Parse(urlStr)
if err != nil {
Expand All @@ -170,16 +174,16 @@ func getURL(ctx context.Context, urlStr string) (io.ReadCloser, error) {
case "gs":
return getGS(ctx, u)
case "http", "https":
return getHTTP(ctx, u)
return getHTTP(ctx, u, &conf.HTTP)
case "file":
return getFile(ctx, u)
default:
return nil, fmt.Errorf("manifest URL scheme must be s3, gs, http(s) or file: %s", urlStr)
}
}

func getManifest(ctx context.Context, manifestURL string) (*Manifest, error) {
rc, err := getURL(ctx, manifestURL)
func getManifest(ctx context.Context, manifestURL string, conf *Config) (*Manifest, error) {
rc, err := getURL(ctx, manifestURL, conf)
if err != nil {
return nil, err
}
Expand Down