-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.sh
executable file
·191 lines (153 loc) · 6.72 KB
/
run.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#!/bin/bash
CONFIG_FILE="$HOME/.oci/config"
set_auth() {
local key_file="$HOME/.oci/api_key.pem"
mkdir -p $HOME/.oci
if [ -e "$CONFIG_FILE" ]; then
warn 'OCI config file already exists in home directory and will be overwritten'
fi
#Write the key to a file
echo "${WERCKER_OCI_OBJECTSTORE_API_KEY}" > $key_file
echo '[DEFAULT]' > "$CONFIG_FILE"
echo "user=${WERCKER_OCI_OBJECTSTORE_USER_OCID}" >> "$CONFIG_FILE"
echo "tenancy=${WERCKER_OCI_OBJECTSTORE_TENANCY_OCID}" >> "$CONFIG_FILE"
echo "fingerprint=${WERCKER_OCI_OBJECTSTORE_FINGERPRINT}" >> "$CONFIG_FILE"
echo "region=${WERCKER_OCI_OBJECTSTORE_REGION}" >> "$CONFIG_FILE"
echo "key_file=${key_file}" >> "$CONFIG_FILE"
chmod 600 $key_file
chmod 600 $CONFIG_FILE
debug "generated OCI config file"
}
validate_oci_flags() {
if [ ! -n "$WERCKER_OCI_OBJECTSTORE_TENANCY_OCID" ]; then
fail 'missing or empty option tenancy_ocid, please check wercker.yml'
fi
if [ ! -n "$WERCKER_OCI_OBJECTSTORE_USER_OCID" ]; then
fail 'missing or empty option user_ocid, please check wercker.yml'
fi
if [ ! -n "$WERCKER_OCI_OBJECTSTORE_FINGERPRINT" ]; then
fail 'missing or empty option fingerprint, please check wercker.yml'
fi
if [ ! -n "$WERCKER_OCI_OBJECTSTORE_REGION" ]; then
fail 'missing or empty option region, please check wercker.yml'
fi
#TODO should we check for key? if it is a public bucket do they still need a key?
if [ ! -n "$WERCKER_OCI_OBJECTSTORE_API_KEY" ]; then
fail 'missing or empty option api_key, please check wercker.yml'
fi
if [ ! -n "$WERCKER_OCI_OBJECTSTORE_BUCKET_NAME" ]; then
fail 'missing or empty option bucket_name, please check wercker.yml'
fi
if [ ! -n "$WERCKER_OCI_OBJECTSTORE_NAMESPACE" ]; then
fail 'missing or empty option namespace, please check wercker.yml'
fi
}
set_overwrite_flag_for_bulk() {
if [[ "$WERCKER_OCI_OBJECTSTORE_OVERWRITE" == "true" || "$WERCKER_OCI_OBJECTSTORE_OVERWRITE" == "TRUE" ]]; then
OVERWRITE_FLAG="--overwrite"
else
OVERWRITE_FLAG="--no-overwrite"
fi
WERCKER_OCI_OBJECTSTORE_OPTIONS="$WERCKER_OCI_OBJECTSTORE_OPTIONS $OVERWRITE_FLAG"
}
runCommand() {
set +e
local cmd="$1"
debug "$cmd"
echo "running"
$cmd
if [[ $? -ne 0 ]];then
fail "oci object store $WERCKER_OCI_OBJECTSTORE_COMMAND failed";
else
success "completed oci object store $WERCKER_OCI_OBJECTSTORE_COMMAND";
fi
set -e
}
bulk_upload_cmd() {
set_overwrite_flag_for_bulk
if [ ! -n "$WERCKER_OCI_OBJECTSTORE_LOCAL_DIR" ]; then
fail 'missing or empty option local_dir, please check wercker.yml'
fi
if [[ ! -d $WERCKER_OCI_OBJECTSTORE_LOCAL_DIR || -L $WERCKER_OCI_OBJECTSTORE_LOCAL_DIR ]] ; then
fail 'specified local directory does not exist or is not readable'
fi
if [ ! -n "$WERCKER_OCI_OBJECTSTORE_PREFIX" ]; then
WERCKER_OCI_OBJECTSTORE_PREFIX="$(basename $WERCKER_OCI_OBJECTSTORE_LOCAL_DIR)/"
fi
local ocicmd="$WERCKER_STEP_ROOT/oci --config-file $CONFIG_FILE os object bulk-upload $WERCKER_OCI_OBJECTSTORE_OPTIONS --namespace $WERCKER_OCI_OBJECTSTORE_NAMESPACE --bucket-name $WERCKER_OCI_OBJECTSTORE_BUCKET_NAME --src-dir $WERCKER_OCI_OBJECTSTORE_LOCAL_DIR --object-prefix $WERCKER_OCI_OBJECTSTORE_PREFIX"
runCommand "$ocicmd"
}
bulk_download_cmd() {
set_overwrite_flag_for_bulk
if [ ! -n "$WERCKER_OCI_OBJECTSTORE_LOCAL_DIR" ]; then
fail 'missing or empty option local_dir, please check wercker.yml'
fi
if [[ ! -d $WERCKER_OCI_OBJECTSTORE_LOCAL_DIR || -L $WERCKER_OCI_OBJECTSTORE_LOCAL_DIR || ! -w $WERCKER_OCI_OBJECTSTORE_LOCAL_DIR ]] ; then
fail 'specified local directory does not exist or is not writable'
fi
if [ -n "$WERCKER_OCI_OBJECTSTORE_PREFIX" ]; then
WERCKER_OCI_OBJECTSTORE_OPTIONS="$WERCKER_OCI_OBJECTSTORE_OPTIONS --prefix ""$WERCKER_OCI_OBJECTSTORE_PREFIX"""
fi
local ocicmd="$WERCKER_STEP_ROOT/oci --config-file $CONFIG_FILE os object bulk-download $WERCKER_OCI_OBJECTSTORE_OPTIONS -ns $WERCKER_OCI_OBJECTSTORE_NAMESPACE -bn $WERCKER_OCI_OBJECTSTORE_BUCKET_NAME --download-dir $WERCKER_OCI_OBJECTSTORE_LOCAL_DIR"
runCommand "$ocicmd"
}
single_file_upload_cmd() {
#for the put operation there is no --overwrite - instead there is a --force flag. The --no-overwrite flag is supported.
if [[ "$WERCKER_OCI_OBJECTSTORE_OVERWRITE" == "true" || "$WERCKER_OCI_OBJECTSTORE_OVERWRITE" == "TRUE" ]]; then
OVERWRITE_FLAG="--force"
else
OVERWRITE_FLAG="--no-overwrite"
fi
WERCKER_OCI_OBJECTSTORE_OPTIONS="$WERCKER_OCI_OBJECTSTORE_OPTIONS $OVERWRITE_FLAG"
if [ ! -n "$WERCKER_OCI_OBJECTSTORE_LOCAL_FILE" ]; then
fail 'missing or empty option local_file is required for uploading a single file, please check wercker.yml'
fi
if [[ ! -f $WERCKER_OCI_OBJECTSTORE_LOCAL_FILE || ! -r $WERCKER_OCI_OBJECTSTORE_LOCAL_FILE ]] ; then
fail 'specified local_file must be a regular file and must be readable'
fi
#default the object name to the same as the file's basename
if [ ! -n "$WERCKER_OCI_OBJECTSTORE_OBJECT_NAME" ]; then
WERCKER_OCI_OBJECTSTORE_OBJECT_NAME="$(basename $WERCKER_OCI_OBJECTSTORE_LOCAL_FILE)"
fi
local ocicmd="$WERCKER_STEP_ROOT/oci --config-file $CONFIG_FILE os object put $WERCKER_OCI_OBJECTSTORE_OPTIONS -ns $WERCKER_OCI_OBJECTSTORE_NAMESPACE -bn $WERCKER_OCI_OBJECTSTORE_BUCKET_NAME --file $WERCKER_OCI_OBJECTSTORE_LOCAL_FILE"
runCommand "$ocicmd"
}
single_file_download_cmd() {
if [ ! -n "$WERCKER_OCI_OBJECTSTORE_OBJECT_NAME" ]; then
fail 'missing or empty option object_name is required for downloading a single object, please check wercker.yml'
fi
#default the local file name to the same as the object name
if [ ! -n "$WERCKER_OCI_OBJECTSTORE_LOCAL_FILE" ]; then
WERCKER_OCI_OBJECTSTORE_LOCAL_FILE="$(basename $WERCKER_OCI_OBJECTSTORE_OBJECT_NAME)"
fi
local ocicmd="$WERCKER_STEP_ROOT/oci --config-file $CONFIG_FILE os object get $WERCKER_OCI_OBJECTSTORE_OPTIONS -ns $WERCKER_OCI_OBJECTSTORE_NAMESPACE -bn $WERCKER_OCI_OBJECTSTORE_BUCKET_NAME --name $WERCKER_OCI_OBJECTSTORE_OBJECT_NAME --file $WERCKER_OCI_OBJECTSTORE_LOCAL_FILE"
runCommand "$ocicmd"
}
main() {
validate_oci_flags
set_auth
#Python 3 has ascii as locale default which makes a library ("click") used by ocicli to fail.
#Explicitly set locale to UTF-8
export LANG=C.UTF-8
export LC_ALL=C.UTF-8
info 'starting OCI object store synchronisation'
#$WERCKER_STEP_ROOT/oci --version
case "$WERCKER_OCI_OBJECTSTORE_COMMAND" in
bulk-upload)
bulk_upload_cmd
;;
bulk-download)
bulk_download_cmd
;;
put)
single_file_upload_cmd
;;
get)
single_file_download_cmd
;;
*)
fail "unknown oci command $WERCKER_OCI_OBJECTSTORE_COMMAND - currently supported commands are [bulk-upload, bulk-download, get, put]"
;;
esac
}
main