From 24f531c22f8c07112d4d71ae37f6f65a85f6e815 Mon Sep 17 00:00:00 2001
From: James Meyer <james.meyer@operamail.com>
Date: Thu, 20 Sep 2012 16:10:42 -0500
Subject: LinHES-config:  write out /etc/exports for non-sg systems

---
 abs/core/LinHES-config/PKGBUILD                 |  12 +--
 abs/core/LinHES-config/mv_config.py             |   6 ++
 abs/core/LinHES-config/mv_fileshare.py          | 122 ++++++++++++++++++++----
 abs/core/LinHES-config/mv_install.py            |   3 +-
 abs/core/LinHES-config/myth_settings_wrapper.sh |  15 ++-
 abs/core/LinHES-config/systemconfig.py          |   4 +
 6 files changed, 134 insertions(+), 28 deletions(-)

diff --git a/abs/core/LinHES-config/PKGBUILD b/abs/core/LinHES-config/PKGBUILD
index 2d9e0d6..e1879b9 100755
--- a/abs/core/LinHES-config/PKGBUILD
+++ b/abs/core/LinHES-config/PKGBUILD
@@ -1,6 +1,6 @@
 pkgname=LinHES-config
 pkgver=2.3
-pkgrel=132
+pkgrel=140
 conflicts=(MythVantage-config MythVantage-config-dev LinHES-config-dev LinHes-config )
 pkgdesc="Install and configure your system"
 depends=('bc' 'libstatgrab'  'mysql-python' 'expect' 'curl' 'dnsutils' 'parted'
@@ -125,9 +125,9 @@ build() {
     install -o root -g root  -D -m 0755 blacklist_pcspkr.conf $startdir/pkg/etc/modprobe.d/blacklist_pcspkr.conf
     install -o root -g root  -D -m 0755 blacklist_nouveau.conf $startdir/pkg/etc/modprobe.d/blacklist_nouveau.conf
 }
-md5sums=('5d5a33523956d3e56d8d5913fcdc0855'
+md5sums=('d27979591231e647f59c23ae18a3d37b'
          'f33e1a6f7985091b8d47cbaf7433f90f'
-         '04c72de15178f1577ce40a8cb045930a'
+         '1afacd0337d95f140b2a1777c27f041a'
          '2596460462cf6c889cf8f95485537b20'
          '985891a43f7c4c983eb2a362162f1a0f'
          'fda01259a4bc74d83c9092d338bd247a'
@@ -135,7 +135,7 @@ md5sums=('5d5a33523956d3e56d8d5913fcdc0855'
          'f73d6d6f98839e900cb6685bf5dc4eae'
          '68199e861c2933ccbb84735b9b440157'
          '2a7f3b34e522acfd08283b86c8926aba'
-         '60103d94ada98ac583fea620294f14d3'
+         '4a538bdfbb2833c48bcffb4f82729de6'
          '689b01f7636e09b2f9657c6ce6006ee7'
          'ecf9e5df20683a769c4a8a8f2d65de85'
          'a6faa20d905e2fd92ce79acab044b759'
@@ -160,8 +160,8 @@ md5sums=('5d5a33523956d3e56d8d5913fcdc0855'
          'cc5ee06e2182bb825ca3712a1c1afb64'
          '919909f4d23b9ee3caf71193c042cc40'
          '85101b79d1ee30bff8244c810828033f'
-         'f15d7fef91565494deb0de50bb67a957'
-         '419a2eaa52b8639da1076828a85bf355'
+         '923197a517cb83d77263e8cea8b8c16a'
+         '60611c3f90dbaafde9c41421b2bf759d'
          '2596460462cf6c889cf8f95485537b20'
          '4cba2bb55c6b8e27c57a6171f42d0455'
          '4804aa93aaad3dfcfff08cd9ffd68836'
diff --git a/abs/core/LinHES-config/mv_config.py b/abs/core/LinHES-config/mv_config.py
index fa8f3bd..381f424 100755
--- a/abs/core/LinHES-config/mv_config.py
+++ b/abs/core/LinHES-config/mv_config.py
@@ -13,6 +13,12 @@ NOOPDEBUG="FALSE"
 TEMP_TEMPLATES="/tmp/templates"
 share_exclude_dir=['mysql','srv']
 
+SG_MAP_AUTONFS_SKIP=['media/tv','media/video','media/video_stuff']
+
+MAP_AUTONFS_SKIP=['lost+found','media','backup']
+
+
+
 
 
 SQUASHFILE="/tmp/.squashlist"
diff --git a/abs/core/LinHES-config/mv_fileshare.py b/abs/core/LinHES-config/mv_fileshare.py
index d8866c4..ebb53c7 100755
--- a/abs/core/LinHES-config/mv_fileshare.py
+++ b/abs/core/LinHES-config/mv_fileshare.py
@@ -2,10 +2,12 @@
 import logging, os, re
 import mv_common
 import glob
+from urllib2 import Request, urlopen, URLError, HTTPError
 #client side
 
 def setup_nfs_client(systemconfig,data_config):
     nfslist=[]
+    nfsmap_serverfile ="nfsmap"
     logging.info("____Start of setup_nfs__client____")
     scrubnfs(systemconfig.get("TEMPLATES"))
 
@@ -17,22 +19,28 @@ def setup_nfs_client(systemconfig,data_config):
             if not os.path.exists(nfsmap_file):
                 logging.debug("    Couldn't find local %s",nfsmap_file)
                 logging.info("    Trying to download nfsmap from MBE")
-                nfsmap_file = download_nfsmap(systemconfig.get("dbhost"))
+                nfsmap_file = download_nfsmap(systemconfig.get("dbhost"),nfsmap_serverfile)
             nfslist = process_nfsmap_file(nfsmap_file)
+
         # if it's an ip  parse ip and download file
         elif re.search(systemconfig.get("NFSserver"),":nfsmap"):
             ip=systemconfig.get("NFSserver").split(":")[0]
-            nfsmap_file = download_nfsmap(ip)
+            nfsmap_file = download_nfsmap(ip,nfsmap_serverfile)
             nfslist = process_nfsmap_file(nfsmap_file)
         #else treat it as a single mount point
         else:
             item = (systemconfig.get("NFSserver") , systemconfig["NFSmount"])
             nfslist.append(item)
     else:
-        #if standalone or slave try to use MBE
+        #if frontend_only or slave try to use MBE  nfs_map_auto
         if systemconfig.get("SystemType") == "Frontend_only" or systemconfig["SystemType"] == "Slave_Backend":
-            item = (systemconfig.get("dbhost")+":"+ data_config.DATAMOUNT, data_config.DATAMOUNT)
-            nfslist.append(item)
+            logging.info("     Central NFS not found, trying to use MBE as nfs server")
+            nfsmap_serverfile = "nfsmap_auto"
+            ip=systemconfig.get("dbhost")
+            nfsmap_file = download_nfsmap(ip,nfsmap_serverfile)
+            nfslist = process_nfsmap_file(nfsmap_file)
+
+
     setup_nfs_fstab(nfslist)
     logging.info("__End of nfs\n")
 
@@ -61,10 +69,11 @@ def scrubnfs(templates):
     cmd='''sed '/^#STARTSCRUB.*$/,/^#ENDSCRUB.*$/d' %s/fstab.conf.template > /etc/fstab''' %templates
     mv_common.runcmd(cmd)
 
-def download_nfsmap(ip):
+def download_nfsmap(ip,nfsmap_serverfile):
     nfsmap_file="/tmp/nfsmap"
-    myurl="http://%s:1337/templates/nfsmap" %ip
+    myurl="http://%s:1337/templates/%s" %(ip,nfsmap_serverfile)
     req = Request(myurl)
+
     try:
         f = urlopen(req)
         logging.info("    downloading %s", myurl)
@@ -84,6 +93,15 @@ def download_nfsmap(ip):
 
     return nfsmap_file
 
+def setup_nfs_mkdir(nfs_dirname):
+    logging.info("    Creating NFS mount point %s" , nfs_dirname)
+    try:
+        os.makedirs(nfs_dirname)
+    except:
+        logging.debug("   Failed creating  nfs mount point")
+
+
+
 def setup_nfs_fstab(nfslist):
     logging.info("    Adding nfs paths to fstab")
     try:
@@ -92,6 +110,7 @@ def setup_nfs_fstab(nfslist):
         f.write(line)
         for s, m in nfslist:
             line = "%s %s    nfs \n" %(s,m)
+            setup_nfs_mkdir(m)
             logging.debug("    %s",line)
             f.write(line)
         line = "#ENDSCRUB\n"
@@ -252,7 +271,7 @@ def scan_for_shares():
     share_list = []
     for conf_file in file_list:
         try:
-            logging.debug("    mv_advanced: reading in %s" %conf_file)
+            logging.debug("    mv_fileshare: reading in %s" %conf_file)
             config.read(conf_file)
             shareable = config.get('storage','shareable')
             if shareable == "True" :
@@ -261,24 +280,87 @@ def scan_for_shares():
                 if config.get('storage','mmount') == "True" :
                     share_list.append("/myth")
         except:
-            logging.debug("    mv_advanced: Couldn't open %s for reading" %conf_file)
+            logging.debug("    mv_fileshare: Couldn't open %s for reading" %conf_file)
 
     return share_list
 
-def setup_etc_exports(shares,data_config):
+def setup_etc_exports(shares,data_config,servername):
     #read in /etc/ exports
     conf_file="/etc/exports"
     excludes = data_config.share_exclude_dir
+    nfsmap_sg_auto_skip = data_config.SG_MAP_AUTONFS_SKIP
+    nfsmap_auto_skip = data_config.MAP_AUTONFS_SKIP
+
+    #print nfsmap_auto_skip
+    nfs_auto_line ="%s:%s %s"
+
+
+    nfs_map_auto = []
+
+    share_list = []
+
+
+    #create the lists for both /etc/exports and nfs_map_auto
+    for share in shares:
+        if share == "/myth":
+            continue
+        os.chdir(share)
+        file_list=glob.glob("*")
+        for dirname in file_list:
+            if os.path.isdir(dirname):
+                tempname = "%s/%s" %(share,dirname)
+                if dirname not in nfsmap_auto_skip:
+                    share_list.append(tempname)
+                    if dirname not in nfsmap_sg_auto_skip:
+                        templine = nfs_auto_line %(servername,tempname,tempname)
+                        nfs_map_auto.append(templine)
+
+
+        file_list=glob.glob("media/*")
+        for dirname in file_list:
+            if os.path.isdir(dirname):
+                tempname = "%s/%s" %(share,dirname)
+                if dirname not in nfsmap_auto_skip:
+                    share_list.append(tempname)
+                    if dirname not in nfsmap_sg_auto_skip:
+                        templine = nfs_auto_line %(servername,tempname,tempname)
+                        nfs_map_auto.append(templine)
+
+
+
+
     new_exports=[]
-    new_exports.append("#This file was generated by systemconfig.py -m advanced")
+    new_exports.append("#This file was generated by systemconfig.py -m fileshare")
     new_exports.append("#Use exportfs -arv to reread. \n\n")
+    #for exclude in excludes:
+    #    share_line='''/%s   *(noaccess)''' %exclude
+    #    new_exports.append(share_line)
 
-    for share in shares:
+    for share in share_list:
         share_line='''%s   *(rw,all_squash,anonuid=1000,anongid=1000,no_subtree_check)''' %share
         new_exports.append(share_line)
-        for exclude in excludes:
-            share_line='''/%s   *(noaccess)''' %exclude
-            new_exports.append(share_line)
+        #for exclude in excludes:
+            #share_line='''/%s   *(noaccess)''' %exclude
+            #new_exports.append(share_line)
+
+    #write out nfs_map_auto
+    try:
+
+        logging.info("    writing nfsmap_auto")
+        try:
+            os.makedirs("%s/templates/" %data_config.MYTHHOME)
+        except:
+            pass
+        conf_file_nfs = "%s/templates/nfsmap_auto" %(data_config.MYTHHOME)
+        f=open(conf_file_nfs,'w')
+        for line in nfs_map_auto:
+            f.write(line)
+            f.write("\n")
+            logging.debug("%s" %line)
+        f.close()
+    except:
+        logging.info("*   error writing /data/srv/htdocs/nfsmap_auto")
+
     try:
         logging.info("    writing /etc/exports")
         f=open(conf_file,'w')
@@ -292,17 +374,19 @@ def setup_etc_exports(shares,data_config):
 
 
 
-def setup_NFSshares(UseNFS,templatefile,data_config):
+def setup_NFSshares(UseNFS,templatefile,data_config,servername):
     if UseNFS == "1":
         logging.info("    Activating NFS server")
         mv_common.pacinstall("nfs-utils")
         mv_common.pacinstall("rpcbind")
         shares = scan_for_shares()
-        setup_etc_exports(shares,data_config)
+        setup_etc_exports(shares,data_config,servername)
 
         mv_common.add_service("nfsd")
         mv_common.add_service("nfs-common")
         mv_common.add_service("rpcbind")
+        cmd="exportfs -arv"
+        mv_common.runcmd(cmd)
     else:
         logging.info("    Removing NFS server")
         mv_common.remove_service("nfsd")
@@ -328,7 +412,7 @@ def setup_fileshare(systemconfig,data_config):
     setup_nfs_client(systemconfig,data_config)
     #server
     setup_NFSshares(systemconfig.get("UseNFS"),
-                    systemconfig.get("TEMPLATES")+"/exports.template",data_config)
+                    systemconfig.get("TEMPLATES")+"/exports.template",data_config,systemconfig.get("hostname"))
     setup_samba(systemconfig,data_config)
 
-    logging.info("__End of advanced configuration\n")
+    logging.info("__End of fileshare configuration\n")
diff --git a/abs/core/LinHES-config/mv_install.py b/abs/core/LinHES-config/mv_install.py
index ccfcb8e..54f4051 100755
--- a/abs/core/LinHES-config/mv_install.py
+++ b/abs/core/LinHES-config/mv_install.py
@@ -1156,7 +1156,8 @@ def double_mount(fe_only=False, upgrade=False):
             if fe_only == True:
                 cmd = "chroot " + data_config.MOUNTPOINT +" /usr/LH/bin/add_storage.py --double_myth --no_mount --fe_only"
             else:
-                cmd = "chroot " + data_config.MOUNTPOINT +" /usr/LH/bin/add_storage.py --double_myth --no_mount"
+                #cmd = "chroot " + data_config.MOUNTPOINT +" /usr/LH/bin/add_storage.py --double_myth --no_mount"
+                cmd = "chroot " + data_config.MOUNTPOINT +" /usr/LH/bin/add_storage.py --double_myth "
 
         if runcmd(cmd)[0] == 0:
             logging.debug("    Add storage worked, breaking out of loop")
diff --git a/abs/core/LinHES-config/myth_settings_wrapper.sh b/abs/core/LinHES-config/myth_settings_wrapper.sh
index 7bf8782..90d41de 100644
--- a/abs/core/LinHES-config/myth_settings_wrapper.sh
+++ b/abs/core/LinHES-config/myth_settings_wrapper.sh
@@ -1,4 +1,5 @@
 #!/bin/bash
+MYTH_RUN_STATUS="1"
 . /etc/profile
 . /etc/systemconfig
 shopt -s -o nounset
@@ -103,6 +104,9 @@ do
                             echo "template is hostsettings"
                             Thistemplate=syssettings
                             loadhost=true
+                    elif [  x$Thistemplate = x"custom"  ]
+                    then
+                            echo "template is custom"
                     else
                             echo "invalid template name"
                             exit 1
@@ -171,8 +175,15 @@ case $OPERATION in
         ;;
 
     load)
-        #this loads the distro default file, it's not active by any hosts
-        mythutil --import-settings --infile $TEMPLATES/$Thistemplate/distro_default.xml
+        if [ x$Thistemplate = "xcustom" ]
+        then
+            define_xml $5 $hostname settings $6
+            load_xml $gen_xml > /tmp/load_xml_custom.log
+
+        else
+            #this loads the distro default file, it's not active by any hosts
+            mythutil --import-settings --infile $TEMPLATES/$Thistemplate/distro_default.xml
+        fi
         ;;
 
 
diff --git a/abs/core/LinHES-config/systemconfig.py b/abs/core/LinHES-config/systemconfig.py
index d2596e7..73cb45b 100755
--- a/abs/core/LinHES-config/systemconfig.py
+++ b/abs/core/LinHES-config/systemconfig.py
@@ -281,6 +281,10 @@ def main(argv):
         import mv_vnc
         mv_vnc.setupvnc(systemconfig,data_config)
 
+    if cmdmodule["fileshare"]:
+        import mv_fileshare
+        mv_fileshare.setup_fileshare(systemconfig, data_config)
+
 
 
 if __name__ == "__main__":
-- 
cgit v0.12