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_install-user.sh
1928 views
1
#!/bin/bash
2
#-------------------------------------------------------------------------------------------------#
3
# NAME: jnotebook_install-user.sh #
4
# #
5
# PURPOSE: This program is designed to install the libraries that are needed to install Jupyter #
6
# Notebook software 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
# virtualEnvDir #
12
# #
13
# 2) Log in as the Jupyter Notebook user and issue the following command from a Linux #
14
# terminal window: #
15
# #
16
# ./jnotebook_install-user.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
# installation script log file (jnotebook_install-user.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_install-user.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-user.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: install_jupyter_notebook() #
72
# #
73
# PURPOSE: This function installs the Jupyter Notebook, along with libraries needed to work #
74
# with IBM Db2 (on Ubuntu Linux). #
75
# #
76
# PARAMETERS: $1 - Log file name #
77
# $2 - Python virtual environment location #
78
# $3 - User home directory #
79
# #
80
# RETURNS: TRUE (1) - Packages and libraries needed were installed #
81
# FALSE (0) - One or more packages or libraries were not installed #
82
#-------------------------------------------------------------------------------------------------#
83
function install_jupyter_notebook
84
{
85
# Define All Local Variables Used
86
local returnCode=${returnCode:=0} # Function Return Code
87
local errorFlag=${errorFlag:=0} # Error Encountered Flag
88
local outputMsg=${outputMsg:=''} # Command Output Message Text
89
local virtualEnvDir=${virtualEnvDir:=''} # Python Virtual Environment Location
90
local cfgFileName=${cfgFileName:=''} # Python Configuration File Name
91
local searchStr=${searchStr:=''} # Python Configuration File Search String
92
local replaceStr=${replaceStr:=''} # Python Configuration File Replace String
93
94
# Initialize The Appropriate Memory Variables
95
returnCode=${FALSE}
96
errorFlag=${FALSE}
97
virtualEnvDir=${2}
98
cfgFileName="${3}/.jupyter/jupyter_notebook_config.py"
99
searchStr="# c.NotebookApp.use_redirect_file = True"
100
replaceStr="c.NotebookApp.use_redirect_file = False"
101
102
# Move To The Jupyter Notebook Virtual Environment Directory
103
cd ${virtualEnvDir}
104
105
# Create A Python-Jupyter Notebook Virtual Environment (Named "notebook_env")
106
echo "Creating a Python-Jupyter Notebook virtual environment." | tee -a ${1}
107
echo "" >> ${1}
108
outputMsg=$(virtualenv notebook_env 2>&1)
109
echo "${outputMsg}" >> ${1} 2>&1
110
if [[ ${outputMsg} =~ "E:" ]]; then
111
echo "ERROR: Could not create a Python-Jupyter Notebook virtual environment."
112
errorFlag=${TRUE}
113
fi
114
echo "" >> ${1}
115
echo "" | tee -a ${1}
116
117
# Activate The Python-Jupyter Notebook Virtual Environment Just Created
118
echo "Activating the Python-Jupyter Notebook virtual environment." | tee -a ${1}
119
echo "" >> ${1}
120
outputMsg=$(source notebook_env/bin/activate 2>&1)
121
echo "${outputMsg}" >> ${1} 2>&1
122
if [[ ${outputMsg} =~ "E:" ]]; then
123
echo "ERROR: Could not activate a Python-Jupyter Notebook virtual environment."
124
errorFlag=${TRUE}
125
fi
126
echo ""
127
128
# Install Jupyter Notebook In The Python-Jupyter Notebook Virtual Environment Created Earlier
129
echo "Installing the Jupyter Notebook package in the virtual environment." | tee -a ${1}
130
echo "" >> ${1}
131
outputMsg=$(python3 -m pip install jupyter 2>&1)
132
echo "${outputMsg}" >> ${1} 2>&1
133
if [[ ${outputMsg} =~ "E:" ]]; then
134
echo "ERROR: Could not install jupyter."
135
errorFlag=${TRUE}
136
fi
137
echo "" >> ${1}
138
echo "" | tee -a ${1}
139
140
# If A Jupyter Notebook Default Configuration File Exists, Delete It
141
if [ -e ${cfgFileName} ]; then
142
echo "Removing the file \"${cfgFileName}\"." | tee -a ${logFile}
143
echo "" >> ${1}
144
outputMsg=$(rm -r ${cfgFileName} 2>&1)
145
echo "${outputMsg}" >> ${1} 2>&1
146
if [[ ${outputMsg} =~ "E:" ]]; then
147
echo "ERROR: Could not remove file \"${cfgFileName}\"."
148
errorFlag=${TRUE}
149
returnCode=${FALSE}
150
return ${returnCode}
151
fi
152
echo ""
153
fi
154
155
# Generate A Jupyter Notebook Default Configuration File
156
echo "Generating the default Jupyter Notebook configuration file." | tee -a ${1}
157
echo "" >> ${1}
158
outputMsg=$(jupyter notebook --generate-config 2>&1)
159
echo "${outputMsg}" >> ${1} 2>&1
160
if [[ ${outputMsg} =~ "E:" ]]; then
161
echo "ERROR: Could not generate the default Jupyter Notebook configuration file."
162
errorFlag=${TRUE}
163
fi
164
echo "" >> ${1}
165
echo "" | tee -a ${1}
166
167
# If The Jupyter Notebook Default Configuration File Was Created Successfully, Modify It
168
if [ -e ${cfgFileName} ]; then
169
echo "Updating the file \"${cfgFileName}\"." | tee -a ${logFile}
170
echo "" >> ${1}
171
outputMsg=$(sed -i "s/${searchStr}/${replaceStr}/" ${cfgFileName} 2>&1)
172
echo "${outputMsg}" >> ${1} 2>&1
173
if [[ ${outputMsg} =~ "E:" ]]; then
174
echo "ERROR: Could not update file \"${cfgFileName}\"."
175
errorFlag=${TRUE}
176
returnCode=${FALSE}
177
return ${returnCode}
178
fi
179
echo ""
180
fi
181
182
# Install The PyHamcrest Library In The Python-Jupyter Notebook Virtual Environment
183
echo "Installing the PyHamcrest libary in the virtual environment." | tee -a ${1}
184
echo "" >> ${1}
185
outputMsg=$(python3 -m pip install PyHamcrest 2>&1)
186
echo "${outputMsg}" >> ${1} 2>&1
187
if [[ ${outputMsg} =~ "E:" ]]; then
188
echo "ERROR: Could not install ibm_db."
189
errorFlag=${TRUE}
190
fi
191
echo "" >> ${1}
192
echo "" | tee -a ${1}
193
194
# Install The IBM_DB Library In The Python-Jupyter Notebook Virtual Environment
195
echo "Installing the ibm_db libary in the virtual environment." | tee -a ${1}
196
echo "" >> ${1}
197
outputMsg=$(python3 -m pip install ibm_db 2>&1)
198
echo "${outputMsg}" >> ${1} 2>&1
199
if [[ ${outputMsg} =~ "E:" ]]; then
200
echo "ERROR: Could not install ibm_db."
201
errorFlag=${TRUE}
202
fi
203
echo "" >> ${1}
204
echo "" | tee -a ${1}
205
206
# Write An Appropriate Message To The Log File
207
if [[ ${errorFlag} -eq ${TRUE} ]]; then
208
echo "Problem(s) encountered. Refer to the file \"${1}\" for more information."
209
returnCode=${FALSE}
210
else
211
date >> ${1} 2>&1
212
echo "Finished installing Jupyter Notebook." | tee -a ${1}
213
echo "" | tee -a ${1}
214
returnCode=${TRUE}
215
fi
216
217
# Return The Appropriate Value To The Calling Function
218
return ${returnCode}
219
}
220
221
222
#=================================================================================================#
223
# NAME: main() #
224
# #
225
# PURPOSE: This is the main body of the script. #
226
# #
227
# PARAMETERS: $1 - Script key word #
228
# $2 - Option #
229
# #
230
# RETURNS: 0 - The script executed successfully #
231
# 1 - The script did NOT execute successfully #
232
#=================================================================================================#
233
234
# Define All Variables Used
235
TRUE=${TRUE:=1} # Boolean TRUE
236
FALSE=${FALSE:=0} # Boolean FALSE
237
238
currentDir=${currentDir:=''} # Current Working Directory
239
userDir=${userDir:=''} # User Home Directory
240
profileFile=${profileFile:=''} # User Profile File Name
241
logFile=${logFile:=''} # Log File Name
242
virtualEnvDir=${virtualEnvDir:=''} # Jupyter Notebook Virtual Environment Directory
243
timeStamp=${timeStamp:=''} # Current Date And Time Value
244
245
# Initialize The Appropriate Memory Variables
246
currentDir=$(pwd)
247
userDir=${HOME}
248
profileFile="${HOME}/.bashrc"
249
logFile="${currentDir}/jnotebook_install-user.log"
250
virtualEnvDir="${HOME}/JNotebook"
251
timeStamp=$(date '+%m-%d-%Y:%H-%M-%S')
252
253
# Clear The Screen
254
clear
255
256
# Create A Log File In The Current Directory And Write Header Information To It
257
init_log_file ${logFile}
258
259
# Display A "Setting Up" Message
260
date >> ${logFile} 2>&1
261
echo "Installing the Jupyter Notebook software. Please wait ..." | tee -a ${logFile}
262
echo "" | tee -a ${logFile}
263
264
# Add The Location Of The Scripts "pip", "pip3", And "pip3.10", Which Are Installed In The User's
265
# ".local/bin" Directory To The PATH Environment Variable
266
export PATH="$PATH:${HOME}/.local/bin"
267
echo "" >> ${profileFile} 2>&1
268
echo "# set PATH so it includes user's private bin if it exists" >> ${profileFile} 2>&1
269
echo "if [ -d \"\$HOME/.local/bin\" ] ; then" >> ${profileFile} 2>&1
270
echo " PATH=\"\$HOME/.local/bin:\$PATH\"" >> ${profileFile} 2>&1
271
echo "fi" >> ${profileFile} 2>&1
272
echo "" >> ${profileFile} 2>&1
273
274
# Install The Jupyter Notebook Libraries Needed For Ubuntu Linux
275
install_jupyter_notebook ${logFile} ${virtualEnvDir} ${userDir}
276
returnCode=$?
277
if [ ${returnCode} == ${FALSE} ]; then
278
exit 1
279
fi
280
281
# Write The End Timestamp To the Log File
282
date >> ${logFile} 2>&1
283
echo "Done!" >> ${logFile}
284
285
# Display A "Finished" Message And Write A "Finished" Message To The Log File
286
echo "Done!"
287
echo "Refer to the file \"${logFile}\" for more information."
288
echo "" | tee -a ${logFile}
289
echo "To start Jupyter run the following commands:" | tee -a ${logFile}
290
echo "" | tee -a ${logFile}
291
echo " source ${profileFile}" | tee -a ${logFile}
292
echo " jupyter notebook" | tee -a ${logFile}
293
echo "" | tee -a ${logFile}
294
295
# Exit And Return Control To The Operating System
296
exit 0
297
298