9f82b75e57
* removed packer.Cache and references since packer.Cache is never used except in the download step. The download step now uses the new func packer.CachePath(targetPath) for this, the behavior is the same. * removed download code from packer that was reimplemented into the go-getter library: progress bar, http download restart, checksuming from file, skip already downloaded files, symlinking, make a download cancellable by context. * on windows if packer is running without symlinking rights and we are getting a local file, the file will be copied instead to avoid errors. * added unit tests for step_download that are now CI tested on windows, mac & linux. * files are now downloaded under cache dir `sha1(filename + "?checksum=" + checksum) + file_extension` * since the output dir is based on the source url and the checksum, when the checksum fails, the file is auto deleted. * a download file is protected and locked by a file lock, * updated docs * updated go modules and vendors
78 lines
1.6 KiB
Go
78 lines
1.6 KiB
Go
// Copyright 2018 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
// +build appengine
|
|
|
|
// This file applies to App Engine first generation runtimes (<= Go 1.9).
|
|
|
|
package google
|
|
|
|
import (
|
|
"context"
|
|
"sort"
|
|
"strings"
|
|
"sync"
|
|
|
|
"golang.org/x/oauth2"
|
|
"google.golang.org/appengine"
|
|
)
|
|
|
|
func init() {
|
|
appengineTokenFunc = appengine.AccessToken
|
|
appengineAppIDFunc = appengine.AppID
|
|
}
|
|
|
|
// See comment on AppEngineTokenSource in appengine.go.
|
|
func appEngineTokenSource(ctx context.Context, scope ...string) oauth2.TokenSource {
|
|
scopes := append([]string{}, scope...)
|
|
sort.Strings(scopes)
|
|
return &gaeTokenSource{
|
|
ctx: ctx,
|
|
scopes: scopes,
|
|
key: strings.Join(scopes, " "),
|
|
}
|
|
}
|
|
|
|
// aeTokens helps the fetched tokens to be reused until their expiration.
|
|
var (
|
|
aeTokensMu sync.Mutex
|
|
aeTokens = make(map[string]*tokenLock) // key is space-separated scopes
|
|
)
|
|
|
|
type tokenLock struct {
|
|
mu sync.Mutex // guards t; held while fetching or updating t
|
|
t *oauth2.Token
|
|
}
|
|
|
|
type gaeTokenSource struct {
|
|
ctx context.Context
|
|
scopes []string
|
|
key string // to aeTokens map; space-separated scopes
|
|
}
|
|
|
|
func (ts *gaeTokenSource) Token() (*oauth2.Token, error) {
|
|
aeTokensMu.Lock()
|
|
tok, ok := aeTokens[ts.key]
|
|
if !ok {
|
|
tok = &tokenLock{}
|
|
aeTokens[ts.key] = tok
|
|
}
|
|
aeTokensMu.Unlock()
|
|
|
|
tok.mu.Lock()
|
|
defer tok.mu.Unlock()
|
|
if tok.t.Valid() {
|
|
return tok.t, nil
|
|
}
|
|
access, exp, err := appengineTokenFunc(ts.ctx, ts.scopes...)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
tok.t = &oauth2.Token{
|
|
AccessToken: access,
|
|
Expiry: exp,
|
|
}
|
|
return tok.t, nil
|
|
}
|