def ifail2ban():
    
    ipt_cmds = ["iptables -A INPUT -i lo -j ACCEPT",
                "iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT",
                "iptables -A INPUT -p tcp --dport 22 -j ACCEPT",
                "iptables -A INPUT -p tcp --dport 80 -j ACCEPT",
                "iptables -A INPUT -p tcp --dport 443 -j ACCEPT",
                "iptables -A INPUT -p tcp --dport 5060 -j ACCEPT",
                "iptables -A INPUT -p udp --dport 5060 -j ACCEPT",
                "iptables -A INPUT -p tcp --dport 5080 -j ACCEPT",
                "iptables -A INPUT -p udp --dport 5080 -j ACCEPT",
                "iptables -A INPUT -p udp --dport 16384:32768 -j ACCEPT",
                "iptables -P INPUT DROP",
                "iptables -P FORWARD DROP",
                "iptables -P OUTPUT ACCEPT",
                "iptables -A INPUT -p icmp --icmp-type echo-request -j ACCEPT",
                "iptables -I INPUT -j DROP -p tcp --dport 5060 -m string --string \"friendly-scanner\" --algo bm",
                "iptables -I INPUT -j DROP -p tcp --dport 5080 -m string --string \"friendly-scanner\" --algo bm",
                "iptables -I INPUT -j DROP -p udp --dport 5060 -m string --string \"friendly-scanner\" --algo bm",
                "iptables -I INPUT -j DROP -p udp --dport 5080 -m string --string \"friendly-scanner\" --algo bm"                
                ]
    
    INSTALL_ROOT = os.getcwd()
    if os.path.isfile("%s/resources/install.json" % (INSTALL_ROOT)):
        FPBXParms.PARMS = FPBXParms.load_parms(FPBXParms.PARMS)
    else:
        print("Error no install parameters")  
        sys.exit(1)  
    
    print("Setting up fail2ban to protect your system from several types of attacks")
    if os.path.isfile("%s/resources/fail2ban/jail.local" % (INSTALL_ROOT)):
        if FPBXParms.whitelist != None:
            shutil.copyfile("%s/resources/fail2ban/jail.package" % (INSTALL_ROOT), "/etc/fail2ban/jail.local")
        else:
            shutil.copyfile("%s/resources/fail2ban/jail.source" % (INSTALL_ROOT), "/etc/fail2ban/jail.local")
            shutil.copyfile("%s/resources/fail2ban/freeswitch-dos.conf" % (INSTALL_ROOT), "/etc/fail2ban/filter.d/freeswitch-dos.conf")
            shutil.copyfile("%s/resources/fail2ban/fusionpbx.conf" % (INSTALL_ROOT), "/etc/fail2ban/filter.d/fusionpbx.conf")
            if FPBXParms.whitelist != None:
                ftb = open("/etc/fail2ban/jail.local", 'a')
                ftb.write("[DEFAULT]")
                ftb.write("\n")
                ftb.write("ignoreip = %s" % (FPBXParms.whitelist))
                ftb.write("\n")
                ftb.close()
            ret = subprocess.call("systemctl restart fail2ban", stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, shell=True)
            FPBXParms.check_ret(ret, "Restart fail2ban")
    
    print("Setting iptables rules")      
    for cmd in ipt_cmds:
        ret = subprocess.call(cmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, shell=True)
        FPBXParms.check_ret(ret, cmd)
    print("")
    print("NOTE: Please read this before you continue!")
    print("Installing iptables persistence module")
    print("You will see two questions about saving the iptable rules")
    print("Please select yes for both when asked")
    input("press ENTER when you are ready")
    subprocess.call("apt-get -y install iptables-persistent", shell=True)
    return
def ipostgresql():
	INSTALL_ROOT = os.getcwd()
	
	if FPBXParms.PARMS["Distro"][0] == "trusty":
		keyfile = open("/etc/apt/sources.list.d/pgdg.list", 'w')
		keyfile.write(PG_REPO[FPBXParms.PARMS["Distro"][0]])
		keyfile.write("\n")
		keyfile.close()
		ret = subprocess.call("curl %s  | apt-key add -" % (PG_KEY[FPBXParms.PARMS["Distro"][0]]) , stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, shell=True)
		FPBXParms.check_ret(ret, "Adding package key for postgresql")
		ret = subprocess.call("apt-get update", stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, shell=True)
		FPBXParms.check_ret(ret, "Updating repositories after adding in postgresql 9.4")
		
	if os.path.isfile("%s/resources/install.json" % (INSTALL_ROOT)):
		FPBXParms.PARMS = FPBXParms.load_parms(FPBXParms.PARMS)
	else:
		print("Error no install parameters")
		sys.exit(1)
		
	if FPBXParms.PARMS["DatabaseType"][0] == "P":
		print("Installing Postgresql")
		pgidbg = open("pginstall.out", 'w')
		pgierr = open("pginstall.err", 'w')
		ret = subprocess.call("apt-get -y install postgresql-9.4 postgresql-client-9.4 postgresql-client-common postgresql-common ssl-cert", stdout=pgidbg, stderr=pgierr, shell=True)
		FPBXParms.check_ret(ret, "installing postgresql")
		pgidbg.close()
		pgierr.close()
		
	if FPBXParms.PARMS["DatabaseType"][0] == 's':
		ret = subprocess.call("apt-get install sqlite3", stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, shell=True)
		FPBXParms.check_ret(ret, "Installing sqlite3")
		
	return
def ipackages():
    INSTALL_ROOT = os.getcwd()
    if os.path.isfile("%s/resources/install.json" % (INSTALL_ROOT)):
        FPBXParms.PARMS = FPBXParms.load_parms(FPBXParms.PARMS)
    else:
        print("Error no install parameters")
        sys.exit(1)

    print("Updating the OS repository")
    ret = subprocess.call("apt-get update", stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, shell=True)
    FPBXParms.check_ret(ret, "Updating the OS repository")
    print("Upgrading the OS to the latest packages")
    ret = subprocess.call("apt-get -y upgrade", stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, shell=True)
    FPBXParms.check_ret(ret, "Upgrading the OS to the latest packages")
    print("Installing additional packages needed for FusionPBX to run under OS %s" % (FPBXParms.PARMS["Distro"][0]))
    for i in range(0, len(COMMON_APPS) + 1):
        print(i, end="", flush=True)
    print("", end="\r", flush=True)
    for addons in COMMON_APPS:
        print("*", end="", flush=True)
        ret = subprocess.call(
            "apt-get -y install %s" % (addons), stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, shell=True
        )
        FPBXParms.check_ret(ret, "Installing additional packages")
    addons = ADDON_APPS[FPBXParms.PARMS["Distro"][0]]
    print("*", end="")
    ret = subprocess.call(
        "apt-get -y install %s" % (addons), stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, shell=True
    )
    FPBXParms.check_ret(ret, "Installing additional packages")
    print("\n")
    return
def ipackages():
    INSTALL_ROOT = os.getcwd()
    if os.path.isfile("%s/resources/install.json" % (INSTALL_ROOT)):
        FPBXParms.PARMS = FPBXParms.load_parms(FPBXParms.PARMS)
    else:
        print("Error no install parameters")
        sys.exit(1)
        
    print("Updating the Debian repository")
    ret = subprocess.call("apt-get update", stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, shell=True)
    FPBXParms.check_ret(ret, "Updating the Debian repository")
    print("Upgrading the OS to the latest packages")
    ret = subprocess.call("apt-get -y upgrade", stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, shell=True)
    FPBXParms.check_ret(ret,"Upgrading the OS to the latest packages")
    print("Installing additional packages needed for FusionPBX to run under Debian %s" % (FPBXParms.PARMS["Distro"][0]))
    for i in range(0,len(COMMON_APPS) + 1):
        print(i, end="", flush=True)
    print("", end="\r", flush=True)
    for addons in COMMON_APPS:
        print("*", end="", flush=True)
        ret = subprocess.call("apt-get -y install %s" % (addons), stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, shell=True)
        FPBXParms.check_ret(ret,"Installing additional packages")
    addons = ADDON_APPS[FPBXParms.PARMS["Distro"][0]]
    print("*", end="")
    ret = subprocess.call("apt-get -y install %s" % (addons), stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, shell=True)
    FPBXParms.check_ret(ret,"Installing additional packages")
    print("\n")
    return
