From 841af294b3de32594fc380a54df1938117f976e8 Mon Sep 17 00:00:00 2001
From: James Meyer <james.meyer@operamail.com>
Date: Sat, 20 Jun 2009 20:37:54 -0500
Subject: linhes-config: setup_mysql

---
 abs/core-testing/LinHES-config/mv_hostype.py | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/abs/core-testing/LinHES-config/mv_hostype.py b/abs/core-testing/LinHES-config/mv_hostype.py
index fff0e8a..093ed5a 100755
--- a/abs/core-testing/LinHES-config/mv_hostype.py
+++ b/abs/core-testing/LinHES-config/mv_hostype.py
@@ -39,6 +39,7 @@ def setup_db():
 def setup_mysqlnetwork():
     logging.debug("    Setting up mysql network")
 
+
 def setup_mysql():
     logging.debug("    Configuring mysql")
 
@@ -71,6 +72,7 @@ def hostypeprint(systemconfig):
         logging.info("Stand alone system being configured")
         setup_ntp(False,"null")
         remove_avahi_service()
+        setup_mysqlnetwork(False)
         if systemconfig["hostypec"]:
             setup_db()
         install_list=("mysql", "mythdb-initial", "avahi", "portmap", "nfs-utils", "local-website", "myth2ipod", "mythtv-status")
@@ -82,6 +84,7 @@ def hostypeprint(systemconfig):
         setup_avahi(systemconfig["TEMPLATES"])
         install_list=("mysql", "mythdb-initial", "avahi", "portmap", "nfs-utils", "local-website", "myth2ipod", "mythtv-status")
         daemon_list=("mysql", "mythbackend", "avahi", "portmap", "nfs-utils", "netfs", "lighttpd")
+        setup_mysqlnetwork(True)
         if data_config.SYSTEMTYPE == "MythVantage":
             install_list.append('func')
             install_list.append('certmaster')
@@ -138,7 +141,7 @@ def hostypeprint(systemconfig):
         mv_common.remove_service("frontend")
         mv_common.remove_service("hal")
 
-    setup_mysqlnetwork()
+
     setup_mysql()
     if data_config.SYSTEMTYPE == "MythVantage":
         setup_func_minion(dbhost)
-- 
cgit v0.12


From c9d85b81497f3036db26d3d481108072ba2ac560 Mon Sep 17 00:00:00 2001
From: James Meyer <james.meyer@operamail.com>
Date: Sun, 21 Jun 2009 18:59:31 -0500
Subject: LinHES-config: several minor fixes and finish some functions that
 slipped through the cracks. introduce remove_file fix slave and fe only
 finish mysql_network_connection finish mysql_network fix ownership for
 .mythtv ir mv_ir fix myth_ip to always report the ip even if dhcp is used

---
 abs/core-testing/LinHES-config/PKGBUILD        |   2 +-
 abs/core-testing/LinHES-config/mv_common.py    |   7 ++
 abs/core-testing/LinHES-config/mv_hostype.py   |  83 ++++++++++++++++--
 abs/core-testing/LinHES-config/mv_ir.py        |  18 +++-
 abs/core-testing/LinHES-config/mv_misc.py      |  13 +--
 abs/core-testing/LinHES-config/mv_network.py   | 113 ++++++++++++++++++++-----
 abs/core-testing/LinHES-config/systemconfig.py |   7 +-
 abs/core-testing/LinHES-config/systemconfig.sh |   2 +
 8 files changed, 206 insertions(+), 39 deletions(-)

diff --git a/abs/core-testing/LinHES-config/PKGBUILD b/abs/core-testing/LinHES-config/PKGBUILD
index 40413ed..277a351 100755
--- a/abs/core-testing/LinHES-config/PKGBUILD
+++ b/abs/core-testing/LinHES-config/PKGBUILD
@@ -1,6 +1,6 @@
 pkgname=LinHES-config
 pkgver=2.0
