From d1759e01708595072a4f095ed4321066740d3687 Mon Sep 17 00:00:00 2001
From: James Meyer <james.meyer@operamail.com>
Date: Mon, 27 Aug 2012 15:04:54 -0500
Subject: LinHES-system: new features for add_storage.py - now writes out conf
 files in /etc/storage.d  for each disk that it adds.  These files will be
 used to determine nfs & smb shares - enabled the autoadding of the disk to
 storage groups - added --double_myth.  This option is used to find the third
 partiton of the install drive and treat it like any other storage auto_added.
  The one thing it does differently is that no format/reparition take place

---
 abs/core/LinHES-system/PKGBUILD           |  10 +-
 abs/core/LinHES-system/add_storage.py     | 198 ++++++++++++++++++++++++------
 abs/core/LinHES-system/add_storage.readme |   3 +
 3 files changed, 168 insertions(+), 43 deletions(-)
 create mode 100644 abs/core/LinHES-system/add_storage.readme

diff --git a/abs/core/LinHES-system/PKGBUILD b/abs/core/LinHES-system/PKGBUILD
index 3da9578..6e02fdb 100755
--- a/abs/core/LinHES-system/PKGBUILD
+++ b/abs/core/LinHES-system/PKGBUILD
@@ -1,6 +1,6 @@
 pkgname=LinHES-system
 pkgver=2
-pkgrel=57
+pkgrel=59
 arch=('i686' 'x86_64')
 MVDIR=$startdir/pkg/usr/LH
 BINDIR=$startdir/pkg/usr/bin
