Files
CyberPanel/install/install.py

474 lines
17 KiB
Python
Raw Normal View History

2017-10-24 19:16:36 +05:00
import sys
import subprocess
import shutil
2019-12-12 13:51:35 +05:00
import installLog as logging
2017-10-24 19:16:36 +05:00
import argparse
import os
import shlex
2019-12-12 13:51:35 +05:00
from firewallUtilities import FirewallUtilities
import time
2018-06-11 21:04:55 +05:00
import string
import random
2018-11-06 13:03:12 +05:00
import socket
from os.path import *
from stat import *
import stat
char_set = {'small': 'abcdefghijklmnopqrstuvwxyz',
'nums': '0123456789',
'big': 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
}
def generate_pass(length=14):
2019-12-10 22:34:39 +05:00
chars = string.ascii_uppercase + string.ascii_lowercase + string.digits
size = length
return ''.join(random.choice(chars) for x in range(size))
2017-10-24 19:16:36 +05:00
# There can not be peace without first a great suffering.
2017-10-24 19:16:36 +05:00
2018-11-16 14:41:40 +05:00
# distros
2018-11-06 00:19:58 +05:00
2018-11-16 14:41:40 +05:00
centos = 0
ubuntu = 1
2018-10-25 16:10:01 -04:00
2019-03-26 16:19:03 +05:00
def get_distro():
distro = -1
distro_file = ""
if exists("/etc/lsb-release"):
distro_file = "/etc/lsb-release"
with open(distro_file) as f:
for line in f:
if line == "DISTRIB_ID=Ubuntu\n":
distro = ubuntu
elif exists("/etc/os-release"):
distro_file = "/etc/os-release"
distro = centos
else:
logging.InstallLog.writeToFile("Can't find linux release file - fatal error")
preFlightsChecks.stdOut("Can't find linux release file - fatal error")
os._exit(os.EX_UNAVAILABLE)
if distro == -1:
logging.InstallLog.writeToFile("Can't find distro name in " + distro_file + " - fatal error")
preFlightsChecks.stdOut("Can't find distro name in " + distro_file + " - fatal error")
os._exit(os.EX_UNAVAILABLE)
return distro
def get_Ubuntu_release():
release = -1
if exists("/etc/lsb-release"):
distro_file = "/etc/lsb-release"
with open(distro_file) as f:
for line in f:
if line[:16] == "DISTRIB_RELEASE=":
release = float(line[16:])
if release == -1:
preFlightsChecks.stdOut("Can't find distro release name in " + distro_file + " - fatal error", 1, 1,
os.EX_UNAVAILABLE)
else:
logging.InstallLog.writeToFile("Can't find linux release file - fatal error")
preFlightsChecks.stdOut("Can't find linux release file - fatal error")
os._exit(os.EX_UNAVAILABLE)
return release
2018-11-06 00:19:58 +05:00
2018-11-16 14:41:40 +05:00
class preFlightsChecks:
cyberPanelMirror = "mirror.cyberpanel.net/pip"
2018-03-04 13:37:58 +05:00
2018-11-16 14:41:40 +05:00
def __init__(self, rootPath, ip, path, cwd, cyberPanelPath, distro):
2017-10-24 19:16:36 +05:00
self.ipAddr = ip
self.path = path
self.cwd = cwd
self.server_root_path = rootPath
2017-12-09 22:30:10 +05:00
self.cyberPanelPath = cyberPanelPath
2018-10-25 16:10:01 -04:00
self.distro = distro
2017-10-24 19:16:36 +05:00
2019-03-26 16:19:03 +05:00
@staticmethod
def stdOut(message, log=0, do_exit=0, code=os.EX_OK):
print("\n\n")
2019-12-10 15:09:10 +05:00
print(("[" + time.strftime(
"%m.%d.%Y_%H-%M-%S") + "] #########################################################################\n"))
print(("[" + time.strftime("%m.%d.%Y_%H-%M-%S") + "] " + message + "\n"))
print(("[" + time.strftime(
"%m.%d.%Y_%H-%M-%S") + "] #########################################################################\n"))
2019-03-26 16:19:03 +05:00
if log:
logging.InstallLog.writeToFile(message)
if do_exit:
2019-11-13 19:59:42 +05:00
logging.InstallLog.writeToFile(message)
2019-03-26 16:19:03 +05:00
sys.exit(code)
2019-03-21 23:26:42 +05:00
def mountTemp(self):
try:
## On OpenVZ there is an issue using .tempdisk for /tmp as it breaks network on container after reboot.
if subprocess.check_output('systemd-detect-virt').find("openvz") > -1:
varTmp = "/var/tmp /tmp none bind 0 0\n"
fstab = "/etc/fstab"
writeToFile = open(fstab, "a")
writeToFile.writelines(varTmp)
writeToFile.close()
else:
command = "dd if=/dev/zero of=/usr/.tempdisk bs=100M count=15"
2019-11-13 16:13:52 +05:00
preFlightsChecks.call(command, self.distro, command,
command,
2019-11-13 13:08:26 +05:00
1, 0, os.EX_OSERR)
command = "mkfs.ext4 -F /usr/.tempdisk"
2019-11-13 16:13:52 +05:00
preFlightsChecks.call(command, self.distro, command,
command,
2019-11-13 13:08:26 +05:00
1, 0, os.EX_OSERR)
command = "mkdir -p /usr/.tmpbak/"
2019-11-13 16:13:52 +05:00
preFlightsChecks.call(command, self.distro, command,
command,
2019-11-13 13:08:26 +05:00
1, 0, os.EX_OSERR)
command = "cp -pr /tmp/* /usr/.tmpbak/"
subprocess.call(command, shell=True)
command = "mount -o loop,rw,nodev,nosuid,noexec,nofail /usr/.tempdisk /tmp"
2019-11-13 16:13:52 +05:00
preFlightsChecks.call(command, self.distro, command,
command,
2019-11-13 13:08:26 +05:00
1, 0, os.EX_OSERR)
command = "chmod 1777 /tmp"
2019-11-13 16:13:52 +05:00
preFlightsChecks.call(command, self.distro, command,
command,
2019-11-13 13:08:26 +05:00
1, 0, os.EX_OSERR)
command = "cp -pr /usr/.tmpbak/* /tmp/"
subprocess.call(command, shell=True)
command = "rm -rf /usr/.tmpbak"
2019-11-13 16:13:52 +05:00
preFlightsChecks.call(command, self.distro, command,
command,
2019-11-13 13:08:26 +05:00
1, 0, os.EX_OSERR)
command = "mount --bind /tmp /var/tmp"
2019-11-13 16:13:52 +05:00
preFlightsChecks.call(command, self.distro, command,
command,
2019-11-13 13:08:26 +05:00
1, 0, os.EX_OSERR)
tmp = "/usr/.tempdisk /tmp ext4 loop,rw,noexec,nosuid,nodev,nofail 0 0\n"
varTmp = "/tmp /var/tmp none bind 0 0\n"
fstab = "/etc/fstab"
writeToFile = open(fstab, "a")
writeToFile.writelines(tmp)
writeToFile.writelines(varTmp)
writeToFile.close()
2019-03-21 23:26:42 +05:00
2019-12-10 15:09:10 +05:00
except BaseException as msg:
preFlightsChecks.stdOut('[ERROR] ' + str(msg))
2019-03-21 23:26:42 +05:00
return 0
2018-11-07 09:20:05 -05:00
@staticmethod
2018-11-07 09:28:09 -05:00
def pureFTPDServiceName(distro):
2018-11-07 09:20:05 -05:00
if distro == ubuntu:
return 'pure-ftpd'
2018-11-07 09:20:05 -05:00
return 'pure-ftpd'
2018-11-14 11:37:17 -05:00
@staticmethod
def resFailed(distro, res):
if distro == ubuntu and res != 0:
return True
elif distro == centos and res != 0:
2018-11-14 11:37:17 -05:00
return True
return False
@staticmethod
2018-11-16 14:41:40 +05:00
def call(command, distro, bracket, message, log=0, do_exit=0, code=os.EX_OK):
2019-11-13 18:08:14 +05:00
finalMessage = 'Running: %s' % (message)
preFlightsChecks.stdOut(finalMessage, log)
2018-11-14 11:37:17 -05:00
count = 0
while True:
2019-03-26 16:19:03 +05:00
res = subprocess.call(shlex.split(command))
2018-11-14 11:37:17 -05:00
if preFlightsChecks.resFailed(distro, res):
count = count + 1
2019-11-13 18:08:14 +05:00
finalMessage = 'Running %s failed. Running again, try number %s' % (message, str(count))
preFlightsChecks.stdOut(finalMessage)
2018-11-14 11:37:17 -05:00
if count == 3:
fatal_message = ''
if do_exit:
fatal_message = '. Fatal error, see /var/log/installLogs.txt for full details'
2019-11-13 18:08:14 +05:00
preFlightsChecks.stdOut("[ERROR] We are not able to run " + message + ' return code: ' + str(res) +
fatal_message + ".", 1, do_exit, code)
2018-11-14 11:37:17 -05:00
return False
else:
preFlightsChecks.stdOut('Successfully ran: %s.' % (message), log)
2018-11-14 11:37:17 -05:00
break
2019-11-13 18:08:14 +05:00
2018-11-14 11:37:17 -05:00
return True
2018-11-06 00:19:58 +05:00
def checkIfSeLinuxDisabled(self):
try:
command = "sestatus"
2019-03-26 16:19:03 +05:00
output = subprocess.check_output(shlex.split(command))
2018-11-06 00:19:58 +05:00
if output.find("disabled") > -1 or output.find("permissive") > -1:
logging.InstallLog.writeToFile("SELinux Check OK. [checkIfSeLinuxDisabled]")
preFlightsChecks.stdOut("SELinux Check OK.")
return 1
else:
2018-11-16 14:41:40 +05:00
logging.InstallLog.writeToFile(
"SELinux is enabled, please disable SELinux and restart the installation!")
2018-11-06 00:19:58 +05:00
preFlightsChecks.stdOut("Installation failed, consult: /var/log/installLogs.txt")
os._exit(0)
2019-12-10 15:09:10 +05:00
except BaseException as msg:
logging.InstallLog.writeToFile('[ERROR] ' + str(msg) + "[checkIfSeLinuxDisabled]")
logging.InstallLog.writeToFile('[ERROR] ' + "SELinux Check OK. [checkIfSeLinuxDisabled]")
preFlightsChecks.stdOut('[ERROR] ' + "SELinux Check OK.")
2018-11-06 00:19:58 +05:00
return 1
def checkPythonVersion(self):
2019-12-10 15:16:11 +05:00
if sys.version_info[0] == 3:
return 1
else:
preFlightsChecks.stdOut("You are running Unsupported python version, please install python 2.7")
2018-07-05 15:22:48 +05:00
os._exit(0)
2017-12-09 22:30:10 +05:00
def setup_account_cyberpanel(self):
try:
2017-12-09 22:30:10 +05:00
2018-10-26 09:56:12 -04:00
if self.distro == centos:
2018-12-13 04:23:08 +05:00
command = "yum install sudo -y"
2019-11-13 16:13:52 +05:00
preFlightsChecks.call(command, self.distro, command,
command,
2018-12-17 18:46:34 +05:00
1, 0, os.EX_OSERR)
2017-12-09 22:30:10 +05:00
##
2017-12-09 22:30:10 +05:00
2018-10-26 09:56:12 -04:00
if self.distro == ubuntu:
2019-03-30 14:21:52 +05:00
# self.stdOut("Fix sudoers")
# try:
# fileName = '/etc/sudoers'
# data = open(fileName, 'r').readlines()
#
# writeDataToFile = open(fileName, 'w')
# for line in data:
# if line[:5] == '%sudo':
# writeDataToFile.write('%sudo ALL=(ALL:ALL) NOPASSWD: ALL\n')
# else:
# writeDataToFile.write(line)
# writeDataToFile.close()
# except IOError as err:
# self.stdOut("Error in fixing sudoers file: " + str(err), 1, 1, os.EX_OSERR)
2018-10-26 09:52:07 -04:00
self.stdOut("Add Cyberpanel user")
2019-04-15 15:54:23 +05:00
command = 'adduser --disabled-login --gecos "" cyberpanel'
2019-11-13 16:13:52 +05:00
preFlightsChecks.call(command, self.distro, command,command,1, 1, os.EX_OSERR)
2017-11-05 03:02:51 +05:00
2018-10-25 16:10:01 -04:00
else:
2019-03-30 14:21:52 +05:00
command = "useradd -s /bin/false cyberpanel"
2019-11-13 16:13:52 +05:00
preFlightsChecks.call(command, self.distro, command,command,1, 1, os.EX_OSERR)
2017-11-05 03:02:51 +05:00
2019-03-30 14:21:52 +05:00
# ##
#
# command = "usermod -aG wheel cyberpanel"
# preFlightsChecks.call(command, self.distro, '[setup_account_cyberpanel]',
# 'add user cyberpanel',
# 1, 0, os.EX_OSERR)
2017-11-05 03:02:51 +05:00
2018-11-16 14:41:40 +05:00
###############################
2017-11-05 03:02:51 +05:00
2019-03-30 14:21:52 +05:00
# path = "/etc/sudoers"
#
# data = open(path, 'r').readlines()
#
# writeToFile = open(path, 'w')
#
# for items in data:
# if items.find("wheel ALL=(ALL) NOPASSWD: ALL") > -1:
# writeToFile.writelines("%wheel ALL=(ALL) NOPASSWD: ALL")
# else:
# writeToFile.writelines(items)
#
# writeToFile.close()
2018-11-06 00:19:58 +05:00
###############################
2019-04-15 15:54:23 +05:00
### Docker User/group
if self.distro == ubuntu:
command = 'adduser --disabled-login --gecos "" docker'
else:
command = "adduser docker"
2019-11-13 16:13:52 +05:00
preFlightsChecks.call(command, self.distro, command,command,1, 0, os.EX_OSERR)
2019-04-15 15:54:23 +05:00
command = 'groupadd docker'
2019-11-13 16:13:52 +05:00
preFlightsChecks.call(command, self.distro, command,command,1, 0, os.EX_OSERR)
2019-04-15 15:54:23 +05:00
command = 'usermod -aG docker docker'
2019-11-13 16:13:52 +05:00
preFlightsChecks.call(command, self.distro, command,command,1, 0, os.EX_OSERR)
2019-04-15 15:54:23 +05:00
command = 'usermod -aG docker cyberpanel'
2019-11-13 16:13:52 +05:00
preFlightsChecks.call(command, self.distro, command,command,1, 0, os.EX_OSERR)
2019-04-15 15:54:23 +05:00
###
2018-12-13 04:23:08 +05:00
command = "mkdir -p /etc/letsencrypt/live/"
2019-11-13 16:13:52 +05:00
preFlightsChecks.call(command, self.distro, command,command,1, 0, os.EX_OSERR)
2018-11-06 00:19:58 +05:00
2019-12-10 15:09:10 +05:00
except BaseException as msg:
2018-12-13 04:23:08 +05:00
logging.InstallLog.writeToFile("[ERROR] setup_account_cyberpanel. " + str(msg))
2017-11-05 03:02:51 +05:00
def yum_update(self):
2019-11-13 16:13:52 +05:00
command = 'yum update -y'
preFlightsChecks.call(command, self.distro, command, command, 1, 0, os.EX_OSERR)
2017-10-24 19:16:36 +05:00
def installCyberPanelRepo(self):
2018-10-26 09:11:17 -04:00
self.stdOut("Install Cyberpanel repo")
2017-10-24 19:16:36 +05:00
2018-10-26 09:56:12 -04:00
if self.distro == ubuntu:
2018-10-25 16:10:01 -04:00
try:
filename = "enable_lst_debain_repo.sh"
command = "wget http://rpms.litespeedtech.com/debian/" + filename
2019-11-13 16:13:52 +05:00
preFlightsChecks.call(command, self.distro, command, command, 1, 1, os.EX_OSERR)
2018-10-25 16:10:01 -04:00
os.chmod(filename, S_IRWXU | S_IRWXG)
2018-10-25 16:10:01 -04:00
command = "./" + filename
2019-11-13 16:13:52 +05:00
preFlightsChecks.call(command, self.distro, command, command, 1, 1, os.EX_OSERR)
2018-10-25 16:10:01 -04:00
except:
logging.InstallLog.writeToFile("[ERROR] Exception during CyberPanel install")
preFlightsChecks.stdOut("[ERROR] Exception during CyberPanel install")
2018-10-25 16:10:01 -04:00
os._exit(os.EX_SOFTWARE)
2018-11-09 22:01:28 +05:00
else:
2019-11-13 13:08:26 +05:00
command = 'rpm -ivh http://rpms.litespeedtech.com/centos/litespeed-repo-1.1-1.el7.noarch.rpm'
2019-11-13 16:13:52 +05:00
preFlightsChecks.call(command, self.distro, command, command, 1, 1, os.EX_OSERR)
2017-10-24 19:16:36 +05:00
2017-12-09 22:30:10 +05:00
def enableEPELRepo(self):
2019-11-13 13:08:26 +05:00
command = 'yum -y install epel-release'
2019-11-13 16:13:52 +05:00
preFlightsChecks.call(command, self.distro, command, command, 1, 1, os.EX_OSERR)
2017-10-24 19:16:36 +05:00
def install_pip(self):
2018-10-26 09:11:17 -04:00
self.stdOut("Install pip")
2019-11-13 13:08:26 +05:00
if self.distro == ubuntu:
command = "apt-get -y install python-pip"
else:
command = "yum -y install python-pip"
2017-10-24 19:16:36 +05:00
2019-11-13 16:13:52 +05:00
preFlightsChecks.call(command, self.distro, command, command, 1, 1, os.EX_OSERR)
2017-10-24 19:16:36 +05:00
def install_python_dev(self):
2018-10-26 09:11:17 -04:00
self.stdOut("Install python development environment")
2017-10-24 19:16:36 +05:00
2019-11-13 13:08:26 +05:00
if self.distro == centos:
command = "yum -y install python-devel"
else:
command = "apt-get -y install python-dev"
2019-11-13 16:13:52 +05:00
preFlightsChecks.call(command, self.distro, command, command, 1, 1, os.EX_OSERR)
2017-10-24 19:16:36 +05:00
2019-11-13 13:08:26 +05:00
def install_gcc(self):
self.stdOut("Install gcc")
2017-10-24 19:16:36 +05:00
2019-11-13 13:08:26 +05:00
if self.distro == centos:
command = "yum -y install gcc"
else:
command = "apt-get -y install gcc"
2017-10-24 19:16:36 +05:00
2019-11-13 16:13:52 +05:00
preFlightsChecks.call(command, self.distro, command, command, 1, 1, os.EX_OSERR)
2017-10-24 19:16:36 +05:00
2019-11-13 13:08:26 +05:00
def install_python_setup_tools(self):
command = "yum -y install python-setuptools"
2019-11-13 16:13:52 +05:00
preFlightsChecks.call(command, self.distro, command, command, 1, 1, os.EX_OSERR)
2017-10-24 19:16:36 +05:00
def install_python_mysql_library(self):
2018-10-26 09:11:17 -04:00
self.stdOut("Install MySQL python library")
2017-10-24 19:16:36 +05:00
2019-11-13 13:08:26 +05:00
if self.distro == centos:
2019-12-10 18:49:06 +05:00
command = "yum install mariadb-devel gcc python36u-devel -y"
2019-11-13 13:08:26 +05:00
else:
command = "apt-get -y install libmysqlclient-dev"
2019-11-13 16:13:52 +05:00
preFlightsChecks.call(command, self.distro, command, command, 1, 1, os.EX_OSERR)
2017-10-24 19:16:36 +05:00
2019-11-13 13:08:26 +05:00
if self.distro == ubuntu:
command = "pip install MySQL-python"
2019-11-13 16:13:52 +05:00
preFlightsChecks.call(command, self.distro, command, command, 1, 1, os.EX_OSERR)
2017-10-24 19:16:36 +05:00
def fix_selinux_issue(self):
2017-11-05 03:02:51 +05:00
try:
cmd = []
2017-10-24 19:16:36 +05:00
2017-11-05 03:02:51 +05:00
cmd.append("setsebool")
cmd.append("-P")
cmd.append("httpd_can_network_connect")
cmd.append("1")
2017-10-24 19:16:36 +05:00
2019-03-26 16:19:03 +05:00
res = subprocess.call(cmd)
2017-11-05 03:02:51 +05:00
2018-11-14 11:37:17 -05:00
if preFlightsChecks.resFailed(self.distro, res):
2017-11-05 03:02:51 +05:00
logging.InstallLog.writeToFile("fix_selinux_issue problem")
else:
pass
2017-12-09 22:30:10 +05:00
except:
logging.InstallLog.writeToFile("[ERROR] fix_selinux_issue problem")
2017-10-24 19:16:36 +05:00
def install_psmisc(self):
2018-10-26 09:11:17 -04:00
self.stdOut("Install psmisc")
2017-10-24 19:16:36 +05:00
2019-11-13 13:08:26 +05:00
if self.distro == centos:
command = "yum -y install psmisc"
else:
command = "apt-get -y install psmisc"
2019-11-07 14:47:02 +05:00
2019-11-13 16:13:52 +05:00
preFlightsChecks.call(command, self.distro, command, command, 1, 0, os.EX_OSERR)
2019-11-07 14:47:02 +05:00
2019-11-13 13:08:26 +05:00
def installGit(self):
if os.path.exists("/etc/lsb-release"):
command = 'apt -y install git'
2019-11-13 16:13:52 +05:00
preFlightsChecks.call(command, self.distro, command, command, 1, 1, os.EX_OSERR)
2019-11-13 13:08:26 +05:00
else:
command = 'yum -y install http://repo.iotti.biz/CentOS/7/noarch/lux-release-7-1.noarch.rpm'
2019-11-13 19:37:55 +05:00
preFlightsChecks.call(command, self.distro, command, command, 1, 0, os.EX_OSERR)
2017-10-24 19:16:36 +05:00
2019-11-13 13:08:26 +05:00
command = 'yum install git -y'
2019-11-13 16:13:52 +05:00
preFlightsChecks.call(command, self.distro, command, command, 1, 1, os.EX_OSERR)
2017-10-24 19:16:36 +05:00
2019-11-13 13:08:26 +05:00
def download_install_CyberPanel(self, mysqlPassword, mysql):
##
2017-10-24 19:16:36 +05:00
os.chdir(self.path)
2019-11-07 14:47:02 +05:00
self.installGit()
os.chdir('/usr/local')
command = "git clone https://github.com/usmannasir/cyberpanel"
2019-11-13 16:13:52 +05:00
preFlightsChecks.call(command, self.distro, command, command, 1, 1, os.EX_OSERR)
2017-10-24 19:16:36 +05:00
2019-11-07 14:47:02 +05:00
shutil.move('cyberpanel', 'CyberCP')
2017-10-24 19:16:36 +05:00
2019-11-07 14:47:02 +05:00
##
2017-10-24 19:16:36 +05:00
### update password:
passFile = "/etc/cyberpanel/mysqlPassword"
f = open(passFile)
data = f.read()
password = data.split('\n', 1)[0]
2019-12-12 13:51:35 +05:00
### Put correct mysql passwords in settings f