Deleted server.services module.

develop
Самоукин Алексей 14 years ago
parent b5ed0efaa0
commit cf95efe682

@ -1,223 +0,0 @@
#-*- coding: utf-8 -*-
# Copyright 2008-2010 Calculate Ltd. http://www.calculate-linux.org
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import sys, os
from cl_print import color_print
from cl_datavars import DataVars
import cl_overriding
# translate language
import cl_lang
tr = cl_lang.lang()
tr.setLocalDomain('cl_lib')
tr.setLanguage(sys.modules[__name__])
class services(color_print):
'''Общие методы для серверных программ,
Методы для работы с сервисами'''
clLibVars = False
def createClLibVars(self, clLibVars=False):
"""Создает объект библиотечных переменных"""
if not clLibVars:
clLibVars = DataVars()
# Заменяем значения переменных переменными из env файлов
clLibVars.flIniFile()
# Устанавливаем у объекта атрибут объект переменных
self.clLibVars = clLibVars
return True
def allServicesApi(self):
self.createClLibVars(clLibVars=self.clLibVars)
apiDict = self.clLibVars.Get('cl_api')
if not apiDict or not type(apiDict) is dict:
return []
apiFiles = apiDict.values()
apiObjects = []
for apiFile in apiFiles:
importPath, moduleName = os.path.split(apiFile)
moduleName = moduleName.rpartition(".py")[0]
if not importPath in sys.path:
sys.path.insert(0, importPath)
try:
className = getattr(__import__(moduleName,
globals(), locals(),
['serviceAPI']),
'serviceAPI')
except (ImportError, AttributeError):
self.printERROR(_("Can not import module %s")% apiFile)
return False
try:
apiObjects.append(className())
except:
self.printERROR(_("Can not create API object"))
self.printERROR(_("Module %s")% apiFile)
return False
return apiObjects
def runMethodToPkg(self, pkgName, methodName, argv=[]):
"""Execute method from pkg"""
allServObjs = self.allServicesApi()
if allServObjs is False:
return False
allSetupServObjs = filter(lambda x:\
x.isSetup() and x.getPkgName()==pkgName,
allServObjs)
if not allSetupServObjs:
return False
pkgObj = allSetupServObjs[0]
if not hasattr(pkgObj, methodName):
self.printERROR(_("Can not found method %(method)s in \
API object service %(service)s") %{'method':methodName,
'service':pkgObj.getServiceName()})
cl_overriding.exit(1)
if argv:
res = getattr(pkgObj, methodName)(*argv)
else:
res = getattr(pkgObj, methodName)()
return res
def runMethodFromAllServices(self, methodName, sortedServices="normal",
argv=[], result='bool'):
"""Execute method from all setup services"""
if not result in ('bool', 'list'):
self.printERROR(_("Method: runMethodFromAllServices()"))
self.printERROR(_("Can't result argv in bool or list"))
cl_overriding.exit(1)
allServObjs = self.allServicesApi()
if allServObjs is False:
return False
allSetupServObjs = filter(lambda x: x.isSetup(), allServObjs)
def smpPrioritetReverse(objectA, objectB):
'''Comparison of the run priorities of objects
to sort in descending order
'''
prioritetA = objectA.getPrioritet()
prioritetB = objectB.getPrioritet()
if prioritetA == prioritetB:
return 0
elif prioritetA < prioritetB:
return 1
else:
return -1
def smpPrioritetNormal(objectA, objectB):
'''Comparison of the run priorities of objects
to sort in descending order
'''
prioritetA = objectA.getPrioritet()
prioritetB = objectB.getPrioritet()
if prioritetA == prioritetB:
return 0
elif prioritetA > prioritetB:
return 1
else:
return -1
if sortedServices == "normal":
smpPrioritet = smpPrioritetNormal
elif sortedServices == "reverse":
smpPrioritet = smpPrioritetReverse
else:
smpPrioritet = smpPrioritetNormal
allSetupServObjs.sort(smpPrioritet)
if result=='list':
data = []
for service in allSetupServObjs:
if not hasattr(service, methodName):
self.printERROR(_("Can not found method %(method)s in \
API object service %(service)s") %{'method':methodName,
'service':service.getServiceName()})
cl_overriding.exit(1)
if argv:
res = getattr(service, methodName)(*argv)
else:
res = getattr(service, methodName)()
if result=='bool':
if res is False:
return False
elif result=='list':
serviceName = service.getServiceName()
data.append((serviceName, res))
if result=='bool':
return True
elif result=='list':
return data
def stopAllServices(self):
"""Stop all services that were setup"""
return self.runMethodFromAllServices("stop", sortedServices="reverse")
def startAllServices(self):
"""Start all services that were setup"""
return self.runMethodFromAllServices("start")
def delRunlevelAllServices(self):
"""Delete all services that were setup from runlevel"""
return self.runMethodFromAllServices("delRunlevel",
sortedServices="reverse")
def delVarsFromAllServices(self):
"""Delete template vars from all services"""
return self.runMethodFromAllServices("delVarsFromEnv",
sortedServices="reverse")
def getInfoFromAllServices(self, request):
"""Get info from all services"""
return self.runMethodFromAllServices("getServiceInfo", argv=[request],
result='list')
def clGetVar(self, pkgName, varName):
"""Get variable value from pkg"""
clVars = self.runMethodToPkg(pkgName, 'getVars')
if clVars is False:
return ""
try:
value = clVars.Get(varName)
except:
self.printERROR(_("Can not found variable %(var)s "
"in package %(pkg)s") %{'var':varName,
'pkg':pkgName})
cl_overriding.exit(1)
return value
def clGet(self, pkgNameOrAll, request):
"""Get info from service or all services"""
res = ""
if pkgNameOrAll == "all":
listData = self.getInfoFromAllServices(request)
if not listData:
return res
res = "\n".join(map(lambda x: x[1], listData))
else:
res = self.runMethodToPkg(pkgNameOrAll, "getServiceInfo",
argv=[request])
if not res:
res = ""
return res
def applyTemplates(self, pkgNameOrAll):
"""Apply templates from service or all services"""
res = ""
if pkgNameOrAll == "all":
if self.runMethodFromAllServices("applyTemplates") is False:
return False
else:
if self.runMethodToPkg(pkgNameOrAll, "applyTemplates") is False:
return False
return True
Loading…
Cancel
Save