Merge "Restore vpp-csit-*tx2 jobs in production"
[ci-management.git] / jjb / scripts / terraform_s3_docs_ship.sh
1 #!/bin/bash
2
3 # Copyright (c) 2021 Cisco and/or its affiliates.
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at:
7 #
8 #     http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 echo "---> terraform_s3_docs_ship.sh"
17
18 set -exuo pipefail
19
20 cat >"/w/workspace/main.tf" <<'END_OF_TERRAFORM_SCRIPT'
21 provider "aws" {
22   region                      = "us-east-1"
23   profile                     = "default"
24   s3_force_path_style         = false
25   skip_credentials_validation = true
26   skip_metadata_api_check     = true
27   skip_requesting_account_id  = true
28 }
29
30 locals {
31   mime_types = {
32     xml   = "application/xml",
33     html  = "text/html",
34     txt   = "text/plain",
35     log   = "text/plain",
36     css   = "text/css",
37     md    = "text/markdown",
38     rst   = "text/x-rst",
39     csv   = "text/csv",
40     svg   = "image/svg+xml",
41     jpg   = "image/jpeg",
42     png   = "image/png",
43     gif   = "image/gif",
44     js    = "application/javascript",
45     pdf   = "application/pdf"
46     json  = "application/json",
47     otf   = "font/otf",
48     ttf   = "font/ttf",
49     woff  = "font/woff",
50     woff2 = "font/woff2"
51   }
52 }
53
54 variable "workspace_dir" {
55   description = "Workspace base directory"
56   type        = string
57 }
58
59 variable "file_match_pattern" {
60   description = "File matching pattern"
61   type        = string
62   default     = "**/*"
63 }
64
65 variable "bucket" {
66   description = "S3 bucket name"
67   type        = string
68   default     = "fdio-docs-s3-cloudfront-index"
69 }
70
71 variable "bucket_path" {
72   description = "S3 bucket path to key"
73   type        = string
74 }
75
76 resource "aws_s3_bucket_object" "object" {
77   for_each = fileset(var.workspace_dir, var.file_match_pattern)
78
79   bucket = var.bucket
80   key    = "${var.bucket_path}${each.value}"
81   source = "${var.workspace_dir}/${each.value}"
82
83   cache_control = "no-store,max-age=0,s-maxage=0"
84   etag          = filemd5("${var.workspace_dir}/${each.value}")
85   content_type = lookup(
86     local.mime_types,
87     regex("\\.(?P<extension>[A-Za-z0-9]+)$", each.value).extension,
88     "application/octet-stream"
89   )
90 }
91 END_OF_TERRAFORM_SCRIPT