def ipostgresql():
    INSTALL_ROOT = os.getcwd()
    if os.path.isfile("%s/resources/install.json" % (INSTALL_ROOT)):
        FPBXParms.PARMS = FPBXParms.load_parms(FPBXParms.PARMS)
    else:
        print("Error no install parameters")
        sys.exit(1)

    if FPBXParms.PARMS["DatabaseType"][0] == "P":
        print("Installing Postgresql")
        pgidbg = open("pginstall.out", 'w')
        pgierr = open("pginstall.err", 'w')
        ret = subprocess.call(
            "apt-get -y install postgresql-9.4 postgresql-client-9.4 postgresql-client-common postgresql-common ssl-cert",
            stdout=pgidbg,
            stderr=pgierr,
            shell=True)
        FPBXParms.check_ret(ret, "installing postgresql")
        pgidbg.close()
        pgierr.close()
    return
Пример #6
0
def ifail2ban():

    INSTALL_ROOT = os.getcwd()
    if os.path.isfile("%s/resources/install.json" % (INSTALL_ROOT)):
        FPBXParms.PARMS = FPBXParms.load_parms(FPBXParms.PARMS)
    else:
        print("Error no install parameters")
        sys.exit(1)

    print(
        "Setting up fail2ban to protect your system from some types of attacks"
    )
    if os.path.isfile("%s/resources/fail2ban/jail.local" % (INSTALL_ROOT)):
        if FPBXParms.whitelist != '':
            shutil.copyfile(
                "%s/resources/fail2ban/jail.package" % (INSTALL_ROOT),
                "/etc/fail2ban/jail.local")
        else:
            shutil.copyfile(
                "%s/resources/fail2ban/jail.source" % (INSTALL_ROOT),
                "/etc/fail2ban/jail.local")
            shutil.copyfile(
                "%s/resources/fail2ban/freeswitch-dos.conf" % (INSTALL_ROOT),
                "/etc/fail2ban/filter.d/freeswitch-dos.conf")
            shutil.copyfile(
                "%s/resources/fail2ban/fusionpbx.conf" % (INSTALL_ROOT),
                "/etc/fail2ban/filter.d/fusionpbx.conf")
            if FPBXParms.PARMS["FS_Install_Type"][0] == "P":
                ftb = open("/etc/fail2ban/jail.local", 'a')
                ftb.write("[DEFAULT]")
                ftb.write("\n")
                ftb.write("ignoreip = %s" % (FPBXParms.whitelist))
                ftb.write("\n")
                ftb.close()
            ret = subprocess.call("systemctl restart fail2ban",
                                  stdout=subprocess.DEVNULL,
                                  stderr=subprocess.DEVNULL,
                                  shell=True)
            FPBXParms.check_ret(ret, "Restart fail2ban")
    return
def iwebserver():
	retcode = 0
	INSTALL_ROOT = os.getcwd()
	if os.path.isfile("%s/resources/install.json" % (INSTALL_ROOT)):
		FPBXParms.PARMS = FPBXParms.load_parms(FPBXParms.PARMS)
	else:
		print("Error no install parameters")  
		sys.exit(1) 
				
	#=============================================================================== 
	# We need a web server for FusionPBX
	#=============================================================================== 
	
	if FPBXParms.PARMS["WebServer"][0] == "a":
		ws = "Apache2"
	if FPBXParms.PARMS["WebServer"][0] == "N":
		ws = "nginx"
	print("Installing %s for our Webserver " % (ws))
	if FPBXParms.PARMS["WebServer"][0] == "a":
		ret = subprocess.call("apt-get -y install apache2 libapache2-mod-php5", stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, shell=True)
		FPBXParms.check_ret(ret, "Installing Apache2")
	elif FPBXParms.PARMS["WebServer"][0] == "N":
		# apache2 may have been installed as a dependency for another package
		# Shutdown and disable apache2
		subprocess.call("systemctl --quiet disable apache2", stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, shell=True)
		subprocess.call("systemctl --quiet stop apache2", stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, shell=True)
		subprocess.call("apt-get -y remove apache2 apache2-mpm-prefork apache2-utils apache2.2-bin apache2.2-common libapache2-mod-php5 libapr1 libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap", stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, shell=True)
		ret = subprocess.call("apt-get -y install nginx", stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, shell=True)
		FPBXParms.check_ret(ret, "Installing nginx")
	else:
		print("No Web Server was defined, FusionPBX will not operate with out a Web Server")
		sys.exit(6)

	
	#=============================================================================== 
	# www-data needs access to all of Freeswitch
	#=============================================================================== 
	
	print("Setting access on Freeswitch for FusionPBX")
	if os.path.isfile("/usr/bin/freeswitch"):
		subprocess.call("systemctl stop freeswitch", stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, shell=True)
		paths = ["/usr/lib/freeswitch", "/usr/share/freeswitch", "/etc/freeswitch"]
		for path in paths:
			for root, dirs, files in os.walk(path):  
				for directory in dirs:  
					ret = subprocess.call("setfacl -R -m u:www-data:rwx,g:www-data:rwx %s" % (os.path.join(root, directory)), stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, shell=True)
					FPBXParms.check_ret(ret, "Setting user acl for %s" % (os.path.join(root, directory)))
					ret = subprocess.call("setfacl -R -d -m u:www-data:rwx,g:www-data:rwx %s" % (os.path.join(root, directory)), stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, shell=True)
					FPBXParms.check_ret(ret, "Setting user acl for %s" % (os.path.join(root, directory)))					
				for filename in files:
					ret = subprocess.call("setfacl -R -m u:www-data:rwx,g:www-data:rwx %s" % (os.path.join(root, filename)), stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, shell=True)
					FPBXParms.check_ret(ret, "Setting user acl for %s" % (os.path.join(root, filename)))					
					ret = subprocess.call("setfacl -R -d -m u:www-data:rwx,g:www-data:rwx %s" % (os.path.join(root, filename)), stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, shell=True)
					FPBXParms.check_ret(ret, "Setting user acl for %s" % (os.path.join(root, filename)))					
		subprocess.call("systemctl start freeswitch", stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, shell=True)
	else:
		print("Freeswitch is not installed on this system")
		sys.exit(5)	
	
	return retcode
