mirror of
https://github.com/usmannasir/cyberpanel.git
synced 2025-11-03 11:55:57 +01:00
bug fixes to isntall code
This commit is contained in:
@@ -5263,13 +5263,31 @@ def main():
|
|||||||
checks.cyberpanel_db_password = checks.mysql_Root_password
|
checks.cyberpanel_db_password = checks.mysql_Root_password
|
||||||
|
|
||||||
# Create cyberpanel database and user (restored from v2.4.4 installCyberPanel.py)
|
# Create cyberpanel database and user (restored from v2.4.4 installCyberPanel.py)
|
||||||
|
logging.InstallLog.writeToFile(f"Attempting to create cyberpanel database with password length: {len(checks.cyberpanel_db_password)}")
|
||||||
|
|
||||||
result = mysqlUtilities.mysqlUtilities.createDatabase("cyberpanel", "cyberpanel", checks.cyberpanel_db_password, "localhost")
|
result = mysqlUtilities.mysqlUtilities.createDatabase("cyberpanel", "cyberpanel", checks.cyberpanel_db_password, "localhost")
|
||||||
if result == 1:
|
if result == 1:
|
||||||
logging.InstallLog.writeToFile("Cyberpanel database and user created successfully!")
|
logging.InstallLog.writeToFile("✅ Cyberpanel database and user created successfully!")
|
||||||
preFlightsChecks.stdOut("Cyberpanel database and user created successfully!", 1)
|
preFlightsChecks.stdOut("Cyberpanel database and user created successfully!", 1)
|
||||||
|
|
||||||
|
# Verify the user was created by testing the connection
|
||||||
|
try:
|
||||||
|
import subprocess
|
||||||
|
test_cmd = f"mysql -u cyberpanel -p{checks.cyberpanel_db_password} -e 'SELECT 1;'"
|
||||||
|
test_result = subprocess.call(test_cmd, shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
||||||
|
if test_result == 0:
|
||||||
|
logging.InstallLog.writeToFile("✅ Verified: cyberpanel user can connect to MySQL")
|
||||||
else:
|
else:
|
||||||
logging.InstallLog.writeToFile("Warning: Cyberpanel database creation returned error code")
|
logging.InstallLog.writeToFile("❌ WARNING: cyberpanel user cannot connect to MySQL - authentication may have failed")
|
||||||
preFlightsChecks.stdOut("Warning: Database creation issue", 1)
|
except Exception as verify_error:
|
||||||
|
logging.InstallLog.writeToFile(f"Could not verify MySQL connection: {str(verify_error)}")
|
||||||
|
else:
|
||||||
|
error_msg = f"❌ CRITICAL ERROR: Cyberpanel database creation failed (returned {result})"
|
||||||
|
logging.InstallLog.writeToFile(error_msg)
|
||||||
|
preFlightsChecks.stdOut("Database creation failed - installation cannot continue", 1)
|
||||||
|
|
||||||
|
# Don't continue with settings.py update if database creation failed
|
||||||
|
raise Exception("Database creation failed - cyberpanel user does not exist")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.InstallLog.writeToFile(f"Error creating cyberpanel database: {str(e)}")
|
logging.InstallLog.writeToFile(f"Error creating cyberpanel database: {str(e)}")
|
||||||
preFlightsChecks.stdOut(f"Error: Database creation failed: {str(e)}", 1)
|
preFlightsChecks.stdOut(f"Error: Database creation failed: {str(e)}", 1)
|
||||||
|
|||||||
@@ -6,9 +6,21 @@ class mysqlUtilities:
|
|||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def createDatabase(dbname, dbuser, dbpassword, publicip):
|
def createDatabase(dbname, dbuser, dbpassword, publicip):
|
||||||
|
"""
|
||||||
|
Create database and user with improved error handling and validation
|
||||||
|
"""
|
||||||
|
import logging
|
||||||
|
|
||||||
|
# Validate input parameters
|
||||||
|
if not dbname or not dbuser or not dbpassword:
|
||||||
|
logging.InstallLog.writeToFile(f"ERROR: Missing required parameters - dbname: {dbname}, dbuser: {dbuser}, password: {'SET' if dbpassword else 'EMPTY'}")
|
||||||
|
return 0
|
||||||
|
|
||||||
|
logging.InstallLog.writeToFile(f"Creating database '{dbname}' and user '{dbuser}' with password length: {len(dbpassword)}")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
createDB = "CREATE DATABASE " + dbname
|
# Step 1: Create database
|
||||||
|
createDB = "CREATE DATABASE IF NOT EXISTS " + dbname
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from json import loads
|
from json import loads
|
||||||
@@ -16,6 +28,7 @@ class mysqlUtilities:
|
|||||||
|
|
||||||
initCommand = 'mariadb -h %s --port %s -u %s -p%s -e "' % (mysqlData['mysqlhost'], mysqlData['mysqlport'], mysqlData['mysqluser'], mysqlData['mysqlpassword'])
|
initCommand = 'mariadb -h %s --port %s -u %s -p%s -e "' % (mysqlData['mysqlhost'], mysqlData['mysqlport'], mysqlData['mysqluser'], mysqlData['mysqlpassword'])
|
||||||
remote = 1
|
remote = 1
|
||||||
|
logging.InstallLog.writeToFile("Using remote MySQL configuration")
|
||||||
except:
|
except:
|
||||||
passFile = "/etc/cyberpanel/mysqlPassword"
|
passFile = "/etc/cyberpanel/mysqlPassword"
|
||||||
|
|
||||||
@@ -25,9 +38,12 @@ class mysqlUtilities:
|
|||||||
|
|
||||||
initCommand = 'mariadb -u root -p' + password + ' -e "'
|
initCommand = 'mariadb -u root -p' + password + ' -e "'
|
||||||
remote = 0
|
remote = 0
|
||||||
|
logging.InstallLog.writeToFile("Using local MySQL configuration")
|
||||||
|
|
||||||
command = initCommand + createDB + '"'
|
command = initCommand + createDB + '"'
|
||||||
|
|
||||||
|
logging.InstallLog.writeToFile(f"Executing database creation: CREATE DATABASE IF NOT EXISTS {dbname}")
|
||||||
|
|
||||||
if install.preFlightsChecks.debug:
|
if install.preFlightsChecks.debug:
|
||||||
print(command)
|
print(command)
|
||||||
time.sleep(10)
|
time.sleep(10)
|
||||||
@@ -35,15 +51,36 @@ class mysqlUtilities:
|
|||||||
cmd = shlex.split(command)
|
cmd = shlex.split(command)
|
||||||
res = subprocess.call(cmd)
|
res = subprocess.call(cmd)
|
||||||
|
|
||||||
if res == 1:
|
if res != 0:
|
||||||
|
logging.InstallLog.writeToFile(f"ERROR: Database creation failed with return code: {res}")
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
if remote:
|
|
||||||
createUser = "CREATE USER '" + dbuser + "'@'%s' IDENTIFIED BY '" % (publicip) + dbpassword + "'"
|
|
||||||
else:
|
else:
|
||||||
createUser = "CREATE USER '" + dbuser + "'@'localhost' IDENTIFIED BY '" + dbpassword + "'"
|
logging.InstallLog.writeToFile(f"✓ Database '{dbname}' created successfully")
|
||||||
|
|
||||||
|
# Step 2: Create user (drop first if exists to avoid conflicts)
|
||||||
|
if remote:
|
||||||
|
dropUser = f"DROP USER IF EXISTS '{dbuser}'@'{publicip}'"
|
||||||
|
createUser = f"CREATE USER '{dbuser}'@'{publicip}' IDENTIFIED BY '{dbpassword}'"
|
||||||
|
host = publicip
|
||||||
|
else:
|
||||||
|
dropUser = f"DROP USER IF EXISTS '{dbuser}'@'localhost'"
|
||||||
|
createUser = f"CREATE USER '{dbuser}'@'localhost' IDENTIFIED BY '{dbpassword}'"
|
||||||
|
host = 'localhost'
|
||||||
|
|
||||||
|
# Drop user if exists
|
||||||
|
command = initCommand + dropUser + '"'
|
||||||
|
logging.InstallLog.writeToFile(f"Dropping existing user '{dbuser}'@'{host}' if exists")
|
||||||
|
|
||||||
|
if install.preFlightsChecks.debug:
|
||||||
|
print(command)
|
||||||
|
time.sleep(10)
|
||||||
|
|
||||||
|
cmd = shlex.split(command)
|
||||||
|
subprocess.call(cmd) # Ignore return code for DROP USER IF EXISTS
|
||||||
|
|
||||||
|
# Create user
|
||||||
command = initCommand + createUser + '"'
|
command = initCommand + createUser + '"'
|
||||||
|
logging.InstallLog.writeToFile(f"Creating user '{dbuser}'@'{host}' with password")
|
||||||
|
|
||||||
if install.preFlightsChecks.debug:
|
if install.preFlightsChecks.debug:
|
||||||
print(command)
|
print(command)
|
||||||
@@ -52,37 +89,21 @@ class mysqlUtilities:
|
|||||||
cmd = shlex.split(command)
|
cmd = shlex.split(command)
|
||||||
res = subprocess.call(cmd)
|
res = subprocess.call(cmd)
|
||||||
|
|
||||||
if res == 1:
|
if res != 0:
|
||||||
|
logging.InstallLog.writeToFile(f"ERROR: User creation failed with return code: {res}")
|
||||||
return 0
|
return 0
|
||||||
else:
|
else:
|
||||||
|
logging.InstallLog.writeToFile(f"✓ User '{dbuser}'@'{host}' created successfully")
|
||||||
|
else:
|
||||||
|
|
||||||
|
# Step 3: Handle special cases and grant privileges
|
||||||
if remote:
|
if remote:
|
||||||
|
### DigitalOcean MySQL compatibility
|
||||||
### DO Check
|
|
||||||
|
|
||||||
if mysqlData['mysqlhost'].find('ondigitalocean') > -1:
|
if mysqlData['mysqlhost'].find('ondigitalocean') > -1:
|
||||||
|
alterUserPassword = f"ALTER USER '{dbuser}'@'{publicip}' IDENTIFIED WITH mysql_native_password BY '{dbpassword}'"
|
||||||
alterUserPassword = "ALTER USER 'cyberpanel'@'%s' IDENTIFIED WITH mysql_native_password BY '%s'" % (
|
|
||||||
publicip, dbpassword)
|
|
||||||
command = initCommand + alterUserPassword + '"'
|
command = initCommand + alterUserPassword + '"'
|
||||||
|
|
||||||
if install.preFlightsChecks.debug:
|
logging.InstallLog.writeToFile(f"Applying DigitalOcean MySQL compatibility for '{dbuser}'@'{publicip}'")
|
||||||
print(command)
|
|
||||||
time.sleep(10)
|
|
||||||
|
|
||||||
cmd = shlex.split(command)
|
|
||||||
subprocess.call(cmd)
|
|
||||||
|
|
||||||
## RDS Check
|
|
||||||
|
|
||||||
if mysqlData['mysqlhost'].find('rds.amazon') == -1:
|
|
||||||
dropDB = "GRANT ALL PRIVILEGES ON " + dbname + ".* TO '" + dbuser + "'@'%s'" % (publicip)
|
|
||||||
else:
|
|
||||||
dropDB = "GRANT INDEX, DROP, UPDATE, ALTER, CREATE, SELECT, INSERT, DELETE ON " + dbname + ".* TO '" + dbuser + "'@'%s'" % (publicip)
|
|
||||||
else:
|
|
||||||
dropDB = "GRANT ALL PRIVILEGES ON " + dbname + ".* TO '" + dbuser + "'@'localhost'"
|
|
||||||
|
|
||||||
command = initCommand + dropDB + '"'
|
|
||||||
|
|
||||||
if install.preFlightsChecks.debug:
|
if install.preFlightsChecks.debug:
|
||||||
print(command)
|
print(command)
|
||||||
@@ -91,9 +112,53 @@ class mysqlUtilities:
|
|||||||
cmd = shlex.split(command)
|
cmd = shlex.split(command)
|
||||||
res = subprocess.call(cmd)
|
res = subprocess.call(cmd)
|
||||||
|
|
||||||
if res == 1:
|
if res != 0:
|
||||||
return 0
|
logging.InstallLog.writeToFile(f"WARNING: DigitalOcean ALTER USER failed with return code: {res}")
|
||||||
|
|
||||||
return 1
|
## RDS vs Standard MySQL permissions
|
||||||
except BaseException as msg:
|
if mysqlData['mysqlhost'].find('rds.amazon') == -1:
|
||||||
|
grantPrivileges = f"GRANT ALL PRIVILEGES ON {dbname}.* TO '{dbuser}'@'{publicip}'"
|
||||||
|
else:
|
||||||
|
grantPrivileges = f"GRANT INDEX, DROP, UPDATE, ALTER, CREATE, SELECT, INSERT, DELETE ON {dbname}.* TO '{dbuser}'@'{publicip}'"
|
||||||
|
host = publicip
|
||||||
|
else:
|
||||||
|
grantPrivileges = f"GRANT ALL PRIVILEGES ON {dbname}.* TO '{dbuser}'@'localhost'"
|
||||||
|
host = 'localhost'
|
||||||
|
|
||||||
|
# Grant privileges
|
||||||
|
command = initCommand + grantPrivileges + '"'
|
||||||
|
logging.InstallLog.writeToFile(f"Granting privileges on database '{dbname}' to user '{dbuser}'@'{host}'")
|
||||||
|
|
||||||
|
if install.preFlightsChecks.debug:
|
||||||
|
print(command)
|
||||||
|
time.sleep(10)
|
||||||
|
|
||||||
|
cmd = shlex.split(command)
|
||||||
|
res = subprocess.call(cmd)
|
||||||
|
|
||||||
|
if res != 0:
|
||||||
|
logging.InstallLog.writeToFile(f"ERROR: Grant privileges failed with return code: {res}")
|
||||||
|
return 0
|
||||||
|
else:
|
||||||
|
logging.InstallLog.writeToFile(f"✓ Privileges granted successfully")
|
||||||
|
|
||||||
|
# Step 4: Flush privileges
|
||||||
|
flushCommand = initCommand + "FLUSH PRIVILEGES" + '"'
|
||||||
|
logging.InstallLog.writeToFile("Flushing MySQL privileges")
|
||||||
|
|
||||||
|
cmd = shlex.split(flushCommand)
|
||||||
|
res = subprocess.call(cmd)
|
||||||
|
|
||||||
|
if res != 0:
|
||||||
|
logging.InstallLog.writeToFile(f"WARNING: FLUSH PRIVILEGES failed with return code: {res}")
|
||||||
|
else:
|
||||||
|
logging.InstallLog.writeToFile("✓ Privileges flushed successfully")
|
||||||
|
|
||||||
|
logging.InstallLog.writeToFile(f"✅ Database '{dbname}' and user '{dbuser}' created successfully!")
|
||||||
|
return 1
|
||||||
|
|
||||||
|
except BaseException as msg:
|
||||||
|
logging.InstallLog.writeToFile(f"❌ CRITICAL ERROR in createDatabase: {str(msg)}")
|
||||||
|
import traceback
|
||||||
|
logging.InstallLog.writeToFile(f"Full traceback: {traceback.format_exc()}")
|
||||||
return 0
|
return 0
|
||||||
Reference in New Issue
Block a user