Path: blob/main/Db2-L3-Tech-Lab/jnotebook_setup-root.sh
1928 views
#!/bin/bash1#-------------------------------------------------------------------------------------------------#2# NAME: jnotebook_setup-root.sh #3# #4# PURPOSE: This program is designed to install the libraries that are needed to set up and run #5# Jupyter Notebook 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# userDir #11# #12# 2) Log in as the root user and issue the following command from a Linux terminal #13# window: #14# #15# ./jnotebook_setup-root.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# environment setup up script log file (jnotebook_setup-root.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_setup-root.log #" >> ${1} 2>&158echo "# CREATED: ${timeStamp} #" >> ${1} 2>&159echo "# #" >> ${1} 2>&160echo "# CONTENTS: Log data produced by jnotebook_setup-root.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: setup_jupyter_notebook_env() #71# #72# PURPOSE: This function installs the libraries and packages needed to work wit Jupyter #73# Notebooks (on Ubuntu Linux). #74# #75# PARAMETERS: $1 - Log file name #76# $2 - Python Virtual Environment location #77# #78# RETURNS: TRUE (1) - Packages and libraries needed were installed #79# FALSE (0) - One or more packages or libraries were not installed #80#-------------------------------------------------------------------------------------------------#81function setup_jupyter_notebook_env82{83# Define All Local Variables Used84local returnCode=${returnCode:=0} # Function Return Code85local errorFlag=${errorFlag:=0} # Error Encountered Flag86local outputMsg=${outputMsg:=''} # Command Output Message Text8788# Initialize The Appropriate Memory Variables89returnCode=${FALSE}90errorFlag=${FALSE}91virtualEnvLocation=${2}9293# Remove All Packages Currently Installed On The System That Are Not Needed94echo "Uninstalling all packages currently installed that are not needed." | tee -a ${1}95outputMsg=$(apt-get -y autoremove 2>&1)96echo "${outputMsg}" >> ${1} 2>&197if [[ ${outputMsg} =~ "E:" ]]; then98echo "ERROR: Could not uninstall unneeded packages."99errorFlag=${TRUE}100fi101echo "" >> ${1}102echo "" | tee -a ${1}103104# Upgrade All Packages Installed On The System105echo "Installing newer versions of all packages currently installed." | tee -a ${1}106echo "(This may take a few minutes)."107outputMsg=$(apt-get -y upgrade 2>&1)108echo "${outputMsg}" >> ${1} 2>&1109if [[ ${outputMsg} =~ "E:" ]]; then110echo "ERROR: Could not upgrade packages."111errorFlag=${TRUE}112fi113echo "" >> ${1}114echo "" | tee -a ${1}115116# Fix Any Broken Packages Or Package Dependencies Found117echo "Fixing any broken packages and package dependencies found." | tee -a ${1}118echo "" >> ${1}119outputMsg=$(apt-get --fix-broken install)120echo "${outputMsg}" >> ${1} 2>&1121if [[ ${outputMsg} =~ "E:" ]]; then122echo "ERROR: Could not fix broken packages and package dependencies."123errorFlag=${TRUE}124fi125echo "" >> ${1}126echo "" | tee -a ${1}127128# Generate A New List Of All Packages Installed On The System129echo "Updating the installed package list." | tee -a ${1}130echo "" >> ${1}131outputMsg=$(apt-get update 2>&1)132echo "${outputMsg} Done." >> ${1} 2>&1133if [[ ${outputMsg} =~ "E:" ]]; then134echo "ERROR: Could not update the installed package list."135errorFlag=${TRUE}136fi137echo "" >> ${1}138echo "" | tee -a ${1}139140# Install The PYTHON3 Linux Package141echo "Installing the python3 package." | tee -a ${1}142echo "" >> ${1}143outputMsg=$(apt-get -y install python3 2>&1)144echo "${outputMsg}" >> ${1} 2>&1145if [[ ${outputMsg} =~ "E:" ]]; then146echo "ERROR: Could not install python3."147errorFlag=${TRUE}148fi149echo "" >> ${1}150echo "" | tee -a ${1}151152# Install The PYTHON3-PIP Package Manager153echo "Installing the python3-pip package manager." | tee -a ${1}154echo "" >> ${1}155outputMsg=$(apt-get -y install python3-pip 2>&1)156echo "${outputMsg}" >> ${1} 2>&1157if [[ ${outputMsg} =~ "E:" ]]; then158echo "ERROR: Could not install python3-pip."159errorFlag=${TRUE}160fi161echo "" >> ${1}162echo "" | tee -a ${1}163164# Upgrade The PYTHON3-PIP (PIP3) Linux Library165echo "Upgrading the pip3 package." | tee -a ${1}166echo "" >> ${1}167outputMsg=$(python3 -m pip install --upgrade pip 2>&1)168echo "${outputMsg}" >> ${1} 2>&1169if [[ ${outputMsg} =~ "E:" ]]; then170echo "ERROR: Could not upgrade pip3."171errorFlag=${TRUE}172fi173echo "" >> ${1}174echo "" | tee -a ${1}175176# Install The VIRTUALENV Linux Package (Used For Creating Virtual177# Python Environments)178echo "Installing the virtualenv package." | tee -a ${1}179echo "" >> ${1}180outputMsg=$(python3 -m pip install virtualenv 2>&1)181echo "${outputMsg}" >> ${1} 2>&1182if [[ ${outputMsg} =~ "E:" ]]; then183echo "ERROR: Could not install virtualenv."184errorFlag=${TRUE}185fi186echo "" >> ${1}187echo "" | tee -a ${1}188189# If The Jupyter Notebook Virtual Environment Directory Desired Already Exists, Remove It190if [ -e ${virtualEnvLocation} ]; then191echo "Removing the directory \"${virtualEnvLocation}\"." | tee -a ${logFile}192echo "" >> ${1}193outputMsg=$(rm -r ${virtualEnvLocation} 2>&1)194echo "${outputMsg}" >> ${1} 2>&1195if [[ ${outputMsg} =~ "E:" ]]; then196echo "ERROR: Could not remove directory \"${virtualEnvLocation}\"."197errorFlag=${TRUE}198returnCode=${FALSE}199return ${returnCode}200fi201echo ""202fi203204# Create A Jupyter Notebook Virtual Environment Directory205outputMsg=$(mkdir -m 777 ${virtualEnvLocation} 2>&1)206returnCode=$?207if [ ${returnCode} != 0 ]; then208echo "ERROR: Could not create the \"${virtualEnvLocation}\" directory." | tee -a ${1}209echo " ${outputMsg}" >> ${1} 2>&1210returnCode=${FALSE}211return ${returnCode}212fi213214# Change The Permissions Of The Jupyter Notebook Virtual Environment Directory So Anyone215# Can Access It216outputMsg=$(chmod 777 ${virtualEnvLocation} 2>&1)217returnCode=$?218if [ ${returnCode} != 0 ]; then219echo -e "ERROR: Could not change "220echo "permissions for the \"${virtualEnvLocation}\" directory." | tee -a ${1}221echo " ${outputMsg}" >> ${1} 2>&1222returnCode=${FALSE}223return ${returnCode}224fi225226# Generate A New List Of All Packages Installed On The System227echo "Updating the installed package list." | tee -a ${1}228echo "" >> ${1}229outputMsg=$(apt-get update 2>&1)230echo "${outputMsg} Done." >> ${1} 2>&1231if [[ ${outputMsg} =~ "E:" ]]; then232echo "ERROR: Could not update the installed package list."233errorFlag=${TRUE}234fi235echo "" >> ${1}236echo "" | tee -a ${1}237238# Write An Appropriate Message To The Log File239if [[ ${errorFlag} -eq ${TRUE} ]]; then240echo "Problem(s) encountered. Refer to the file \"${1}\" for more information."241returnCode=${FALSE}242else243date >> ${1} 2>&1244echo "Finished installing the libraries needed for Jupyter Notebook." | tee -a ${1}245echo "" | tee -a ${1}246returnCode=${TRUE}247fi248249# Return The Appropriate Value To The Calling Function250return ${returnCode}251}252253254#=================================================================================================#255# NAME: main() #256# #257# PURPOSE: This is the main body of the script. #258# #259# PARAMETERS: $1 - Script key word #260# $2 - Option #261# #262# RETURNS: 0 - The script executed successfully #263# 1 - The script did NOT execute successfully #264#=================================================================================================#265266# Define All Variables Used267TRUE=${TRUE:=1} # Boolean TRUE268FALSE=${FALSE:=0} # Boolean FALSE269270currentDir=${currentDir:=''} # Current Working Directory271userDir=${userDir:=''} # User Home Directory272logFile=${logFile:=''} # Log File Name273virtualEnvDir=${virtualEnvDir:=''} # Jupyter Notebook Virtual Environment Directory274timeStamp=${timeStamp:=''} # Current Date And Time Value275outputMsg=${outputMsg:=''} # Command Output Message Text276277# Initialize The Appropriate Memory Variables278currentDir=$(pwd)279userDir="/home/resanders"280logFile="${currentDir}/jnotebook_setup-root.log"281virtualEnvDir="${userDir}/JNotebook"282timeStamp=$(date '+%m-%d-%Y:%H-%M-%S')283284# Clear The Screen285clear286287# Create A Log File In The Current Directory And Write Header Information To It288init_log_file ${logFile}289290# Display A "Setting Up" Message291date >> ${logFile} 2>&1292echo "Installing the libraries and packages needed for Jupyter Notebook. Please wait ..." | tee -a ${logFile}293echo "" | tee -a ${logFile}294295# Install The Libraries And Packages Needed For Jupyter Notebook296setup_jupyter_notebook_env ${logFile} ${virtualEnvDir}297returnCode=$?298if [ ${returnCode} == ${FALSE} ]; then299exit 1300fi301302# Write The End Timestamp To the Log File303date >> ${logFile} 2>&1304echo "Done!" >> ${logFile}305306# Display A "Finished" Message And Write A "Finished" Message To The Log File307echo "Done!"308echo ""309echo "Refer to the file \"${logFile}\" for more information."310echo ""311312# Exit And Return Control To The Operating System313exit 0314315316