diff --git a/genloader.py b/genloader.py index 979f719f..dc2bd192 100644 --- a/genloader.py +++ b/genloader.py @@ -273,11 +273,11 @@ def CheckBaseSoftware(self): if self.PipChecked: return True - command_list = [self.pipProgram, "-V"] + command_list = [sys.executable, "-m", "pip", "-V"] + #command_list = [self.pipProgram, "-V"] if not self.ExecuteCommandList(command_list): self.InstallBaseSoftware() - "--break-system-packages" # /usr/lib/python3.11/EXTERNALLY-MANAGED managedfile = f"/usr/lib/python{sys.version_info.major:d}.{sys.version_info.minor:d}/EXTERNALLY-MANAGED" if os.path.isfile(managedfile): @@ -457,7 +457,7 @@ def GetLibararyVersion(self, libraryname, importonly=False): if "linux" in sys.platform: self.CheckBaseSoftware() - install_list = [self.pipProgram, "freeze", libraryname] + install_list = [sys.executable, "-m", "pip", "freeze", libraryname] process = Popen(install_list, stdout=PIPE, stderr=PIPE) output, _error = process.communicate() @@ -519,17 +519,11 @@ def InstallLibrary(self, libraryname, update=False, version=None, uninstall=Fals self.CheckBaseSoftware() if update: - install_list = [self.pipProgram, "install", libraryname, "-U"] + install_list = [sys.executable, "-m", "pip", "install", libraryname, "-U"] elif uninstall: - install_list = [self.pipProgram, "uninstall", "-y", libraryname] + install_list = [sys.executable, "-m", "pip", "uninstall", "-y", libraryname] else: - install_list = [self.pipProgram, "install", libraryname] - - if self.OverrideManagedPackages: - # starting with bookworm the raspberry pi os has managed system - # packages enabled so we will override that to install genmon - # requirements - install_list.append("--break-system-packages") + install_list = [sys.executable, "-m", "pip", "install", libraryname] process = Popen(install_list, stdout=PIPE, stderr=PIPE) output, _error = process.communicate() @@ -537,9 +531,7 @@ def InstallLibrary(self, libraryname, update=False, version=None, uninstall=Fals if _error: self.LogInfo( "Error in InstallLibrary using pip : " - + libraryname - + " : UnInstall: " - + str(uninstall) + + str(install_list) + ": " + str(_error) ) @@ -549,9 +541,7 @@ def InstallLibrary(self, libraryname, update=False, version=None, uninstall=Fals except Exception as e1: self.LogInfo( "Error installing module: " - + libraryname - + " : UnInstall: " - + str(uninstall) + + str(install_list) + ": " + str(e1), LogLine=True, diff --git a/genmonmaint.sh b/genmonmaint.sh index deb31134..5138b5fd 100755 --- a/genmonmaint.sh +++ b/genmonmaint.sh @@ -30,9 +30,10 @@ noprompt_opt=false cleanpython_opt=false copyfiles_opt=false update_os=false +managedpackages=false #------------------------------------------------------------------------------- -function removemanagedpip() { +function checkmanagedpackages() { # /usr/lib/python3.11/EXTERNALLY-MANAGED pythonmajor=$($pythoncommand -c 'import sys; print(sys.version_info.major)') @@ -40,8 +41,13 @@ function removemanagedpip() { managedfile="/usr/lib/python$pythonmajor.$pythonminor/EXTERNALLY-MANAGED" if [ -f $managedfile ]; then - pipoptions="--break-system-packages" - echo "Overriding system managed package system.." + managedpackages=true + echo "Managed system packages found, installing python virtual environment" + sudo apt-get -yqq install python3-venv + # create the virtual environment + echo "Creating virtual python environmnet for genmon" + $pythoncommand -m venv genenv + pythoncommand="./genenv/bin/python" fi } @@ -87,32 +93,7 @@ function copyconffiles() { function updatelibraries() { echo "Updating libraries...." - sudo $pipcommand install crcmod -U $pipoptions - sudo $pipcommand install configparser -U $pipoptions - sudo $pipcommand install pyserial -U $pipoptions - sudo $pipcommand install Flask -U $pipoptions - if [ "$usepython3" = true ] ; then - sudo $pipcommand install pyowm==2.10.0 -U $pipoptions - else - sudo $pipcommand install pyowm==2.9.0 -U $pipoptions - fi - sudo $pipcommand install pytz -U $pipoptions - sudo $pipcommand install pyopenssl -U $pipoptions - sudo $pipcommand install twilio -U $pipoptions - sudo $pipcommand install chump -U $pipoptions - sudo $pipcommand install paho-mqtt -U $pipoptions - sudo $pipcommand install pysnmp -U $pipoptions - sudo $pipcommand install ldap3 -U $pipoptions - sudo $pipcommand install pyasn1==0.4.8 -U $pipoptions - sudo $pipcommand install smbus -U $pipoptions - sudo $pipcommand install psutil -U $pipoptions - if [ "$usepython3" = true ] ; then - sudo $pipcommand install pyotp -U $pipoptions - else - sudo $pipcommand install pyotp==2.3.0 -U $pipoptions - fi - sudo $pipcommand install mopeka_pro_check -U $pipoptions - sudo $pipcommand install fluids -U $pipoptions + sudo $pythoncommand -m pip install -r requirements.txt -U $pipoptions echo "Done." } @@ -143,7 +124,8 @@ function installrpirtscts() { # This function will install the required libraries for genmon function installgenmon() { - echo "Installing...." + checkmanagedpackages + echo "Installing genmon package requirements...." # possibly use "sudo easy_install3 -U pip" sudo apt-get -yqq update if [ "$usepython3" = true ] ; then @@ -151,40 +133,13 @@ function installgenmon() { else sudo apt-get -yqq install python-pip fi - sudo $pipcommand install crcmod $pipoptions - sudo $pipcommand install configparser $pipoptions - sudo $pipcommand install pyserial $pipoptions - sudo $pipcommand install Flask $pipoptions - if [ "$usepython3" = true ] ; then - sudo $pipcommand install pyowm==2.10.0 $pipoptions - else - sudo $pipcommand install pyowm==2.9.0 $pipoptions - fi - sudo $pipcommand install pytz $pipoptions if [ "$usepython3" = true ] ; then sudo apt-get -yqq install build-essential libssl-dev libffi-dev python3-dev cargo else sudo apt-get -yqq install build-essential libssl-dev libffi-dev python-dev cargo fi sudo apt-get -yqq install cmake - sudo $pipcommand install pyopenssl $pipoptions - sudo $pipcommand install twilio $pipoptions - sudo $pipcommand install chump $pipoptions - sudo $pipcommand install paho-mqtt $pipoptions - sudo $pipcommand install pysnmp $pipoptions - sudo $pipcommand install ldap3 $pipoptions - sudo $pipcommand install smbus $pipoptions - sudo $pipcommand install psutil $pipoptions - if [ "$usepython3" = true ] ; then - sudo $pipcommand install pyotp $pipoptions - else - sudo $pipcommand install pyotp==2.3.0 $pipoptions - fi - # correct problem with LDAP3 module install - sudo $pipcommand install pyasn1==0.4.8 -U $pipoptions - sudo $pipcommand install mopeka_pro_check $pipoptions - sudo $pipcommand install fluids $pipoptions - sudo $pipcommand install voipms $pipoptions + sudo $pythoncommand -m pip install -r requirements.txt $pipoptions sudo chmod 775 "$genmondir/startgenmon.sh" sudo chmod 775 "$genmondir/genmonmaint.sh" @@ -445,7 +400,7 @@ shift $((OPTIND -1)) if [ "$update_os" = true ] ; then sudo apt-get --allow-releaseinfo-change update && sudo apt-get upgrade fi -removemanagedpip + if [ "$install_opt" = true ] ; then if [ "$noprompt_opt" = true ] ; then installgenmon "noprompt" diff --git a/startgenmon.sh b/startgenmon.sh index 291c78fe..78b9e20f 100755 --- a/startgenmon.sh +++ b/startgenmon.sh @@ -10,7 +10,36 @@ pipcommand="pip3" config_path="" usepython3=true found_action=false +managedpackages=false + +#------------------------------------------------------------------------------- +function env_activate() { + + if [ "$managedpackages" = true ] ; then + source genenv/bin/activate + fi +} +#------------------------------------------------------------------------------- +function env_deactivate() { + if [ "$managedpackages" = true ] ; then + deactivate + fi +} +#------------------------------------------------------------------------------- +function checkmanagedpackages() { + + # /usr/lib/python3.11/EXTERNALLY-MANAGED + pythonmajor=$($pythoncommand -c 'import sys; print(sys.version_info.major)') + pythonminor=$($pythoncommand -c 'import sys; print(sys.version_info.minor)') + managedfile="/usr/lib/python$pythonmajor.$pythonminor/EXTERNALLY-MANAGED" + + if [ -f $managedfile ]; then + pythoncommand="./genenv/bin/python" + managedpackages=true + echo "using binary: $pythoncommand" + fi +} #------------------------------------------------------------------------------- function setuppython3() { @@ -79,28 +108,36 @@ while (( "$#" )); do ;; esac done - +checkmanagedpackages for val in $PARAMS; do case "$val" in start) echo "Starting genmon python scripts" + env_activate found_action=true sudo $pythoncommand "$genmondir/genloader.py" -s $config_path + env_deactivate ;; stop) found_action=true + env_activate echo "Stopping genmon python scripts" sudo $pythoncommand "$genmondir/genloader.py" -x $config_path + env_deactivate ;; hardstop) found_action=true + env_activate echo "Hard Stopping genmon python scripts" sudo $pythoncommand "$genmondir/genloader.py" -z $config_path + env_deactivate ;; restart) found_action=true + env_activate echo "Restarting genmon python scripts" sudo $pythoncommand "$genmondir/genloader.py" -r $config_path + env_deactivate ;; *) #