-
Notifications
You must be signed in to change notification settings - Fork 7
/
entrypoint.sh
executable file
·141 lines (109 loc) · 2.8 KB
/
entrypoint.sh
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/bin/bash
warn () {
text=$1
echo -e "\e[1;33m $text \e[0m"
}
fail () {
text=$1
echo -e "\e[1;31m $text \e[0m"
exit 1
}
success () {
text=$1
echo -e "\e[1;32m $text \e[0m"
}
info () {
text=$1
echo -e "\e[1;34m $text \e[0m"
}
debug () {
text=$1
echo -e "\e[1;40m $text \e[0m"
}
set_auth() {
local s3cnf="$HOME/.s3cfg"
if [ -e "$s3cnf" ]; then
warn '.s3cfg file already exists in home directory and will be overwritten'
fi
echo '[default]' > "$s3cnf"
echo "access_key=$AWS_ACCESS_KEY_ID" >> "$s3cnf"
echo "secret_key=$AWS_SECRET_ACCESS_KEY" >> "$s3cnf"
echo "Generated .s3cfg for key $AWS_ACCESS_KEY_ID"
}
main() {
set_auth
info 'Starting S3 Synchronisation'
info 'Check s3cmd version'
info $(s3cmd --version)
if [ -z "$AWS_S3_BUCKET" ]; then
fail 'AWS_S3_BUCKET is not set. Quitting.'
fi
if [ -z "$AWS_ACCESS_KEY_ID" ]; then
fail 'AWS_ACCESS_KEY_ID is not set. Quitting.'
fi
if [ -z "$AWS_SECRET_ACCESS_KEY" ]; then
fail 'AWS_SECRET_ACCESS_KEY is not set. Quitting.'
fi
# Default to us-east-1 if AWS_REGION not set.
if [ -z "$AWS_REGION" ]; then
export AWS_REGION="us-east-1"
fi
if [ -z "$S3CMD_SOURCE_DIR" ]; then
FILES_SOURCE_DIR="./*"
else
FILES_SOURCE_DIR="./$S3CMD_SOURCE_DIR"
fi
if [ -n "$S3CMD_CF_INVALIDATE" ]; then
if [ "$S3CMD_CF_INVALIDATE" = "true" ]; then
S3CMD_CF_INVALIDATE="--cf-invalidate"
else
unset S3CMD_CF_INVALIDATE
fi
fi
if [ -z "$S3CMD_EXTRA_OPTS" ]; then
S3CMD_EXTRA_OPTS="--verbose"
fi
if [ -n "$S3CMD_DELETE_REMOVED" ]; then
if [ "$S3CMD_DELETE_REMOVED" = "true" ]; then
export S3CMD_DELETE_REMOVED="--delete-removed"
else
unset S3CMD_DELETE_REMOVED
fi
else
export S3CMD_DELETE_REMOVED="--delete-removed"
fi
if [ -n "$S3CMD_EXCLUDE" ]; then
if [ -e "$S3CMD_EXCLUDE" ]; then
S3CMD_EXCLUDE="--exclude $S3CMD_EXCLUDE"
else
unset S3CMD_EXCLUDE
fi
fi
if [ -n "$S3CMD_EXCLUDE_FROM" ]; then
if [ -e "$S3CMD_EXCLUDE_FROM" ]; then
S3CMD_EXCLUDE_FROM="--exclude-from $S3CMD_EXCLUDE_FROM"
else
unset S3CMD_EXCLUDE_FROM
fi
fi
if [ -z "$S3CMD_ADD_HEADERS" ]; then
S3CMD_ADD_HEADERS=""
fi
export IFS='|'
for header in $S3CMD_ADD_HEADERS; do
ADD_HEADERS="--add-header=\"$header\" $ADD_HEADERS"
done
COMMAND_SUFIX="sync $FILES_SOURCE_DIR s3://$AWS_S3_BUCKET"
command="s3cmd --no-preserve $S3CMD_EXTRA_OPTS $S3CMD_EXCLUDE_FROM $S3CMD_DELETE_REMOVED $ADD_HEADERS $S3CMD_CF_INVALIDATE $COMMAND_SUFIX"
debug $command
bash -c $command
RESULT=$?
if [[ $? -eq 0 ]]; then
success 'Finished S3 Synchronisation';
else
fail 'Failed s3cmd command';
fi
warn 'Removing .s3cfg credentials'
rm "$HOME/.s3cfg"
}
main