Files
CyberPanel/manageServices/serviceManager.py
2020-08-21 12:09:36 +05:00

225 lines
6.9 KiB
Python
Executable File

import os.path
import sys
import django
sys.path.append('/usr/local/CyberCP')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "CyberCP.settings")
django.setup()
from random import randint
from plogical.processUtilities import ProcessUtilities
from dns.models import Supermasters
from manageServices.models import SlaveServers
import argparse
from serverStatus.serverStatusUtil import ServerStatusUtil
from plogical import CyberCPLogFileWriter as logging
import subprocess
class ServiceManager:
slaveConfPath = '/home/cyberpanel/slaveConf'
def __init__(self, extraArgs):
self.extraArgs = extraArgs
def managePDNS(self):
type = self.extraArgs['type']
path = '/etc/pdns/pdns.conf'
data = ProcessUtilities.outputExecutioner('sudo cat ' + path).splitlines()
#data = subprocess.check_output(shlex.split('sudo cat ' + path)).decode("utf-8").splitlines()
if type == 'MASTER':
counter = 0
ipsString = ''
ipStringNoSubnet = ''
for items in SlaveServers.objects.all():
ipsString = ipsString + '%s/32, ' % (items.slaveServerIP)
ipStringNoSubnet = ipStringNoSubnet + '%s, ' % (items.slaveServerIP)
ipsString = ipsString.rstrip(', ')
ipStringNoSubnet = ipStringNoSubnet.rstrip(', ')
tempPath = "/home/cyberpanel/" + str(randint(1000, 9999))
writeToFile = open(tempPath, 'w')
for items in data:
if items.find('allow-axfr-ips') > -1:
continue
if items.find('also-notify') > -1:
continue
if items.find('daemon=') > -1:
continue
if items.find('disable-axfr') > -1:
continue
if items.find('slave') > -1:
continue
if items.find('master') > -1:
continue
counter = counter + 1
writeToFile.writelines(items + '\n')
writeToFile.writelines('allow-axfr-ips=' + ipsString + '\n')
writeToFile.writelines('also-notify=' + ipStringNoSubnet + '\n')
writeToFile.writelines('daemon=no\n')
writeToFile.writelines('disable-axfr=no\n')
writeToFile.writelines('master=yes\n')
writeToFile.close()
else:
import os
if not os.path.exists(ServiceManager.slaveConfPath):
writeToFile = open(ServiceManager.slaveConfPath, 'w')
writeToFile.write('configured')
writeToFile.close()
counter = 0
for items in data:
if items.find('allow-axfr-ips') > -1:
continue
if items.find('also-notify') > -1:
continue
if items.find('daemon=') > -1:
continue
if items.find('disable-axfr') > -1:
continue
if items.find('slave') > -1:
continue
counter = counter + 1
tempPath = "/home/cyberpanel/" + str(randint(1000, 9999))
writeToFile = open(tempPath, 'w')
for items in data:
writeToFile.writelines(items + '\n')
slaveData = """
slave=yes
daemon=yes
disable-axfr=yes
guardian=yes
local-address=0.0.0.0
local-port=53
master=no
slave-cycle-interval=60
setgid=pdns
setuid=pdns
superslave=yes
"""
writeToFile.writelines(slaveData)
writeToFile.close()
command = 'sudo mv ' + tempPath + ' ' + path
ProcessUtilities.executioner(command)
for items in Supermasters.objects.all():
items.delete()
Supermasters(ip=self.extraArgs['masterServerIP'], nameserver=self.extraArgs['slaveServerNS'], account='').save()
@staticmethod
def InstallElasticSearch():
statusFile = open(ServerStatusUtil.lswsInstallStatusPath, 'w')
if ProcessUtilities.decideDistro() == ProcessUtilities.centos or ProcessUtilities.decideDistro() == ProcessUtilities.cent8:
command = 'rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch'
ServerStatusUtil.executioner(command, statusFile)
repoPath = '/etc/yum.repos.d/elasticsearch.repo'
content = '''[elasticsearch]
name=Elasticsearch repository for 7.x packages
baseurl=https://artifacts.elastic.co/packages/7.x/yum
gpgcheck=1
gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch
enabled=0
autorefresh=1
type=rpm-md
'''
writeToFile = open(repoPath, 'w')
writeToFile.write(content)
writeToFile.close()
command = 'yum install --enablerepo=elasticsearch elasticsearch -y'
ServerStatusUtil.executioner(command, statusFile)
else:
command = 'wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo apt-key add -'
subprocess.call(command, shell=True)
command = 'apt-get install apt-transport-https -y'
ServerStatusUtil.executioner(command, statusFile)
command = 'echo "deb https://artifacts.elastic.co/packages/7.x/apt stable main" | sudo tee /etc/apt/sources.list.d/elastic-7.x.list'
subprocess.call(command, shell=True)
command = 'apt-get update -y'
ServerStatusUtil.executioner(command, statusFile)
command = 'apt-get install elasticsearch -y'
ServerStatusUtil.executioner(command, statusFile)
### Tmp folder configurations
command = 'mkdir -p /home/elasticsearch/tmp'
ServerStatusUtil.executioner(command, statusFile)
command = 'chown elasticsearch:elasticsearch /home/elasticsearch/tmp'
ServerStatusUtil.executioner(command, statusFile)
jvmOptions = '/etc/elasticsearch/jvm.options'
writeToFile = open(jvmOptions, 'a')
writeToFile.write('-Djava.io.tmpdir=/home/elasticsearch/tmp\n')
writeToFile.close()
command = 'systemctl enable elasticsearch'
ServerStatusUtil.executioner(command, statusFile)
command = 'systemctl start elasticsearch'
ServerStatusUtil.executioner(command, statusFile)
command = 'touch /home/cyberpanel/elasticsearch'
ServerStatusUtil.executioner(command, statusFile)
logging.CyberCPLogFileWriter.statusWriter(ServerStatusUtil.lswsInstallStatusPath,
"Packages successfully installed.[200]\n", 1)
return 0
def main():
parser = argparse.ArgumentParser(description='CyberPanel Application Manager')
parser.add_argument('--function', help='Function')
args = vars(parser.parse_args())
if args["function"] == "InstallElasticSearch":
ServiceManager.InstallElasticSearch()
if __name__ == "__main__":
main()