From f2789b027e189c7daedbb42fa5bd47204defc862 Mon Sep 17 00:00:00 2001
From: James Meyer <james.meyer@operamail.com>
Date: Wed, 29 Aug 2012 13:47:24 -0500
Subject: LinHES-system: add_storage.py   added multiple new options including
 --no_destruction and --no_mount

---
 abs/core/LinHES-system/add_storage.py | 149 +++++++++++++++++++++++++---------
 1 file changed, 109 insertions(+), 40 deletions(-)

diff --git a/abs/core/LinHES-system/add_storage.py b/abs/core/LinHES-system/add_storage.py
index 626640e..367106e 100644
--- a/abs/core/LinHES-system/add_storage.py
+++ b/abs/core/LinHES-system/add_storage.py
@@ -25,7 +25,7 @@ SG_MAP={ 'Default'    :'tv',
   'LiveTV'     :'tv/live',
   'Videos'     :'video',
   'Trailers'   :'video_stuff/trailers',
-  'Coverart'   :'video_stuff/converart',
+  'Coverart'   :'video_stuff/coverart',
   'Fanart'     :'video_stuff/fanart',
   'Banners'    :'video_stuff/banners',
   'Screenshots':'video_stuff/screenshots',
@@ -259,13 +259,14 @@ class disk_device:
         f.close()
         return
 
-    def mount_disk(self):
+    def mount_disk(self,mount=True):
         try:
             os.stat(self.new_mount_point)
         except:
             os.makedirs(self.new_mount_point)
-        cmd = "mount %s" %self.new_mount_point
-        runcmd(cmd)
+        if mount == True:
+            cmd = "mount %s" %self.new_mount_point
+            runcmd(cmd)
         return
 
     def mkdirs(self,FS_LIST):
@@ -281,7 +282,7 @@ class disk_device:
                 cmd="chmod -R 775 /%s" %self.new_mount_point
                 runcmd(cmd)
 
-    def add_sg(self,DB,host,SG_MAP,weight='0'):
+    def add_sg(self,DB,host,SG_MAP,weight='0',install_call=False):
         print "    Adding to storage groups"
         sgweight=int(weight)
         for key in SG_MAP.iterkeys():
@@ -292,25 +293,28 @@ class disk_device:
             #print dn
             #print gn
             #print hn
-            with DB as c:
-                try:
-                    c.execute("""insert into storagegroup (groupname,hostname,dirname) values (%s,%s,%s)""",(gn,hn,dn))
-                    print "        Adding  location: %s  to storagegroup %s" %(dn,gn)
-                except:
-                    print "        *Error inserting %s into storage groups" %dn
-
-                if sgweight > 0:
+            if install_call == True :
+                print "Will write SG for stuff after the fact"
+            else:
+                with DB as c:
                     try:
-                        #("SGweightPerDir:server2:/mnt/video", 99, "server2");
-                        sgw="SGweightPerDir:%s:%s" %(hn,dn)
-                        #print sgw
-                        #print sgweight
-                        #print hn
-
-                        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)
+                        c.execute("""insert into storagegroup (groupname,hostname,dirname) values (%s,%s,%s)""",(gn,hn,dn))
+                        print "        Adding  location: %s  to storagegroup %s" %(dn,gn)
                     except:
-                        print "        *Error setting storage group weight %s for %s\n" %(sgweight,gn)
+                        print "        *Error inserting %s into storage groups" %dn
+
+                    if sgweight > 0:
+                        try:
+                            #("SGweightPerDir:server2:/mnt/video", 99, "server2");
+                            sgw="SGweightPerDir:%s:%s" %(hn,dn)
+                            #print sgw
+                            #print sgweight
+                            #print hn
+
+                            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)
 
         return
 
@@ -336,6 +340,8 @@ class disk_device:
         print "    Creating symlink"
         cmd = "ln -s %s %s/myth " %(self.new_mount_point, self.top_mount_dir)
         runcmd(cmd)
+        cmd = "ln -s %s %s/data " %(self.new_mount_point, self.top_mount_dir)
+        runcmd(cmd)
 
 
 
@@ -420,6 +426,23 @@ def prompt_to_add(current_drive):
         rc = False
     return rc
 
