diff --git a/CyberCP/settings.py b/CyberCP/settings.py index 2b9518660..0dd5d5dd9 100644 --- a/CyberCP/settings.py +++ b/CyberCP/settings.py @@ -24,7 +24,7 @@ BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) SECRET_KEY = 'xr%j*p!*$0d%(-(e%@-*hyoz4$f%y77coq0u)6pwmjg4)q&19f' # SECURITY WARNING: don't run with debug turned on in production! -DEBUG = True +DEBUG = False ALLOWED_HOSTS = ['*'] @@ -110,15 +110,15 @@ DATABASES = { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'cyberpanel', 'USER': 'cyberpanel', - 'PASSWORD': 'a9AwLb7zY7ZwCd', - 'HOST': 'localhost', - 'PORT': '', + 'PASSWORD': 'Bz9gF7Hr7X4RtD', + 'HOST': '127.0.0.1', + 'PORT':'3307' }, 'rootdb': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'mysql', 'USER': 'root', - 'PASSWORD': '3bL8X7wGo0kT3b', + 'PASSWORD': 'sXm5VlRaAsXkDd', 'HOST': 'localhost', 'PORT': '', }, diff --git a/CyberCP/urls.py b/CyberCP/urls.py index 4c81d06f2..644aa6156 100644 --- a/CyberCP/urls.py +++ b/CyberCP/urls.py @@ -21,7 +21,6 @@ urlpatterns = [ url(r'^', include('loginSystem.urls')), url(r'^packages/',include('packages.urls')), url(r'^websites/',include('websiteFunctions.urls')), - url(r'^docker/',include('dockerManager.urls')), url(r'^tuning/',include('tuning.urls')), url(r'^ftp/',include('ftp.urls')), url(r'^serverstatus/',include('serverStatus.urls')), @@ -41,4 +40,5 @@ urlpatterns = [ url(r'^plugins/',include('pluginHolder.urls')), url(r'^emailMarketing/', include('emailMarketing.urls')), url(r'^cloudAPI/', include('cloudAPI.urls')), + url(r'^docker/', include('dockerManager.urls')), ] diff --git a/api/views.py b/api/views.py index be6badb08..b76582714 100644 --- a/api/views.py +++ b/api/views.py @@ -570,7 +570,7 @@ def changeAdminPassword(request): firstName="Cyber", lastName="Panel", acl=acl, token=token) admin.save() - vers = version(currentVersion="1.7", build=6) + vers = version(currentVersion="1.7", build=7) vers.save() package = Package(admin=admin, packageName="Default", diskSpace=1000, diff --git a/baseTemplate/templates/baseTemplate/index.html b/baseTemplate/templates/baseTemplate/index.html index 77c8fc2c7..7acf17c20 100644 --- a/baseTemplate/templates/baseTemplate/index.html +++ b/baseTemplate/templates/baseTemplate/index.html @@ -666,9 +666,9 @@ - + \ No newline at end of file diff --git a/cloudAPI/cloudManager.py b/cloudAPI/cloudManager.py index 37ac89e8b..4902b3a50 100644 --- a/cloudAPI/cloudManager.py +++ b/cloudAPI/cloudManager.py @@ -1089,6 +1089,81 @@ class CloudManager: finalData = mysqlUtilities.restartMySQL() + finalData = json.dumps(finalData) + return HttpResponse(finalData) + except BaseException, msg: + return self.ajaxPre(0, str(msg)) + + def fetchDatabasesMYSQL(self, request): + try: + request.session['userID'] = self.admin.pk + currentACL = ACLManager.loadedACL( self.admin.pk) + + if currentACL['admin'] == 0: + return self.ajaxPre(0, 'Only administrators can see MySQL status.') + + finalData = mysqlUtilities.fetchDatabases() + + finalData = json.dumps(finalData) + return HttpResponse(finalData) + except BaseException, msg: + return self.ajaxPre(0, str(msg)) + + def fetchTables(self, request): + try: + request.session['userID'] = self.admin.pk + currentACL = ACLManager.loadedACL( self.admin.pk) + + if currentACL['admin'] == 0: + return self.ajaxPre(0, 'Only administrators can see MySQL status.') + + finalData = mysqlUtilities.fetchTables(self.data) + + finalData = json.dumps(finalData) + return HttpResponse(finalData) + except BaseException, msg: + return self.ajaxPre(0, str(msg)) + + def deleteTable(self, request): + try: + request.session['userID'] = self.admin.pk + currentACL = ACLManager.loadedACL( self.admin.pk) + + if currentACL['admin'] == 0: + return self.ajaxPre(0, 'Only administrators can see MySQL status.') + + finalData = mysqlUtilities.deleteTable(self.data) + + finalData = json.dumps(finalData) + return HttpResponse(finalData) + except BaseException, msg: + return self.ajaxPre(0, str(msg)) + + def fetchTableData(self, request): + try: + request.session['userID'] = self.admin.pk + currentACL = ACLManager.loadedACL( self.admin.pk) + + if currentACL['admin'] == 0: + return self.ajaxPre(0, 'Only administrators can see MySQL status.') + + finalData = mysqlUtilities.fetchTableData(self.data) + + finalData = json.dumps(finalData) + return HttpResponse(finalData) + except BaseException, msg: + return self.ajaxPre(0, str(msg)) + + def fetchStructure(self, request): + try: + request.session['userID'] = self.admin.pk + currentACL = ACLManager.loadedACL( self.admin.pk) + + if currentACL['admin'] == 0: + return self.ajaxPre(0, 'Only administrators can see MySQL status.') + + finalData = mysqlUtilities.fetchStructure(self.data) + finalData = json.dumps(finalData) return HttpResponse(finalData) except BaseException, msg: diff --git a/cloudAPI/views.py b/cloudAPI/views.py index 902fa6d90..20494bfc6 100644 --- a/cloudAPI/views.py +++ b/cloudAPI/views.py @@ -229,6 +229,16 @@ def router(request): return cm.applyMySQLChanges(request) elif controller == 'restartMySQL': return cm.restartMySQL(request) + elif controller == 'fetchDatabasesMYSQL': + return cm.fetchDatabasesMYSQL(request) + elif controller == 'fetchTables': + return cm.fetchTables(request) + elif controller == 'deleteTable': + return cm.deleteTable(request) + elif controller == 'fetchTableData': + return cm.fetchTableData(request) + elif controller == 'fetchStructure': + return cm.fetchStructure(request) else: return cm.ajaxPre(0, 'This function is not available in your version of CyberPanel.') diff --git a/dockerManager/templates/dockerManager/images.html b/dockerManager/templates/dockerManager/images.html index 480dd7ae5..040953872 100644 --- a/dockerManager/templates/dockerManager/images.html +++ b/dockerManager/templates/dockerManager/images.html @@ -19,8 +19,7 @@

- {% trans "Installed Images" %} - {{ test }} + {% trans "Locally Available Images" %}


diff --git a/dockerManager/templates/dockerManager/manageImages.html b/dockerManager/templates/dockerManager/manageImages.html index 4e219abdd..35af6387e 100644 --- a/dockerManager/templates/dockerManager/manageImages.html +++ b/dockerManager/templates/dockerManager/manageImages.html @@ -12,7 +12,7 @@

{% trans "Manage Images" %} - Create + Create Container

{% trans "On this page you can manage docker images." %}

