From d71a8c4bf9661ab7e25c85dacede49375b624fa7 Mon Sep 17 00:00:00 2001
From: Britney Fransen <brfransen@gmail.com>
Date: Wed, 21 Dec 2022 17:26:21 -0500
Subject: linhes-system: add_storage.py & remove_storage.py: more fixes

---
 linhes/linhes-system/PKGBUILD          |   6 +-
 linhes/linhes-system/add_storage.py    | 102 +++++++++++++++++++--------------
 linhes/linhes-system/remove_storage.py |  23 ++++----
 3 files changed, 75 insertions(+), 56 deletions(-)

diff --git a/linhes/linhes-system/PKGBUILD b/linhes/linhes-system/PKGBUILD
index 20286a3..ae993b7 100755
--- a/linhes/linhes-system/PKGBUILD
+++ b/linhes/linhes-system/PKGBUILD
@@ -1,6 +1,6 @@
 pkgname=linhes-system
 pkgver=9.0.0
-pkgrel=10
+pkgrel=11
 arch=('x86_64')
 #install=$pkgname.install
 pkgdesc="Everything that makes LinHES a system"
@@ -19,10 +19,10 @@ source=($binfiles
     '10-monitor.conf' 'x11vnc.override.conf'
     'system-sudo.rules' 'linhes-profile.sh'
     'lh_system_start.sh.desktop')