def ifreeswitch():
    INSTALL_ROOT = os.getcwd()
    if os.path.isfile("%s/resources/install.json" % (INSTALL_ROOT)):
        FPBXParms.PARMS = FPBXParms.load_parms(FPBXParms.PARMS)
    else:
        print("Error no install parameters")
        sys.exit(1)
        
    if FPBXParms.PARMS["FS_Install_Type"][0] == "P":
        
    #===========================================================================
    # Install Freeswitch from packages
    #===========================================================================
   
        if FS_KEY[FPBXParms.PARMS["Distro"][0]] != "add repo here":
            print("Installing Freeswitch from Binary packages")
            # Check to see if the Freeswitch key has been installed
            keys = str(subprocess.check_output("apt-key list", shell = True))
            install_flag = True
            for line in keys:
                if "FreeSWITCH Package Signing Key" in line:
                    install_flag = False
            if install_flag:
                # Install the Freeswitch binary information
                ret = subprocess.call("curl %s  | apt-key add -" % (FS_KEY[FPBXParms.PARMS["Distro"][0]]) , stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, shell=True)
                FPBXParms.check_ret(ret, "Adding package key")
                ret = subprocess.call("echo %s  > /etc/apt/sources.list.d/freeswitch.list" % (FS_REPO[FPBXParms.PARMS["Distro"][0]]), stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, shell=True)
                FPBXParms.check_ret(ret, "Setting distro in package list")
                
            # Now we actually install Freeswitch from binary packages
            ret = subprocess.call("apt-get update && apt-get install -y freeswitch-all freeswitch-all-dbg gdb", stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, shell=True)
            FPBXParms.check_ret(ret, "Installing Freeswitch")
        else:
            print("I can not install from binary packages for the %s distro" % (FPBXParms.PARMS["Distro"][0]))
            sys.exit(4)
    else:
        
    #=============================================================================== 
    # Install Freeswitch from source
    #===============================================================================
    #===============================================================================
    # Set up necessary files
    #===============================================================================
        if not os.path.isdir("/usr/src/freeswitch/libs/spandsp/m4"):
            os.makedirs("/usr/src/freeswitch/libs/spandsp/m4")
        if os.path.isfile("%s/resources/freeswitch/libs/spandsp/m4/memmove.m4" % (INSTALL_ROOT)):
            shutil.copyfile("%s/resources/freeswitch/libs/spandsp/m4/memmove.m4" % (INSTALL_ROOT), "/usr/src/freeswitch/libs/spandsp/m4/memmove.m4")
        else:
            print("Missing needed file %s/resources/freeswitch/libs/spandsp/m4/memmove.m4" % (INSTALL_ROOT))
            sys.exit(12)
        if os.path.isfile("%s/resources/default/freeswitch" % (INSTALL_ROOT)):
            if not os.path.isdir("/etc/default"):
                os.makedirs("/etc/default")
            shutil.copyfile("%s/resources/default/freeswitch" % (INSTALL_ROOT), "/etc/default/freeswitch")

        if FS_KEY[FPBXParms.PARMS["Distro"][0]] != "add repo here":
            print("Installing Freeswitch from Source")
            # Check to see if the Freeswitch key has been installed
            keys = str(subprocess.check_output("apt-key list", shell=True))
            install_flag = True
            for line in keys:
                if "FreeSWITCH Package Signing Key" in line:
                    install_flag = False
            if install_flag:
                print("Installing source definitions")
                ret = subprocess.call("curl %s  | apt-key add -" % (FS_KEY[FPBXParms.PARMS["Distro"][0]]), stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, shell = True)
                FPBXParms.check_ret(ret, "Setting source definitions")
                ret = subprocess.call("echo %s  > /etc/apt/sources.list.d/freeswitch.list" % (FS_REPO[FPBXParms.PARMS["Distro"][0]]), stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, shell = True)
                FPBXParms.check_ret(ret, "Setting source list")
                print("Installing freeswitch video dependencies")
                ret = subprocess.call("apt-get update && apt-get install -y --force-yes freeswitch-video-deps-most", stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, shell = True)
                print("Installing freeswitch libraries")
                ret = subprocess.call("apt-get update && apt-get install -y %s" % (DEPENDENCIES), stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, shell = True)
                FPBXParms.check_ret(ret, "Installing video dependencies")
                print("Setting up git to pull rebase (Prevents conflicts)")
                ret = subprocess.call("git config --global pull.rebase true", stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, shell = True)
                FPBXParms.check_ret(ret, "Configuring Git")
                os.chdir("/usr/src")
                if not os.path.exists("/usr/src/freeswitch.git"):
                    print("Retrieving the source from Freeswitch")
                    ret = subprocess.call("git clone https://freeswitch.org/stash/scm/fs/freeswitch.git -bv1.6 freeswitch.git", stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, shell = True)
                    FPBXParms.check_ret(ret, "Retrieving the source using Git")
                os.chdir("freeswitch.git")
                print("Configuring the source install. (log in freeswitch_configure.log)")
                fslog = open("%s/freeswitch_configure.log" % (INSTALL_ROOT), 'w')
                ret = subprocess.call("./bootstrap.sh -j", stdout=fslog, stderr=fslog, shell = True)
                FPBXParms.check_ret(ret, "Setting bootstrap")
                fslog.close()
                ret = subprocess.call("./configure  --enable-core-pgsql-support", stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, shell = True)
                FPBXParms.check_ret(ret, "Configuring source for your machine")
                print("Compiling Freeswitch (This may take a long time, log in freeswitch_compile.log)")
                fslog = open("%s/freeswitch_compile.log" % (INSTALL_ROOT), 'w')
                ret = subprocess.call("make", stdout=fslog, stderr=fslog, shell = True)
                FPBXParms.check_ret(ret, "Compiling Freeswitch")
                fslog.close()
                print("Installing Freeswitch. (log in freeswitch_install.log)")
                fslog = open("%s/freeswitch_install.log" % (INSTALL_ROOT), 'w')
                ret = subprocess.call("make install", stdout=fslog, stderr=fslog, shell = True)
                FPBXParms.check_ret(ret, "Installing Freeswitch")
                fslog.close()
                fslog = open("freeswitch_sounds_install.log", 'w')
                fslog.close()
                print("Installing sounds, log in freeswitch_sounds_install.log")
                fslog = open("freeswitch_sounds_install.log", 'a')
                print("64 Bit Sounds")
                ret = subprocess.call("make cd-sounds-install", stdout=fslog, stderr=fslog, shell=True)
                print("64 Bit Music on hold")
                ret = subprocess.call("make cd-moh-install", stdout=fslog, stderr=fslog, shell=True)
                print("32 Bit Sounds")
                ret = subprocess.call("make uhd-sounds-install", stdout=fslog, stderr=fslog, shell=True)
                print("32 Bit Music on hold")
                ret = subprocess.call("make uhd-moh-install", stdout=fslog, stderr=fslog, shell=True)
                print("16 Bit Sounds")
                ret = subprocess.call("make hd-sounds-install", stdout=fslog, stderr=fslog, shell=True)
                print("16 Bit Music on hold")
                ret = subprocess.call("make hd-moh-install", stdout=fslog, stderr=fslog, shell=True)
                print("8 Bit Sounds")
                ret = subprocess.call("make sounds-install", stdout=fslog, stderr=fslog, shell=True)
                print("8 Bit Music on hold")
                ret = subprocess.call("make moh-install", stdout=fslog, stderr=fslog, shell=True)
                print("Samples")
                ret = subprocess.call("make samples", stdout=fslog, stderr=fslog, shell=True)
                fslog.close()
                ret = subprocess.call("/usr/sbin/adduser --disabled-login --no-create-home --shell /bin/false --gecos FreeSWITCH freeswitch", stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, shell = True)
                FPBXParms.check_ret(ret, "Adding freeswitch user")
                ret= subprocess.call("/usr/sbin/usermod -a -G audio freeswitch", stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, shell = True)
                FPBXParms.check_ret(ret, "Adding freeswitch to audio group")
                print("Setting freeswitch to run automatically")
                ret = subprocess.call("chown -R www-data:www-data /usr/local/freeswitch", stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, shell = True)
                FPBXParms.check_ret(ret, "Changing ownership of freeswitch")
                if os.path.isfile("/usr/local/freeswitch/bin/freeswitch"):
                    os.symlink("/usr/local/freeswitch/bin/freeswitch", "/usr/bin/freeswitch")
                    os.symlink("/usr/local/freeswitch/bin/fs_cli", "/usr/bin/fs_cli")
                else:
                    print("Abort freeswitch is not in the usual location")
                    sys.exit(15)
                shutil.copyfile("%s/resources/freeswitch/freeswitch.service" % (INSTALL_ROOT), "/lib/systemd/system/freeswitch.service")
                shutil.copyfile("%s/resources/etc/default/freeswitch" % (INSTALL_ROOT), "/etc/default/freeswitch")
                if not os.path.exists("/etc/freeswitch"):
                    os.makedirs("/etc/freeswitch")
                ret = subprocess.call("cp -a /usr/local/freeswitch/conf/* /etc/freeswitch/", stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, shell = True)
                ret = subprocess.call("systemctl enable freeswitch", stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, shell = True)
                ret = subprocess.call("systemctl unmask freeswitch.service", stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, shell = True)
                ret = subprocess.call("systemctl daemon-reload ", stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, shell = True)
                ret = subprocess.call("systemctl start freeswitch", stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, shell = True)
                FPBXParms.check_ret(ret, "freeswitch start failed")
    os.chdir("%s" % INSTALL_ROOT)
    return
Пример #9
0
def ifreeswitch():
    INSTALL_ROOT = os.getcwd()
    if os.path.isfile("%s/resources/install.json" % (INSTALL_ROOT)):
        FPBXParms.PARMS = FPBXParms.load_parms(FPBXParms.PARMS)
    else:
        print("Error no install parameters")
        sys.exit(1)
        
    if FPBXParms.PARMS["FS_Install_Type"][0] == "P":
        
    #===========================================================================
    # Install Freeswitch from packages
    #===========================================================================
   
        if FS_KEY[FPBXParms.PARMS["Distro"][0]] != "add repo here":
            print("Installing Freeswitch from Binary packages")
            # Check to see if the Freeswitch key has been installed
            keys = str(subprocess.check_output("apt-key list", shell = True))
            install_flag = True
            for line in keys:
                if "FreeSWITCH Package Signing Key" in line:
                    install_flag = False
            if install_flag:
                # Install the Freeswitch binary information
                ret = subprocess.call("curl %s  | apt-key add -" % (FS_KEY[FPBXParms.PARMS["Distro"][0]]) , stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, shell=True)
                FPBXParms.check_ret(ret, "Adding package key")
                ret = subprocess.call("echo %s  > /etc/apt/sources.list.d/freeswitch.list" % (FS_REPO[FPBXParms.PARMS["Distro"][0]]), stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, shell=True)
                FPBXParms.check_ret(ret, "Setting distro in package list")
                
            # Now we actually install Freeswitch from binary packages
            ret = subprocess.call("apt-get update && apt-get install -y freeswitch-all freeswitch-all-dbg gdb", stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, shell=True)
            FPBXParms.check_ret(ret, "Installing Freeswitch Freeswitch")
        else:
            print("I can not install from binary packages for the %s distro" % (FPBXParms.PARMS["Distro"][0]))
            sys.exit(4)
    else:
        
    #=============================================================================== 
    # Install Freeswitch from source
    #===============================================================================
    #===============================================================================
    # Set up necessary files
    #===============================================================================
        if not os.path.isdir("/usr/src/freeswitch/libs/spandsp/m4"):
            os.makedirs("/usr/src/freeswitch/libs/spandsp/m4")
        if os.path.isfile("%s/resources/freeswitch/libs/spandsp/m4/memmove.m4" % (INSTALL_ROOT)):
            shutil.copyfile("%s/resources/freeswitch/libs/spandsp/m4/memmove.m4" % (INSTALL_ROOT), "/usr/src/freeswitch/libs/spandsp/m4/memmove.m4")
        else:
            print("Missing needed file %s/resources/freeswitch/libs/spandsp/m4/memmove.m4" % (INSTALL_ROOT))
            sys.exit(12)
        if os.path.isfile("%s/resources/default/freeswitch" % (INSTALL_ROOT)):
            if not os.path.isdir("/etc/default"):
                os.makedirs("/etc/default")
            shutil.copyfile("%s/resources/default/freeswitch" % (INSTALL_ROOT), "/etc/default/freeswitch")

        if FS_KEY[FPBXParms.PARMS["Distro"][0]] != "add repo here":
            print("Installing Freeswitch from Source")
            # Check to see if the Freeswitch key has been installed
            keys = str(subprocess.check_output("apt-key list", shell=True))
            install_flag = True
            for line in keys:
                if "FreeSWITCH Package Signing Key" in line:
                    install_flag = False
            if install_flag:
                print("Installing source definitions")
                ret = subprocess.call("curl %s  | apt-key add -" % (FS_KEY[FPBXParms.PARMS["Distro"][0]]), stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, shell = True)
                FPBXParms.check_ret(ret, "Setting source definitions")
                ret = subprocess.call("echo %s  > /etc/apt/sources.list.d/freeswitch.list" % (FS_REPO[FPBXParms.PARMS["Distro"][0]]), stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, shell = True)
                FPBXParms.check_ret(ret, "Setting source list")
                print("Installing freeswitch video dependencies")
                ret = subprocess.call("apt-get update && apt-get install -y --force-yes freeswitch-video-deps-most", stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, shell = True)
                print("Installing freeswitch libraries")
                ret = subprocess.call("apt-get update && apt-get install -y %s" % (DEPENDENCIES), stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, shell = True)
                FPBXParms.check_ret(ret, "Installing video dependencies")
                print("Setting up git to pull rebase (Prevents conflicts)")
                ret = subprocess.call("git config --global pull.rebase true", stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, shell = True)
                FPBXParms.check_ret(ret, "Configuring Git")
                os.chdir("/usr/src")
                if not os.path.exists("/usr/src/freeswitch.git"):
                    print("Retrieving the source from Freeswitch")
                    ret = subprocess.call("git clone https://freeswitch.org/stash/scm/fs/freeswitch.git -bv1.6 freeswitch.git", stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, shell = True)
                    FPBXParms.check_ret(ret, "Retrieving the source using Git")
                os.chdir("freeswitch.git")
                print("Configuring the source install. (log in freeswitch_configure.log)")
                fslog = open("%s/freeswitch_configure.log" % (INSTALL_ROOT), 'w')
                ret = subprocess.call("./bootstrap.sh -j", stdout=fslog, stderr=fslog, shell = True)
                FPBXParms.check_ret(ret, "Setting bootstrap")
                fslog.close()
                ret = subprocess.call("./configure  --enable-core-pgsql-support", stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, shell = True)
                FPBXParms.check_ret(ret, "Configuring source for your machine")
                print("Compiling Freeswitch (This may take a long time, log in freeswitch_compile.log)")
                fslog = open("%s/freeswitch_compile.log" % (INSTALL_ROOT), 'w')
                ret = subprocess.call("make", stdout=fslog, stderr=fslog, shell = True)
                FPBXParms.check_ret(ret, "Compiling Freeswitch")
                fslog.close()
                print("Installing Freeswitch. (log in freeswitch_install.log)")
                fslog = open("%s/freeswitch_install.log" % (INSTALL_ROOT), 'w')
                ret = subprocess.call("make install", stdout=fslog, stderr=fslog, shell = True)
                FPBXParms.check_ret(ret, "Installing Freeswitch")
                fslog.close()
                fslog = open("freeswitch_sounds_install.log", 'w')
                fslog.close()
                print("Installing sounds, log in freeswitch_sounds_install.log")
                fslog = open("freeswitch_sounds_install.log", 'a')
                print("64 Bit Sounds")
                ret = subprocess.call("make cd-sounds-install", stdout=fslog, stderr=fslog, shell=True)
                print("64 Bit Music on hold")
                ret = subprocess.call("make cd-moh-install", stdout=fslog, stderr=fslog, shell=True)
                print("32 Bit Sounds")
                ret = subprocess.call("make uhd-sounds-install", stdout=fslog, stderr=fslog, shell=True)
                print("32 Bit Music on hold")
                ret = subprocess.call("make uhd-moh-install", stdout=fslog, stderr=fslog, shell=True)
                print("16 Bit Sounds")
                ret = subprocess.call("make hd-sounds-install", stdout=fslog, stderr=fslog, shell=True)
                print("16 Bit Music on hold")
                ret = subprocess.call("make hd-moh-install", stdout=fslog, stderr=fslog, shell=True)
                print("8 Bit Sounds")
                ret = subprocess.call("make sounds-install", stdout=fslog, stderr=fslog, shell=True)
                print("8 Bit Music on hold")
                ret = subprocess.call("make moh-install", stdout=fslog, stderr=fslog, shell=True)
                print("Samples")
                ret = subprocess.call("make samples", stdout=fslog, stderr=fslog, shell=True)
                fslog.close()
                ret = subprocess.call("/usr/sbin/adduser --disabled-login --no-create-home --shell /bin/false --gecos FreeSWITCH freeswitch", stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, shell = True)
                FPBXParms.check_ret(ret, "Adding freeswitch user")
                ret= subprocess.call("/usr/sbin/usermod -a -G audio freeswitch", stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, shell = True)
                FPBXParms.check_ret(ret, "Adding freeswitch to audio group")
                print("Setting freeswitch to run automatically")
                ret = subprocess.call("chown -R www-data:www-data /usr/local/freeswitch", stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, shell = True)
                FPBXParms.check_ret(ret, "Changing ownership of freeswitch")
                if os.path.isfile("/usr/local/freeswitch/bin/freeswitch"):
                    os.symlink("/usr/local/freeswitch/bin/freeswitch", "/usr/bin/freeswitch")
                    os.symlink("/usr/local/freeswitch/bin/fs_cli", "/usr/bin/fs_cli")
                else:
                    print("Abort freeswitch is not in the usual location")
                    sys.exit(15)
                shutil.copyfile("%s/resources/freeswitch/freeswitch.service" % (INSTALL_ROOT), "/lib/systemd/system/freeswitch.service")
                shutil.copyfile("%s/resources/etc/default/freeswitch" % (INSTALL_ROOT), "/etc/default/freeswitch")
                if not os.path.exists("/etc/freeswitch"):
                    os.makedirs("/etc/freeswitch")
                ret = subprocess.call("cp -a /usr/local/freeswitch/conf/* /etc/freeswitch/", stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, shell = True)
                ret = subprocess.call("systemctl enable freeswitch", stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, shell = True)
                ret = subprocess.call("systemctl unmask freeswitch.service", stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, shell = True)
                ret = subprocess.call("systemctl daemon-reload ", stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, shell = True)
                ret = subprocess.call("systemctl start freeswitch", stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, shell = True)
                FPBXParms.check_ret(ret, "freeswitch start failed")
    os.chdir("%s" % INSTALL_ROOT)
    return
