File zm_database_init of Package ZoneMinder

#!/usr/bin/python

# zm_database_init version 0.3.1
#
# Author: Monex
# this script is under the same license as the package itself.
#
# Please submit bugfixes or comments to monex@liquid-co.de

import os.path
import re
import sys
import os

########## important settings ################
VERSION = "1.22.3"# current version of zm

ZM_PATH ="/srv/www/htdocs/zm" # path to zm webdir

ZM_CONFIG = "/etc/zm.conf"
########## /important settings ###############

########## globals ###############
version_string = re.compile("ZM_VERSION=(.*)")
sql_string = re.compile("zm_update-(.*)\.sql")
db_pass_string = re.compile("ZM_DB_PASS=")
zm_db_string = re.compile("ZM_PATH_BUILD=(.*)")

def updateVersionString(config, oldversion, toversion):
	""" replaces the version string in config file with new version string"""
	config = config.replace(oldversion, toversion, 1)
	writeConfig(config)
	
def checkZmPath(config):
	""" checks for the correct path in zm.conf for db upgrade"""
	path = zm_db_string.search(config)
	if path:
		if path.group(1) != ZM_PATH:
			print "found wrong ZM_BUILD Path in config file could not perform db upgrade"
			proceed = raw_input("should it be updated? [Y/n]: ")
			if proceed == "Y" or proceed == "" or proceed == "y":
				lines = config.split("\n")
				config = ""
				for line in lines:
					if zm_db_string.search(line):
						line = "ZM_PATH_BUILD=" + ZM_PATH
					config += line + "\n"
				writeConfig(config)
				print "ZM_BUILD_PATH successfully updated"
			else:
				print "WARNING: update may fail when ZM_PATH_BUILD not set to " + ZM_PATH
	else:
		print "config file wrong"
		sys.exit(1)
	return config

def writeConfig(config):
	""" writes the configuration to the config file"""
	file = open(ZM_CONFIG, "w")
	file.write(config)
	file.close()
	
def enter_mysql_root_pw():
	"""dialog for the mysql root password"""
	os.system("stty -echo")
	root_passwd = raw_input("enter mysql root password: ")
	os.system("stty echo")
	print # newline
	return root_passwd

def run_stuff():
	file = open(ZM_CONFIG, "r")
	config = file.read()
	file.close()
	version = version_string.search(config)
	mysql_root_password = ""
	if version == None:
		print "ERROR: could not find out version from config file"
		sys.exit(1)

	if VERSION == version.group(1):
		print "new install create db"
		proceed = raw_input("run mysql command to create db as user root? [Y/n]: ")
		if proceed == "Y" or proceed == "" or proceed == "y":
			mysql_root_password = enter_mysql_root_pw()
			done = os.system("mysql mysql < "+ZM_PATH+"/db/zm_create.sql --password="+mysql_root_password+" -u root")
			if done != 0:
				print "SQL ERROR: exiting"
				sys.exit(1)
		
		proceed = raw_input("create user zm_admin for zoneminder? [Y/n]: ")
		if proceed == "Y" or proceed == "" or proceed == "y":
			os.system("stty -echo")
			passwd = raw_input("Enter new passwd for user zm_admin: ")
			os.system("stty echo")
			if passwd != "":
				print # newline
				os.system("stty -echo")
				t_pass = raw_input("retype password: ")
				os.system("stty echo")
				if (t_pass != passwd):
					print "ERROR: password do not match. exiting"
					sys.exit(1)
				print "\n" # 2 newlines
				if mysql_root_password == "":
					mysql_root_password = enter_mysql_root_pw()
				done = os.system("""echo "GRANT USAGE ON * . * TO 'zm_admin'@'localhost' IDENTIFIED BY '"""+ passwd + """' WITH MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 ; GRANT SELECT , INSERT , UPDATE , DELETE ON zm . * TO 'zm_admin'@'localhost';" | mysql mysql -u root --password=""" + mysql_root_password)
				if done != 0:
					print "error on mysql. exiting"
					sys.exit(1)
			else:
				print "ERROR: empty passphrase. exiting"
				sys.exit(1)
				
			config_update = raw_input("should i update config file with new passwd? [Y/n]: ")
			if config_update == "Y" or config_update == "y" or config_update == "":
				lines = config.split("\n")
				config = ""
				for line in lines:
					if db_pass_string.search(line):
						line = "ZM_DB_PASS=" + passwd
					config += line + "\n"
			writeConfig(config)
	else:
		print "when update fails or you are not upgrading from"
		print "previous rpm version please ensure that the ZM_PATH_BUILD is set to"
		print ZM_PATH + " to find the database update skripts"
		print
		print "when done upgrade using zmupdate.pl before then answer this with n"
		proceed = raw_input("Do you want to perform db update? [Y/n]: ")
		if proceed == "Y" or proceed == "" or proceed == "y":
			config = checkZmPath(config)
			print "updating config file version string"
			updateVersionString(config, version.group(1), VERSION)
			mysql_root_password = enter_mysql_root_pw()
			print
			done = os.system("zmupdate.pl -version="+version.group(1)+" --user=root --pass="+mysql_root_password )
			if done != 0:
				print "ERROR: upgrade fails"
				# undo config update
				print "undo config file update version string"
				updateVersionString(config, VERSION, version.group(1))
				sys.exit(1)
		else:
			print "updating config file version string"
			updateVersionString(config, version.group(1), VERSION)
		print "check documentation for new config file settings"
	
	if os.path.isfile(ZM_PATH + "/lock"):
		print "removing lock file"
		os.remove(ZM_PATH + "/lock")
	print "done"

def main():
	try:
		print "INFO: when db is correctly installed and you just reinstalled rpm, the answer all questions with 'n'"
		if os.path.isfile(ZM_PATH + "/lock"):
			run_stuff()
		else:
			proceed = raw_input("no lockfile found, proceed anyway? [y/N]: ")
			if proceed == "y" or proceed == "Y":
				run_stuff()
	except KeyboardInterrupt:
		print "Interrupted exiting"
		sys.exit(0)


if __name__ == "__main__":
	main()
	
	
openSUSE Build Service is sponsored by