Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
CloudPak-Outcomes
GitHub Repository: CloudPak-Outcomes/Outcomes-Projects
Path: blob/main/Db2-L3-Tech-Lab/jnotebook_setup-root.sh
1928 views
1
#!/bin/bash
2
#-------------------------------------------------------------------------------------------------#
3
# NAME: jnotebook_setup-root.sh #
4
# #
5
# PURPOSE: This program is designed to install the libraries that are needed to set up and run #
6
# Jupyter Notebook on an Ubuntu Linux server. #
7
# #
8
# USAGE: 1) Locate the following variable in the main() function of this file and assign it #
9
# the appropriate value: #
10
# #
11
# userDir #
12
# #
13
# 2) Log in as the root user and issue the following command from a Linux terminal #
14
# window: #
15
# #
16
# ./jnotebook_setup-root.sh #
17
# #
18
#-------------------------------------------------------------------------------------------------#
19
# DISCLAIMER OF WARRANTIES AND LIMITATION OF LIABILITY #
20
# #
21
# (C) COPYRIGHT International Business Machines Corp. 2017, 2018, & 2022. All Rights Reserved #
22
# Licensed Materials - Property of IBM #
23
# #
24
# US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP #
25
# Schedule Contract with IBM Corp. #
26
# #
27
# The following source code ("Sample") is owned by International Business Machines Corporation #
28
# or one of its subsidiaries ("IBM") and is copyrighted and licensed, not sold. You may use, #
29
# copy, modify, and distribute the Sample in any form without payment to IBM, for the purpose of #
30
# assisting you with the installation of Db2 on a Ubuntu Linux server. #
31
# #
32
# The Sample code is provided to you on an "AS IS" basis, without warranty of any kind. IBM #
33
# HEREBY EXPRESSLY DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT #
34
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. #
35
# Some jurisdictions do not allow for the exclusion or limitation of implied warranties, so the #
36
# above limitations or exclusions may not apply to you. IBM shall not be liable for any damages #
37
# you suffer as a result of using, copying, modifying or distributing the Sample, even if IBM #
38
# has been advised of the possibility of such damages. #
39
#-------------------------------------------------------------------------------------------------#
40
# HISTORY: 02AUG2022 - Initial Coding Roger E. Sanders #
41
#-------------------------------------------------------------------------------------------------#
42
43
44
#-------------------------------------------------------------------------------------------------#
45
# NAME: init_log_file() #
46
# #
47
# PURPOSE: This function writes the appropriate header information to the Jupyter Notebook #
48
# environment setup up script log file (jnotebook_setup-root.log). #
49
# #
50
# PARAMETERS: $1 - Log file name #
51
# #
52
# RETURNS: None #
53
#-------------------------------------------------------------------------------------------------#
54
function init_log_file
55
{
56
# Create A New Log File And Write The Appropriate Header Information To It
57
echo "#--------------------------------------------------------------------#" > ${1} 2>&1
58
echo "# NAME: jnotebook_setup-root.log #" >> ${1} 2>&1
59
echo "# CREATED: ${timeStamp} #" >> ${1} 2>&1
60
echo "# #" >> ${1} 2>&1
61
echo "# CONTENTS: Log data produced by jnotebook_setup-root.sh #" >> ${1} 2>&1
62
echo "#--------------------------------------------------------------------#" >> ${1} 2>&1
63
echo "" >> ${1} 2>&1
64
65
# Change Permissions On The Log File So Anyone Can Access It
66
chmod 777 ${1}
67
}
68
69
70
#-------------------------------------------------------------------------------------------------#
71
# NAME: setup_jupyter_notebook_env() #
72
# #
73
# PURPOSE: This function installs the libraries and packages needed to work wit Jupyter #
74
# Notebooks (on Ubuntu Linux). #
75
# #
76
# PARAMETERS: $1 - Log file name #
77
# $2 - Python Virtual Environment location #
78
# #
79
# RETURNS: TRUE (1) - Packages and libraries needed were installed #
80
# FALSE (0) - One or more packages or libraries were not installed #
81
#-------------------------------------------------------------------------------------------------#
82
function setup_jupyter_notebook_env
83
{
84
# Define All Local Variables Used
85
local returnCode=${returnCode:=0} # Function Return Code
86
local errorFlag=${errorFlag:=0} # Error Encountered Flag
87
local outputMsg=${outputMsg:=''} # Command Output Message Text
88
89
# Initialize The Appropriate Memory Variables
90
returnCode=${FALSE}
91
errorFlag=${FALSE}
92
virtualEnvLocation=${2}
93
94
# Remove All Packages Currently Installed On The System That Are Not Needed
95
echo "Uninstalling all packages currently installed that are not needed." | tee -a ${1}
96
outputMsg=$(apt-get -y autoremove 2>&1)
97
echo "${outputMsg}" >> ${1} 2>&1
98
if [[ ${outputMsg} =~ "E:" ]]; then
99
echo "ERROR: Could not uninstall unneeded packages."
100
errorFlag=${TRUE}
101
fi
102
echo "" >> ${1}
103
echo "" | tee -a ${1}
104
105
# Upgrade All Packages Installed On The System
106
echo "Installing newer versions of all packages currently installed." | tee -a ${1}
107
echo "(This may take a few minutes)."
108
outputMsg=$(apt-get -y upgrade 2>&1)
109
echo "${outputMsg}" >> ${1} 2>&1
110
if [[ ${outputMsg} =~ "E:" ]]; then
111
echo "ERROR: Could not upgrade packages."
112
errorFlag=${TRUE}
113
fi
114
echo "" >> ${1}
115
echo "" | tee -a ${1}
116
117
# Fix Any Broken Packages Or Package Dependencies Found
118
echo "Fixing any broken packages and package dependencies found." | tee -a ${1}
119
echo "" >> ${1}
120
outputMsg=$(apt-get --fix-broken install)
121
echo "${outputMsg}" >> ${1} 2>&1
122
if [[ ${outputMsg} =~ "E:" ]]; then
123
echo "ERROR: Could not fix broken packages and package dependencies."
124
errorFlag=${TRUE}
125
fi
126
echo "" >> ${1}
127
echo "" | tee -a ${1}
128
129
# Generate A New List Of All Packages Installed On The System
130
echo "Updating the installed package list." | tee -a ${1}
131
echo "" >> ${1}
132
outputMsg=$(apt-get update 2>&1)
133
echo "${outputMsg} Done." >> ${1} 2>&1
134
if [[ ${outputMsg} =~ "E:" ]]; then
135
echo "ERROR: Could not update the installed package list."
136
errorFlag=${TRUE}
137
fi
138
echo "" >> ${1}
139
echo "" | tee -a ${1}
140
141
# Install The PYTHON3 Linux Package
142
echo "Installing the python3 package." | tee -a ${1}
143
echo "" >> ${1}
144
outputMsg=$(apt-get -y install python3 2>&1)
145
echo "${outputMsg}" >> ${1} 2>&1
146
if [[ ${outputMsg} =~ "E:" ]]; then
147
echo "ERROR: Could not install python3."
148
errorFlag=${TRUE}
149
fi
150
echo "" >> ${1}
151
echo "" | tee -a ${1}
152
153
# Install The PYTHON3-PIP Package Manager
154
echo "Installing the python3-pip package manager." | tee -a ${1}
155
echo "" >> ${1}
156
outputMsg=$(apt-get -y install python3-pip 2>&1)
157
echo "${outputMsg}" >> ${1} 2>&1
158
if [[ ${outputMsg} =~ "E:" ]]; then
159
echo "ERROR: Could not install python3-pip."
160
errorFlag=${TRUE}
161
fi
162
echo "" >> ${1}
163
echo "" | tee -a ${1}
164
165
# Upgrade The PYTHON3-PIP (PIP3) Linux Library
166
echo "Upgrading the pip3 package." | tee -a ${1}
167
echo "" >> ${1}
168
outputMsg=$(python3 -m pip install --upgrade pip 2>&1)
169
echo "${outputMsg}" >> ${1} 2>&1
170
if [[ ${outputMsg} =~ "E:" ]]; then
171
echo "ERROR: Could not upgrade pip3."
172
errorFlag=${TRUE}
173
fi
174
echo "" >> ${1}
175
echo "" | tee -a ${1}
176
177
# Install The VIRTUALENV Linux Package (Used For Creating Virtual
178
# Python Environments)
179
echo "Installing the virtualenv package." | tee -a ${1}
180
echo "" >> ${1}
181
outputMsg=$(python3 -m pip install virtualenv 2>&1)
182
echo "${outputMsg}" >> ${1} 2>&1
183
if [[ ${outputMsg} =~ "E:" ]]; then
184
echo "ERROR: Could not install virtualenv."
185
errorFlag=${TRUE}
186
fi
187
echo "" >> ${1}
188
echo "" | tee -a ${1}
189
190
# If The Jupyter Notebook Virtual Environment Directory Desired Already Exists, Remove It
191
if [ -e ${virtualEnvLocation} ]; then
192
echo "Removing the directory \"${virtualEnvLocation}\"." | tee -a ${logFile}
193
echo "" >> ${1}
194
outputMsg=$(rm -r ${virtualEnvLocation} 2>&1)
195
echo "${outputMsg}" >> ${1} 2>&1
196
if [[ ${outputMsg} =~ "E:" ]]; then
197
echo "ERROR: Could not remove directory \"${virtualEnvLocation}\"."
198
errorFlag=${TRUE}
199
returnCode=${FALSE}
200
return ${returnCode}
201
fi
202
echo ""
203
fi
204
205
# Create A Jupyter Notebook Virtual Environment Directory
206
outputMsg=$(mkdir -m 777 ${virtualEnvLocation} 2>&1)
207
returnCode=$?
208
if [ ${returnCode} != 0 ]; then
209
echo "ERROR: Could not create the \"${virtualEnvLocation}\" directory." | tee -a ${1}
210
echo " ${outputMsg}" >> ${1} 2>&1
211
returnCode=${FALSE}
212
return ${returnCode}
213
fi
214
215
# Change The Permissions Of The Jupyter Notebook Virtual Environment Directory So Anyone
216
# Can Access It
217
outputMsg=$(chmod 777 ${virtualEnvLocation} 2>&1)
218
returnCode=$?
219
if [ ${returnCode} != 0 ]; then
220
echo -e "ERROR: Could not change "
221
echo "permissions for the \"${virtualEnvLocation}\" directory." | tee -a ${1}
222
echo " ${outputMsg}" >> ${1} 2>&1
223
returnCode=${FALSE}
224
return ${returnCode}
225
fi
226
227
# Generate A New List Of All Packages Installed On The System
228
echo "Updating the installed package list." | tee -a ${1}
229
echo "" >> ${1}
230
outputMsg=$(apt-get update 2>&1)
231
echo "${outputMsg} Done." >> ${1} 2>&1
232
if [[ ${outputMsg} =~ "E:" ]]; then
233
echo "ERROR: Could not update the installed package list."
234
errorFlag=${TRUE}
235
fi
236
echo "" >> ${1}
237
echo "" | tee -a ${1}
238
239
# Write An Appropriate Message To The Log File
240
if [[ ${errorFlag} -eq ${TRUE} ]]; then
241
echo "Problem(s) encountered. Refer to the file \"${1}\" for more information."
242
returnCode=${FALSE}
243
else
244
date >> ${1} 2>&1
245
echo "Finished installing the libraries needed for Jupyter Notebook." | tee -a ${1}
246
echo "" | tee -a ${1}
247
returnCode=${TRUE}
248
fi
249
250
# Return The Appropriate Value To The Calling Function
251
return ${returnCode}
252
}
253
254
255
#=================================================================================================#
256
# NAME: main() #
257
# #
258
# PURPOSE: This is the main body of the script. #
259
# #
260
# PARAMETERS: $1 - Script key word #
261
# $2 - Option #
262
# #
263
# RETURNS: 0 - The script executed successfully #
264
# 1 - The script did NOT execute successfully #
265
#=================================================================================================#
266
267
# Define All Variables Used
268
TRUE=${TRUE:=1} # Boolean TRUE
269
FALSE=${FALSE:=0} # Boolean FALSE
270
271
currentDir=${currentDir:=''} # Current Working Directory
272
userDir=${userDir:=''} # User Home Directory
273
logFile=${logFile:=''} # Log File Name
274
virtualEnvDir=${virtualEnvDir:=''} # Jupyter Notebook Virtual Environment Directory
275
timeStamp=${timeStamp:=''} # Current Date And Time Value
276
outputMsg=${outputMsg:=''} # Command Output Message Text
277
278
# Initialize The Appropriate Memory Variables
279
currentDir=$(pwd)
280
userDir="/home/resanders"
281
logFile="${currentDir}/jnotebook_setup-root.log"
282
virtualEnvDir="${userDir}/JNotebook"
283
timeStamp=$(date '+%m-%d-%Y:%H-%M-%S')
284
285
# Clear The Screen
286
clear
287
288
# Create A Log File In The Current Directory And Write Header Information To It
289
init_log_file ${logFile}
290
291
# Display A "Setting Up" Message
292
date >> ${logFile} 2>&1
293
echo "Installing the libraries and packages needed for Jupyter Notebook. Please wait ..." | tee -a ${logFile}
294
echo "" | tee -a ${logFile}
295
296
# Install The Libraries And Packages Needed For Jupyter Notebook
297
setup_jupyter_notebook_env ${logFile} ${virtualEnvDir}
298
returnCode=$?
299
if [ ${returnCode} == ${FALSE} ]; then
300
exit 1
301
fi
302
303
# Write The End Timestamp To the Log File
304
date >> ${logFile} 2>&1
305
echo "Done!" >> ${logFile}
306
307
# Display A "Finished" Message And Write A "Finished" Message To The Log File
308
echo "Done!"
309
echo ""
310
echo "Refer to the file \"${logFile}\" for more information."
311
echo ""
312
313
# Exit And Return Control To The Operating System
314
exit 0
315
316