aboutsummaryrefslogtreecommitdiff
blob: b3d830c449170f7e34558033c61f0275822a1d87 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/bin/bash

# TODO: refactor this to extract from central secrets
: "${CREDCOMMAND:=/bin/false}"
AWS_ACCESS_KEY_ID=$(${CREDCOMMAND} | yq .access-key)
AWS_SECRET_ACCESS_KEY=$(${CREDCOMMAND} | yq .secret-key)
bucket=$(${CREDCOMMAND} | yq .bucket)
endpoint=$(${CREDCOMMAND} | yq .endpoint)

export AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY
if [[ $bucket == null ]]; then
  echo "Something is wrong with your CREDCOMMAND to fetch credentials" 1>&2
  echo "Should return YAML, with keys of access-key, secret-key, endpoint, bucket" 1>&2
  exit 1
fi

dest=s3://${bucket}/
_s3cmd() {
  CMD=(
    "s3cmd"
    "--host-bucket=${endpoint}"
    "--host=${endpoint}"
    # Yep, it doesn't read from ENV in some cases.
    "--access_key=${AWS_ACCESS_KEY_ID}"
    "--secret_key=${AWS_SECRET_ACCESS_KEY}"
    "--acl-public"
    "--recursive"
    "--exclude-from=.s3ignore"
    "--force"
    #--dry-run
  )
  ( set -x ; "${CMD[@]}" "$@" )
}
#_s3cmd     --acl-public   --exclude-from=.s3ignore  --no-mime-magic --guess-mime-type    ${cmd} --recursive   . ${dest} --dry-run
EXT_MIMES=(
 'css=text/css'
 'eot=application/vnd.ms-fontobject'
 'html=text/html'
 'js=text/javascript'
 'map=application/json'
 'md=text/markdown'
 'otf=font/otf'
 'png=image/png'
 'svg=image/svg+xml'
 'ttf=font/ttf'
 'webp=image/webp'
 'webp=image/webp'
 'woff2=font/woff2'
 'woff=font/woff'
 #'txt=text/plain' # Skip this, so the default upload doesn't throw an error.
)

( date -uR ; date -u --iso=sec ) >marker.txt

(set -x ; aws --endpoint "$endpoint" s3api put-bucket-website --bucket "$bucket" --website-configuration file://./website.json )
(set -x ; aws --endpoint "$endpoint" s3api put-bucket-cors --bucket "$bucket" --cors-configuration file://./cors.json )

_ext=''
#cmd='put --no-check-md5' # To force-upload
cmd='sync' # Delta
for ext_mime in "${EXT_MIMES[@]}" ; do
  ext="${ext_mime/=*}"
  mime="${ext_mime/*=}"
  _s3cmd ${cmd} . ${dest} --exclude='*' --include="*.${ext}"  --mime-type="${mime}"
  _ext+=" --exclude=*.${ext}"
done
# This should upload marker.txt
_s3cmd ${cmd} . ${dest} $_ext --no-mime-magic --guess-mime-type

# vim: sts=2 sw=2 ts=2 et: