From 4cc2306549593b5738e84772aa3a5db3e7801954 Mon Sep 17 00:00:00 2001
From: Britney Fransen <brfransen@gmail.com>
Date: Tue, 22 Mar 2016 19:29:41 +0000
Subject: LinHES-system: add_storage.py: add --reconstruct_sg option to
 recreate storage group dirs and db entries

---
 abs/core/LinHES-system/PKGBUILD       |   4 +-
 abs/core/LinHES-system/add_storage.py | 114 ++++++++++++++++++++++++++++++----
 2 files changed, 104 insertions(+), 14 deletions(-)

diff --git a/abs/core/LinHES-system/PKGBUILD b/abs/core/LinHES-system/PKGBUILD
index 602becc..0f38617 100755
--- a/abs/core/LinHES-system/PKGBUILD
+++ b/abs/core/LinHES-system/PKGBUILD
@@ -1,6 +1,6 @@
 pkgname=LinHES-system
 pkgver=8.4
-pkgrel=2
+pkgrel=3
 arch=('i686' 'x86_64')
 install=system.install
 pkgdesc="Everything that makes LinHES an automated system"
@@ -100,7 +100,7 @@ md5sums=('c03d5e54f00e404c561c7dc69fff9168'
          'bc69a520add58ede9b060c73e67ace13'
          '74e17d6f7453c52d56fecaed5c3f6ad5'
          '47e093e8cfe4b5b96602358e1f540832'
-         '63bbc62240f46a9997eaae4a84b09b76'
+         'b5a73816813b8a9ca17338ce02e580ae'
          '8ed2474fa52775769255cfb0dba00a1a'
          '7753c7ce3f0db5944b7372f5d19bb374'
          '2c005d95312018bef80092136f80f254'
diff --git a/abs/core/LinHES-system/add_storage.py b/abs/core/LinHES-system/add_storage.py
index 2518927..d97c96e 100755
--- a/abs/core/LinHES-system/add_storage.py
+++ b/abs/core/LinHES-system/add_storage.py
@@ -2,7 +2,7 @@
 #add_storage.py used to auto add new storage to MythTV storage groups
 #If it's a new disk it will erase the entire disk and reformat.
 #
-#Disks that are mounted, in fstab, size < 5000 bytes, optical or 
+#Disks that are mounted, in fstab, size < 5000 bytes, optical or
 #have already been seen will not be presented as an option.
 #
 
@@ -31,9 +31,11 @@ SG_MAP={
     'Music'      :'media/music',
     'Streaming'  :'media/streaming',
     'Videos'     :'media/video',
+    'Photographs':'media/photos',
     'Banners'    :'media/artwork/banners',
     'Coverart'   :'media/artwork/coverart',
     'Fanart'     :'media/artwork/fanart',
+    'MusicArt'   :'media/artwork/musicart',
     'Screenshots':'media/artwork/screenshots',
     'Trailers'   :'media/artwork/trailers',
     }
@@ -296,7 +298,7 @@ class disk_device:
         print "    Creating directory structure:"
         print "       %s" %self.new_mount_point
         for y in FS_LIST:
-            print "          %s" %y 
+            print "          %s" %y
             new_dir="%s/%s" %(self.new_mount_point,y)
             try:
                 os.stat(new_dir)
@@ -351,6 +353,7 @@ class disk_device:
         self.config.set('storage','fstype',self.new_fstype)
         self.config.set('storage','shareable','True')
         self.config.set('storage','mmount',self.mmount)
+        self.config.set('storage','storage_groups',self.dir_sg)
         self.config.set('storage','disk_num',self.disk_num)
 
 
@@ -364,7 +367,7 @@ class disk_device:
         return
 
     def symlink_disk(self):
-        print "    Creating symlink for disk%s" %self.disk_num 
+        print "    Creating symlink for disk%s" %self.disk_num
         disk_ln="%s/disk%s" %(self.top_mount_dir,self.disk_num)
         cmd = "ln -s %s %s" %(self.new_mount_point,disk_ln)
         runcmd(cmd)
@@ -479,7 +482,7 @@ def prompt_to_continue(process_list):
     if destruction:
         print "** WARNING: These disk(s) WILL be partitioned and formatted. **\n   ** All content on these disk(s) will be erased. **"
     else:
-        print "   ** These disk(s) will NOT be partitioned and formatted. **"  
+        print "   ** These disk(s) will NOT be partitioned and formatted. **"
     for i in process_list:
         print "      %s" %(i.get_name())
     str1 = raw_input("\n   Press Y to add disk(s), any other key to cancel:")
@@ -503,12 +506,12 @@ def prompt_sg(dir_sg):
         loop = True
         prompt_string='''
     MythTV Storage Groups are used for artwork, database backups,
-    music, streaming, TV recordings, and videos.
+    photos, music, streaming, TV recordings, and videos.
 
     The content on these storage groups will
     only be available while the system is online.
 
-    Enabling MythTV Storage Groups will create the directories 
+    Enabling MythTV Storage Groups will create the directories
     on the disk(s) and add the paths to the MythTV database.
 
     Enable MythTV Storage Groups (Y/N)?:'''
@@ -551,7 +554,7 @@ def last_disk_num():
     return num_list[-1]
 #--------------------------------------------
 
-def main(scan_only, destruction, no_mount, install_call , dir_sg):
+def main(scan_only, destruction, no_mount, install_call, dir_sg):
     global bus
     bus = dbus.SystemBus()
 
@@ -742,6 +745,82 @@ def myth_main(no_mount,install_call,dir_sg):
     cmd = "systemconfig.py -m fileshare"
     runcmd(cmd)
 
+def reconstruct_storagegroups():
+    print "\nRecreating Storage Groups from contents of /etc/storage.d/\n"
+
+    DB = MythDB()
+    host=gethostname()
+
+    for conf_file in glob.glob('%s/*.conf' %storage_dir):
+        parser = SafeConfigParser()
+        parser.read(conf_file)
+        mount_point = parser.get('storage', 'mountpoint')
+        mmount = parser.getboolean('storage', 'mmount')
+        if not os.path.ismount(mount_point):
+            print "Skipping: " + mount_point + " not mounted"
+            continue
+        try:
+            dir_sg = parser.getboolean('storage', 'storage_groups')
+        except ConfigParser.NoOptionError, err:
+            print "SG not found in conf, get setting from DB"
+            dir_sg = False
+            # Get Storage Group directories from DB
+            recs = DB.getStorageGroup(groupname="Default")
+            for record in recs:
+                if record.dirname.startswith(mount_point):
+                    dir_sg = True
+            # Write SG usage to conf
+            parser.set('storage','storage_groups',str(dir_sg))
+            with open(conf_file, 'wb') as conf_file:
+                parser.write(conf_file)
+
+        if dir_sg is True:
+            print "SGs Enabled for: " + mount_point
+            print "    Creating directory structure:"
+            print "       %s" %mount_point
+            for y in FS_LIST:
+                print "          %s" %y
+                new_dir="%s/%s" %(mount_point,y)
+                try:
+                    os.stat(new_dir)
+                except:
+                    os.makedirs(new_dir)
+                    cmd="chown -R mythtv:mythtv /%s" %mount_point
+                    runcmd(cmd)
+                    cmd="chmod -R 775 /%s" %mount_point
+                    runcmd(cmd)
+
+            print "    Adding storage groups to DB"
+            if mmount is True:
+                sgweight=99
+            else:
+                sgweight=0
+
+            for key in SG_MAP.iterkeys():
+                gn=key
+                hn=host
+                dn="%s/%s" %(mount_point,SG_MAP[key])
+                with DB as c:
+                    try:
+                        c.execute("""insert into storagegroup (groupname,hostname,dirname) values (%s,%s,%s)""",(gn,hn,dn))
+                        print "        Added: %s to storagegroup %s" %(dn,gn)
+                    except:
+                        print "        Skipping: %s exists" %dn
+                    if sgweight > 0:
+                        try:
+                            sgw="SGweightPerDir:%s:%s" %(hn,dn)
+                            if DB.settings[hn][sgw] == u'99':
+                                print "        Skipping: storage group weight DB entry exists"
+                            else:
+                                c.execute("""insert into settings (value,data,hostname) values (%s,%s,%s)""",(sgw,sgweight,hn))
+                                print "        Adding storage group weight of %s for %s\n" %(sgweight,gn)
+                        except:
+                            print "        *Error setting storage group weight %s for %s\n" %(sgweight,gn)
+
+        else:
+            print "SGs Disabled for: " + mount_point
+    return
+
 class reconstruct_path:
     def  __init__(self,conf_file):
         self.conf_file = conf_file
@@ -841,7 +920,7 @@ class reconstruct_path:
             print "       Skipping symlink, /myth already exists"
 
     def symlink_disk(self):
-        print "    Creating symlink for disk%s" %self.disk_num 
+        print "    Creating symlink for disk%s" %self.disk_num
         disk_ln="%s/disk%s" %(self.top_mount_dir,self.disk_num)
         cmd = "ln -s %s %s" %(self.mount_point,disk_ln)
         runcmd(cmd)
@@ -897,6 +976,7 @@ class reconstruct_path:
         self.config.set('storage','fstype',self.fstype)
         self.config.set('storage','shareable','True')
         self.config.set('storage','mmount',self.myth_mount)
+        self.config.set('storage','storage_groups',self.dir_sg)
         self.config.set('storage','disk_num',self.disk_num)
 
         print "       %s" %self.conf_file
@@ -935,7 +1015,7 @@ def usage():
     Scanned disks are ignored if they are mounted or have been
         previously skipped by add_storage.py.
 
-    The file system type for disks added by add_storage.py is 
+    The file system type for disks added by add_storage.py is
         automatically set to the type selected for the data partition
         at install.
 
@@ -953,9 +1033,10 @@ def usage():
             (if user enables MythTV Storage Groups)
 
     Options:
-    --add_sg:           Create the MythTV Storage Group directories and 
+    --add_sg:           Create the MythTV Storage Group directories and
                            database entries for database backups, TV
-                           recordings, music, streaming, videos and artwork.
+                           recordings, photos, music, streaming, videos
+                           and artwork.
     -h, --help:         Show this help message.
     --new_init:         Erase the list of known disks and rescan.
     --no_destruction:   Will not partition or format the disk.
@@ -969,6 +1050,9 @@ def usage():
                            /data/storage/disk# symlink, and mount the disk.
                            --no_mount is the only option that works with
                            --reconstruct.
+    --reconstruct_sg:   Recreate the MythTV Storage Group directories and
+                           database entries if they don't exist.
+                           No other options work with --reconstruct_sg.
     --report:           Scan disks and print new found disks.
     '''
     print help
@@ -985,6 +1069,7 @@ if __name__ == "__main__":
     install_call = False
     dir_sg = False
     reconstruct = False
+    reconstruct_sg = False
     try:
         os.remove("/tmp/scan_report")
     except:
@@ -1025,9 +1110,14 @@ if __name__ == "__main__":
     if "--reconstruct" in sys.argv:
         reconstruct = True
 
+    if "--reconstruct_sg" in sys.argv:
+        reconstruct_sg = True
+
     if "--double_myth" in sys.argv:
         myth_main(no_mount, install_call, dir_sg)
     elif reconstruct == True:
         reconstruct_mounts(no_mount)
+    elif reconstruct_sg == True:
+        reconstruct_storagegroups()
     else:
-        main(scan_only,destruction,no_mount, install_call, dir_sg)
+        main(scan_only, destruction, no_mount, install_call, dir_sg)
-- 
cgit v0.12

