mirror of https://github.com/docker/docs.git
lab: move s3 redirects to lambda function
Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
parent
92948cbfd7
commit
a0c0aec486
|
@ -37,6 +37,7 @@ jobs:
|
|||
elif [ "${{ github.ref }}" = "refs/heads/lab" ]; then
|
||||
DOCS_URL="https://docs-lab.docker.com"
|
||||
DOCS_S3_BUCKET=""
|
||||
DOCS_S3_CONFIG="s3-config.json"
|
||||
DOCS_LAMBDA_FUNCTION_CACHE=""
|
||||
DOCS_S3_CONFIG="_website-config-docs-lab.json"
|
||||
else
|
||||
|
|
|
@ -63,5 +63,6 @@ RUN --mount=type=bind,target=. \
|
|||
--mount=type=secret,id=AWS_SECRET_ACCESS_KEY \
|
||||
--mount=type=secret,id=AWS_SESSION_TOKEN \
|
||||
AWS_LAMBDA_FUNCTION_FILE=cloudfront-lambda-redirects.js \
|
||||
REDIRECTS_JSON=$(cat /site/redirects.json) \
|
||||
REDIRECTS_JSON=$(jq -c '.' /site/redirects.json) \
|
||||
REDIRECTS_PREFIXES_JSON=$(jq -c '.' redirects-prefixes.json) \
|
||||
releaser aws cloudfront-update
|
||||
|
|
|
@ -1,388 +0,0 @@
|
|||
{
|
||||
"ErrorDocument": {
|
||||
"Key": "404.html"
|
||||
},
|
||||
"IndexDocument": {
|
||||
"Suffix": "index.html"
|
||||
},
|
||||
"RedirectAllRequestsTo": null,
|
||||
"RoutingRules": [
|
||||
{
|
||||
"Condition": {
|
||||
"HttpErrorCodeReturnedEquals": null,
|
||||
"KeyPrefixEquals": "v1.4/"
|
||||
},
|
||||
"Redirect": {
|
||||
"HostName": "docs-lab.docker.com",
|
||||
"HttpRedirectCode": null,
|
||||
"Protocol": "https",
|
||||
"ReplaceKeyPrefixWith": "",
|
||||
"ReplaceKeyWith": null
|
||||
}
|
||||
},
|
||||
{
|
||||
"Condition": {
|
||||
"HttpErrorCodeReturnedEquals": null,
|
||||
"KeyPrefixEquals": "v1.5/"
|
||||
},
|
||||
"Redirect": {
|
||||
"HostName": "docs-lab.docker.com",
|
||||
"HttpRedirectCode": null,
|
||||
"Protocol": "https",
|
||||
"ReplaceKeyPrefixWith": "",
|
||||
"ReplaceKeyWith": null
|
||||
}
|
||||
},
|
||||
{
|
||||
"Condition": {
|
||||
"HttpErrorCodeReturnedEquals": null,
|
||||
"KeyPrefixEquals": "v1.6/"
|
||||
},
|
||||
"Redirect": {
|
||||
"HostName": "docs-lab.docker.com",
|
||||
"HttpRedirectCode": null,
|
||||
"Protocol": "https",
|
||||
"ReplaceKeyPrefixWith": "",
|
||||
"ReplaceKeyWith": null
|
||||
}
|
||||
},
|
||||
{
|
||||
"Condition": {
|
||||
"HttpErrorCodeReturnedEquals": null,
|
||||
"KeyPrefixEquals": "v1.7/"
|
||||
},
|
||||
"Redirect": {
|
||||
"HostName": "docs-lab.docker.com",
|
||||
"HttpRedirectCode": null,
|
||||
"Protocol": "https",
|
||||
"ReplaceKeyPrefixWith": "",
|
||||
"ReplaceKeyWith": null
|
||||
}
|
||||
},
|
||||
{
|
||||
"Condition": {
|
||||
"HttpErrorCodeReturnedEquals": null,
|
||||
"KeyPrefixEquals": "v1.8/"
|
||||
},
|
||||
"Redirect": {
|
||||
"HostName": "docs-lab.docker.com",
|
||||
"HttpRedirectCode": null,
|
||||
"Protocol": "https",
|
||||
"ReplaceKeyPrefixWith": "",
|
||||
"ReplaceKeyWith": null
|
||||
}
|
||||
},
|
||||
{
|
||||
"Condition": {
|
||||
"HttpErrorCodeReturnedEquals": null,
|
||||
"KeyPrefixEquals": "v1.9/"
|
||||
},
|
||||
"Redirect": {
|
||||
"HostName": "docs-lab.docker.com",
|
||||
"HttpRedirectCode": null,
|
||||
"Protocol": "https",
|
||||
"ReplaceKeyPrefixWith": "",
|
||||
"ReplaceKeyWith": null
|
||||
}
|
||||
},
|
||||
{
|
||||
"Condition": {
|
||||
"HttpErrorCodeReturnedEquals": null,
|
||||
"KeyPrefixEquals": "v1.10/"
|
||||
},
|
||||
"Redirect": {
|
||||
"HostName": "docs-lab.docker.com",
|
||||
"HttpRedirectCode": null,
|
||||
"Protocol": "https",
|
||||
"ReplaceKeyPrefixWith": "",
|
||||
"ReplaceKeyWith": null
|
||||
}
|
||||
},
|
||||
{
|
||||
"Condition": {
|
||||
"HttpErrorCodeReturnedEquals": null,
|
||||
"KeyPrefixEquals": "v1.11/"
|
||||
},
|
||||
"Redirect": {
|
||||
"HostName": "docs-lab.docker.com",
|
||||
"HttpRedirectCode": null,
|
||||
"Protocol": "https",
|
||||
"ReplaceKeyPrefixWith": "",
|
||||
"ReplaceKeyWith": null
|
||||
}
|
||||
},
|
||||
{
|
||||
"Condition": {
|
||||
"HttpErrorCodeReturnedEquals": null,
|
||||
"KeyPrefixEquals": "v1.12/"
|
||||
},
|
||||
"Redirect": {
|
||||
"HostName": "docs-lab.docker.com",
|
||||
"HttpRedirectCode": null,
|
||||
"Protocol": "https",
|
||||
"ReplaceKeyPrefixWith": "",
|
||||
"ReplaceKeyWith": null
|
||||
}
|
||||
},
|
||||
{
|
||||
"Condition": {
|
||||
"HttpErrorCodeReturnedEquals": null,
|
||||
"KeyPrefixEquals": "v1.13/"
|
||||
},
|
||||
"Redirect": {
|
||||
"HostName": "docs-lab.docker.com",
|
||||
"HttpRedirectCode": null,
|
||||
"Protocol": "https",
|
||||
"ReplaceKeyPrefixWith": "",
|
||||
"ReplaceKeyWith": null
|
||||
}
|
||||
},
|
||||
{
|
||||
"Condition": {
|
||||
"HttpErrorCodeReturnedEquals": null,
|
||||
"KeyPrefixEquals": "v17.03/"
|
||||
},
|
||||
"Redirect": {
|
||||
"HostName": "docs-lab.docker.com",
|
||||
"HttpRedirectCode": null,
|
||||
"Protocol": "https",
|
||||
"ReplaceKeyPrefixWith": "",
|
||||
"ReplaceKeyWith": null
|
||||
}
|
||||
},
|
||||
{
|
||||
"Condition": {
|
||||
"HttpErrorCodeReturnedEquals": null,
|
||||
"KeyPrefixEquals": "v17.09/"
|
||||
},
|
||||
"Redirect": {
|
||||
"HostName": "docs-lab.docker.com",
|
||||
"HttpRedirectCode": null,
|
||||
"Protocol": "https",
|
||||
"ReplaceKeyPrefixWith": "",
|
||||
"ReplaceKeyWith": null
|
||||
}
|
||||
},
|
||||
{
|
||||
"Condition": {
|
||||
"HttpErrorCodeReturnedEquals": null,
|
||||
"KeyPrefixEquals": "v17.12/"
|
||||
},
|
||||
"Redirect": {
|
||||
"HostName": "docs-lab.docker.com",
|
||||
"HttpRedirectCode": null,
|
||||
"Protocol": "https",
|
||||
"ReplaceKeyPrefixWith": "",
|
||||
"ReplaceKeyWith": null
|
||||
}
|
||||
},
|
||||
{
|
||||
"Condition": {
|
||||
"HttpErrorCodeReturnedEquals": null,
|
||||
"KeyPrefixEquals": "ee/licensing/"
|
||||
},
|
||||
"Redirect": {
|
||||
"HostName": "docs-lab.docker.com",
|
||||
"HttpRedirectCode": null,
|
||||
"Protocol": "https",
|
||||
"ReplaceKeyPrefixWith": null,
|
||||
"ReplaceKeyWith": ""
|
||||
}
|
||||
},
|
||||
{
|
||||
"Condition": {
|
||||
"HttpErrorCodeReturnedEquals": null,
|
||||
"KeyPrefixEquals": "ee/get-support/"
|
||||
},
|
||||
"Redirect": {
|
||||
"HostName": "docs-lab.docker.com",
|
||||
"HttpRedirectCode": null,
|
||||
"Protocol": "https",
|
||||
"ReplaceKeyPrefixWith": null,
|
||||
"ReplaceKeyWith": ""
|
||||
}
|
||||
},
|
||||
{
|
||||
"Condition": {
|
||||
"HttpErrorCodeReturnedEquals": null,
|
||||
"KeyPrefixEquals": "ee/cluster/"
|
||||
},
|
||||
"Redirect": {
|
||||
"HostName": "docs-lab.docker.com",
|
||||
"HttpRedirectCode": null,
|
||||
"Protocol": "https",
|
||||
"ReplaceKeyPrefixWith": null,
|
||||
"ReplaceKeyWith": ""
|
||||
}
|
||||
},
|
||||
{
|
||||
"Condition": {
|
||||
"HttpErrorCodeReturnedEquals": null,
|
||||
"KeyPrefixEquals": "ee/supported-platforms/"
|
||||
},
|
||||
"Redirect": {
|
||||
"HostName": "docs-lab.docker.com",
|
||||
"HttpRedirectCode": null,
|
||||
"Protocol": "https",
|
||||
"ReplaceKeyPrefixWith": null,
|
||||
"ReplaceKeyWith": ""
|
||||
}
|
||||
},
|
||||
{
|
||||
"Condition": {
|
||||
"HttpErrorCodeReturnedEquals": null,
|
||||
"KeyPrefixEquals": "ee/ucp/"
|
||||
},
|
||||
"Redirect": {
|
||||
"HostName": "docs-lab.docker.com",
|
||||
"HttpRedirectCode": null,
|
||||
"Protocol": "https",
|
||||
"ReplaceKeyPrefixWith": null,
|
||||
"ReplaceKeyWith": ""
|
||||
}
|
||||
},
|
||||
{
|
||||
"Condition": {
|
||||
"HttpErrorCodeReturnedEquals": null,
|
||||
"KeyPrefixEquals": "ee/dtr/"
|
||||
},
|
||||
"Redirect": {
|
||||
"HostName": "docs-lab.docker.com",
|
||||
"HttpRedirectCode": null,
|
||||
"Protocol": "https",
|
||||
"ReplaceKeyPrefixWith": null,
|
||||
"ReplaceKeyWith": ""
|
||||
}
|
||||
},
|
||||
{
|
||||
"Condition": {
|
||||
"HttpErrorCodeReturnedEquals": null,
|
||||
"KeyPrefixEquals": "compliance/"
|
||||
},
|
||||
"Redirect": {
|
||||
"HostName": "docs-lab.docker.com",
|
||||
"HttpRedirectCode": null,
|
||||
"Protocol": "https",
|
||||
"ReplaceKeyPrefixWith": null,
|
||||
"ReplaceKeyWith": ""
|
||||
}
|
||||
},
|
||||
{
|
||||
"Condition": {
|
||||
"HttpErrorCodeReturnedEquals": null,
|
||||
"KeyPrefixEquals": "datacenter/"
|
||||
},
|
||||
"Redirect": {
|
||||
"HostName": "docs-lab.docker.com",
|
||||
"HttpRedirectCode": null,
|
||||
"Protocol": "https",
|
||||
"ReplaceKeyPrefixWith": null,
|
||||
"ReplaceKeyWith": ""
|
||||
}
|
||||
},
|
||||
{
|
||||
"Condition": {
|
||||
"HttpErrorCodeReturnedEquals": null,
|
||||
"KeyPrefixEquals": "v18.09/ee/"
|
||||
},
|
||||
"Redirect": {
|
||||
"HostName": "docs-lab.docker.com",
|
||||
"HttpRedirectCode": null,
|
||||
"Protocol": "https",
|
||||
"ReplaceKeyPrefixWith": null,
|
||||
"ReplaceKeyWith": ""
|
||||
}
|
||||
},
|
||||
{
|
||||
"Condition": {
|
||||
"HttpErrorCodeReturnedEquals": null,
|
||||
"KeyPrefixEquals": "v18.03/ee/"
|
||||
},
|
||||
"Redirect": {
|
||||
"HostName": "docs-lab.docker.com",
|
||||
"HttpRedirectCode": null,
|
||||
"Protocol": "https",
|
||||
"ReplaceKeyPrefixWith": null,
|
||||
"ReplaceKeyWith": ""
|
||||
}
|
||||
},
|
||||
{
|
||||
"Condition": {
|
||||
"HttpErrorCodeReturnedEquals": null,
|
||||
"KeyPrefixEquals": "v17.06/enterprise/"
|
||||
},
|
||||
"Redirect": {
|
||||
"HostName": "docs-lab.docker.com",
|
||||
"HttpRedirectCode": null,
|
||||
"Protocol": "https",
|
||||
"ReplaceKeyPrefixWith": null,
|
||||
"ReplaceKeyWith": ""
|
||||
}
|
||||
},
|
||||
{
|
||||
"Condition": {
|
||||
"HttpErrorCodeReturnedEquals": null,
|
||||
"KeyPrefixEquals": "ee/docker-ee/"
|
||||
},
|
||||
"Redirect": {
|
||||
"HostName": "docs-lab.docker.com",
|
||||
"HttpRedirectCode": null,
|
||||
"Protocol": "https",
|
||||
"ReplaceKeyPrefixWith": null,
|
||||
"ReplaceKeyWith": ""
|
||||
}
|
||||
},
|
||||
{
|
||||
"Condition": {
|
||||
"HttpErrorCodeReturnedEquals": null,
|
||||
"KeyPrefixEquals": "ee/"
|
||||
},
|
||||
"Redirect": {
|
||||
"HostName": "docs-lab.docker.com",
|
||||
"HttpRedirectCode": null,
|
||||
"Protocol": "https",
|
||||
"ReplaceKeyPrefixWith": null,
|
||||
"ReplaceKeyWith": ""
|
||||
}
|
||||
},
|
||||
{
|
||||
"Condition": {
|
||||
"HttpErrorCodeReturnedEquals": null,
|
||||
"KeyPrefixEquals": "v17.06/"
|
||||
},
|
||||
"Redirect": {
|
||||
"HostName": "docs-lab.docker.com",
|
||||
"HttpRedirectCode": null,
|
||||
"Protocol": "https",
|
||||
"ReplaceKeyPrefixWith": "",
|
||||
"ReplaceKeyWith": null
|
||||
}
|
||||
},
|
||||
{
|
||||
"Condition": {
|
||||
"HttpErrorCodeReturnedEquals": null,
|
||||
"KeyPrefixEquals": "v18.03/"
|
||||
},
|
||||
"Redirect": {
|
||||
"HostName": "docs-lab.docker.com",
|
||||
"HttpRedirectCode": null,
|
||||
"Protocol": "https",
|
||||
"ReplaceKeyPrefixWith": "",
|
||||
"ReplaceKeyWith": null
|
||||
}
|
||||
},
|
||||
{
|
||||
"Condition": {
|
||||
"HttpErrorCodeReturnedEquals": null,
|
||||
"KeyPrefixEquals": "v18.09/"
|
||||
},
|
||||
"Redirect": {
|
||||
"HostName": "docs-lab.docker.com",
|
||||
"HttpRedirectCode": null,
|
||||
"Protocol": "https",
|
||||
"ReplaceKeyPrefixWith": "",
|
||||
"ReplaceKeyWith": null
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
|
@ -96,18 +96,19 @@ func (s *AwsLambdaInvokeCmd) Run() error {
|
|||
}
|
||||
|
||||
type AwsCloudfrontUpdateCmd struct {
|
||||
Region string `kong:"name='region',env='AWS_REGION'"`
|
||||
Function string `kong:"name='lambda-function',env='AWS_LAMBDA_FUNCTION'"`
|
||||
FunctionFile string `kong:"name='lambda-function-file',env='AWS_LAMBDA_FUNCTION_FILE'"`
|
||||
CloudfrontID string `kong:"name='cloudfront-id',env='AWS_CLOUDFRONT_ID'"`
|
||||
RedirectsJSON string `kong:"name='redirects-json',env='REDIRECTS_JSON'"`
|
||||
Region string `kong:"name='region',env='AWS_REGION'"`
|
||||
Function string `kong:"name='lambda-function',env='AWS_LAMBDA_FUNCTION'"`
|
||||
FunctionFile string `kong:"name='lambda-function-file',env='AWS_LAMBDA_FUNCTION_FILE'"`
|
||||
CloudfrontID string `kong:"name='cloudfront-id',env='AWS_CLOUDFRONT_ID'"`
|
||||
RedirectsJSON string `kong:"name='redirects-json',env='REDIRECTS_JSON'"`
|
||||
RedirectsPrefixesJSON string `kong:"name='redirects-prefixes-json',env='REDIRECTS_PREFIXES_JSON'"`
|
||||
}
|
||||
|
||||
func (s *AwsCloudfrontUpdateCmd) Run() error {
|
||||
var err error
|
||||
ver := time.Now().UTC().Format(time.RFC3339)
|
||||
|
||||
zipdt, err := getLambdaFunctionZip(s.FunctionFile, s.RedirectsJSON)
|
||||
zipdt, err := getLambdaFunctionZip(s.FunctionFile, s.RedirectsJSON, s.RedirectsPrefixesJSON)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot create lambda function zip: %w", err)
|
||||
}
|
||||
|
@ -227,18 +228,20 @@ func (s *AwsCloudfrontUpdateCmd) Run() error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func getLambdaFunctionZip(funcFilename string, redirectsJSON string) ([]byte, error) {
|
||||
func getLambdaFunctionZip(funcFilename string, redirectsJSON string, redirectsPrefixesJSON string) ([]byte, error) {
|
||||
funcdt, err := os.ReadFile(funcFilename)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to read lambda function file %q: %w", err)
|
||||
return nil, fmt.Errorf("failed to read lambda function file %q: %w", funcFilename, err)
|
||||
}
|
||||
|
||||
var funcbuf bytes.Buffer
|
||||
functpl := template.Must(template.New("").Parse(string(funcdt)))
|
||||
if err = functpl.Execute(&funcbuf, struct {
|
||||
RedirectsJSON string
|
||||
RedirectsJSON string
|
||||
RedirectsPrefixesJSON string
|
||||
}{
|
||||
redirectsJSON,
|
||||
redirectsPrefixesJSON,
|
||||
}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
@ -3,12 +3,13 @@
|
|||
exports.handler = (event, context, callback) => {
|
||||
//console.log("event", JSON.stringify(event));
|
||||
const request = event.Records[0].cf.request;
|
||||
|
||||
const redirects = JSON.parse(`{{.RedirectsJSON}}`);
|
||||
for (let key in redirects) {
|
||||
if (key !== request.uri) {
|
||||
continue;
|
||||
}
|
||||
//console.log(`redirect: ${key} to ${redirects[key]}`);
|
||||
//console.log(`redirect: ${request.uri} to ${redirects[key]}`);
|
||||
const response = {
|
||||
status: '301',
|
||||
statusDescription: 'Moved Permanently',
|
||||
|
@ -22,5 +23,26 @@ exports.handler = (event, context, callback) => {
|
|||
callback(null, response);
|
||||
return
|
||||
}
|
||||
|
||||
const redirectsPrefixes = JSON.parse(`{{.RedirectsPrefixesJSON}}`);
|
||||
for (let key in redirectsPrefixes) {
|
||||
if (!request.uri.startsWith(key)) {
|
||||
continue;
|
||||
}
|
||||
//console.log(`redirect: ${request.uri} to ${redirectsPrefixes[key]}`);
|
||||
const response = {
|
||||
status: '301',
|
||||
statusDescription: 'Moved Permanently',
|
||||
headers: {
|
||||
location: [{
|
||||
key: 'Location',
|
||||
value: redirectsPrefixes[key],
|
||||
}],
|
||||
},
|
||||
}
|
||||
callback(null, response);
|
||||
return
|
||||
}
|
||||
|
||||
callback(null, request);
|
||||
};
|
||||
|
|
|
@ -0,0 +1,21 @@
|
|||
{
|
||||
"/compliance/": "/",
|
||||
"/datacenter/": "/",
|
||||
"/ee/": "/",
|
||||
"/v1.4/": "/",
|
||||
"/v1.5/": "/",
|
||||
"/v1.6/": "/",
|
||||
"/v1.7/": "/",
|
||||
"/v1.8/": "/",
|
||||
"/v1.9/": "/",
|
||||
"/v1.10/": "/",
|
||||
"/v1.11/": "/",
|
||||
"/v1.12/": "/",
|
||||
"/v1.13/": "/",
|
||||
"/v17.03/": "/",
|
||||
"/v17.06/": "/",
|
||||
"/v17.09/": "/",
|
||||
"/v17.12/": "/",
|
||||
"/v18.03/": "/",
|
||||
"/v18.09/": "/"
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
{
|
||||
"ErrorDocument": {
|
||||
"Key": "404.html"
|
||||
},
|
||||
"IndexDocument": {
|
||||
"Suffix": "index.html"
|
||||
},
|
||||
"RedirectAllRequestsTo": null
|
||||
}
|
|
@ -112,7 +112,7 @@ variable "AWS_S3_BUCKET" {
|
|||
default = ""
|
||||
}
|
||||
variable "AWS_S3_CONFIG" {
|
||||
default = "_website-config-docs-stage.json"
|
||||
default = ""
|
||||
}
|
||||
variable "AWS_CLOUDFRONT_ID" {
|
||||
default = ""
|
||||
|
|
Loading…
Reference in New Issue