-pkgrel=15
+pkgrel=17
 conflicts=(MythVantage-config MythVantage-config-dev LinHES-config-dev )
 pkgdesc="Install and configure your system"
 depends=(bc libstatgrab  mysql-python expect curl dnsutils parted sg3_utils nmbscan system-templates rsync python-parted ddcxinfo)
diff --git a/abs/core-testing/LinHES-config/mv_common.py b/abs/core-testing/LinHES-config/mv_common.py
index 31740c3..da3e25f 100755
--- a/abs/core-testing/LinHES-config/mv_common.py
+++ b/abs/core-testing/LinHES-config/mv_common.py
@@ -129,6 +129,13 @@ def pacremove(pkg):
 def getpid(process):
     return commands.getoutput('pidof %s' % process)
 
+def remove_file(filename):
+    logging.debug("    Removing %s", filename)
+    try:
+        os.remove(filename)
+    except:
+        logging.debug("*     Could not remove %s", filename)
+
 
 def restartLCD(RESTART_LCD):
     if  RESTART_LCD :
diff --git a/abs/core-testing/LinHES-config/mv_hostype.py b/abs/core-testing/LinHES-config/mv_hostype.py
index 093ed5a..b3899ff 100755
--- a/abs/core-testing/LinHES-config/mv_hostype.py
+++ b/abs/core-testing/LinHES-config/mv_hostype.py
@@ -29,19 +29,80 @@ def remove_avahi_service():
     if os.path.exists(file):
         logging.debug("    Removing avahi mysql.service file")
         cmd="rm -rf %s" %file
-        runcmd(cmd)
+        mv_common.runcmd(cmd)
         cmd="sudo sv restart avahi"
         mv_common.runcmd(cmd)
 
 def setup_db():
     logging.debug("    Setting up the database")
 
-def setup_mysqlnetwork():
+def setup_mysqlnetwork(EnableNetwork):
     logging.debug("    Setting up mysql network")
+    mysqlconf = "/etc/my.cnf"
+    cmd = '''grep "#skip-networking" %s ''' %mysqlconf
+    status = mv_common.runcmd(cmd)
+
+    if EnableNetwork :
+        logging.debug("    Enabling mysql networking")
+        if status == 0 :
+            logging.debug("    Network is already enabled for mysql")
+        else:
+            cmd = '''sed -ie "s/^skip-networking/#skip-networking/g" %s ''' %mysqlconf
+            mv_common.runcmd(cmd)
+            mv_common.restart_service("mysqld")
+    else:
+        logging.debug("    Disabling mysql networking")
+        if status == 1 :
+            logging.debug("    Network is already disabled for mysql")
+        else:
+            cmd = '''sed -ie "s/#skip-networking/skip-networking/g" %s ''' %mysqlconf
+            mv_common.runcmd(cmd)
+            mv_common.restart_service("mysqld")
+
+def setup_mysql_connection(systemconfig):
+    logging.info("    Configuring database connection")
+    mythip = systemconfig["mythip"]
+    systemtype = systemconfig["SystemType"]
+    mv_root = systemconfig["MVROOT"]
+    mysqltxt = "/usr/share/mythtv/mysql.txt"
+    templates = systemconfig["TEMPLATES"]
+    mysqltemplate = templates + "/mysql.txt"
+    dbhost = systemconfig["dbhost"]
+
+    if systemtype == "Master_backend":
+        logging.debug("    Setting dbhost to %s in systemconfig", mythip)
+        dbhost = mythip
+        cmd = '''sed -ei "s/^dbhost=.*$/dbhost=\"%s\"/" /etc/systemconfig ''' %dbhost
+        mv_common.runcmd(cmd)
+        logging.debug("    Changing dbhost in settings table for master_backend")
+        cmd = '''%s/bin/restore_default_settings.sh -c BECONFIG -s master -a %s ''' %(mv_root, dbhost)
+        mv_common.runcmd(cmd)
+
+    if systemtype == "Slave_backend":
+        slavehost = mythip
+        logging.debug("    Changing slave in settings: %s", slavehost)
+        cmd = '''%s/bin/restore_default_settings.sh -c BECONFIG -s slave  -a %s ''' %(mv_root, slavehost)
+        mv_common.runcmd(cmd)
+
+    if systemtype == "Master_backend":
+        logging.debug("   changing mysql.txt file to use localhost")
+        cmd = '''sed -e "s/^DBHostName=.*$/DBHostName="localhost"/" %s > %s ''' %(mysqltemplate, mysqltxt)
+        mv_common.runcmd(cmd)
+    else:
+        logging.debug("   changing mysql.txt file to use %s", dbhost)
+        cmd = '''sed -e "s/^DBHostName=.*$/DBHostName=%s/" %s > %s ''' %(dbhost, mysqltemplate, mysqltxt)
+        mv_common.runcmd(cmd)
+
+    delfile = data_config.MYTHHOME + "/.mythtv/mysql.txt"
+    mv_common.remove_file(delfile)
+    delfile = mv_root + "/bin/mythtv/.mythtv/mysql.txt"
+    mv_common.remove_file(delfile)
+    filecheck = data_config.MYTHHOME + "/templates/mysql.txt"
+    if os.path.exists(filecheck):
+        logging.info("    Scrapping all the previous work and using %s for mysql.txt", filecheck)
+        mv_common.cp_and_log(filecheck, mysqltxt)
 
 
-def setup_mysql():
-    logging.debug("    Configuring mysql")
 
 def setup_func_key():
     if data_config.SYSTEMTYPE == "MythVantage":