Пример #10
0
user = os.getuid()
if not user == 0:
    print("This install script must be run as root")
    sys.exit(1)
    
#===============================================================================
# Install dbus up front
# NOTE: It is possible to install with out dbus
#       dbus is needed to set the timezone and for other operations
#===============================================================================

print("Welcome to FusionPBX installation.")
print ("Installing dbus as it is required")
ret = subprocess.call("apt-get install dbus", stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, shell=True)
FPBXParms.check_ret(ret, "Updating the OS repository")
    
#===============================================================================
# Make sure we have current parameters to continue
#===============================================================================

if args.restart and os.path.isfile("%s/resources/install.json" % (INSTALL_ROOT)):
    FPBXParms.PARMS = FPBXParms.load_parms(FPBXParms.PARMS)
else:
    ask_questions.iask_questions()
    if os.path.isfile("%s/resources/install.json" % (INSTALL_ROOT)):
        FPBXParms.PARMS = FPBXParms.load_parms(FPBXParms.PARMS)
    else:
        print("ERROR: no saved parameters to load")
        sys.exit(3)
        
Пример #11
0
    
#===============================================================================
# Check for running as root
#===============================================================================

user = os.getuid()
if not user == 0:
    print("This install script must run as root")
    sys.exit(1)
    
#===============================================================================
# Make sure we have current parameters to continue
#===============================================================================

if args.restart and os.path.isfile("%s/resources/install.json" % (INSTALL_ROOT)):
    FPBXParms.PARMS = FPBXParms.load_parms(FPBXParms.PARMS)
else:
    ask_questions.iask_questions()
    if os.path.isfile("%s/resources/install.json" % (INSTALL_ROOT)):
        FPBXParms.PARMS = FPBXParms.load_parms(FPBXParms.PARMS)
    else:
        print("ERROR: no saved parameters to load")
        sys.exit(3)
        
#===============================================================================
# Set TimeZone before we do anything
#===============================================================================

if not args.restart:
    print()
    print("FusionPBX recommends setting the server time zone to GMT")
def iask_questions():    
    
    print("I am checking to see if there are parameters already defined")
    
    #===============================================================================
    # Load the previously saved parameters
    #===============================================================================
    
    if os.path.isfile("%s/resources/install.json" % (INSTALL_ROOT)):
    
        FPBXParms.PARMS = FPBXParms.load_parms(FPBXParms.PARMS)
    
        print("I found the following parameters")
        FPBXParms.show_parms()
    
        USEPARMS = ask_yn("Do you want to use them")
        if USEPARMS == "Yes":
            pass
#             print("In loop %d" % (LOOPNUMBER))
#             LOOPNUMBER += 1
        else:
    #===============================================================================
    # Start asking the hard questions
    #===============================================================================
            print("Very well I will ask you several questions.")
            print("Note: if a question has '(a/B)' in it, 'B' is the default")
            print("If a question has a value listed that is the default")
            print("All default values can be selected by just pressing Enter")
            print("If a parameter does not have a default, a value must be entered")
            
            s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
            s.connect(('google.com', 0))
            FPBXParms.PARMS["IP"][0] = (s.getsockname()[0])
        
            BDR_ANS = "No"
#             BDR_ANS = ask_yn(FPBXParms.PARMS["BDR"][1])
            FPBXParms.PARMS["BDR"][0] = BDR_ANS
            if BDR_ANS == "Yes":
                PARM_LIST = FPBXParms.BDR_PARMS
            else:
                PARM_LIST = FPBXParms.NON_BDR_PARMS
    
            if len(PARM_LIST) > 0:
                for index in PARM_LIST:
                    FPBXParms.ask_parm(index)
    
            for index in FPBXParms.COMMON_PARMS:
                FPBXParms.ask_parm(index)
    
    #======================================================================
    # Make up some passwords that are secure and easy to remember
    #======================================================================
    
    #         FPBXParms.PARMS["FPBXuserPassword"][0] = mkpass()
    #         FPBXParms.PARMS["FPBXDBUserPassword"][0] = mkpass()
    
            FPBXParms.save_parms()
    else:
        print("There is no predefined parameter file.")
        print("Please answer the following questions.")
        
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        s.connect(('google.com', 0))
        FPBXParms.PARMS["IP"][0] = (s.getsockname()[0])
#        For now we are only installing a single switch, No BDR
        BDR_ANS = "No"
#         BDR_ANS = ask_yn(FPBXParms.PARMS["BDR"][1])
        FPBXParms.PARMS["BDR"][0] = BDR_ANS
        if BDR_ANS == "Yes":
            PARM_LIST = FPBXParms.BDR_PARMS
        else:
            PARM_LIST = FPBXParms.NON_BDR_PARMS
        if len(PARM_LIST) > 0:
            for index in PARM_LIST:
                FPBXParms.ask_parm(index)
    
        for index in FPBXParms.COMMON_PARMS:
            FPBXParms.ask_parm(index)
    
    #======================================================================
    # Make up some passwords that are secure and easy to remember
    #======================================================================
    
    #     FPBXParms.PARMS["FPBXuserPassword"][0] = mkpass()
    #     FPBXParms.PARMS["FPBXDBUserPassword"][0] = mkpass()
    
    #===========================================================================
    # We need to check for Ubuntu and insure it is a source install
    #===========================================================================
        
        if FPBXParms.PARMS["Distro"][0] == "trusty":
            FPBXParms.PARMS["FS_Install_Type"][0] = "s"
            print("This install is on Ubuntu and only source is currently configured")
            print("I have set your Freeswitch install type to source")
        
        FPBXParms.save_parms()
            
    return
Пример #13
0
def iask_questions():
    print("Welcome to FusionPBX installation.")
    print("I am checking to see if there are parameters already defined")

    #===============================================================================
    # Load the previously saved parameters
    #===============================================================================

    if os.path.isfile("%s/resources/install.json" % (INSTALL_ROOT)):

        FPBXParms.PARMS = FPBXParms.load_parms(FPBXParms.PARMS)

        print("I found the following parameters")
        FPBXParms.show_parms()

        USEPARMS = ask_yn("Do you want to use them")
        if USEPARMS == "Yes":
            pass
#             print("In loop %d" % (LOOPNUMBER))
#             LOOPNUMBER += 1
        else:
            #===============================================================================
            # Start asking the hard questions
            #===============================================================================
            print("Very well I will ask you several questions.")
            print("Note: if a question has '(a/B)' in it, 'B' is the default")
            print("If a question has a value listed that is the default")
            print("All default values can be selected by just pressing Enter")
            print(
                "If a parameter does not have a default, a value must be entered"
            )
            BDR_ANS = "No"
            #             BDR_ANS = ask_yn(FPBXParms.PARMS["BDR"][1])
            FPBXParms.PARMS["BDR"][0] = BDR_ANS
            if BDR_ANS == "Yes":
                PARM_LIST = FPBXParms.BDR_PARMS
            else:
                PARM_LIST = FPBXParms.NON_BDR_PARMS

            for index in PARM_LIST:
                FPBXParms.ask_parm(index)

            for index in FPBXParms.COMMON_PARMS:
                FPBXParms.ask_parm(index)

    #======================================================================
    # Make up some passwords that are secure and easy to remember
    #======================================================================

    #         FPBXParms.PARMS["FPBXuserPassword"][0] = mkpass()
    #         FPBXParms.PARMS["FPBXDBUserPassword"][0] = mkpass()

            FPBXParms.save_parms()
    else:
        print("There is no predefined parameter file.")
        print("Please answer the following questions.")
        #        For now we are only installing a single switch, No BDR
        BDR_ANS = "No"
        #         BDR_ANS = ask_yn(FPBXParms.PARMS["BDR"][1])
        FPBXParms.PARMS["BDR"][0] = BDR_ANS
        if BDR_ANS == "Yes":
            PARM_LIST = FPBXParms.BDR_PARMS
        else:
            PARM_LIST = FPBXParms.NON_BDR_PARMS

        for index in PARM_LIST:
            FPBXParms.ask_parm(index)

        for index in FPBXParms.COMMON_PARMS:
            FPBXParms.ask_parm(index)

    #======================================================================
    # Make up some passwords that are secure and easy to remember
    #======================================================================

    #     FPBXParms.PARMS["FPBXuserPassword"][0] = mkpass()
    #     FPBXParms.PARMS["FPBXDBUserPassword"][0] = mkpass()

        FPBXParms.save_parms()

#         if FPBXParms.PARMS["FS_Install_Type"][0] == "s":
#             print("WARNING: There are currently problems with Video and installing from source")
#             print("As of Freeswitch 1.6.")
#             print("You may need to disable video to compile Freeswitch")
#             ans = ask_yn("Do you wish to continue")
#             if ans == "No":
#                 sys.exit()

    return
def ifusionpbx():
	INSTALL_ROOT = os.getcwd()
	if os.path.isfile("%s/resources/install.json" % (INSTALL_ROOT)):
		FPBXParms.PARMS = FPBXParms.load_parms(FPBXParms.PARMS)
	else:
		print("No predefined parameters to install FusionPBX")
		sys.exit(1)
		
	#=======================================================================
	# Determine web server type
	#=======================================================================
	
	if FPBXParms.PARMS["WebServer"][0] == "a":
		ws = "apache2"
	if FPBXParms.PARMS["WebServer"][0] == "N":
		ws = "nginx"
		
	#===============================================================================
	# Set up Postgresql
	#===============================================================================
	
	if FPBXParms.PARMS["DatabaseType"][0] == "P":
		print("Setting up Postgresql to support FusionPBX")
		# temp fix for pg_hba.conf permission problem
		if os.path.isfile("%s/resources/postgresql/pg_hba.conf" % (INSTALL_ROOT)):
			shutil.copyfile("%s/resources/postgresql/pg_hba.conf" % (INSTALL_ROOT), "/etc/postgresql/9.4/main/pg_hba.conf")
			ret = subprocess.call("systemctl restart postgresql", stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, shell=True)
			FPBXParms.check_ret(ret, "Restarting postgres for pg_hba.conf changes")
			time.sleep(5) # Give postgresql time to settle
		tmp = open("/var/lib/postgresql/install.sql", 'w')
		tmp.write("CREATE ROLE %s WITH SUPERUSER CREATEROLE CREATEDB LOGIN PASSWORD '%s';\n" % (FPBXParms.PARMS["DBUser"][0], FPBXParms.PARMS["DBUserPassword"][0]))
		tmp.write("ALTER ROLE %s WITH PASSWORD '%s';\n" % (FPBXParms.PARMS["DBUser"][0], FPBXParms.PARMS["DBUserPassword"][0]))
		tmp.write("CREATE DATABASE %s with OWNER = %s;\n" % (FPBXParms.PARMS["DBName"][0], FPBXParms.PARMS["DBUser"][0]))
		tmp.write("CREATE DATABASE freeswitch with OWNER = %s;\n" % (FPBXParms.PARMS["DBUser"][0]))
		tmp.write("GRANT ALL PRIVILEGES ON DATABASE %s to %s;\n" % (FPBXParms.PARMS["DBName"][0], FPBXParms.PARMS["DBUser"][0]))
		tmp.write("GRANT ALL PRIVILEGES ON DATABASE freeswitch to %s;\n" % (FPBXParms.PARMS["DBUser"][0]))
		tmp.close()
		ret = subprocess.call("su -l postgres -c \"psql < /var/lib/postgresql/install.sql\"", stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, shell=True)
		FPBXParms.check_ret(ret, "Create DB, User, Password")
		os.remove("/var/lib/postgresql/install.sql")
	
	#=============================================================================== 
	# Install FusionPBX
	#=============================================================================== 
	
	os.chdir("/var/www")
	
	#=============================================================================== 
	# Create a git repository and clone from GitHub
	#=============================================================================== 
	
	if os.path.isfile(".git"):
		print("Updating FusionPBX code from GitHub")
		if os.path.isfile("/usr/bin/git"):
			ret = subprocess.call("/usr/bin/git pull", stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, shell=True)
		else:
			print("git version control is not installed")
			sys.exit(6)

	else:
		print("Cloning the FusionPBX code from GitHub")
		if os.path.isfile("/usr/bin/git"):
			ret = subprocess.call("/usr/bin/git init", stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, shell=True)
			ret = subprocess.call("/usr/bin/git clone http://github.com/fusionpbx/fusionpbx", stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, shell=True)
		else:
			print("git version control is not installed")
			sys.exit(6)
		
	#=============================================================================== 
	# make sure www-data and freeswitch have access to the land
	#=============================================================================== 
	
	if FPBXParms.PARMS["FS_Install_Type"][0] == "P":
		for folder in paclfolders:
			if os.path.isdir(folder):
				ret = subprocess.call("setfacl -R -d -m u:www-data:rwx,g:www-data:rwx %s" % (folder), stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, shell=True)
				FPBXParms.check_ret(ret, "Setting user acl for %s" % (folder))
				ret = subprocess.call("setfacl -R -m u:www-data:rwx,g:www-data:rwx %s" % (folder), stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, shell=True)
				FPBXParms.check_ret(ret, "Setting user acl for %s" % (folder))
				ret = subprocess.call("setfacl -R -d -m u:freeswitch:rwx,g:freeswitch:rwx %s" % (folder), stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, shell=True)
				FPBXParms.check_ret(ret, "Setting user acl for %s" % (folder))
				ret = subprocess.call("setfacl -R -m u:freeswitch:rwx,g:freeswitch:rwx %s" % (folder), stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, shell=True)
				FPBXParms.check_ret(ret, "Setting user acl for %s" % (folder))
			else:
				print("SetFacl:")
				print("%s is not a directory on this system" % (folder))
	else:
		for folder in saclfolders:
			if os.path.isdir(folder):
				ret = subprocess.call("setfacl -R -d -m u:www-data:rwx,g:www-data:rwx %s" % (folder), stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, shell=True)
				FPBXParms.check_ret(ret, "Setting user acl for %s" % (folder))
				ret = subprocess.call("setfacl -R -m u:www-data:rwx,g:www-data:rwx %s" % (folder), stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, shell=True)
				FPBXParms.check_ret(ret, "Setting user acl for %s" % (folder))
				ret = subprocess.call("setfacl -R -d -m u:freeswitch:rwx,g:freeswitch:rwx %s" % (folder), stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, shell=True)
				FPBXParms.check_ret(ret, "Setting user acl for %s" % (folder))
				ret = subprocess.call("setfacl -R -m u:freeswitch,g:freeswitch:rwx %s" % (folder), stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, shell=True)
				FPBXParms.check_ret(ret, "Setting user acl for %s" % (folder))
			else:
				print("SetFacl:")
				print("%s is not a directory on this system" % (folder))
					
	#===============================================================================
	# Insure FusionPBX config.php is not present
	#===============================================================================
	if os.path.isfile("/var/www/fusionpbx/resources/config.php"):
		os.remove("/var/www/fusionpbx/resources/config.php")
	
	#========================================================================
	# Make sure we are back in the install root directory
	#========================================================================
	os.chdir(INSTALL_ROOT)
	   
	#===========================================================================
	# Set up the ssl certificate
	#TODO: Set up our own signed certificate when we can
	#===========================================================================
	
	if not os.path.islink("/etc/ssl/private/%s.key" % (ws)):
		os.symlink("/etc/ssl/private/ssl-cert-snakeoil.key", "/etc/ssl/private/%s.key" % (ws))
	if not os.path.islink("/etc/ssl/certs/%s.crt" % (ws)):
		os.symlink("/etc/ssl/certs/ssl-cert-snakeoil.pem", "/etc/ssl/certs/%s.crt" % (ws))
	
	#===============================================================================
	# Set up Web Server configuration to support FusionPBX
	#===============================================================================
	
	print("Setting Webserver to run FusionPBX")
	ret = subprocess.call("systemctl stop %s" % (ws), stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, shell=True)
	FPBXParms.check_ret(ret, "Stopping %s" % (ws))
	if FPBXParms.PARMS["WebServer"][0] == "a":
		ret = subprocess.call("a2enmod ssl", stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, shell=True)
		FPBXParms.check_ret(ret, "Enableing ssl")
	if FPBXParms.PARMS["WebServer"][0] == "a":
		print("We need to set up a temporary certificate for ssl")
		print("Use the server IP address for the FQDN question")
		print("This temporary certificate is set to expire in 1 year")
		print("Please set up a permanant certificate before your switch in placed in service")
		print("Please answer the questions when they appear")
		os.mkdir("/etc/apache2/ssl")
		subprocess.call("openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/apache2/ssl/apache.key -out /etc/apache2/ssl/apache.crt", shell=True)
		
	shutil.copyfile("%s/resources/%s/sites-available/fusionpbx.conf" % (INSTALL_ROOT, ws), "/etc/%s/sites-available/fusionpbx.conf" % (ws))
	if not os.path.islink("/etc/%s/sites-enabled/fusionpbx.conf" % (ws)):
		os.symlink("/etc/%s/sites-available/fusionpbx.conf" % (ws), "/etc/%s/sites-enabled/fusionpbx.conf" % (ws))
	# We need to remove the default so it does not interfere with FusionPBX
	if os.path.islink("/etc/%s/sites-enabled/default.conf" % (ws)):
		os.remove("/etc/%s/sites-enabled/default.conf" % (ws))
	if os.path.islink("/etc/%s/sites-enabled/000-default.conf" % (ws)):
		os.remove("/etc/%s/sites-enabled/000-default.conf" % (ws))
	if os.path.islink("/etc/%s/sites-enabled/default" % (ws)):
		os.remove("/etc/%s/sites-enabled/default" % (ws))
	ret = subprocess.call("systemctl start %s" % (ws), stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, shell=True)
	FPBXParms.check_ret(ret, "Starting %s" %(ws))
	
	#===============================================================================
	# Request user to run the install procedure in FusionPBX
	#===============================================================================
	
	print("The installation of FusionPBX is almost done.")
	print("Point your browser at https://%s/" % (FPBXParms.PARMS["IP"][0]))
	print("Hint: you may copy and paste the URL from the line above.")
	print("Please fill out the install pages using the information listed here.")
	FPBXParms.show_parms()
	print("I'll wait here while you do that.")
	input("Press Enter after the login screen appears. ")
	if os.path.isfile("/var/www/fusionpbx/resources/config.php"):
		print("Thank you")
	else:
		print("The configuration file is missing,")
		print("Confiuration of FusionPBX failed,")
		print("Your FusionPBX server is not installed!")
		sys.exit(6)
	return

#===============================================================================
# Restart freeswitch so FS will see any changes to config we just made.
#===============================================================================
	
	ret = subprocess.call("sytemctl restart freeswitch", stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, shell=True)
	FPBXParms.check_ret(ret, "Restarting Freeswitch")
	
