forked from IBM/fhe-toolkit-linux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PersistData.sh
executable file
·257 lines (225 loc) · 8.04 KB
/
PersistData.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#!/bin/bash
# MIT License
#
# Copyright (c) 2020 International Business Machines
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# The purpose of this script is to setup a persistent location where FHE toolkit
# source code and IDe configuration can persist across container instantiations.
# It is not needed if sandbox mode is used (i.e., when no persistence is desired)
#
# Most users will want to persist their source code changes, and this script sets
# up a location within the host file system to persist some state.
echo ".............................................................."
# Print how we were invoked...
echo "Invoked: $(printf %q "$BASH_SOURCE")$((($#)) && printf ' %q' "$@")"
echo ".............................................................."
# Initialize the container image name and id as empty for further consistency checks
ToolkitImageName=""
ToolkitImageId=""
# The default location on this host where a project directory of FHE toolkit source
# code will be persisted
PERSISTENT_FHE_WORKSPACE_PATH="$PWD"/FHE-Toolkit-Workspace
# Check if we need to us zCX specific persistent workspace path
zCX=$(docker system info | grep platform)
if [ $zCX ] && [ $zCX == "platform=zOS" ]; then
PERSISTENT_FHE_WORKSPACE_PATH=/media/azd_shared_volume
fi
# Formatting helpers
bold=$(tput bold)
normal=$(tput sgr0)
#
BASEDIR="$PWD"/$(dirname $0)
SCRIPTNAME=$(basename $0)
print_usage(){
cat <<EOF
${bold}Usage: $scriptname [options] CONTAINER_IMG${normal}
${bold}CONTAINER_IMG${normal} Selects the name of toolkit container image to persist data from.
This should be the IBMCOM pre-built toolkit from Docker Hub
or the locally built toolkit tagged container name. This script
exects this container image name to exist in the local docker image catalog.
Supported container OS are:
x86_64/amd64: {ubuntu, fedora, centos}
s390x: {ubuntu}
${bold}Options:${normal}
-h Displays this help information.
${bold}Example:${normal}
To persist a local toolkit build:
PersistData.sh local/fhe-toolkit-ubuntu
To persist a fetched toolkit build:
PersistData.sh ibmcom/fhe-toolkit-fedora
EOF
}
#Check for supported host architecture
ARCH=`uname -m`
if [[ "$ARCH" == "x86_64" ]] || [[ "$ARCH" == "amd64" ]]; then
#echo "Determined AMD64/x86_64 Architecture"
architecture="AMD64/x86_64"
ARCH="amd64"
elif [[ $ARCH == "s390x" ]]; then
# echo "Determined s390x Architecture"
architecture="s390x"
else
echo " "
echo " FATAL: Aborting. $ARCH is not a suppported platform for hosting or building the FHE Toolkit."
echo " "
exit -1
fi
# The number of parameters passed to this script
NPARAM=$#
while getopts ":h" opt; do
case ${opt} in
h ) # Usage
print_usage
exit 0
;;
\? ) # Usage
print_usage
exit 0
;;
: ) # Invalid option. Print usage
echo "Invalid option: -$OPTARG requires an argument" 1>&2
print_usage
exit -1
;;
esac
done
# Check we have the right number of mandatory parameters
shift $((OPTIND -1))
OPTINDNUMBER=$((OPTIND-1))
if [[ $((NPARAM - OPTINDNUMBER)) -ne 1 ]]; then
echo " "
echo "FATAL: Options and parameters mismatch or missing parameters. Please check order and number of parameters."
echo " "
print_usage
exit -2
fi
# Since we have one parameter, let's initialize the container image
ToolkitImageName=$1
if ! [ $ToolkitImageName ]
then
echo " "
echo " Mandatory parameter ${bold}CONTAINER_IMG${normal} is empty."
echo " "
print_usage
exit -3
fi
IFS='/-' read -a ToolkitImageNameTokens <<< "$ToolkitImageName"
tokenSize=${#ToolkitImageNameTokens[@]}
token0=${ToolkitImageNameTokens[0]} # <local|ibmcom>
token1=${ToolkitImageNameTokens[1]} # fhe
token2=${ToolkitImageNameTokens[2]} # toolkit
platform=${ToolkitImageNameTokens[3]} # {ubuntu, fedora, centos}
if ! [ $tokenSize -eq 4 ]
then
echo " "
echo "FATAL: Mandatory parameter ${bold}CONTAINER_IMG${normal} is malformed."
echo " "
print_usage
exit -4
fi
if [ $ARCH == "s390x" ]
then
if ! [ "$platform" = "ubuntu" ] \
&& ! [ "$platform" = "fedora" ]
then
echo " "
echo " Invalid platform: $ToolkitImageName ($platform on $ARCH) is not supported."
echo " Please specify a support platform"
echo " "
print_usage
exit -5
fi
elif [ $ARCH == "amd64" ]
then
if ! [ "$platform" = "fedora" ] \
&& ! [ "$platform" = "centos" ] \
&& ! [ "$platform" = "ubuntu" ]
then
echo " "
echo " Invalid platform: $ToolkitImageName ($platform on $ARCH) is not supported."
echo " Please specify a support platform"
echo " "
print_usage
exit -6
fi
else
echo " "
echo " FATAL: Aborting. $ARCH is not a suppported platform for hosting or building the FHE Toolkit."
echo " "
exit -7
fi
# Now we check whether the persistent location already exists. If if it does, we will exit and will try to use it
# If we're on zCX, the persistent location will already exist.
if [ ! "$(ls -A ${PERSISTENT_FHE_WORKSPACE_PATH})" ] || [ $zCX ]
then
echo "Creating FHE-Workspace and adding FHE examples: ${PERSISTENT_FHE_WORKSPACE_PATH}"
if ! mkdir -p "$PERSISTENT_FHE_WORKSPACE_PATH/"
then
echo " "
echo "FATAL: Fail to create $PERSISTENT_FHE_WORKSPACE_PATH ."
echo " Please check you have the necessary permission to create directories"
echo " "
exit -8
fi
# Set up access to a container instance file system without running the container...
if ! ToolkitImageId=$(docker create $ToolkitImageName)
then
echo " "
echo "FATAL: Could not find container $ToolkitImageName in your system or in Docker HUB,"
echo " or Docker utility is not installed. Please check that you have a working Docker"
echo " installation in your system and the necessary user privileges to run docker commands."
echo " "
echo " Also check, using the ${bold}docker images${normal} command that you have the docker"
echo " image in your system"
echo " "
exit -9
else # Copy the upstream HElib example and IDE configutation to the persistent workspace ...
if ! docker cp $ToolkitImageId:/opt/IBM/FHE-Workspace/. "$PERSISTENT_FHE_WORKSPACE_PATH/"
then
echo " "
echo "FATAL: Failed copying FHE Workspace from container image to locally persited workspace on host"
echo " Please check for access permissions to ${PERSISTENT_FHE_WORKSPACE_PATH} and any error"
echo " messages above"
echo " "
exit -10
else # Remove temporary file access to container image
if ! docker rm -v $ToolkitImageId
then
echo " "
echo "FATAL: Falure removing temporary file access to container image."
echo " Please check for any error messages above"
echo " "
exit -11
else
echo " "
echo "Persistent FHE-Workspace ready for use"
echo " "
fi
fi
fi
else
echo "Using existing FHE-Workspace: $PERSISTENT_FHE_WORKSPACE_PATH"
exit 1
fi
if [ $? -ne 0 ]
then
echo "Failed to setup workspace"
exit -1
fi