+def prompt_to_continue():
+    loop = True
+    #while loop:
+    str1 = raw_input("\n    Ready to start, press Y to continue, anything else to abort:")
+
+        #if str1 in ['Y','N','y','n']:
+        #    loop = False
+        #    break
+        #print "\n"
+    if str1 == 'Y' or str1 == 'y':
+        rc = True
+    else:
+        rc = False
+    return rc
+
+
+
 def remove_pickle():
     try:
         print "* Resetting list of known drives."
@@ -430,7 +453,7 @@ def remove_pickle():
 
 #--------------------------------------------
 
-def main(scan_only):
+def main(scan_only, destruction, no_mount, install_call):
     global bus
     bus = dbus.SystemBus()
 
@@ -493,20 +516,23 @@ def main(scan_only):
     write_known_drive_list(system_drive_list)
     if len(process_list) > 0:
         print "\n    Adding storage: \n"
-    for i in process_list:
-        print "    Drive: %s" %(i.get_name())
-        i.partition_disk()
-        i.format_disk()
-        i.add_fstab()
-        i.mount_disk()
-        i.mkdirs(FS_LIST)
-        i.write_config()
-        i.add_sg(DB,host,SG_MAP)
-        print "-----"
+    if prompt_to_continue() == True:
+        for i in process_list:
+            print "    Drive: %s" %(i.get_name())
+            if destruction == True:
+                i.partition_disk()
+                i.format_disk()
+            i.add_fstab()
+            i.mount_disk(no_mount)
+            if destruction == True:
+                i.mkdirs(FS_LIST)
+            i.write_config()
+            i.add_sg(DB,host,SG_MAP)
+            print "-----"
 
         #i.add_sg(DB,host,SG_MAP)
 
-def myth_main():
+def myth_main(no_mount,install_call):
     global bus
     bus = dbus.SystemBus()
     #search for root
@@ -543,26 +569,57 @@ def myth_main():
     else:
         print "Could'nt find root device in block list"
         sys.exit(1)
+    if not install_call == True:
+        DB = MythDB()
+        host=gethostname()
+    else:
+        DB = None
+        host=gethostname()
 
-    DB = MythDB()
-    host=gethostname()
 
     print "    Drive: %s" %(i.get_name())
     i.set_mmount(True)
     i.set_partition("3")
     i.add_fstab(True)
-    i.mount_disk()
+    #if not install_call:
+    i.mount_disk(no_mount)
     i.write_config()
-    i.add_sg(DB,host,SG_MAP,'99')
+    i.add_sg(DB,host,SG_MAP,'99',install_call)
     i.symlink()
 
 
+def usage():
+    help='''\n
+    Add storage is designed to find and setup new disks for mythtv usage.
+    It's a powerfull that could destroy data if not used correctly, so please be careful.
+    Normal operations include (in this order):
+        Partition the disk
+        Format the disk
+        Add disk to /etc/fstab
+        Mount the disk
+        Create the directory
+        Write out the config file to /etc/storage.d
+        Add new locations to mythtv storage groups
+
+    options:
+
+    --no_mount :  Do not mount the disk, only add it to /etc/fstab and create the dir.
+    --no_destruction:  Will not  partition or format the disk
+    --new_init :  Erase the list of new disks and rescan.
+    --report :  will scan the disks and print out if it found new storage.
+    '''
+    print help
+    sys.exit(0)
+
 
 
 
 if __name__ == "__main__":
     scan_only = False
     myth_mount = False
+    no_mount = False
+    destruction = True
+    install_call = False
     try:
         os.remove("/tmp/scan_report")
     except:
@@ -571,6 +628,18 @@ if __name__ == "__main__":
     if not os.geteuid()==0:
         sys.exit("\nRoot access is required to run this program\n")
 
+    if "--help" in sys.argv:
+        usage()
+
+    if "--install_call" in sys.argv:
+        install_call = True
+
+    if "--no_mount" in sys.argv :
+        no_mount = True
+
+    if "--no_destruction" in sys.argv:
+        destruction = False
+
     if "--new_init" in sys.argv :
         remove_pickle()
 
@@ -578,9 +647,9 @@ if __name__ == "__main__":
         scan_only = True
 
     if "--double_myth" in sys.argv:
-        myth_main()
+        myth_main(no_mount,install_call)
     else:
-        main(scan_only)
+        main(scan_only,destruction,no_mount, install_call)
 
 
 
-- 
cgit v0.12