@@ -67,12 +128,14 @@ def hostypeprint(systemconfig):
     remove_list=''
     daemon_list=''
     daemon_remove_list=''
+    run_mysqlnetwork = False
 
     if systemconfig["SystemType"] == "Standalone":
         logging.info("Stand alone system being configured")
         setup_ntp(False,"null")
         remove_avahi_service()
-        setup_mysqlnetwork(False)
+        EnableNetwork = False
+        run_mysqlnetwork = True
         if systemconfig["hostypec"]:
             setup_db()
         install_list=("mysql", "mythdb-initial", "avahi", "portmap", "nfs-utils", "local-website", "myth2ipod", "mythtv-status")
@@ -84,7 +147,8 @@ def hostypeprint(systemconfig):
         setup_avahi(systemconfig["TEMPLATES"])
         install_list=("mysql", "mythdb-initial", "avahi", "portmap", "nfs-utils", "local-website", "myth2ipod", "mythtv-status")
         daemon_list=("mysql", "mythbackend", "avahi", "portmap", "nfs-utils", "netfs", "lighttpd")
-        setup_mysqlnetwork(True)
+        EnableNetwork = True
+        run_mysqlnetwork = True
         if data_config.SYSTEMTYPE == "MythVantage":
             install_list.append('func')
             install_list.append('certmaster')
@@ -142,7 +206,12 @@ def hostypeprint(systemconfig):
         mv_common.remove_service("hal")
 
 
-    setup_mysql()
+    if run_mysqlnetwork:
+        setup_mysqlnetwork(EnableNetwork)
+    else:
+        logging.debug("    Not running setup_mysql_connection")
+
+    setup_mysql_connection(systemconfig)
     if data_config.SYSTEMTYPE == "MythVantage":
         setup_func_minion(dbhost)
         setup_func_key()
diff --git a/abs/core-testing/LinHES-config/mv_ir.py b/abs/core-testing/LinHES-config/mv_ir.py
index efc8a60..b8e9741 100755
--- a/abs/core-testing/LinHES-config/mv_ir.py
+++ b/abs/core-testing/LinHES-config/mv_ir.py
@@ -3,9 +3,17 @@ import logging, mv_common
 import os, re , glob
 from time import time, localtime, strftime
 import time