@@ -112,7 +112,7 @@ - + diff --git a/dockerManager/views.py b/dockerManager/views.py index ce9b31bef..626c95146 100644 --- a/dockerManager/views.py +++ b/dockerManager/views.py @@ -4,14 +4,11 @@ from __future__ import unicode_literals from django.shortcuts import render,redirect from loginSystem.models import Administrator from loginSystem.views import loadLoginPage -from django.http import HttpResponse from plogical.container import ContainerManager from dockerManager.pluginManager import pluginManager from decorators import preDockerRun from plogical.acl import ACLManager import json -import requests -import docker # Create your views here. diff --git a/firewall/firewallManager.py b/firewall/firewallManager.py index 2e5a14bba..48fbd676a 100644 --- a/firewall/firewallManager.py +++ b/firewall/firewallManager.py @@ -1345,9 +1345,9 @@ class FirewallManager: if fileName == 'categories.conf': continue - if fileName.endswith('dis'): + if fileName.endswith('bak'): status = 0 - fileName = fileName.rstrip('.dis') + fileName = fileName.rstrip('.bak') elif fileName.endswith('conf'): status = 1 else: diff --git a/install/install.py b/install/install.py index 43433f44c..d1e941c24 100644 --- a/install/install.py +++ b/install/install.py @@ -780,7 +780,7 @@ class preFlightsChecks: os.chdir(self.path) - command = "wget http://cyberpanel.sh/CyberPanel.1.7.6.tar.gz" + command = "wget http://cyberpanel.sh/CyberPanel.1.7.7.tar.gz" #command = "wget http://cyberpanel.sh/CyberPanelTemp.tar.gz" preFlightsChecks.call(command, self.distro, '[download_install_CyberPanel]', 'CyberPanel Download', @@ -789,7 +789,7 @@ class preFlightsChecks: ## count = 0 - command = "tar zxf CyberPanel.1.7.6.tar.gz" + command = "tar zxf CyberPanel.1.7.7.tar.gz" #command = "tar zxf CyberPanelTemp.tar.gz" preFlightsChecks.call(command, self.distro, '[download_install_CyberPanel]', 'Extract CyberPanel',1, 1, os.EX_OSERR) diff --git a/locale/ja/LC_MESSAGES/django.po b/locale/ja/LC_MESSAGES/django.po index db24bfb33..0539084ac 100644 --- a/locale/ja/LC_MESSAGES/django.po +++ b/locale/ja/LC_MESSAGES/django.po @@ -5409,97 +5409,4 @@ msgstr "Web サイト " #: websiteFunctions/templates/websiteFunctions/suspendWebsite.html:73 msgid "Successfully " -msgstr "成功しました " - -#~ msgid "CPU Status" -#~ msgstr "CPU の状態" - -#~ msgid "Fullscreen" -#~ msgstr "フルスクリーン" - -#~ msgid "System Status" -#~ msgstr "システムの状態" - -#~ msgid "Update started..." -#~ msgstr "更新を開始..." - -#~ msgid "Update finished..." -#~ msgstr "更新を終了..." - -#~ msgid "Account Type" -#~ msgstr "アカウント種別" - -#~ msgid "User Accounts Limit" -#~ msgstr "ユーザーアカウントの制限" - -#~ msgid "Only Numbers" -#~ msgstr "数字のみ" - -#~ msgid "Username should be lowercase alphanumeric." -#~ msgstr "ユーザー名は小文字の英数字である必要があります。" - -#~ msgid "Must contain one number and one special character." -#~ msgstr "1 つの数字と 1 つの特殊文字を含める必要があります。" - -#~ msgid "Cannot create website. Error message:" -#~ msgstr "Web サイトを作成できません。エラーメッセージ:" - -#~ msgid "Website with domain" -#~ msgstr "ドメインを持つ Web サイト" - -#~ msgid " is Successfully Created" -#~ msgstr " 作成されました" - -#~ msgid "Installation successful. To complete the setup visit:" -#~ msgstr "インストールに成功しました。 セットアップを完了するには:" - -#, fuzzy -#~| msgid "Username" -#~ msgid "Admin Username" -#~ msgstr "ユーザー名" - -#, fuzzy -#~| msgid "Password" -#~ msgid "Admin Password" -#~ msgstr "パスワード" - -#, fuzzy -#~| msgid "Database Name" -#~ msgid "Database prefix" -#~ msgstr "データベース名" - -#~ msgid "Daily" -#~ msgstr "日次" - -#~ msgid "Weekly" -#~ msgstr "週次" - -#~ msgid "HTTP Statistics" -#~ msgstr "HTTP の統計情報" - -#~ msgid "Available/Max Connections" -#~ msgstr "利用可能/最大 接続数" - -#~ msgid "Available/Max SSL Connections" -#~ msgstr "利用可能/最大 SSL 接続数" - -#~ msgid "Requests Processing" -#~ msgstr "リクエスト処理数" - -#~ msgid "Total Requests" -#~ msgstr "合計リクエスト数" - -#~ msgid "IPV6" -#~ msgstr "IPv6" - -#~ msgid "Normal User" -#~ msgstr "通常のユーザー" - -#~ msgid "Edit Virtual Host Main Configurations" -#~ msgstr "仮想ホストのメイン設定の編集" - -#~ msgid "Configuration saved. Restart LiteSpeed put them in effect." -#~ msgstr "設定が保存されました。 LiteSpeed を再起動して有効にします。" - -#~ msgid "Urdu" -#~ msgstr "ウルドゥー語" +msgstr "成功しました " \ No newline at end of file diff --git a/loginSystem/views.py b/loginSystem/views.py index 150137107..5785f7704 100644 --- a/loginSystem/views.py +++ b/loginSystem/views.py @@ -156,7 +156,7 @@ def loadLoginPage(request): firstName="Cyber",lastName="Panel", acl=acl, token=token) admin.save() - vers = version(currentVersion="1.7", build=6) + vers = version(currentVersion="1.7", build=7) vers.save() package = Package(admin=admin, packageName="Default", diskSpace=1000, diff --git a/plogical/acl.py b/plogical/acl.py index 3ed15b444..a3a9d76ee 100644 --- a/plogical/acl.py +++ b/plogical/acl.py @@ -8,12 +8,12 @@ from loginSystem.models import Administrator, ACL from django.shortcuts import HttpResponse from packages.models import Package from websiteFunctions.models import Websites, ChildDomains -from dockerManager.models import Containers from dns.models import Domains import json from subprocess import call, CalledProcessError from shlex import split from CyberCPLogFileWriter import CyberCPLogFileWriter as logging +from dockerManager.models import Containers class ACLManager: @@ -364,73 +364,6 @@ class ACLManager: return websiteNames - - @staticmethod - def findAllContainers(currentACL, userID): - containerName = [] - - if currentACL['admin'] == 1: - allContainers = Containers.objects.all() - for items in allContainers: - containerName.append(items.name) - else: - admin = Administrator.objects.get(pk=userID) - - containers = admin.containers_set.all() - admins = Administrator.objects.filter(owner=admin.pk) - - for items in containers: - containerName.append(items.name) - - for items in admins: - cons = items.containers_set.all() - for con in cons: - containerName.append(con.name) - - return containerName - - - @staticmethod - def findContainersObjects(currentACL, userID): - - if currentACL['admin'] == 1: - return Containers.objects.all() - else: - - containerList = [] - admin = Administrator.objects.get(pk=userID) - - containers = admin.containers_set.all() - - for items in containers: - containerList.append(items) - - admins = Administrator.objects.filter(owner=admin.pk) - - for items in admins: - cons = items.containers_set.all() - for con in cons: - containerList.append(web) - - return containerList - - - @staticmethod - def checkContainerOwnership(name, userID): - try: - container = Containers.objects.get(name=name) - currentACL = ACLManager.loadedACL(userID) - admin = Administrator.objects.get(pk=userID) - - if currentACL['admin'] == 1: - return 1 - elif container.admin == admin: - return 1 - else: - return 0 - except: - return 0 - @staticmethod def findWebsiteObjects(currentACL, userID): @@ -518,6 +451,72 @@ class ACLManager: logging.writeToFile(str(msg) + ' [ACLManager.executeCall]') return 0, str(msg) + @staticmethod + def checkContainerOwnership(name, userID): + return 1 + try: + container = Containers.objects.get(name=name) + currentACL = ACLManager.loadedACL(userID) + admin = Administrator.objects.get(pk=userID) + + if currentACL['admin'] == 1: + return 1 + elif container.admin == admin: + return 1 + else: + return 0 + except: + return 0 + + @staticmethod + def findAllContainers(currentACL, userID): + containerName = [] + + if currentACL['admin'] == 1: + allContainers = Containers.objects.all() + for items in allContainers: + containerName.append(items.name) + else: + admin = Administrator.objects.get(pk=userID) + + containers = admin.containers_set.all() + admins = Administrator.objects.filter(owner=admin.pk) + + for items in containers: + containerName.append(items.name) + + for items in admins: + cons = items.containers_set.all() + for con in cons: + containerName.append(con.name) + + + return containerName + + @staticmethod + def findContainersObjects(currentACL, userID): + + if currentACL['admin'] == 1: + return Containers.objects.all() + else: + + containerList = [] + admin = Administrator.objects.get(pk=userID) + + containers = admin.containers_set.all() + + for items in containers: + containerList.append(items) + + admins = Administrator.objects.filter(owner=admin.pk) + + for items in admins: + cons = items.containers_set.all() + for con in cons: + containerList.append(web) + + return containerList + diff --git a/plogical/container.py b/plogical/container.py index ff28c88f0..764d695d7 100644 --- a/plogical/container.py +++ b/plogical/container.py @@ -5,6 +5,7 @@ import os.path import sys import django import mimetypes + sys.path.append('/usr/local/CyberCP') os.environ.setdefault("DJANGO_SETTINGS_MODULE", "CyberCP.settings") django.setup() @@ -24,34 +25,35 @@ import docker import docker.utils import requests + # Use default socket to connect class ContainerManager: - def __init__(self, name = None): + def __init__(self, name=None): self.name = name - - def submitInstallDocker(self, userID = None, data = None): + + def submitInstallDocker(self, userID=None, data=None): try: currentACL = ACLManager.loadedACL(userID) if ACLManager.currentContextPermission(currentACL, 'createContainer') == 0: return ACLManager.loadError() - + command = 'sudo yum install -y docker' cmd = shlex.split(command) res = subprocess.call(cmd) - + command = 'sudo groupadd docker' cmd = shlex.split(command) res2 = subprocess.call(cmd) - + command = 'sudo usermod -aG docker cyberpanel' cmd = shlex.split(command) res3 = subprocess.call(cmd) - + command = 'sudo service docker start' cmd = shlex.split(command) res4 = subprocess.call(cmd) - - if res == 0 and res2 == 0 and res3 == 0 and res4 ==0: + + if res == 0 and res2 == 0 and res3 == 0 and res4 == 0: data_ret = {'installDockerStatus': 1, 'error_message': 'None'} json_data = json.dumps(data_ret) return HttpResponse(json_data) @@ -59,79 +61,80 @@ class ContainerManager: data_ret = {'installDockerStatus': 0, 'error_message': 'Failed to install. Manual install required.'} json_data = json.dumps(data_ret) return HttpResponse(json_data) - + return HttpResponse(res) except BaseException, msg: - return HttpResponse(str(msg)) - - def createContainer(self, request = None, userID = None, data = None): - try: + return HttpResponse(str(msg)) + + def createContainer(self, request=None, userID=None, data=None): + try: admin = Administrator.objects.get(pk=userID) if admin.acl.adminStatus != 1: return ACLManager.loadError() - + client = docker.from_env() dockerAPI = docker.APIClient() - + adminNames = ACLManager.loadAllUsers(userID) tag = request.GET.get('tag') image = request.GET.get('image') tag = tag.split(" (")[0] - + if "/" in image: name = image.split("/")[0] + "." + image.split("/")[1] else: name = image - + try: - inspectImage = dockerAPI.inspect_image(image+":"+tag) + inspectImage = dockerAPI.inspect_image(image + ":" + tag) except docker.errors.ImageNotFound: val = request.session['userID'] admin = Administrator.objects.get(pk=val) - return render(request,'dockerManager/images.html',{"type":admin.type, - 'image':image, - 'tag':tag}) - + return render(request, 'dockerManager/images.html', {"type": admin.type, + 'image': image, + 'tag': tag}) + envList = {}; if 'Env' in inspectImage['Config']: for item in inspectImage['Config']['Env']: if '=' in item: - splitedItem = item.split('=',1) + splitedItem = item.split('=', 1) print splitedItem envList[splitedItem[0]] = splitedItem[1] else: envList[item] = "" - + portConfig = {}; if 'ExposedPorts' in inspectImage['Config']: for item in inspectImage['Config']['ExposedPorts']: portDef = item.split('/') - portConfig[portDef[0]] = portDef[1] - + portConfig[portDef[0]] = portDef[1] + if image is None or image is '' or tag is None or tag is '': return redirect(loadImages) - - Data = {"ownerList": adminNames, "image":image, "name":name, "tag":tag, "portConfig": portConfig, "envList":envList} - + + Data = {"ownerList": adminNames, "image": image, "name": name, "tag": tag, "portConfig": portConfig, + "envList": envList} + return render(request, 'dockerManager/runContainer.html', Data) except BaseException, msg: return HttpResponse(str(msg)) - - def loadContainerHome(self, request = None, userID = None, data = None): + + def loadContainerHome(self, request=None, userID=None, data=None): name = self.name - + if ACLManager.checkContainerOwnership(name, userID) != 1: return ACLManager.loadError() - + client = docker.from_env() dockerAPI = docker.APIClient() try: container = client.containers.get(name) except docker.errors.NotFound as err: return HttpResponse("Container not found") - + data = {} con = Containers.objects.get(name=name) data['name'] = name @@ -140,10 +143,10 @@ class ContainerManager: data['cid'] = con.cid data['envList'] = json.loads(con.env) print data['envList'] - + stats = container.stats(decode=False, stream=False) logs = container.logs(stream=True) - + data['status'] = container.status data['memoryLimit'] = con.memory if con.startOnReboot == 1: @@ -152,12 +155,12 @@ class ContainerManager: else: data['startOnReboot'] = 'false' data['restartPolicy'] = "No" - + if 'usage' in stats['memory_stats']: - # Calculate Usage + # Calculate Usage # Source: https://github.com/docker/docker/blob/28a7577a029780e4533faf3d057ec9f6c7a10948/api/client/stats.go#L309 data['memoryUsage'] = (stats['memory_stats']['usage'] / stats['memory_stats']['limit']) * 100 - + cpu_count = len(stats["cpu_stats"]["cpu_usage"]["percpu_usage"]) data['cpuUsage'] = 0.0 cpu_delta = float(stats["cpu_stats"]["cpu_usage"]["total_usage"]) - \ @@ -168,36 +171,34 @@ class ContainerManager: data['cpuUsage'] = round(cpu_delta / system_delta * 100.0 * cpu_count, 3) else: data['memoryUsage'] = 0 - data['cpuUsage'] = 0; - + data['cpuUsage'] = 0 + return render(request, 'dockerManager/viewContainer.html', data) - - def listContainers(self, request = None, userID = None, data = None): + + def listContainers(self, request=None, userID=None, data=None): try: - if ACLManager.checkContainerOwnership(name, userID) != 1: - return ACLManager.loadError() client = docker.from_env() dockerAPI = docker.APIClient() - + currentACL = ACLManager.loadedACL(userID) containers = ACLManager.findAllContainers(currentACL, userID) - + allContainers = client.containers.list() containersList = [] showUnlistedContainer = True - + # TODO: Add condition to show unlisted Containers only if user has admin level access unlistedContainers = [] for container in allContainers: if container.name not in containers: unlistedContainers.append(container) - + if not unlistedContainers: showUnlistedContainer = False - + adminNames = ACLManager.loadAllUsers(userID) - + pages = float(len(containers)) / float(10) pagination = [] @@ -212,24 +213,24 @@ class ContainerManager: pagination.append('
  • ' + str(i) + '
  • ') return render(request, 'dockerManager/listContainers.html', {"pagination": pagination, - "unlistedContainers": unlistedContainers, - "adminNames": adminNames, - "showUnlistedContainer":showUnlistedContainer}) + "unlistedContainers": unlistedContainers, + "adminNames": adminNames, + "showUnlistedContainer": showUnlistedContainer}) except BaseException, msg: return HttpResponse(str(msg)) - def getContainerLogs(self, userID = None, data = None): + def getContainerLogs(self, userID=None, data=None): try: name = data['name'] - + # Check if container is registered in database or unlisted - if Containers.objects.filter(name=name).exists(): + if Containers.objects.filter(name=name).exists(): if ACLManager.checkContainerOwnership(name, userID) != 1: - return ACLManager.loadErrorJson('containerLogStatus',0) - + return ACLManager.loadErrorJson('containerLogStatus', 0) + client = docker.from_env() - dockerAPI = docker.APIClient() - + dockerAPI = docker.APIClient() + container = client.containers.get(name) logs = container.logs() @@ -239,17 +240,17 @@ class ContainerManager: except BaseException, msg: - data_ret = {'containerLogStatus': 0, 'containerLog':'Error', 'error_message': str(msg)} + data_ret = {'containerLogStatus': 0, 'containerLog': 'Error', 'error_message': str(msg)} json_data = json.dumps(data_ret) return HttpResponse(json_data) - - def submitContainerCreation(self, userID = None, data = None): + + def submitContainerCreation(self, userID=None, data=None): try: admin = Administrator.objects.get(pk=userID) if admin.acl.adminStatus != 1: - return ACLManager.loadErrorJson('createContainerStatus',0) - + return ACLManager.loadErrorJson('createContainerStatus', 0) + client = docker.from_env() dockerAPI = docker.APIClient() @@ -259,16 +260,16 @@ class ContainerManager: dockerOwner = data['dockerOwner'] memory = data['memory'] envList = data['envList'] - - inspectImage = dockerAPI.inspect_image(image+":"+tag) + + inspectImage = dockerAPI.inspect_image(image + ":" + tag) portConfig = {} - + # Formatting envList for usage envDict = {} for key, value in envList.iteritems(): if (value['name'] != '') or (value['value'] != ''): envDict[value['name']] = value['value'] - + if 'ExposedPorts' in inspectImage['Config']: for item in inspectImage['Config']['ExposedPorts']: # Do not allow priviledged port numbers @@ -277,38 +278,38 @@ class ContainerManager: json_data = json.dumps(data_ret) return HttpResponse(json_data) portConfig[item] = data[item] - + ## Create Configurations admin = Administrator.objects.get(userName=dockerOwner) - - containerArgs = {'image':image+":"+tag, - 'detach':True, - 'name':name, - 'ports':portConfig, + + containerArgs = {'image': image + ":" + tag, + 'detach': True, + 'name': name, + 'ports': portConfig, 'publish_all_ports': True, - 'environment':envDict} - - containerArgs['mem_limit'] = memory * 1048576; # Converts MB to bytes ( 0 * x = 0 for unlimited memory) + 'environment': envDict} + + containerArgs['mem_limit'] = memory * 1048576; # Converts MB to bytes ( 0 * x = 0 for unlimited memory) try: container = client.containers.create(**containerArgs) except Exception as err: - if "port is already allocated" in err: # We need to delete container if port is not available + if "port is already allocated" in err: # We need to delete container if port is not available print "Deleting container" container.remove(force=True) data_ret = {'createContainerStatus': 0, 'error_message': str(err)} json_data = json.dumps(data_ret) return HttpResponse(json_data) - + con = Containers(admin=admin, - name=name, - tag=tag, - image=image, - memory=memory, - ports=json.dumps(portConfig), - env=json.dumps(envDict), - cid=container.id) - + name=name, + tag=tag, + image=image, + memory=memory, + ports=json.dumps(portConfig), + env=json.dumps(envDict), + cid=container.id) + con.save() data_ret = {'createContainerStatus': 1, 'error_message': "None"} @@ -320,37 +321,36 @@ class ContainerManager: data_ret = {'createContainerStatus': 0, 'error_message': str(msg)} json_data = json.dumps(data_ret) return HttpResponse(json_data) - - def submitInstallImage(self, userID = None, data = None): + + def submitInstallImage(self, userID=None, data=None): try: admin = Administrator.objects.get(pk=userID) if admin.acl.adminStatus != 1: - return ACLManager.loadErrorJson('installImageStatus',0) - + return ACLManager.loadErrorJson('installImageStatus', 0) + client = docker.from_env() dockerAPI = docker.APIClient() image = data['image'] tag = data['tag'] - + try: - inspectImage = dockerAPI.inspect_image(image+":"+tag) + inspectImage = dockerAPI.inspect_image(image + ":" + tag) data_ret = {'installImageStatus': 0, 'error_message': "Image already installed"} json_data = json.dumps(data_ret) return HttpResponse(json_data) except docker.errors.ImageNotFound: pass - + try: image = client.images.pull(image, tag=tag) print image.id except docker.errors.APIError as msg: data_ret = {'installImageStatus': 0, 'error_message': str(msg)} json_data = json.dumps(data_ret) - return HttpResponse(json_data) - - + return HttpResponse(json_data) + data_ret = {'installImageStatus': 1, 'error_message': "None"} json_data = json.dumps(data_ret) return HttpResponse(json_data) @@ -359,13 +359,13 @@ class ContainerManager: except BaseException, msg: data_ret = {'installImageStatus': 0, 'error_message': str(msg)} json_data = json.dumps(data_ret) - return HttpResponse(json_data) - - def submitContainerDeletion(self, userID = None, data = None, called = False): + return HttpResponse(json_data) + + def submitContainerDeletion(self, userID=None, data=None, called=False): try: name = data['name'] # Check if container is registered in database or unlisted - if Containers.objects.filter(name=name).exists(): + if Containers.objects.filter(name=name).exists(): if ACLManager.checkContainerOwnership(name, userID) != 1: if called: return 'Permission error' @@ -374,9 +374,9 @@ class ContainerManager: client = docker.from_env() dockerAPI = docker.APIClient() - + unlisted = data['unlisted'] - + if 'force' in data: force = True else: @@ -384,7 +384,7 @@ class ContainerManager: if not unlisted: containerOBJ = Containers.objects.get(name=name) - + if not force: try: container = client.containers.get(name) @@ -394,20 +394,20 @@ class ContainerManager: else: data_ret = {'delContainerStatus': 2, 'error_message': 'Container does not exist'} json_data = json.dumps(data_ret) - return HttpResponse(json_data) + return HttpResponse(json_data) try: - container.stop() # Stop container - container.kill() # INCASE graceful stop doesn't work + container.stop() # Stop container + container.kill() # INCASE graceful stop doesn't work except: pass try: - container.remove() # Finally remove container + container.remove() # Finally remove container except docker.errors.APIError as err: data_ret = {'delContainerStatus': 0, 'error_message': str(err)} json_data = json.dumps(data_ret) - return HttpResponse(json_data) + return HttpResponse(json_data) except: if called: return "Unknown" @@ -415,10 +415,10 @@ class ContainerManager: data_ret = {'delContainerStatus': 0, 'error_message': 'Unknown error'} json_data = json.dumps(data_ret) return HttpResponse(json_data) - + if not unlisted and not called: - containerOBJ.delete() - + containerOBJ.delete() + if called: return 0 else: @@ -434,12 +434,12 @@ class ContainerManager: json_data = json.dumps(data_ret) return HttpResponse(json_data) - def getContainerList(self, userID = None, data = None): + def getContainerList(self, userID=None, data=None): try: admin = Administrator.objects.get(pk=userID) if admin.acl.adminStatus != 1: - return ACLManager.loadErrorJson('listContainerStatus',0) - + return ACLManager.loadErrorJson('listContainerStatus', 0) + currentACL = ACLManager.loadedACL(userID) pageNumber = int(data['page']) json_data = self.findContainersJson(currentACL, userID, pageNumber) @@ -449,13 +449,13 @@ class ContainerManager: except BaseException, msg: dic = {'listContainerStatus': 0, 'error_message': str(msg)} json_data = json.dumps(dic) - return HttpResponse(json_data) - + return HttpResponse(json_data) + def findContainersJson(self, currentACL, userID, pageNumber): admin = Administrator.objects.get(pk=userID) if admin.acl.adminStatus != 1: return ACLManager.loadError() - + finalPageNumber = ((pageNumber * 10)) - 10 endPageNumber = finalPageNumber + 10 containers = ACLManager.findContainersObjects(currentACL, userID)[finalPageNumber:endPageNumber] @@ -464,7 +464,7 @@ class ContainerManager: checker = 0 for items in containers: - dic = {'name': items.name,'admin': items.admin.userName, 'tag':items.tag, 'image':items.image} + dic = {'name': items.name, 'admin': items.admin.userName, 'tag': items.tag, 'image': items.image} if checker == 0: json_data = json_data + json.dumps(dic) @@ -475,17 +475,17 @@ class ContainerManager: json_data = json_data + ']' return json_data - - def doContainerAction(self, userID = None, data = None): + + def doContainerAction(self, userID=None, data=None): try: name = data['name'] if ACLManager.checkContainerOwnership(name, userID) != 1: - return ACLManager.loadErrorJson('containerActionStatus',0) - + return ACLManager.loadErrorJson('containerActionStatus', 0) + client = docker.from_env() dockerAPI = docker.APIClient() - + action = data['action'] try: container = client.containers.get(name) @@ -497,7 +497,7 @@ class ContainerManager: data_ret = {'containerActionStatus': 0, 'error_message': 'Unknown'} json_data = json.dumps(data_ret) return HttpResponse(json_data) - + try: if action == 'start': container.start() @@ -514,7 +514,7 @@ class ContainerManager: json_data = json.dumps(data_ret) return HttpResponse(json_data) - time.sleep(3) # Wait 3 seconds for container to finish starting/stopping/restarting + time.sleep(3) # Wait 3 seconds for container to finish starting/stopping/restarting status = container.status data_ret = {'containerActionStatus': 1, 'error_message': 'None', 'status': status} json_data = json.dumps(data_ret) @@ -525,15 +525,15 @@ class ContainerManager: json_data = json.dumps(data_ret) return HttpResponse(json_data) - def getContainerStatus(self, userID = None, data = None): + def getContainerStatus(self, userID=None, data=None): try: name = data['name'] if ACLManager.checkContainerOwnership(name, userID) != 1: - return ACLManager.loadErrorJson('containerStatus',0) - + return ACLManager.loadErrorJson('containerStatus', 0) + client = docker.from_env() dockerAPI = docker.APIClient() - + try: container = client.containers.get(name) except docker.errors.NotFound as err: @@ -544,7 +544,7 @@ class ContainerManager: data_ret = {'containerStatus': 0, 'error_message': 'Unknown'} json_data = json.dumps(data_ret) return HttpResponse(json_data) - + status = container.status data_ret = {'containerStatus': 1, 'error_message': 'None', 'status': status} json_data = json.dumps(data_ret) @@ -553,17 +553,17 @@ class ContainerManager: except BaseException, msg: data_ret = {'containerStatus': 0, 'error_message': str(msg)} json_data = json.dumps(data_ret) - return HttpResponse(json_data) - - def exportContainer(self, request = None, userID = None, data = None): + return HttpResponse(json_data) + + def exportContainer(self, request=None, userID=None, data=None): try: name = request.GET.get('name') if ACLManager.checkContainerOwnership(name, userID) != 1: - return ACLManager.loadErrorJson('containerStatus',0) - + return ACLManager.loadErrorJson('containerStatus', 0) + client = docker.from_env() dockerAPI = docker.APIClient() - + try: container = client.containers.get(name) except docker.errors.NotFound as err: @@ -574,26 +574,26 @@ class ContainerManager: data_ret = {'containerStatus': 0, 'error_message': 'Unknown'} json_data = json.dumps(data_ret) return HttpResponse(json_data) - - eFile = container.export() # Export with default chunk size - response = HttpResponse(eFile, content_type='application/force-download') - response['Content-Disposition'] = 'attachment; filename="'+ name +'.tar"' + + eFile = container.export() # Export with default chunk size + response = HttpResponse(eFile, content_type='application/force-download') + response['Content-Disposition'] = 'attachment; filename="' + name + '.tar"' return response except BaseException, msg: data_ret = {'containerStatus': 0, 'error_message': str(msg)} json_data = json.dumps(data_ret) - return HttpResponse(json_data) - - def getContainerTop(self, userID = None, data = None): + return HttpResponse(json_data) + + def getContainerTop(self, userID=None, data=None): try: name = data['name'] if ACLManager.checkContainerOwnership(name, userID) != 1: - return ACLManager.loadErrorJson('containerTopStatus',0) - + return ACLManager.loadErrorJson('containerTopStatus', 0) + client = docker.from_env() dockerAPI = docker.APIClient() - + try: container = client.containers.get(name) except docker.errors.NotFound as err: @@ -604,38 +604,38 @@ class ContainerManager: data_ret = {'containerTopStatus': 0, 'error_message': 'Unknown'} json_data = json.dumps(data_ret) return HttpResponse(json_data) - + try: top = container.top() except docker.errors.APIError as err: data_ret = {'containerTopStatus': 0, 'error_message': str(err)} json_data = json.dumps(data_ret) - return HttpResponse(json_data) + return HttpResponse(json_data) - data_ret = {'containerTopStatus': 1, 'error_message': 'None', 'processes':top} + data_ret = {'containerTopStatus': 1, 'error_message': 'None', 'processes': top} json_data = json.dumps(data_ret) return HttpResponse(json_data) except BaseException, msg: data_ret = {'containerTopStatus': 0, 'error_message': str(msg)} json_data = json.dumps(data_ret) - return HttpResponse(json_data) - - def assignContainer(self, userID = None, data = None): + return HttpResponse(json_data) + + def assignContainer(self, userID=None, data=None): try: # Todo: add check only for super user i.e. main admin admin = Administrator.objects.get(pk=userID) if admin.acl.adminStatus != 1: - return ACLManager.loadErrorJson('assignContainerStatus',0) - + return ACLManager.loadErrorJson('assignContainerStatus', 0) + client = docker.from_env() dockerAPI = docker.APIClient() - + name = data['name'] dockerOwner = data['admin'] - + admin = Administrator.objects.get(userName=dockerOwner) - + try: container = client.containers.get(name) except docker.errors.NotFound as err: @@ -646,13 +646,13 @@ class ContainerManager: data_ret = {'assignContainerStatus': 0, 'error_message': 'Unknown'} json_data = json.dumps(data_ret) return HttpResponse(json_data) - + con = Containers(admin=admin, - name=name, - cid=container.id) - + name=name, + cid=container.id) + con.save() - + data_ret = {'assignContainerStatus': 1, 'error_message': 'None'} json_data = json.dumps(data_ret) return HttpResponse(json_data) @@ -660,17 +660,17 @@ class ContainerManager: except BaseException, msg: data_ret = {'assignContainerStatus': 0, 'error_message': str(msg)} json_data = json.dumps(data_ret) - return HttpResponse(json_data) - - def searchImage(self, userID = None, data = None): + return HttpResponse(json_data) + + def searchImage(self, userID=None, data=None): try: admin = Administrator.objects.get(pk=userID) if admin.acl.adminStatus != 1: - return ACLManager.loadErrorJson('searchImageStatus',0) - + return ACLManager.loadErrorJson('searchImageStatus', 0) + client = docker.from_env() dockerAPI = docker.APIClient() - + string = data['string'] try: matches = client.images.search(term=string) @@ -682,42 +682,41 @@ class ContainerManager: data_ret = {'searchImageStatus': 0, 'error_message': 'Unknown'} json_data = json.dumps(data_ret) return HttpResponse(json_data) - + print json.dumps(matches) - + for image in matches: if "/" in image['name']: image['name2'] = image['name'].split("/")[0] + ":" + image['name'].split("/")[1] else: image['name2'] = image['name'] - - - data_ret = {'searchImageStatus': 1, 'error_message': 'None', 'matches':matches} + + data_ret = {'searchImageStatus': 1, 'error_message': 'None', 'matches': matches} json_data = json.dumps(data_ret) return HttpResponse(json_data) except BaseException, msg: data_ret = {'searchImageStatus': 0, 'error_message': str(msg)} json_data = json.dumps(data_ret) - return HttpResponse(json_data) - - def images(self, request = None, userID = None, data = None): + return HttpResponse(json_data) + + def images(self, request=None, userID=None, data=None): try: admin = Administrator.objects.get(pk=userID) if admin.acl.adminStatus != 1: return ACLManager.loadError() - + client = docker.from_env() dockerAPI = docker.APIClient() - + try: imageList = client.images.list() except docker.errors.APIError as err: return HttpResponse(str(err)) - + images = {} names = [] - + for image in imageList: name = image.attrs['RepoTags'][0].split(":")[0] if "/" in name: @@ -726,41 +725,41 @@ class ContainerManager: name2 += ":" + item else: name2 = name - + tags = [] for tag in image.tags: getTag = tag.split(":") if len(getTag) == 2: tags.append(getTag[1]) - print tags + print tags if name in names: images[name]['tags'].extend(tags) else: names.append(name) - images[name] = {"name":name, - "name2":name2, - "tags":tags} + images[name] = {"name": name, + "name2": name2, + "tags": tags} print "======" print images - return render(request, 'dockerManager/images.html', {"images":images, "test":'asds'}) - + return render(request, 'dockerManager/images.html', {"images": images, "test": ''}) + except BaseException, msg: return HttpResponse(str(msg)) - - def manageImages(self, request = None, userID = None, data = None): + + def manageImages(self, request=None, userID=None, data=None): try: admin = Administrator.objects.get(pk=userID) if admin.acl.adminStatus != 1: return ACLManager.loadError() - + client = docker.from_env() dockerAPI = docker.APIClient() - + imageList = client.images.list() - + images = {} names = [] - + for image in imageList: name = image.attrs['RepoTags'][0].split(":")[0] if name in names: @@ -768,24 +767,24 @@ class ContainerManager: else: names.append(name) tags = [] - images[name] = {"name":name, - "tags":image.tags} - return render(request, 'dockerManager/manageImages.html', {"images":images}) - + images[name] = {"name": name, + "tags": image.tags} + return render(request, 'dockerManager/manageImages.html', {"images": images}) + except BaseException, msg: return HttpResponse(str(msg)) - - def getImageHistory(self, userID = None, data = None): + + def getImageHistory(self, userID=None, data=None): try: name = data['name'] - + if ACLManager.checkContainerOwnership(name, userID) != 1: return ACLManager.loadError() - + client = docker.from_env() dockerAPI = docker.APIClient() - + try: image = client.images.get(name) except docker.errors.APIError as err: @@ -796,10 +795,8 @@ class ContainerManager: data_ret = {'imageHistoryStatus': 0, 'error_message': 'Unknown'} json_data = json.dumps(data_ret) return HttpResponse(json_data) - - - - data_ret = {'imageHistoryStatus': 1, 'error_message': 'None', 'history':image.history()} + + data_ret = {'imageHistoryStatus': 1, 'error_message': 'None', 'history': image.history()} json_data = json.dumps(data_ret) return HttpResponse(json_data) @@ -807,20 +804,20 @@ class ContainerManager: data_ret = {'imageHistoryStatus': 0, 'error_message': str(msg)} json_data = json.dumps(data_ret) return HttpResponse(json_data) - - def removeImage(self, userID = None, data = None): + + def removeImage(self, userID=None, data=None): try: admin = Administrator.objects.get(pk=userID) if admin.acl.adminStatus != 1: return ACLManager.loadError() - + client = docker.from_env() dockerAPI = docker.APIClient() - + name = data['name'] try: if name == 0: - action = client.images.prune() + action = client.images.prune() else: action = client.images.remove(name) print action @@ -832,9 +829,7 @@ class ContainerManager: data_ret = {'removeImageStatus': 0, 'error_message': 'Unknown'} json_data = json.dumps(data_ret) return HttpResponse(json_data) - - - + data_ret = {'removeImageStatus': 1, 'error_message': 'None'} json_data = json.dumps(data_ret) return HttpResponse(json_data) @@ -842,67 +837,67 @@ class ContainerManager: except BaseException, msg: data_ret = {'removeImageStatus': 0, 'error_message': str(msg)} json_data = json.dumps(data_ret) - return HttpResponse(json_data) - - # Internal function for recreating containers - + return HttpResponse(json_data) + + # Internal function for recreating containers + def doRecreateContainer(self, userID, data, con): try: name = data['name'] - unlisted = data['unlisted'] # Pass this as 1 if image is not known for container + unlisted = data['unlisted'] # Pass this as 1 if image is not known for container image = data['image'] tag = data['tag'] env = data['env'] port = data['ports'] memory = data['memory'] - + if image == 'unknown': return "Image name not known" # Call container delete function delStatus = self.submitContainerDeletion(userID, data, True) if delStatus != 0: return delStatus - + print env - containerArgs = {'image':image+":"+tag, - 'detach':True, - 'name':name, - 'ports':port, - 'environment':env, - 'publish_all_ports': True, - 'mem_limit': memory * 1048576} - + containerArgs = {'image': image + ":" + tag, + 'detach': True, + 'name': name, + 'ports': port, + 'environment': env, + 'publish_all_ports': True, + 'mem_limit': memory * 1048576} + if con.startOnReboot == 1: containerArgs['restart_policy'] = {"Name": "always"} - + container = client.containers.create(**containerArgs) con.cid = container.id con.save() - + return 0 except BaseException, msg: return str(msg) - - def saveContainerSettings(self, userID = None, data = None): - try: + + def saveContainerSettings(self, userID=None, data=None): + try: name = data['name'] if ACLManager.checkContainerOwnership(name, userID) != 1: - return ACLManager.loadErrorJson('saveSettingsStatus',0) - + return ACLManager.loadErrorJson('saveSettingsStatus', 0) + client = docker.from_env() dockerAPI = docker.APIClient() - + memory = data['memory'] startOnReboot = data['startOnReboot'] envList = data['envList'] - + if startOnReboot == True: startOnReboot = 1 rPolicy = {"Name": "always"} else: startOnReboot = 0 rPolicy = {} - + try: container = client.containers.get(name) except docker.errors.NotFound as err: @@ -913,35 +908,36 @@ class ContainerManager: data_ret = {'saveSettingsStatus': 0, 'error_message': 'Unknown'} json_data = json.dumps(data_ret) return HttpResponse(json_data) - + try: container.update(mem_limit=memory * 1048576, - restart_policy = rPolicy) + restart_policy=rPolicy) except docker.errors.APIError as err: data_ret = {'saveSettingsStatus': 0, 'error_message': str(err)} json_data = json.dumps(data_ret) - return HttpResponse(json_data) - + return HttpResponse(json_data) + con = Containers.objects.get(name=name) con.memory = memory con.startOnReboot = startOnReboot - + if 'envConfirmation' in data and data['envConfirmation']: # Formatting envList for usage envDict = {} for key, value in envList.iteritems(): if (value['name'] != '') or (value['value'] != ''): envDict[value['name']] = value['value'] - + print envDict # Prepare data for recreate function data = { 'name': name, - 'unlisted': 0, + 'unlisted': 0, 'image': con.image, 'tag': con.tag, 'env': envDict, - 'ports': json.loads(con.ports), # No filter needed now as its ports are filtered when adding to database + 'ports': json.loads(con.ports), + # No filter needed now as its ports are filtered when adding to database 'memory': con.memory } @@ -949,11 +945,11 @@ class ContainerManager: if recreateStatus != 0: data_ret = {'saveSettingsStatus': 0, 'error_message': str(recreateStatus)} json_data = json.dumps(data_ret) - return HttpResponse(json_data) - + return HttpResponse(json_data) + con.env = json.dumps(envDict) - con.save() - + con.save() + data_ret = {'saveSettingsStatus': 1, 'error_message': 'None'} json_data = json.dumps(data_ret) return HttpResponse(json_data) @@ -961,17 +957,17 @@ class ContainerManager: except BaseException, msg: data_ret = {'saveSettingsStatus': 0, 'error_message': str(msg)} json_data = json.dumps(data_ret) - return HttpResponse(json_data) - - def recreateContainer(self, userID = None, data = None): + return HttpResponse(json_data) + + def recreateContainer(self, userID=None, data=None): try: name = data['name'] if ACLManager.checkContainerOwnership(name, userID) != 1: - return ACLManager.loadErrorJson('saveSettingsStatus',0) - + return ACLManager.loadErrorJson('saveSettingsStatus', 0) + client = docker.from_env() dockerAPI = docker.APIClient() - + try: container = client.containers.get(name) except docker.errors.NotFound as err: @@ -982,9 +978,9 @@ class ContainerManager: data_ret = {'recreateContainerStatus': 0, 'error_message': 'Unknown'} json_data = json.dumps(data_ret) return HttpResponse(json_data) - + con = Containers.objects.get(name=name) - + # Prepare data for recreate function data = { 'name': name, @@ -992,16 +988,17 @@ class ContainerManager: 'image': con.image, 'tag': con.tag, 'env': json.loads(con.env), - 'ports': json.loads(con.ports), # No filter needed now as its ports are filtered when adding to database + 'ports': json.loads(con.ports), + # No filter needed now as its ports are filtered when adding to database 'memory': con.memory } - + recreateStatus = self.doRecreateContainer(userID, data, con) if recreateStatus != 0: data_ret = {'recreateContainerStatus': 0, 'error_message': str(recreateStatus)} json_data = json.dumps(data_ret) - return HttpResponse(json_data) - + return HttpResponse(json_data) + data_ret = {'recreateContainerStatus': 1, 'error_message': 'None'} json_data = json.dumps(data_ret) return HttpResponse(json_data) @@ -1009,15 +1006,15 @@ class ContainerManager: except BaseException, msg: data_ret = {'recreateContainerStatus': 0, 'error_message': str(msg)} json_data = json.dumps(data_ret) - return HttpResponse(json_data) - - def getTags(self, userID = None, data = None): + return HttpResponse(json_data) + + def getTags(self, userID=None, data=None): try: - + admin = Administrator.objects.get(pk=userID) if admin.acl.adminStatus != 1: return ACLManager.loadError() - + image = data['image'] page = data['page'] @@ -1027,16 +1024,17 @@ class ContainerManager: image2 = "library/" + image print image - registryData = requests.get('https://registry.hub.docker.com/v2/repositories/'+image2+'/tags', {'page':page}).json() + registryData = requests.get('https://registry.hub.docker.com/v2/repositories/' + image2 + '/tags', + {'page': page}).json() tagList = [] for tag in registryData['results']: tagList.append(tag['name']) - - data_ret = {'getTagsStatus': 1, 'list': tagList, 'next':registryData['next'], 'error_message': None} + + data_ret = {'getTagsStatus': 1, 'list': tagList, 'next': registryData['next'], 'error_message': None} json_data = json.dumps(data_ret) - return HttpResponse(json_data) + return HttpResponse(json_data) except BaseException, msg: data_ret = {'getTagsStatus': 0, 'error_message': str(msg)} json_data = json.dumps(data_ret) - return HttpResponse(json_data) \ No newline at end of file + return HttpResponse(json_data) \ No newline at end of file diff --git a/plogical/modSec.py b/plogical/modSec.py index 60ec1f8bf..92f2d784b 100644 --- a/plogical/modSec.py +++ b/plogical/modSec.py @@ -498,17 +498,26 @@ modsecurity_rules_file /usr/local/lsws/conf/modsec/owasp/rules/RESPONSE-999-EXCL def disableRuleFile(fileName, packName): try: - confFile = os.path.join(virtualHostUtilities.Server_root, "conf/httpd_config.conf") - confData = open(confFile).readlines() - conf = open(confFile, 'w') + if ProcessUtilities.decideServer() == ProcessUtilities.OLS: + confFile = os.path.join(virtualHostUtilities.Server_root, "conf/httpd_config.conf") + confData = open(confFile).readlines() + conf = open(confFile, 'w') - for items in confData: - if items.find('modsec/'+packName) > -1 and items.find(fileName) > -1: - conf.write("#" + items) - else: - conf.writelines(items) + for items in confData: + if items.find('modsec/'+packName) > -1 and items.find(fileName) > -1: + conf.write("#" + items) + else: + conf.writelines(items) - conf.close() + conf.close() + + else: + path = '/usr/local/lsws/conf/comodo_litespeed/' + completePath = path + fileName + completePathBak = path + fileName + '.bak' + + command = 'mv ' + completePath + ' ' + completePathBak + ProcessUtilities.executioner(command) print "1,None" @@ -521,17 +530,25 @@ modsecurity_rules_file /usr/local/lsws/conf/modsec/owasp/rules/RESPONSE-999-EXCL def enableRuleFile(fileName, packName): try: - confFile = os.path.join(virtualHostUtilities.Server_root, "conf/httpd_config.conf") - confData = open(confFile).readlines() - conf = open(confFile, 'w') + if ProcessUtilities.decideServer() == ProcessUtilities.OLS: + confFile = os.path.join(virtualHostUtilities.Server_root, "conf/httpd_config.conf") + confData = open(confFile).readlines() + conf = open(confFile, 'w') - for items in confData: - if items.find('modsec/' + packName) > -1 and items.find(fileName) > -1: - conf.write(items.lstrip('#')) - else: - conf.writelines(items) + for items in confData: + if items.find('modsec/' + packName) > -1 and items.find(fileName) > -1: + conf.write(items.lstrip('#')) + else: + conf.writelines(items) - conf.close() + conf.close() + else: + path = '/usr/local/lsws/conf/comodo_litespeed/' + completePath = path + fileName + completePathBak = path + fileName + '.bak' + + command = 'mv ' + completePathBak + ' ' + completePath + ProcessUtilities.executioner(command) print "1,None" diff --git a/plogical/mysqlUtilities.py b/plogical/mysqlUtilities.py index a1c5869db..795fe8e8a 100644 --- a/plogical/mysqlUtilities.py +++ b/plogical/mysqlUtilities.py @@ -12,9 +12,37 @@ import MySQLdb as mysql import json from random import randint from plogical.processUtilities import ProcessUtilities +import MySQLdb.cursors as cursors +from math import ceil class mysqlUtilities: + @staticmethod + def getPagination(records, toShow): + pages = float(records) / float(toShow) + + pagination = [] + counter = 1 + + if pages <= 1.0: + pages = 1 + pagination.append(counter) + else: + pages = ceil(pages) + finalPages = int(pages) + 1 + + for i in range(1, finalPages): + pagination.append(counter) + counter = counter + 1 + + return pagination + + @staticmethod + def recordsPointer(page, toShow): + finalPageNumber = ((page * toShow)) - toShow + endPageNumber = finalPageNumber + toShow + return endPageNumber, finalPageNumber + @staticmethod def setupConnection(): try: @@ -25,7 +53,7 @@ class mysqlUtilities: password = data.split('\n', 1)[0] password = password.strip('\n').strip('\r') - conn = mysql.connect(user='root', passwd=password) + conn = mysql.connect(user='root', passwd=password, cursorclass=cursors.SSCursor) cursor = conn.cursor() return conn, cursor @@ -355,4 +383,227 @@ class mysqlUtilities: command = 'sudo mv /etc/my.cnf.bak /etc/my.cnf' subprocess.call(shlex.split(command)) logging.CyberCPLogFileWriter.writeToFile(str(msg)) - return 0, str(msg) \ No newline at end of file + return 0, str(msg) + + @staticmethod + def fetchDatabases(): + try: + + connection, cursor = mysqlUtilities.setupConnection() + + if connection == 0: + return 0 + + data = {} + data['status'] = 1 + + cursor.execute("SHOW DATABASES") + result = cursor.fetchall() + + counter = 1 + json_data = "[" + checker = 0 + + for items in result: + if items[0] == 'information_schema' or items[0] == 'mysql' or items[0] == 'performance_schema' or items[ + 0] == 'performance_schema': + continue + + dic = { + 'id': counter, + 'database': items[0] + + } + counter = counter + 1 + + if checker == 0: + json_data = json_data + json.dumps(dic) + checker = 1 + else: + json_data = json_data + ',' + json.dumps(dic) + + + json_data = json_data + ']' + data['databases'] = json_data + return data + + except BaseException, msg: + logging.CyberCPLogFileWriter.writeToFile(str(msg) + "[fetchDatabases]") + return 0 + + @staticmethod + def fetchTables(name): + try: + + connection, cursor = mysqlUtilities.setupConnection() + + if connection == 0: + return 0 + + data = {} + data['status'] = 1 + + cursor.execute("use " + name['databaseName']) + cursor.execute("SHOW TABLE STATUS") + result = cursor.fetchall() + + counter = 1 + json_data = "[" + checker = 0 + + for items in result: + + dic = { + 'Name': items[0], + 'Engine': items[1], + 'Version': items[2], + 'rowFormat': items[3], + 'rows': items[4], + 'Collation': items[14] + } + counter = counter + 1 + + if checker == 0: + json_data = json_data + json.dumps(dic) + checker = 1 + else: + json_data = json_data + ',' + json.dumps(dic) + + json_data = json_data + ']' + data['tables'] = json_data + return data + + except BaseException, msg: + logging.CyberCPLogFileWriter.writeToFile(str(msg) + "[fetchDatabases]") + return 0 + + @staticmethod + def deleteTable(name): + try: + + connection, cursor = mysqlUtilities.setupConnection() + + if connection == 0: + return 0 + + data = {} + data['status'] = 1 + + cursor.execute("use " + name['databaseName']) + cursor.execute("DROP TABLE " + name['tableName']) + + return data + + except BaseException, msg: + logging.CyberCPLogFileWriter.writeToFile(str(msg) + "[fetchDatabases]") + return 0 + + @staticmethod + def fetchTableData(name): + try: + + connection, cursor = mysqlUtilities.setupConnection() + + if connection == 0: + return 0 + + recordsToShow = int(name['recordsToShow']) + page = int(name['currentPage']) + + data = {} + data['status'] = 1 + + ## + + cursor.execute("use " + name['databaseName']) + cursor.execute("select count(*) from " + name['tableName']) + rows = cursor.fetchall()[0][0] + + + ## + + cursor.execute("desc " + name['tableName']) + result = cursor.fetchall() + + data['completeData'] = '' + + for items in result: + data['completeData'] = data['completeData'] + '' + + data['completeData'] = data['completeData'] + '' + + data['completeData'] = data['completeData'] + '' + + ## + + data['pagination'] = mysqlUtilities.getPagination(rows, recordsToShow) + endPageNumber, finalPageNumber = mysqlUtilities.recordsPointer(page, recordsToShow) + + cursor.execute("select * from " + name['tableName']) + result = cursor.fetchall() + + for items in result[finalPageNumber:endPageNumber]: + data['completeData'] = data['completeData'] + '' + for it in items: + data['completeData'] = data['completeData'] + '' + data['completeData'] = data['completeData'] + '' + + data['completeData'] = data['completeData'] + '' + + ## + + return data + + except BaseException, msg: + logging.CyberCPLogFileWriter.writeToFile(str(msg) + "[fetchTableData]") + return 0 + + @staticmethod + def fetchStructure(name): + try: + + connection, cursor = mysqlUtilities.setupConnection() + + if connection == 0: + return 0 + + cursor.execute("use " + name['databaseName']) + cursor.execute("desc " + name['tableName']) + result = cursor.fetchall() + + ## Columns List + + data = {} + data['status'] = 1 + + json_data = "[" + checker = 0 + + for items in result: + + dic = { + 'Name': items[0], + 'Type': items[1], + 'Null': items[2], + 'Key': items[3], + 'Default': items[4], + 'Extra': items[5] + } + + if checker == 0: + json_data = json_data + json.dumps(dic) + checker = 1 + else: + json_data = json_data + ',' + json.dumps(dic) + + json_data = json_data + ']' + + data['columns'] = json_data + + ## + + return data + + except BaseException, msg: + logging.CyberCPLogFileWriter.writeToFile(str(msg) + "[showStatus]") + return 0 \ No newline at end of file diff --git a/requirments.txt b/requirments.txt index 28635fd36..ec761a32e 100644 --- a/requirments.txt +++ b/requirments.txt @@ -12,6 +12,8 @@ ConfigArgParse==0.13.0 configobj==4.7.2 cryptography==2.2.2 decorator==3.4.0 +docker==3.6.0 +docker-pycreds==0.4.0 Django==1.11 docutils==0.14 enum34==1.1.6
    Name (Installed)Name (Locally Available) Tags Action
    ' + items[0] + '
    ' + str(it) + '