Path: blob/main/Db2-L3-Tech-Lab/jnotebook_install-user.sh
1928 views
#!/bin/bash1#-------------------------------------------------------------------------------------------------#2# NAME: jnotebook_install-user.sh #3# #4# PURPOSE: This program is designed to install the libraries that are needed to install Jupyter #5# Notebook software on an Ubuntu Linux server. #6# #7# USAGE: 1) Locate the following variable in the main() function of this file and assign it #8# the appropriate value: #9# #10# virtualEnvDir #11# #12# 2) Log in as the Jupyter Notebook user and issue the following command from a Linux #13# terminal window: #14# #15# ./jnotebook_install-user.sh #16# #17#-------------------------------------------------------------------------------------------------#18# DISCLAIMER OF WARRANTIES AND LIMITATION OF LIABILITY #19# #20# (C) COPYRIGHT International Business Machines Corp. 2017, 2018, & 2022. All Rights Reserved #21# Licensed Materials - Property of IBM #22# #23# US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP #24# Schedule Contract with IBM Corp. #25# #26# The following source code ("Sample") is owned by International Business Machines Corporation #27# or one of its subsidiaries ("IBM") and is copyrighted and licensed, not sold. You may use, #28# copy, modify, and distribute the Sample in any form without payment to IBM, for the purpose of #29# assisting you with the installation of Db2 on a Ubuntu Linux server. #30# #31# The Sample code is provided to you on an "AS IS" basis, without warranty of any kind. IBM #32# HEREBY EXPRESSLY DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT #33# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. #34# Some jurisdictions do not allow for the exclusion or limitation of implied warranties, so the #35# above limitations or exclusions may not apply to you. IBM shall not be liable for any damages #36# you suffer as a result of using, copying, modifying or distributing the Sample, even if IBM #37# has been advised of the possibility of such damages. #38#-------------------------------------------------------------------------------------------------#39# HISTORY: 02AUG2022 - Initial Coding Roger E. Sanders #40#-------------------------------------------------------------------------------------------------#414243#-------------------------------------------------------------------------------------------------#44# NAME: init_log_file() #45# #46# PURPOSE: This function writes the appropriate header information to the Jupyter Notebook #47# installation script log file (jnotebook_install-user.log). #48# #49# PARAMETERS: $1 - Log file name #50# #51# RETURNS: None #52#-------------------------------------------------------------------------------------------------#53function init_log_file54{55# Create A New Log File And Write The Appropriate Header Information To It56echo "#--------------------------------------------------------------------#" > ${1} 2>&157echo "# NAME: jnotebook_install-user.log #" >> ${1} 2>&158echo "# CREATED: ${timeStamp} #" >> ${1} 2>&159echo "# #" >> ${1} 2>&160echo "# CONTENTS: Log data produced by jnotebook_setup-user.sh #" >> ${1} 2>&161echo "#--------------------------------------------------------------------#" >> ${1} 2>&162echo "" >> ${1} 2>&16364# Change Permissions On The Log File So Anyone Can Access It65chmod 777 ${1}66}676869#-------------------------------------------------------------------------------------------------#70# NAME: install_jupyter_notebook() #71# #72# PURPOSE: This function installs the Jupyter Notebook, along with libraries needed to work #73# with IBM Db2 (on Ubuntu Linux). #74# #75# PARAMETERS: $1 - Log file name #76# $2 - Python virtual environment location #77# $3 - User home directory #78# #79# RETURNS: TRUE (1) - Packages and libraries needed were installed #80# FALSE (0) - One or more packages or libraries were not installed #81#-------------------------------------------------------------------------------------------------#82function install_jupyter_notebook83{84# Define All Local Variables Used85local returnCode=${returnCode:=0} # Function Return Code86local errorFlag=${errorFlag:=0} # Error Encountered Flag87local outputMsg=${outputMsg:=''} # Command Output Message Text88local virtualEnvDir=${virtualEnvDir:=''} # Python Virtual Environment Location89local cfgFileName=${cfgFileName:=''} # Python Configuration File Name90local searchStr=${searchStr:=''} # Python Configuration File Search String91local replaceStr=${replaceStr:=''} # Python Configuration File Replace String9293# Initialize The Appropriate Memory Variables94returnCode=${FALSE}95errorFlag=${FALSE}96virtualEnvDir=${2}97cfgFileName="${3}/.jupyter/jupyter_notebook_config.py"98searchStr="# c.NotebookApp.use_redirect_file = True"99replaceStr="c.NotebookApp.use_redirect_file = False"100101# Move To The Jupyter Notebook Virtual Environment Directory102cd ${virtualEnvDir}103104# Create A Python-Jupyter Notebook Virtual Environment (Named "notebook_env")105echo "Creating a Python-Jupyter Notebook virtual environment." | tee -a ${1}106echo "" >> ${1}107outputMsg=$(virtualenv notebook_env 2>&1)108echo "${outputMsg}" >> ${1} 2>&1109if [[ ${outputMsg} =~ "E:" ]]; then110echo "ERROR: Could not create a Python-Jupyter Notebook virtual environment."111errorFlag=${TRUE}112fi113echo "" >> ${1}114echo "" | tee -a ${1}115116# Activate The Python-Jupyter Notebook Virtual Environment Just Created117echo "Activating the Python-Jupyter Notebook virtual environment." | tee -a ${1}118echo "" >> ${1}119outputMsg=$(source notebook_env/bin/activate 2>&1)120echo "${outputMsg}" >> ${1} 2>&1121if [[ ${outputMsg} =~ "E:" ]]; then122echo "ERROR: Could not activate a Python-Jupyter Notebook virtual environment."123errorFlag=${TRUE}124fi125echo ""126127# Install Jupyter Notebook In The Python-Jupyter Notebook Virtual Environment Created Earlier128echo "Installing the Jupyter Notebook package in the virtual environment." | tee -a ${1}129echo "" >> ${1}130outputMsg=$(python3 -m pip install jupyter 2>&1)131echo "${outputMsg}" >> ${1} 2>&1132if [[ ${outputMsg} =~ "E:" ]]; then133echo "ERROR: Could not install jupyter."134errorFlag=${TRUE}135fi136echo "" >> ${1}137echo "" | tee -a ${1}138139# If A Jupyter Notebook Default Configuration File Exists, Delete It140if [ -e ${cfgFileName} ]; then141echo "Removing the file \"${cfgFileName}\"." | tee -a ${logFile}142echo "" >> ${1}143outputMsg=$(rm -r ${cfgFileName} 2>&1)144echo "${outputMsg}" >> ${1} 2>&1145if [[ ${outputMsg} =~ "E:" ]]; then146echo "ERROR: Could not remove file \"${cfgFileName}\"."147errorFlag=${TRUE}148returnCode=${FALSE}149return ${returnCode}150fi151echo ""152fi153154# Generate A Jupyter Notebook Default Configuration File155echo "Generating the default Jupyter Notebook configuration file." | tee -a ${1}156echo "" >> ${1}157outputMsg=$(jupyter notebook --generate-config 2>&1)158echo "${outputMsg}" >> ${1} 2>&1159if [[ ${outputMsg} =~ "E:" ]]; then160echo "ERROR: Could not generate the default Jupyter Notebook configuration file."161errorFlag=${TRUE}162fi163echo "" >> ${1}164echo "" | tee -a ${1}165166# If The Jupyter Notebook Default Configuration File Was Created Successfully, Modify It167if [ -e ${cfgFileName} ]; then168echo "Updating the file \"${cfgFileName}\"." | tee -a ${logFile}169echo "" >> ${1}170outputMsg=$(sed -i "s/${searchStr}/${replaceStr}/" ${cfgFileName} 2>&1)171echo "${outputMsg}" >> ${1} 2>&1172if [[ ${outputMsg} =~ "E:" ]]; then173echo "ERROR: Could not update file \"${cfgFileName}\"."174errorFlag=${TRUE}175returnCode=${FALSE}176return ${returnCode}177fi178echo ""179fi180181# Install The PyHamcrest Library In The Python-Jupyter Notebook Virtual Environment182echo "Installing the PyHamcrest libary in the virtual environment." | tee -a ${1}183echo "" >> ${1}184outputMsg=$(python3 -m pip install PyHamcrest 2>&1)185echo "${outputMsg}" >> ${1} 2>&1186if [[ ${outputMsg} =~ "E:" ]]; then187echo "ERROR: Could not install ibm_db."188errorFlag=${TRUE}189fi190echo "" >> ${1}191echo "" | tee -a ${1}192193# Install The IBM_DB Library In The Python-Jupyter Notebook Virtual Environment194echo "Installing the ibm_db libary in the virtual environment." | tee -a ${1}195echo "" >> ${1}196outputMsg=$(python3 -m pip install ibm_db 2>&1)197echo "${outputMsg}" >> ${1} 2>&1198if [[ ${outputMsg} =~ "E:" ]]; then199echo "ERROR: Could not install ibm_db."200errorFlag=${TRUE}201fi202echo "" >> ${1}203echo "" | tee -a ${1}204205# Write An Appropriate Message To The Log File206if [[ ${errorFlag} -eq ${TRUE} ]]; then207echo "Problem(s) encountered. Refer to the file \"${1}\" for more information."208returnCode=${FALSE}209else210date >> ${1} 2>&1211echo "Finished installing Jupyter Notebook." | tee -a ${1}212echo "" | tee -a ${1}213returnCode=${TRUE}214fi215216# Return The Appropriate Value To The Calling Function217return ${returnCode}218}219220221#=================================================================================================#222# NAME: main() #223# #224# PURPOSE: This is the main body of the script. #225# #226# PARAMETERS: $1 - Script key word #227# $2 - Option #228# #229# RETURNS: 0 - The script executed successfully #230# 1 - The script did NOT execute successfully #231#=================================================================================================#232233# Define All Variables Used234TRUE=${TRUE:=1} # Boolean TRUE235FALSE=${FALSE:=0} # Boolean FALSE236237currentDir=${currentDir:=''} # Current Working Directory238userDir=${userDir:=''} # User Home Directory239profileFile=${profileFile:=''} # User Profile File Name240logFile=${logFile:=''} # Log File Name241virtualEnvDir=${virtualEnvDir:=''} # Jupyter Notebook Virtual Environment Directory242timeStamp=${timeStamp:=''} # Current Date And Time Value243244# Initialize The Appropriate Memory Variables245currentDir=$(pwd)246userDir=${HOME}247profileFile="${HOME}/.bashrc"248logFile="${currentDir}/jnotebook_install-user.log"249virtualEnvDir="${HOME}/JNotebook"250timeStamp=$(date '+%m-%d-%Y:%H-%M-%S')251252# Clear The Screen253clear254255# Create A Log File In The Current Directory And Write Header Information To It256init_log_file ${logFile}257258# Display A "Setting Up" Message259date >> ${logFile} 2>&1260echo "Installing the Jupyter Notebook software. Please wait ..." | tee -a ${logFile}261echo "" | tee -a ${logFile}262263# Add The Location Of The Scripts "pip", "pip3", And "pip3.10", Which Are Installed In The User's264# ".local/bin" Directory To The PATH Environment Variable265export PATH="$PATH:${HOME}/.local/bin"266echo "" >> ${profileFile} 2>&1267echo "# set PATH so it includes user's private bin if it exists" >> ${profileFile} 2>&1268echo "if [ -d \"\$HOME/.local/bin\" ] ; then" >> ${profileFile} 2>&1269echo " PATH=\"\$HOME/.local/bin:\$PATH\"" >> ${profileFile} 2>&1270echo "fi" >> ${profileFile} 2>&1271echo "" >> ${profileFile} 2>&1272273# Install The Jupyter Notebook Libraries Needed For Ubuntu Linux274install_jupyter_notebook ${logFile} ${virtualEnvDir} ${userDir}275returnCode=$?276if [ ${returnCode} == ${FALSE} ]; then277exit 1278fi279280# Write The End Timestamp To the Log File281date >> ${logFile} 2>&1282echo "Done!" >> ${logFile}283284# Display A "Finished" Message And Write A "Finished" Message To The Log File285echo "Done!"286echo "Refer to the file \"${logFile}\" for more information."287echo "" | tee -a ${logFile}288echo "To start Jupyter run the following commands:" | tee -a ${logFile}289echo "" | tee -a ${logFile}290echo " source ${profileFile}" | tee -a ${logFile}291echo " jupyter notebook" | tee -a ${logFile}292echo "" | tee -a ${logFile}293294# Exit And Return Control To The Operating System295exit 0296297298