+import pwd
 
 def setup_lirc_links(mythhome):
     logging.debug("    Creating links for lirc")
+    try:
+        mythuid = pwd.getpwnam('mythtv')[2]
+        mythgid = pwd.getpwnam('mythtv')[3]
+    except:
+        logging.critical("*    mythuid not found")
+        mythuid = '1000'
+        mythgid = '1000'
     if not os.path.exists(mythhome+"/.mythtv"):
         logging.debug("    Creating %s/.mythtv",mythhome)
         try:
@@ -13,7 +21,15 @@ def setup_lirc_links(mythhome):
         except:
             logging.debug("   Couldn't create .mythtv ")
             return
-        os.chown(mythhome+"/.mythtv","mythtv","mythtv")
+        try:
+            os.chown(mythhome+"/.mythtv", mythuid, mythgid)
+            logging.debug("*    Couldn't chown of %s", mythhome)
+        except:
+            cmd = ''' chown -R mythtv %s/.mythtv''' %mythhome
+            mv_common.runcmd(cmd)
+            cmd = ''' chgrp -R mythtv %s/.mythtv''' %mythhome
+            mv_common.runcmd(cmd)
+            pass
 
     if not os.path.exists(mythhome+"/.mythtv/lircrc"):
         logging.debug("    Creating symlink for myth lircrc")
diff --git a/abs/core-testing/LinHES-config/mv_misc.py b/abs/core-testing/LinHES-config/mv_misc.py
index c93cd71..9ea9c95 100755
--- a/abs/core-testing/LinHES-config/mv_misc.py
+++ b/abs/core-testing/LinHES-config/mv_misc.py
@@ -30,12 +30,13 @@ def setup_tz(timezone,TEMPLATES):
     if not timezone:
         timezone="unknown"
     logging.info("    Setting timezone to %s",timezone)
-    try:
-        logging.debug("    Removing /etc/localtime")
-        os.remove("/etc/localtime")
-    except:
-        logging.debug("    Couldn't remove /etc/localtime")
-        pass
+    mv_common.remove_file("/etc/localtime")
+    #try:
+        #logging.debug("    Removing /etc/localtime")
+        #os.remove("/etc/localtime")
+    #except:
+        #logging.debug("    Couldn't remove /etc/localtime")
+        #pass
 
     srclink="/usr/share/zoneinfo/%s" %timezone
     logging.debug("    symlinking %s to /etc/localtime",srclink)
diff --git a/abs/core-testing/LinHES-config/mv_network.py b/abs/core-testing/LinHES-config/mv_network.py
index ae3aaf7..ae7f861 100755
--- a/abs/core-testing/LinHES-config/mv_network.py
+++ b/abs/core-testing/LinHES-config/mv_network.py
@@ -2,20 +2,10 @@
 import sys , os, commands , glob, time, re
 import logging
 import mv_common
+import socket, fcntl, struct, array
 global etcnetdir
 etcnetdir = "/etc/net/ifaces"
 
-
-def setup_MYTH_IP(systemconfig):
-    default_interface = systemconfig["default_interface"]
-    try:
-        defaultip = systemconfig["Hostip"+default_interface]
-    except:
-        logging.debug("    Error occured finding the defaultip")
-        defaultip = "127.0.0.1"
-    logging.debug("    Using %s as default ip", defaultip)
-    return defaultip
-
 def setup_MYTH_DHCP(systemconfig):
     default_interface = systemconfig["default_interface"]
     try:
@@ -23,9 +13,56 @@ def setup_MYTH_DHCP(systemconfig):
     except:
         logging.critical("    *Error occured finding default dhcp")
         defaultdhcp = "0"
-    logging.debug("    Using %s as dhcp value for %s", default_interface, defaultdhcp)
+    logging.debug("    Using %s as dhcp value for %s",  defaultdhcp, default_interface)
     return defaultdhcp
 
