-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.sh
More file actions
executable file
·138 lines (112 loc) · 4.17 KB
/
Copy pathtest.sh
File metadata and controls
executable file
·138 lines (112 loc) · 4.17 KB
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
#!/bin/bash
# Variables
PREFIX='local'
SUFFIX='test'
FILE_PATH="input.txt"
INPUT_CONTAINER_NAME="input"
OUTPUT_CONTAINER_NAME="output"
STORAGE_ACCOUNT_NAME="${PREFIX}storage${SUFFIX}"
CURRENT_DIR="$(cd "$(dirname "$0")" && pwd)"
TEMP_OUTPUT_FILE=""
cleanup() {
if [ -n "$TEMP_OUTPUT_FILE" ] && [ -f "$TEMP_OUTPUT_FILE" ]; then
rm -f "$TEMP_OUTPUT_FILE"
fi
}
trap cleanup EXIT
# Change the current directory to the script's directory
cd "$CURRENT_DIR" || exit
# Generate a timestamp in the format YYYY-MM-DD-HH-MM-SS
TIMESTAMP=$(date +"%Y-%m-%d-%H-%M-%S")
# Extract the file name and extension
FILE_NAME=$(basename -- "$FILE_PATH")
FILE_BASE_NAME="${FILE_NAME%.*}" # File name without extension
FILE_EXTENSION="${FILE_NAME##*.}" # File extension
# Construct the blob name with the timestamp
BLOB_NAME="${FILE_BASE_NAME}-${TIMESTAMP}.${FILE_EXTENSION}"
# Check whether the input container already exists
CONTAINER_EXISTS=$(az storage container exists \
--name "$INPUT_CONTAINER_NAME" \
--account-name "$STORAGE_ACCOUNT_NAME" \
--auth-mode login | jq .exists)
if [ "$CONTAINER_EXISTS" == "true" ]; then
echo "Container [$INPUT_CONTAINER_NAME] already exists."
else
echo "Container [$INPUT_CONTAINER_NAME] does not exist."
# Create the input container if it doesn't exist
az storage container create \
--name $INPUT_CONTAINER_NAME \
--account-name $STORAGE_ACCOUNT_NAME \
--auth-mode login
fi
# Check whether the output container already exists
CONTAINER_EXISTS=$(az storage container exists \
--name "$OUTPUT_CONTAINER_NAME" \
--account-name "$STORAGE_ACCOUNT_NAME" \
--auth-mode login | jq .exists)
if [ "$CONTAINER_EXISTS" == "true" ]; then
echo "Container [$OUTPUT_CONTAINER_NAME] already exists."
else
echo "Container [$OUTPUT_CONTAINER_NAME] does not exist."
# Create the output container if it doesn't exist
az storage container create \
--name $OUTPUT_CONTAINER_NAME \
--account-name $STORAGE_ACCOUNT_NAME \
--auth-mode login
fi
# Upload the file to the container
az storage blob upload \
--container-name $INPUT_CONTAINER_NAME \
--file "$FILE_PATH" \
--name "$BLOB_NAME" \
--account-name $STORAGE_ACCOUNT_NAME \
--auth-mode login 1>/dev/null
echo "[$BLOB_NAME] file uploaded successfully to the [$INPUT_CONTAINER_NAME] container."
# Verify the upload by checking if the blob exists in the input container
BLOB_EXISTS=$(az storage blob exists \
--container-name "$INPUT_CONTAINER_NAME" \
--name "$BLOB_NAME" \
--account-name "$STORAGE_ACCOUNT_NAME" \
--auth-mode login \
--query "exists" \
--output tsv)
if [ "$BLOB_EXISTS" == "true" ]; then
echo "Blob [$BLOB_NAME] exists in container [$INPUT_CONTAINER_NAME]. Upload verified."
else
echo "Blob [$BLOB_NAME] does not exist in container [$INPUT_CONTAINER_NAME]. Upload failed."
exit 1
fi
# Loop n times to check whether the function app processed the input file and generated the result file in the output container
n=10
seconds=5
for ((i=1; i<=n; i++)); do
echo "Checking for [$BLOB_NAME] file in the [$OUTPUT_CONTAINER_NAME] container (Attempt $i of $n)..."
BLOB_EXISTS=$(az storage blob exists \
--container-name "$OUTPUT_CONTAINER_NAME" \
--name "$BLOB_NAME" \
--account-name "$STORAGE_ACCOUNT_NAME" \
--auth-mode login \
--query "exists" \
--output tsv)
if [ "$BLOB_EXISTS" == "true" ]; then
echo "Processed file [$BLOB_NAME] found in the [$OUTPUT_CONTAINER_NAME] container."
TEMP_OUTPUT_FILE="$(mktemp "${TMPDIR:-/tmp}/processed-output.XXXXXX")" || { echo "Failed to create temporary file." >&2; exit 1; }
az storage blob download \
--container-name "$OUTPUT_CONTAINER_NAME" \
--name "$BLOB_NAME" \
--file "$TEMP_OUTPUT_FILE" \
--account-name "$STORAGE_ACCOUNT_NAME" \
--auth-mode login 1>/dev/null || { echo "Failed to download processed blob [$BLOB_NAME] from container [$OUTPUT_CONTAINER_NAME]." >&2; exit 1; }
echo "Input file [$FILE_PATH]:"
cat "$FILE_PATH"
echo ""
echo "Processed output file [$BLOB_NAME]:"
cat "$TEMP_OUTPUT_FILE"
echo ""
exit 0
fi
echo "Processed file not found yet. Waiting for $seconds seconds before retrying..."
sleep $seconds
done
echo "Processed file was not found in the [$OUTPUT_CONTAINER_NAME] container after $n attempts. Exiting with failure."
exit 1