@@ -26,7 +26,7 @@ binfiles="LinHES-start optimize_mythdb.py
  msg_client.py msg_daemon.py gen_is_xml.py gen_lib_xml.py"
 
 source=(LinHES-session LinHES-profile.sh $binfiles alsa-base myth_mtc.lr
- diskspace cacheclean.cron readme_is_xml)
+ diskspace cacheclean.cron readme_is_xml add_storage.readme)
 
 build() {
         cd $startdir/src
@@ -51,6 +51,7 @@ build() {
         install -m644 -D $startdir/src/alsa-base $startdir/pkg/etc/modprobe.d/alsa-base.conf
         install -m644 -D $startdir/src/readme_is_xml $startdir/pkg/etc/gen_is_xml.d/readme_is_xml
         install -m644 -D $startdir/src/readme_is_xml $startdir/pkg/etc/gen_gen_xml.d/readme_gen_xml
+        install -m644 -D $startdir/src/add_storage.readme $startdir/pkg/etc/storage.d/readme
        # install -m755 -D $startdir/src/smolt.cron $startdir/pkg/etc/cron.weekly/smolt.cron
         install -m755 -D $startdir/src/cacheclean.cron $startdir/pkg/etc/cron.weekly/cacheclean.cron
         install -m755 -D $startdir/src/diskspace $startdir/pkg/etc/cron.tenminutes/diskspace
@@ -77,7 +78,7 @@ md5sums=('4921a6e21c71b263b8d23c51c24b939c'
          '9c2294ccfd5359583497a6b03d918a27'
          '859a80ddb4c234506379c605114c1343'
          '47e093e8cfe4b5b96602358e1f540832'
-         '6faeba0aeb38e772121f751cabda8683'
+         '2b311d0121d4f26c6d6a35e6e612ee97'
          'b8f0414427c5d9afdc32ceb3ae56f82a'
          '6519d209e151cdc39ae7e1091d189c3e'
          'd1cdf28fcb1c88f0e504905771dd6040'
@@ -92,4 +93,5 @@ md5sums=('4921a6e21c71b263b8d23c51c24b939c'
          'f1870a9522c79e6b248fcbf81dec3280'
          '84492954db16740f949d795b74383189'
          '02c810c2f47b7c4495fdacaf54189473'
-         '1e2ca4dd872a50df6b70528e06df558a')
+         '1e2ca4dd872a50df6b70528e06df558a'
+         'da432d68962e109e09beb2b71c8c8571')
diff --git a/abs/core/LinHES-system/add_storage.py b/abs/core/LinHES-system/add_storage.py
index 28d679c..db0d9af 100644
--- a/abs/core/LinHES-system/add_storage.py
+++ b/abs/core/LinHES-system/add_storage.py
@@ -11,23 +11,24 @@ import pickle
 import commands
 import sys,os
 import random, string
+import ConfigParser
 from MythTV import MythDB, MythBE, Recorded, MythError
 from socket import timeout, gethostname
 
 
-
-pickle_file="/usr/LH/etc/storage.pkl"
+storage_dir = "/etc/storage.d"
+pickle_file = "%s/storage.pkl" %storage_dir
 
 
 
 SG_MAP={ 'Default'    :'tv',
-  'LiveTV'     :'live_tv',
+  'LiveTV'     :'tv/live',
   'Videos'     :'video',
-  'Trailers'   :'video/trailers',
-  'Coverart'   :'video/converart',
-  'Fanart'     :'video/fanart',
-  'Banners'    :'video/banners',
-  'Screenshots':'video/screenshots',
+  'Trailers'   :'video_stuff/trailers',
+  'Coverart'   :'video_stuff/converart',
+  'Fanart'     :'video_stuff/fanart',
+  'Banners'    :'video_stuff/banners',
+  'Screenshots':'video_stuff/screenshots',
   'DB Backups' :'db_backups'}
 FS_LIST=[]
 for key in SG_MAP.keys():
@@ -36,15 +37,19 @@ for key in SG_MAP.keys():
 
 
 class disk_device:
-    def  __init__(self,device):
+    def  __init__(self,device,storage_dir):
         device_obj = bus.get_object("org.freedesktop.UDisks", device)
         device_props = dbus.Interface(device_obj, dbus.PROPERTIES_IFACE)
+        self.storage_dir = storage_dir
+        self.top_mount_dir = "/storage"
+        self.config = ConfigParser.RawConfigParser()
         self.fs_map = self.get_fsmap()
         self.is_device = self.get_is_device(device_props)
         self.vendor = self.get_vendor(device_props)
         self.model = self.get_model(device_props)
 
-        self.mount_path = self.get_mounts(device_props)
+        self.mmount = False
+        self.f = self.get_mounts(device_props)
         self.is_mounted = self.get_is_mounted(device_props)
 
         self.parition_size = self.get_partition_size(device_props)
@@ -57,7 +62,14 @@ class disk_device:
         self.device_file_path = self.get_device_file_path(device_props)
         self.block_partition="%s1" %self.block_path
         self.in_use = self.get_in_use()
+        self.uuid=''
+        self.new_mount_point=''
+
+    def set_partition(self,partition):
+        self.block_partition = "%s3" %self.block_path
 
+    def set_mmount(self,flag):
+        self.mmount = flag
 
     def get_is_device(self,device_props):
         return device_props.Get('org.freedesktop.UDisks.Device', "DeviceIsDrive")
@@ -74,6 +86,11 @@ class disk_device:
     def get_is_mounted(self,device_props):
        return device_props.Get('org.freedesktop.UDisks.Device', "DeviceIsMounted")
 
+    def get_name(self):
+        filename="%s_%s" %(self.model.replace(' ',''),
+                                self.serial_number.replace(' ',''))
+        return filename
+
     def get_in_use(self):
         in_use = False
         for i in self.fs_map:
@@ -82,7 +99,6 @@ class disk_device:
                 break
         return in_use
 
-
     def get_partition_size(self,device_props):
         return device_props.Get('org.freedesktop.UDisks.Device', "PartitionSize")
 
@@ -181,7 +197,7 @@ class disk_device:
         if uuid == '':
             print "Could not find a UUID for device: %s" %partition
             sys.exit(1)
-
+        self.uuid = uuid.strip()
         return uuid.strip()
 
     def read_fstab(self):
@@ -211,13 +227,13 @@ class disk_device:
         fstab=self.read_fstab()
 
         #determine mount_path
-        self.new_mount_point="/data/media/%s_%s" %(self.model.replace(' ',''),self.serial_number.replace(' ',''))
+        self.new_mount_point="%s/%s_%s" %(self.top_mount_dir,self.model.replace(' ',''),self.serial_number.replace(' ',''))
 
         #determine options
         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
@@ -260,25 +276,65 @@ class disk_device:
                 cmd="chmod -R 775 /%s" %self.new_mount_point
                 runcmd(cmd)
 
-    def add_sg(self,DB,host,SG_MAP):
+    def add_sg(self,DB,host,SG_MAP,weight='0'):
         print "    Adding to storage groups"
+        sgweight=int(weight)
         for key in SG_MAP.iterkeys():
             #print key," : ", SG_MAP[key]
-            #try:
-                gn=key
-                hn=host
-                dn="%s/%s" %(self.new_mount_point,SG_MAP[key])
-                #print dn
-                #print gn
-                #print hn
-                with DB as c:
+            gn=key
+            hn=host
+            dn="%s/%s" %(self.new_mount_point,SG_MAP[key])
+            #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:
                     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)
+                        #("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 inserting %s into storage groups" %dn
+                        print "        *Error setting storage group weight %s for %s\n" %(sgweight,gn)
+
+        return
+
+    def write_config(self):
+        print "    Writing out storage.d conf file"
+        self.config.add_section('storage')
+        self.config.set('storage','uuid',self.uuid)
+        self.config.set('storage','mountpoint',self.new_mount_point)
+        self.config.set('storage','shareable','True')
+        self.config.set('storage','mmount',self.mmount)
+
+
+        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:
+            self.config.write(configfile)
         return
 
+    def symlink(self):
+        print "    Creating symlink"
+        cmd = "ln -s %s %s/myth " %(self.new_mount_point, self.top_mount_dir)
+        runcmd(cmd)
+
+
+
+#end of class
 
 
 def runcmd(cmd):
@@ -300,7 +356,7 @@ def scan_system():
     ud_manager = dbus.Interface(ud_manager_obj, 'org.freedesktop.UDisks')
     current_drive_list=[]
     for dev in ud_manager.EnumerateDevices():
-        drive = disk_device(dev)
+        drive = disk_device(dev,storage_dir)
 
         if drive.is_device and drive.device_size > 5000 and not drive.is_optical :
             current_drive_list.append(drive)
@@ -333,7 +389,7 @@ def search_for_match(system_drive,known_drive_list):
             y_drive_hash = "%s_%s_%s" %(y.model,y.parition_size,y.device_file_path)
             if system_drive_hash == y_drive_hash :
                 match_drive = True
-                print "No serial number was found, matched using hash method: %s" %system_drive.model
+                print "\n*   No serial number was found, matched using hash method: %s" %system_drive.model
                 break
 
         elif y.serial_number == system_drive.serial_number:
@@ -347,7 +403,7 @@ def search_for_match(system_drive,known_drive_list):
 def prompt_to_add(current_drive):
     loop = True
     while loop:
-        str1 = raw_input("     Adding the drive will remove all contents on the drive. \n\nDo you wish enable this drive for MythTV storage(Y/N)?:")
+        str1 = raw_input("\n    Adding the drive will remove all contents on the drive. \n    Do you wish enable this drive for MythTV storage(Y/N)?:")
 
         if str1 in ['Y','N','y','n']:
             loop = False
@@ -361,8 +417,7 @@ def prompt_to_add(current_drive):
 
 def remove_pickle():
     try:
-        print "Resetting list of known drives."
-
+        print "* Resetting list of known drives."
         os.remove(pickle_file)
     except:
         pass
@@ -390,23 +445,27 @@ def main(scan_only):
         #print "--"
 
         if search_for_match(i,known_drive_list) or i.in_use :
-            print "    Storage is already in use or previously skipped: %s location: %s size: %s" %(i.model,i.block_path,i.device_size)
+            print "\n"
+            print "    Storage is already in use or previously skipped:%s" %i.model
+            print "        location: %s" %i.block_path
+            print "        size: %s" %i.device_size
             continue
 
         else:
-            print "\n"
             if not scan_only:
                 print "\n"
                 print "-------------------------------------------------------------"
-
-                print "Found new hard drive: %s location: %s size: %s \n" %(i.model,i.block_path,i.device_size)
+                print "    Found new hard drive: %s" %i.model
+                print "    location: %s" %i.block_path
+                print "    size: %s " %i.device_size
 
                 if prompt_to_add(i) :
-                    print "\nDisk will be added to the storage pool!"
+                    print "\n    Disk will be added to the storage pool!"
                     process_list.append(i)
             else:
                 process_list.append(i)
-
+    print "    End of scan"
+    print "---------------------------------------"
     if scan_only:
         if len(process_list) > 0:
             f = open('/tmp/scan_report', 'w')
@@ -427,20 +486,78 @@ def main(scan_only):
 
     #save new list to disk_device
     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 "-----"
+
+        #i.add_sg(DB,host,SG_MAP)
+
+def myth_main():
+    global bus
+    bus = dbus.SystemBus()
+    #search for root
+    f = open('/etc/fstab', 'r')
+    fstab=f.readlines()
+    f.close()
+    for i in fstab:
+        split_line=i.split()
+        if not split_line:
+            continue
+
+        try:
+            if split_line[1] ==  "/" :
+                uuid_device = split_line[0]
+                break
+        except:
+            print "Couldn't find / in fstab"
+            sys.exit(1)
+    if uuid_device.startswith("UUID"):
+        uuid_device = uuid_device.split("=")[1]
+        cmd = "blkid -U %s" %uuid_device
+        device = runcmd(cmd)[1]
+    else:
+        device = uuid_device.strip()
+    #should have something like /dev/sda1
+    #remove all the digits
+    device = ''.join([letter for letter in device if not letter.isdigit()])
+
+
+    system_drive_list = scan_system()
+    for i in system_drive_list:
+        if i.block_path == device:
+            break
+    else:
+        print "Could'nt find root device in block list"
+        sys.exit(1)
+
+    DB = MythDB()
+    host=gethostname()
+
+    print "    Drive: %s" %(i.get_name())
+    i.set_mmount(True)
+    i.set_partition("3")
+    i.add_fstab()
+    i.mount_disk()
+    i.write_config()
+    i.add_sg(DB,host,SG_MAP,'99')
+    i.symlink()
+
 
 
 
 
 if __name__ == "__main__":
     scan_only = False
+    myth_mount = False
     try:
         os.remove("/tmp/scan_report")
     except:
@@ -455,7 +572,10 @@ if __name__ == "__main__":
     if "--report" in sys.argv :
         scan_only = True
 
-    main(scan_only)
+    if "--double_myth" in sys.argv:
+        myth_main()
+    else:
+        main(scan_only)
 
 
 
diff --git a/abs/core/LinHES-system/add_storage.readme b/abs/core/LinHES-system/add_storage.readme
new file mode 100644
index 0000000..226b14d
--- /dev/null
+++ b/abs/core/LinHES-system/add_storage.readme
@@ -0,0 +1,3 @@
+Files here are autogenerated by add_storage.py.
+They will be used by systemconfig to generate nfs,smb and mountpoint recovery.
+
-- 
cgit v0.12