+def all_interfaces():
+    max_possible = 128  # arbitrary. raise if needed.
+    bytes = max_possible * 32
+    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
+    names = array.array('B', '\0' * bytes)
+    outbytes = struct.unpack('iL', fcntl.ioctl(
+        s.fileno(),
+        0x8912,  # SIOCGIFCONF
+        struct.pack('iL', bytes, names.buffer_info()[0])
+    ))[0]
+    namestr = names.tostring()
+    return [namestr[i:i+32].split('\0', 1)[0] for i in range(0, outbytes, 32)]
+
+def get_ip(ifname):
+    logging.debug("    Finding ip address for %s", ifname)
+    all_if = all_interfaces()
+    logging.debug("    found interfaces:%s", all_if)
+    if ifname in all_if :
+        logging.debug("    Found %s in all_interfaces", ifname)
+    else:
+        logging.critical("*     Couldn't find %s in list", ifname)
+        ifname = all_if[0]
+        logging.critical("*     Using %s for interface name", ifname)
+
+
+    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
+    return socket.inet_ntoa(fcntl.ioctl(
+        s.fileno(),
+        0x8915,  # SIOCGIFADDR
+        struct.pack('256s', ifname[:15])
+    )[20:24])
+
+def setup_MYTH_IP(systemconfig):
+    default_interface = systemconfig["default_interface"]
+    #check for dhcp in use
+    if setup_MYTH_DHCP(systemconfig) == "0":
+        logging.debug("   dhcp is in use, finding dhcp ip")
+        defaultip = get_ip(default_interface)
+    else:
+        try:
+            defaultip = systemconfig["Hostip"+default_interface]
+        except:
+            logging.debug("    Error occured finding the defaultip")
+            defaultip = "127.0.0.1"
+    logging.debug("    Using %s as default ip", defaultip)
+    return defaultip
+
 def flush(netdev):
     logging.debug("    Flushing %s",netdev)
     cmd = '''ip address flush dev %s''' %netdev
@@ -44,7 +81,7 @@ def kill_dhcp(basedir):
             f.close
             cmd = "kill -9 %s" %pid
             mv_common.runcmd(cmd)
-            os.remove(FILE)
+            mv_common.remove_file(FILE)
         os.chdir(stddir)
     except:
         pass
@@ -68,6 +105,8 @@ def setup_hostname(systemconfig):
         logging.info("    Using default value of me")
         hostname = "me"
     logging.info("    Setting the hostname to %s", hostname)
+    cmd = ''' echo %s > /etc/hostname ''' %hostname
+    mv_common.runcmd(cmd)
     cmd = "cat /etc/hosts | grep -v 127.0.0.1 | grep -v %s > /tmp/hosts" %hostname
     mv_common.runcmd(cmd)
     if systemconfig["mythdhcp"] == "1" :
@@ -193,11 +232,12 @@ def setup_interface(netdev,systemconfig):
         logging.info("    Disabling wireless extensions")
         cmd = '''sed -i -e 's/^CONFIG_WIRLESS=.*$/CONFIG_WIRLESS=no/g' %s/%s/options''' %(etcnetdir, netdev)
         mv_common.runcmd(cmd)
-        try:
-            wpafile=etcnetdir+"/"+netdev+"/wpa_supplicant.conf"
-            os.remove(wpafile)
-        except:
-            logging.debug("    Couldn't remove %s",wpafile)
+        #try:
+        wpafile=etcnetdir+"/"+netdev+"/wpa_supplicant.conf"
+            #os.remove(wpafile)
+        mv_common.remove_file(wpafile)
+        #except:
+            #logging.debug("    Couldn't remove %s",wpafile)
 
     if netinfo["UseDHCP"] == "0" :
         logging.info("    Enabling DHCP support")
@@ -256,10 +296,11 @@ def find_active(systemconfig):
     logging.debug("    _Start of find_active")
 
     if systemconfig["mythdhcp"] == "1":