def ifusionpbx():
    INSTALL_ROOT = os.getcwd()
    if os.path.isfile("%s/resources/install.json" % (INSTALL_ROOT)):
        FPBXParms.PARMS = FPBXParms.load_parms(FPBXParms.PARMS)
    else:
        print("No predefined parameters to install FusionPBX")
        sys.exit(1)

    #===============================================================================
    # Set up Postgresql
    #===============================================================================

    if FPBXParms.PARMS["DatabaseType"][0] == "P":
        print("Setting up Postgresql to support FusionPBX")
        # temp fix for pg_hba.conf permission problem
        if os.path.isfile("%s/resources/postgresql/pg_hba.conf" %
                          (INSTALL_ROOT)):
            shutil.copyfile(
                "%s/resources/postgresql/pg_hba.conf" % (INSTALL_ROOT),
                "/etc/postgresql/9.4/main/pg_hba.conf")
            ret = subprocess.call("systemctl restart postgresql",
                                  stdout=subprocess.DEVNULL,
                                  stderr=subprocess.DEVNULL,
                                  shell=True)
            FPBXParms.check_ret(ret,
                                "Restart postgres for pg_hba.conf changes")
            time.sleep(5)  # Give postgresql time to settle
        tmp = open("/var/lib/postgresql/install.sql", 'w')
        tmp.write(
            "CREATE ROLE %s WITH SUPERUSER CREATEROLE CREATEDB LOGIN PASSWORD '%s';\n"
            % (FPBXParms.PARMS["DBUser"][0],
               FPBXParms.PARMS["DBUserPassword"][0]))
        tmp.write("ALTER ROLE %s WITH PASSWORD '%s';\n" %
                  (FPBXParms.PARMS["DBUser"][0],
                   FPBXParms.PARMS["DBUserPassword"][0]))
        tmp.write("CREATE DATABASE %s with OWNER = %s;\n" %
                  (FPBXParms.PARMS["DBName"][0], FPBXParms.PARMS["DBUser"][0]))
        tmp.write("CREATE DATABASE freeswitch with OWNER = %s;\n" %
                  (FPBXParms.PARMS["DBUser"][0]))
        tmp.write("GRANT ALL PRIVILEGES ON DATABASE %s to %s;\n" %
                  (FPBXParms.PARMS["DBName"][0], FPBXParms.PARMS["DBUser"][0]))
        tmp.write("GRANT ALL PRIVILEGES ON DATABASE freeswitch to %s;\n" %
                  (FPBXParms.PARMS["DBUser"][0]))
        tmp.close()
        ret = subprocess.call(
            "su -l postgres -c \"psql < /var/lib/postgresql/install.sql\"",
            stdout=subprocess.DEVNULL,
            stderr=subprocess.DEVNULL,
            shell=True)
        FPBXParms.check_ret(ret, "Create DB, User, Password")
        os.remove("/var/lib/postgresql/install.sql")

    #===============================================================================
    # Install FusionPBX
    #===============================================================================

    os.chdir("/var/www")

    #===============================================================================
    # Create a git repository and clone from GitHub
    #===============================================================================

    if os.path.isfile(".git"):
        print("Updating FusionPBX code from GitHub")
        if os.path.isfile("/usr/bin/git"):
            ret = subprocess.call("/usr/bin/git pull",
                                  stdout=subprocess.DEVNULL,
                                  stderr=subprocess.DEVNULL,
                                  shell=True)
        else:
            print("git version control is not installed")
            sys.exit(6)

    else:
        print("Cloning the FusionPBX code from GitHub")
        if os.path.isfile("/usr/bin/git"):
            ret = subprocess.call("/usr/bin/git init",
                                  stdout=subprocess.DEVNULL,
                                  stderr=subprocess.DEVNULL,
                                  shell=True)
            ret = subprocess.call(
                "/usr/bin/git clone http://github.com/fusionpbx/fusionpbx",
                stdout=subprocess.DEVNULL,
                stderr=subprocess.DEVNULL,
                shell=True)
        else:
            print("git version control is not installed")
            sys.exit(6)

    #===============================================================================
    # make www-data has access to the land
    #===============================================================================

    if FPBXParms.PARMS["FS_Install_Type"][0] == "P":
        for folder in paclfolders:
            if os.path.isdir(folder):
                ret = subprocess.call(
                    "setfacl -R -d -m u:www-data:rwx,g:www-data:rwx %s" %
                    (folder),
                    stdout=subprocess.DEVNULL,
                    stderr=subprocess.DEVNULL,
                    shell=True)
                FPBXParms.check_ret(ret, "Setting user acl for %s" % (folder))
                ret = subprocess.call(
                    "setfacl -R -m u:www-data:rwx,g:www-data:rwx %s" %
                    (folder),
                    stdout=subprocess.DEVNULL,
                    stderr=subprocess.DEVNULL,
                    shell=True)
                FPBXParms.check_ret(ret, "Setting user acl for %s" % (folder))
            else:
                print("SetFacl:")
                print("Error: %s is not a directory" % (folder))
    else:
        for folder in saclfolders:
            if os.path.isdir(folder):
                ret = subprocess.call(
                    "setfacl -R -d -m u:www-data:rwx,g:www-data:rwx %s" %
                    (folder),
                    stdout=subprocess.DEVNULL,
                    stderr=subprocess.DEVNULL,
                    shell=True)
                FPBXParms.check_ret(ret, "Setting user acl for %s" % (folder))
                ret = subprocess.call(
                    "setfacl -R -m u:www-data:rwx,g:www-data:rwx %s" %
                    (folder),
                    stdout=subprocess.DEVNULL,
                    stderr=subprocess.DEVNULL,
                    shell=True)
                FPBXParms.check_ret(ret, "Setting user acl for %s" % (folder))
            else:
                print("SetFacl:")
                print("Error: %s is not a directory" % (folder))

    #===============================================================================
    # Insure FusionPBX config.php is not present
    #===============================================================================
    if os.path.isfile("/var/www/fusionpbx/resources/config.php"):
        os.remove("/var/www/fusionpbx/resources/config.php")

    #========================================================================
    # Make sure we are back in the install root directory
    #========================================================================
    os.chdir(INSTALL_ROOT)

    #===========================================================================
    # Set up the ssl certificate
    #TODO: Set up our own signed cert when we can
    #===========================================================================

    if not os.path.islink("/etc/ssl/private/nginx.key"):
        os.symlink("/etc/ssl/private/ssl-cert-snakeoil.key",
                   "/etc/ssl/private/nginx.key")
    if not os.path.islink("/etc/ssl/certs/nginx.crt"):
        os.symlink("/etc/ssl/certs/ssl-cert-snakeoil.pem",
                   "/etc/ssl/certs/nginx.crt")

    #===============================================================================
    # Set up Nginx config to support FusionPBX
    #===============================================================================

    print("Setting nginx to run FusionPBX")
    ret = subprocess.call("systemctl stop nginx",
                          stdout=subprocess.DEVNULL,
                          stderr=subprocess.DEVNULL,
                          shell=True)
    shutil.copyfile(
        "%s/resources/nginx/sites-available/fusionpbx" % (INSTALL_ROOT),
        "/etc/nginx/sites-available/fusionpbx")
    if not os.path.islink("/etc/nginx/sites-enabled/fusionpbx"):
        os.symlink("/etc/nginx/sites-available/fusionpbx",
                   "/etc/nginx/sites-enabled/fusionpbx")
    # We need to remove the default so it does not interfere with FusionPBX
    if os.path.islink("/etc/nginx/sites-enabled/default"):
        os.remove("/etc/nginx/sites-enabled/default")
    ret = subprocess.call("systemctl start nginx",
                          stdout=subprocess.DEVNULL,
                          stderr=subprocess.DEVNULL,
                          shell=True)

    #===============================================================================
    # Request user to run the install procedure in FusionPBX
    #===============================================================================

    print("The installation of FusionPBX is almost done.")
    print("Point your browser at http://%s/" % (FPBXParms.PARMS["IP"][0]))
    print("Hint: you may copy and paste the URL from the line above.")
    print(
        "Please fill out the install pages with the information listed here.")
    FPBXParms.show_parms()
    input(
        "I'll wait here while you do that press Enter when you are finished. ")
    if os.path.isfile("/var/www/fusionpbx/resources/config.php"):
        print("Thank you")
    else:
        print("The configuration did not get saved I can not go on.")
        sys.exit(6)
    return