-sha256sums=('d4a4f7ba3445c9f2fb20da0b3cff6fcb2bd6ff845153c10a4a8fa66a8b5faf68'
+sha256sums=('979a916cb54ca8f481a045867b0c77c06cf184a3e8d962df52525dd893e1ace4'
             'b0e8fe4d04e1f779d52a28156489fb51efc13e173efccba2d6d458044bf35904'
             '4ab36bbabf0964f666278c225f4c2d41f4277acb42e9023163fa3a9599282466'
-            '632eb5073219b86667a361e567ad766fbd122f27dc5e775bba9983c417686aa9'
+            'ff261f41efec8a9963f9f59100cbe75f015028a2ed3a863ce0cb473f2ebb7b76'
             '11168c9cd3b117decaab6bc665c183b4aab917cf0a976bce4c1b5e4686a27bc9'
             'ae34515e144830f424d3bd3f6b1b446892d62beed20bca6f0fb19b0bbb779f27'
             '23358a7bff4968eccd469613b81b1415c2ae0ebe77f14f74426697333e4d88d7'
diff --git a/linhes/linhes-system/add_storage.py b/linhes/linhes-system/add_storage.py
index 9f2e61c..e210bf1 100755
--- a/linhes/linhes-system/add_storage.py
+++ b/linhes/linhes-system/add_storage.py
@@ -5,17 +5,17 @@
 #Disks that are mounted, in fstab, size < 5000 bytes, optical or
 #have already been seen will not be presented as an option.
 #
-# Version 2.0.0
+# Version 2.0.1
 
 import dbus
 import pickle
 import subprocess
 import sys,os,re
-import random, string
+import random,string
 import configparser
-from configparser import SafeConfigParser
+from configparser import ConfigParser
 import glob
-
+import logging
 from MythTV import MythDB, MythBE, Recorded, MythError
 from socket import timeout, gethostname
 
@@ -50,19 +50,18 @@ class disk_device:
 
         self.block_path = block_dev.Get('org.freedesktop.UDisks2.Block', 'Device', dbus_interface='org.freedesktop.DBus.Properties')
         self.block_path = bytearray(self.block_path).replace(b'\x00', b'').decode('utf-8')
-        print("Device:", self.block_path)
+        logging.info("Device: %s", self.block_path)
         self.read_only = self.get_is_readonly(block_dev)
-        print("ReadOnly:", self.read_only)
+        logging.info("ReadOnly: %s", self.read_only)
         self.device_file_path = self.get_device_file_path(block_dev)
-        print("Device File Path:", self.device_file_path)
+        logging.info("Device File Path: %s", self.device_file_path)
         self.device_id = block_dev.Get('org.freedesktop.UDisks2.Block', 'Id', dbus_interface='org.freedesktop.DBus.Properties')
-        print("Device Id:", self.device_id)
+        logging.info("Device Id: %s", self.device_id)
         self.is_device = self.get_is_device()
-        print("Is Device:", self.is_device)
-
+        logging.info("Is Device: %s", self.is_device)
 
         self.drive = block_dev.Get('org.freedesktop.UDisks2.Block', 'Drive', dbus_interface='org.freedesktop.DBus.Properties')
-        print("Drive:", self.drive)
+        logging.info("Drive: %s", self.drive)
         drive_dev = bus.get_object("org.freedesktop.UDisks2", self.drive)
 
         self.storage_dir = storage_dir
@@ -71,15 +70,15 @@ class disk_device:
         self.fs_map = self.get_fsmap()
 
         self.vendor = drive_dev.Get('org.freedesktop.UDisks2.Drive', 'Vendor', dbus_interface='org.freedesktop.DBus.Properties')
-        print("Vendor:", self.vendor)
+        logging.info("Vendor: %s", self.vendor)
         self.model = drive_dev.Get('org.freedesktop.UDisks2.Drive', 'Model', dbus_interface='org.freedesktop.DBus.Properties')
-        print("Model:", self.model)
+        logging.info("Model: %s", self.model)
         self.device_size = drive_dev.Get('org.freedesktop.UDisks2.Drive', 'Size', dbus_interface='org.freedesktop.DBus.Properties')
-        print("Drive Size:", self.device_size)
+        logging.info("Drive Size: %s", self.device_size)
         self.serial_number = self.get_serial_number(drive_dev)
-        print("Serial:", self.serial_number)
+        logging.info("Serial: %s", self.serial_number)
         self.is_optical = self.get_is_optical_disc(drive_dev)
-        print("Is Optical:", self.is_optical)
+        logging.info("Is Optical: %s", self.is_optical)
 
         self.mmount = False
         self.dir_sg = False
@@ -88,30 +87,36 @@ class disk_device:
             self.f = block_dev.Get('org.freedesktop.UDisks2.Filesystem', 'MountPoints', dbus_interface='org.freedesktop.DBus.Properties')
             self.is_mounted = True
             self.f[0] = bytearray(self.f[0]).replace(b'\x00', b'').decode('utf-8')
-            print("MountPoints:", self.f[0])
+            logging.info("MountPoints: %s", self.f[0])
         except:
             self.f = ['']
             self.is_mounted = False
-            print("MountPoints:", self.f[0])
-        print("Is Mounted:", self.is_mounted)
+            logging.info("MountPoints: %s", self.f[0])
+        logging.info("Is Mounted: %s", self.is_mounted)
 
         try:
             self.partition_size = block_dev.Get('org.freedesktop.UDisks2.Partition', 'Size', dbus_interface='org.freedesktop.DBus.Properties')
-            print("Partition Size:", self.partition_size)
-            self.block_partition = block_dev.Get('org.freedesktop.UDisks2.Partition', 'Number', dbus_interface='org.freedesktop.DBus.Properties')
-            print("Partition Number:", self.block_partition)
         except:
-            pass
+            self.partition_size = 0
+        logging.info("Partition Size: %s", self.partition_size)
+
+        self.set_partition("1")
+        logging.info("Block Partition: %s", self.block_partition)
 
         self.in_use = self.get_in_use()
-        print("In Use:", self.in_use)
+        logging.info("In Use: %s", self.in_use)
         self.uuid=''
         self.new_mount_point=''
         self.disk_num=''
 
-
     def set_partition(self,partition):
-        self.block_partition = "%s%s" %(self.block_path,partition)
+        if self.is_device:
+            if 'nvme' in self.block_path:
+                self.block_partition = "%sp%s" %(self.block_path,partition)
+            else:
+                self.block_partition = "%s%s" %(self.block_path,partition)
+        else:
+            self.block_partition = self.block_path
 
     def set_mmount(self,flag):
         self.mmount = flag
@@ -142,7 +147,7 @@ class disk_device:
             return True
 
     def get_is_device(self):
-        match = re.search(r'part\d+$', self.device_id)
+        match = re.search(r'part\d+$', self.device_file_path)
         if match is None:
             return True
         else:
@@ -160,7 +165,7 @@ class disk_device:
         serial_number = drive_dev.Get('org.freedesktop.UDisks2.Drive', 'Serial', dbus_interface='org.freedesktop.DBus.Properties')
         random_string = os.urandom(5)
         if serial_number == '':
-            serial_number = "".join( [random.choice(string.letters) for i in range(6)] )
+            serial_number = "".join( [random.choice(string.ascii_letters) for i in range(6)] )
             serial_number = "NSW%s" %serial_number
         return serial_number
 
@@ -292,7 +297,7 @@ class disk_device:
             new_options = self.find_options_type(fstab)[0]
 
             #find blkid
-            #self.block_partition="%s1" %self.block_path
+            self.block_partition="%s1" %self.block_path
             uuid=self.find_uuid(self.block_partition)
 
             #construct new line
@@ -396,13 +401,12 @@ class disk_device:
         self.config.set('storage','storage_groups',self.dir_sg)
         self.config.set('storage','disk_num',self.disk_num)
 
-
         filename="%s_%s.conf" %(self.model.replace(' ',''),
                                 self.serial_number.replace(' ',''))
 
         configfile="/etc/storage.d/%s" %filename
         print("       %s" %configfile)
-        with open(configfile, 'wb') as configfile:
+        with open(configfile, 'w') as configfile:
             self.config.write(configfile)
         return
 
@@ -430,7 +434,7 @@ def runcmd(cmd):
         pass
     else:
         cmd = "echo "+cmd
-    #print cmd
+    #print(cmd)
     cmdout = subprocess.getstatusoutput(cmd)
     #logging.debug("    %s", cmdout)
     return cmdout
@@ -444,7 +448,7 @@ def scan_system():
     current_drive_list=[]
     for dev in ud_manager.GetManagedObjects().items():
         if dev[0].startswith("/org/freedesktop/UDisks2/block_devices"):
-            print('\n' + dev[0])
+            logging.info(dev[0])
             drive = disk_device(dev[0],storage_dir)
             if drive.is_device and drive.device_size > 5000 and not drive.is_optical :
                 current_drive_list.append(drive)
@@ -472,16 +476,16 @@ def search_for_match(system_drive,known_drive_list):
     match_drive=False
     for y in known_drive_list:
         if system_drive.serial_number.startswith("NSW"):
-            #print "Match_test: hash"
-            system_drive_hash = "%s_%s_%s" %(system_drive.model,system_drive.parition_size,system_drive.device_file_path)
-            y_drive_hash = "%s_%s_%s" %(y.model,y.parition_size,y.device_file_path)
+            #print("Match_test: hash")
+            system_drive_hash = "%s_%s_%s" %(system_drive.model,system_drive.partition_size,system_drive.device_file_path)
+            y_drive_hash = "%s_%s_%s" %(y.model,y.partition_size,y.device_file_path)
             if system_drive_hash == y_drive_hash :
                 match_drive = True
                 print("\n*   No serial number was found, matched using hash method: %s" %system_drive.model)
                 break
 
         elif y.serial_number == system_drive.serial_number:
-            #print "Match_test: serial number"
+            #print("Match_test: serial number")
             match_drive=True
             break
 
@@ -579,11 +583,15 @@ def remove_pickle():
         pass
 
 def last_disk_num():
-    parser = SafeConfigParser()
+    parser = ConfigParser()
     num_list = []
     for conf_file in glob.glob('%s/*.conf' %storage_dir):
         parser.read(conf_file)
-        disk_num = parser.get('storage', 'disk_num')
+        try:
+            disk_num = parser.get('storage', 'disk_num')
+        except:
+            print("\nSkipping " + conf_file + "is missing disk_num.")
+            continue
         num_list.append(int(disk_num))
         num_list.sort()
     try:
@@ -804,7 +812,11 @@ def reconstruct_storagegroups():
     for conf_file in glob.glob('%s/*.conf' %storage_dir):
         parser = SafeConfigParser()
         parser.read(conf_file)
-        mount_point = parser.get('storage', 'mountpoint')
+        try:
+            mount_point = parser.get('storage', 'mountpoint')
+        except:
+            print("\nSkipping: " + conf_file + " is missing mountpoint")
+            continue
         mmount = parser.getboolean('storage', 'mmount')
         try:
             removed = parser.getboolean('storage', 'removed')
@@ -1061,7 +1073,7 @@ class reconstruct_path:
         self.config.set('storage','disk_num',self.disk_num)
 
         print("       %s" %self.conf_file)
-        with open(self.conf_file, 'wb') as self.conf_file:
+        with open(self.conf_file, 'w') as self.conf_file:
             self.config.write(self.conf_file)
         return
 
@@ -1162,6 +1174,12 @@ if __name__ == "__main__":
     if not os.geteuid()==0:
         sys.exit("\nRoot access is required to run this program.\n")
 
+    logging.basicConfig(filename='/var/log/add_storage.log', filemode='w',
+                        format='%(asctime)s - %(levelname)s - %(message)s',
+                        datefmt='%y-%m-%d %H:%M:%S')
+    logger = logging.getLogger()
+    logger.setLevel(logging.INFO)
+
     if "--help" in sys.argv or "-h" in sys.argv:
         usage()
 
@@ -1171,7 +1189,7 @@ if __name__ == "__main__":
     if "--no_mount" in sys.argv :
         no_mount = True
 
-    if "--no_destruction" or "-nd" in sys.argv:
+    if "--no_destruction" in sys.argv:
         destruction = False
 
     if "--new_init" in sys.argv :
diff --git a/linhes/linhes-system/remove_storage.py b/linhes/linhes-system/remove_storage.py
index 15dbffc..5de2f7a 100755
--- a/linhes/linhes-system/remove_storage.py
+++ b/linhes/linhes-system/remove_storage.py
@@ -1,13 +1,13 @@
-#!/usr/bin/python2
+#!/usr/bin/python
 #remove_storage.py removes disks that were added using add_storage.py
 #
 #Only disks that have a conf file in /etc/storage.d/ are presented
 #as choices to remove.
-
+# Version 2.0.0
 
 import os,sys,subprocess
 import configparser
-from configparser import SafeConfigParser
+from configparser import ConfigParser
 import glob
 from socket import gethostname
 from MythTV import MythDB
@@ -69,15 +69,13 @@ def remove_disk_mount(conf):
     runcmd(cmd)
 
 def update_conf_file(conf):
-    print("Updating %s file\n    and running systemconfig." %(conf[4]))
-    parser = SafeConfigParser()
+    print("Updating %s file." %(conf[4]))
+    parser = ConfigParser()
     parser.read(conf[4])
     parser.set('storage','shareable',"False")
     parser.set('storage','removed',"True")
-    with open(conf[4], 'wb') as conf[4]:
+    with open(conf[4], 'w') as conf[4]:
         parser.write(conf[4])
-    cmd = "systemconfig.py -m fileshare"
-    runcmd(cmd)
 
 def usage():
     help='''
@@ -96,7 +94,6 @@ def usage():
         Remove disk name mountpoint at /data/storage/
         Make shareable = False in /etc/storage.d/DISKNAME.conf
         Add removed = True in /etc/stoarge.d/DISKNAME.conf
-        Run systemconfig.py -m fileshare to update File Shares
 
     Options:
     -h, --help:         Show this help message.
@@ -112,10 +109,14 @@ def main():
     # get conf files that are not disk0 or not removed
     for conf_file in glob.glob('%s/*.conf' %storage_dir):
         this_conf = []
-        parser = SafeConfigParser()
+        parser = ConfigParser()
         parser.read(conf_file)
         mounted = ""
-        disk_num = parser.get('storage', 'disk_num')
+        try:
+            disk_num = parser.get('storage', 'disk_num')
+        except:
+            print("\nSkipping: " + conf_file + " is missing disk_num")
+            continue
         try:
             removed = parser.get('storage', 'removed')
         except:
-- 
cgit v0.12