-        try:
-            os.remove("/etc/resolv.conf")
-        except:
-            logging.debug("    couldn't remove /etc/resolv.conf")
+        mv_common.remove_file("/etc/resolv.conf")
+        #try:
+            #os.remove("/etc/resolv.conf")
+        #except:
+            #logging.debug("    couldn't remove /etc/resolv.conf")
         cmd = ''' echo search lan > /etc/resolv.conf '''
         mv_common.runcmd(cmd)
         cmd = ''' echo nameserver 127.0.0.1 >> /etc/resolv.conf '''
@@ -329,15 +370,43 @@ def stop_network():
         logging.info("    Will not stop network due to netboot/vnc")
     pass
 
+def hostname_change_check(systemconfig):
+    restartfe = False
+    logging.debug("    _Start of hostname_change_check")
+    oldhostname = socket.gethostname()
+    #oldhostname = "crap"
+    newhostname = systemconfig["hostname"]
+    mv_root = systemconfig["MVROOT"]
+    logging.debug("    Old hostname: %s", oldhostname)
+    logging.debug("    New hostname: %s", newhostname)
+    if oldhostname != newhostname :
+        logging.info("    Changing hostname in database to match new hostname")
+        cmd ="%s/bin/restore_default_settings.sh -cuhostname -o -h%s" %(mv_root, oldhostname)
+        mv_common.runcmd(cmd)
+        logging.info("    Changing hostname to %s", newhostname)
+        cmd = "hostname %s" %newhostname
+        mv_common.runcmd(cmd)
+        if systemconfig["SystemType"] != "Frontend_only" :
+            logging.info("    Restarting backend")
+            mv_common.stop_service("mythbackend")
+            mv_common.start_service("mythbackend")
+            restartfe = True
+    else:
+        logging.debug("    old and new hostnames matched, leaving things along")
+    logging.debug("  __End of hostname_change_check")
+    return restartfe
+
 
 
 def setup_network (systemconfig):
     logging.info("____Start of network____")
     logging.info("    Setting up the network")
+    restartfe = hostname_change_check(systemconfig)
     setup_hostname(systemconfig)
     find_active(systemconfig)
     start_network()
     logging.info("__End of network\n")
+    return restartfe
 
 
 
diff --git a/abs/core-testing/LinHES-config/systemconfig.py b/abs/core-testing/LinHES-config/systemconfig.py
index 5ccaa10..4d69a1f 100755
--- a/abs/core-testing/LinHES-config/systemconfig.py
+++ b/abs/core-testing/LinHES-config/systemconfig.py
@@ -100,7 +100,10 @@ def main(argv):
         cmdmodule["smolt"] = True
 
     if cmdmodule["network"]:
-        mv_network.setup_network(systemconfig)
+        restartfe = mv_network.setup_network(systemconfig)
+        if restartfe == True :
+            logging.debug("    Setting the frontend to restart due to network change")
+            cmdmodule["restartfe"] = True
 
 
 
@@ -109,7 +112,7 @@ def main(argv):
         restart = mv_ir.setup_ir(systemconfig,data_config)
         restartfe = restart[0]
         restartlcd = restart[1]
-        if restartfe == True:
+        if restartfe == True :
             logging.debug("    Setting the frontend to restart due to lirc change")
             cmdmodule["restartfe"] = True
         cmdmodule["smolt"] = True
diff --git a/abs/core-testing/LinHES-config/systemconfig.sh b/abs/core-testing/LinHES-config/systemconfig.sh
index 4f58ee2..57bff5a 100755
--- a/abs/core-testing/LinHES-config/systemconfig.sh
+++ b/abs/core-testing/LinHES-config/systemconfig.sh
@@ -11,4 +11,6 @@ echo "----------------------start of systemconfig $@ ----------------------"
 postfix=`cat ${BASE}/usr/local/share/mythtv/.releasetype`
 
 systemconfig.py -m $@
+echo "exit code $?"
+
 
-- 
cgit v0.12