Files
CyberPanel/manageServices/serviceManager.py

272 lines
8.5 KiB
Python
Raw Normal View History

2020-08-21 12:01:45 +05:00
import os.path
import sys
import django
sys.path.append('/usr/local/CyberCP')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "CyberCP.settings")
django.setup()
2019-02-04 01:03:18 +05:00
from random import randint
from plogical.processUtilities import ProcessUtilities
2019-06-26 03:57:16 +05:00
from dns.models import Supermasters
from manageServices.models import SlaveServers
import argparse
from serverStatus.serverStatusUtil import ServerStatusUtil
2020-08-21 12:01:45 +05:00
from plogical import CyberCPLogFileWriter as logging
2020-08-21 12:09:36 +05:00
import subprocess
2019-02-04 01:03:18 +05:00
class ServiceManager:
slaveConfPath = '/home/cyberpanel/slaveConf'
2019-02-04 01:03:18 +05:00
def __init__(self, extraArgs):
self.extraArgs = extraArgs
def managePDNS(self):
type = self.extraArgs['type']
path = '/etc/pdns/pdns.conf'
2019-06-26 03:57:16 +05:00
data = ProcessUtilities.outputExecutioner('sudo cat ' + path).splitlines()
2019-12-15 13:30:40 +05:00
#data = subprocess.check_output(shlex.split('sudo cat ' + path)).decode("utf-8").splitlines()
2019-06-26 03:57:16 +05:00
2019-02-04 01:03:18 +05:00
if type == 'MASTER':
counter = 0
2019-06-26 03:57:16 +05:00
ipsString = ''
ipStringNoSubnet = ''
for items in SlaveServers.objects.all():
ipsString = ipsString + '%s/32, ' % (items.slaveServerIP)
ipStringNoSubnet = ipStringNoSubnet + '%s, ' % (items.slaveServerIP)
2019-06-26 03:57:16 +05:00
ipsString = ipsString.rstrip(', ')
ipStringNoSubnet = ipStringNoSubnet.rstrip(', ')
2019-06-26 03:57:16 +05:00
tempPath = "/home/cyberpanel/" + str(randint(1000, 9999))
writeToFile = open(tempPath, 'w')
2019-02-04 01:03:18 +05:00
for items in data:
if items.find('allow-axfr-ips') > -1:
2019-06-27 13:07:47 +05:00
continue
2019-02-04 01:03:18 +05:00
if items.find('also-notify') > -1:
2019-06-27 13:07:47 +05:00
continue
2019-02-04 01:03:18 +05:00
if items.find('daemon=') > -1:
2019-06-27 13:07:47 +05:00
continue
2019-02-04 01:03:18 +05:00
if items.find('disable-axfr') > -1:
2019-06-27 13:07:47 +05:00
continue
2019-02-04 01:03:18 +05:00
if items.find('slave') > -1:
2019-06-27 13:07:47 +05:00
continue
2019-02-04 01:03:18 +05:00
if items.find('master') > -1:
continue
2019-02-04 01:03:18 +05:00
counter = counter + 1
2019-02-04 01:03:18 +05:00
writeToFile.writelines(items + '\n')
2019-02-04 01:03:18 +05:00
writeToFile.writelines('allow-axfr-ips=' + ipsString + '\n')
2019-06-26 03:57:16 +05:00
writeToFile.writelines('also-notify=' + ipStringNoSubnet + '\n')
2019-02-04 01:03:18 +05:00
writeToFile.writelines('daemon=no\n')
writeToFile.writelines('disable-axfr=no\n')
writeToFile.writelines('master=yes\n')
writeToFile.close()
else:
import os
2019-02-04 01:03:18 +05:00
if not os.path.exists(ServiceManager.slaveConfPath):
2019-02-04 01:03:18 +05:00
writeToFile = open(ServiceManager.slaveConfPath, 'w')
writeToFile.write('configured')
writeToFile.close()
2019-02-04 01:03:18 +05:00
counter = 0
2019-02-04 01:03:18 +05:00
for items in data:
if items.find('allow-axfr-ips') > -1:
continue
2019-02-04 01:03:18 +05:00
if items.find('also-notify') > -1:
continue
2019-02-04 01:03:18 +05:00
if items.find('daemon=') > -1:
continue
if items.find('disable-axfr') > -1:
continue
2019-02-04 01:03:18 +05:00
if items.find('slave') > -1:
continue
2019-02-04 01:03:18 +05:00
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()
2019-02-04 01:03:18 +05:00
command = 'sudo mv ' + tempPath + ' ' + path
ProcessUtilities.executioner(command)
2019-06-26 03:57:16 +05:00
for items in Supermasters.objects.all():
items.delete()
Supermasters(ip=self.extraArgs['masterServerIP'], nameserver=self.extraArgs['slaveServerNS'], account='').save()
@staticmethod
2020-08-21 12:01:45 +05:00
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)
2020-08-21 12:09:36 +05:00
else:
command = 'wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo apt-key add -'
subprocess.call(command, shell=True)
2020-08-21 12:01:45 +05:00
2020-08-21 12:09:36 +05:00
command = 'apt-get install apt-transport-https -y'
2020-08-21 12:01:45 +05:00
ServerStatusUtil.executioner(command, statusFile)
2020-08-21 12:09:36 +05:00
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'
2020-08-21 12:01:45 +05:00
ServerStatusUtil.executioner(command, statusFile)
2020-08-21 12:09:36 +05:00
command = 'apt-get install elasticsearch -y'
ServerStatusUtil.executioner(command, statusFile)
2020-08-21 12:01:45 +05:00
2020-08-21 12:09:36 +05:00
### Tmp folder configurations
2020-08-21 12:01:45 +05:00
2020-08-21 12:09:36 +05:00
command = 'mkdir -p /home/elasticsearch/tmp'
ServerStatusUtil.executioner(command, statusFile)
2020-08-21 12:01:45 +05:00
2020-08-21 12:09:36 +05:00
command = 'chown elasticsearch:elasticsearch /home/elasticsearch/tmp'
ServerStatusUtil.executioner(command, statusFile)
2020-08-21 12:01:45 +05:00
2020-08-21 12:09:36 +05:00
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)
2020-08-21 12:01:45 +05:00
2020-08-21 12:09:36 +05:00
logging.CyberCPLogFileWriter.statusWriter(ServerStatusUtil.lswsInstallStatusPath,
2020-08-21 12:01:45 +05:00
"Packages successfully installed.[200]\n", 1)
return 0
2020-08-21 14:33:48 +05:00
@staticmethod
def RemoveElasticSearch():
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'
try:
os.remove(repoPath)
except:
pass
command = 'yum erase elasticsearch -y'
ServerStatusUtil.executioner(command, statusFile)
else:
try:
os.remove('/etc/apt/sources.list.d/elastic-7.x.list')
except:
pass
command = 'apt-get remove elasticsearch -y'
ServerStatusUtil.executioner(command, statusFile)
### Tmp folder configurations
command = 'rm -rf /home/elasticsearch/tmp'
ServerStatusUtil.executioner(command, statusFile)
jvmOptions = '/etc/elasticsearch/jvm.options'
command = 'rm -f /home/cyberpanel/elasticsearch'
ServerStatusUtil.executioner(command, statusFile)
logging.CyberCPLogFileWriter.statusWriter(ServerStatusUtil.lswsInstallStatusPath,
"ElasticSearch successfully removed.[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()
2020-08-21 14:33:48 +05:00
elif args["function"] == "RemoveElasticSearch":
ServiceManager.RemoveElasticSearch()
if __name__ == "__main__":
main()