Refactoring fixing some code issues

This commit is contained in:
Adolfo Gómez García 2017-03-03 08:32:46 +01:00
parent 240be81f26
commit 55dd16d508
30 changed files with 71 additions and 44 deletions

1
.gitignore vendored
View File

@ -7,6 +7,7 @@
*_enterprise.* *_enterprise.*
.settings/ .settings/
.ipynb_checkpoints .ipynb_checkpoints
.idea/
# Debian buildings # Debian buildings
*.debhelper* *.debhelper*

View File

@ -13,16 +13,22 @@ middleware here, or combine a Django application with an application of another
framework. framework.
""" """
import sys from django.core.wsgi import get_wsgi_application
reload(sys)
sys.setdefaultencoding('UTF-8') # @UndefinedVariable
import six
import os import os
if six.PY2:
import sys
# noinspection PyCompatibility
reload(sys)
sys.setdefaultencoding('UTF-8') # @UndefinedVariable
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "server.settings") os.environ.setdefault("DJANGO_SETTINGS_MODULE", "server.settings")
# This application object is used by any WSGI server configured to use this # This application object is used by any WSGI server configured to use this
# file. This includes Django's development server, if the WSGI_APPLICATION # file. This includes Django's development server, if the WSGI_APPLICATION
# setting points here. # setting points here.
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application() application = get_wsgi_application()

View File

@ -1,4 +1,10 @@
import sys import six
reload(sys)
sys.setdefaultencoding('utf-8') # @UndefinedVariable if six.PY2:
import sys
# noinspection PyCompatibility
reload(sys)
# noinspection PyUnresolvedReferences
sys.setdefaultencoding('utf-8') # @UndefinedVariable

View File

@ -150,7 +150,7 @@ class RegexLdap(auths.Authenticator):
for line in field.splitlines(): for line in field.splitlines():
equalPos = line.find('=') equalPos = line.find('=')
if equalPos == -1: if equalPos == -1:
line = line + '=(.*)' line += '=(.*)'
equalPos = line.find('=') equalPos = line.find('=')
attr, pattern = (line[:equalPos], line[equalPos + 1:]) attr, pattern = (line[:equalPos], line[equalPos + 1:])
attr = attr.lower() attr = attr.lower()

View File

@ -305,4 +305,4 @@ class SampleAuth(auths.Authenticator):
this this kind of things) modify the userName to a new one, the original plus '-1' this this kind of things) modify the userName to a new one, the original plus '-1'
''' '''
usrData['real_name'] = usrData['name'] + ' ' + usrData['name'] usrData['real_name'] = usrData['name'] + ' ' + usrData['name']
usrData['name'] = usrData['name'] + '-1' usrData['name'] += '-1'

View File

@ -197,7 +197,7 @@ class Authenticator(Module):
user param is a database user object user param is a database user object
''' '''
if self.isExternalSource == True: if self.isExternalSource:
groupsManager = GroupsManager(self._dbAuth) groupsManager = GroupsManager(self._dbAuth)
self.getGroups(user.name, groupsManager) self.getGroups(user.name, groupsManager)
user.groups = [g.dbGroup() for g in groupsManager.getValidGroups()] user.groups = [g.dbGroup() for g in groupsManager.getValidGroups()]

View File

@ -35,6 +35,7 @@ from __future__ import unicode_literals
from uds.core.services.BaseServiceProvider import ServiceProvider from uds.core.services.BaseServiceProvider import ServiceProvider
from uds.core.util.Config import GlobalConfig from uds.core.util.Config import GlobalConfig
import six
import logging import logging
__updated__ = '2016-02-26' __updated__ = '2016-02-26'
@ -85,7 +86,7 @@ class ClusteredServiceProvider(ServiceProvider):
maxCpuLoad = GlobalConfig.CLUSTER_MIGRATE_CPULOAD.getInt(True) maxCpuLoad = GlobalConfig.CLUSTER_MIGRATE_CPULOAD.getInt(True)
minFreeMemPercent = GlobalConfig.CLUSTER_MIGRATE_MEMORYLOAD.getInt(True) minFreeMemPercent = GlobalConfig.CLUSTER_MIGRATE_MEMORYLOAD.getInt(True)
for nodeName, nodeStats in nodesStats.iteritems(): for nodeName, nodeStats in six.iteritems(nodesStats):
if nodeStats['freeMemory'] is None or nodeStats['totalMemory'] is None or nodeStats['cpuLoad'] is None: if nodeStats['freeMemory'] is None or nodeStats['totalMemory'] is None or nodeStats['cpuLoad'] is None:
continue continue
freeMemPercent = (nodeStats['freeMemory'] * 100) / nodeStats['totalMemory'] freeMemPercent = (nodeStats['freeMemory'] * 100) / nodeStats['totalMemory']
@ -123,7 +124,7 @@ class ClusteredServiceProvider(ServiceProvider):
maxCpuLoad = GlobalConfig.CLUSTER_ELEGIBLE_CPULOAD.getInt(True) maxCpuLoad = GlobalConfig.CLUSTER_ELEGIBLE_CPULOAD.getInt(True)
minFreeMemPercent = GlobalConfig.CLUSTER_ELEGIBLE_MEMORYLOAD.getInt(True) minFreeMemPercent = GlobalConfig.CLUSTER_ELEGIBLE_MEMORYLOAD.getInt(True)
for nodeName, nodeStats in nodesStats.iteritems(): for nodeName, nodeStats in six.iteritems(nodesStats):
if nodeStats['freeMemory'] is None or nodeStats['totalMemory'] is None or nodeStats['cpuLoad'] is None: if nodeStats['freeMemory'] is None or nodeStats['totalMemory'] is None or nodeStats['cpuLoad'] is None:
continue continue
freeMemPercent = (nodeStats['freeMemory'] * 100) / nodeStats['totalMemory'] freeMemPercent = (nodeStats['freeMemory'] * 100) / nodeStats['totalMemory']
@ -144,7 +145,7 @@ class ClusteredServiceProvider(ServiceProvider):
def getClusterBestNodeForDeploy(self): def getClusterBestNodeForDeploy(self):
nodesStats = self.clusterStats() nodesStats = self.clusterStats()
nodes = [name for name in nodesStats.iterkeys()] nodes = [name for name in six.iteritems(nodesStats)]
def getNodeStatsKey(name): def getNodeStatsKey(name):
ns = nodesStats[name] ns = nodesStats[name]

View File

@ -71,9 +71,9 @@ class FileStorage(Storage):
self.cache._cache.flush_all() # On start, ensures that cache is empty to avoid surprises self.cache._cache.flush_all() # On start, ensures that cache is empty to avoid surprises
# noinspection PyArgumentList
Storage.__init__(self, *args, **kwargs) Storage.__init__(self, *args, **kwargs)
def get_valid_name(self, name): def get_valid_name(self, name):
return name.replace('\\', os.path.sep) return name.replace('\\', os.path.sep)

View File

@ -46,11 +46,15 @@ logger = logging.getLogger(__name__)
# Decorator that protects pages that needs at least a browser version # Decorator that protects pages that needs at least a browser version
# Default is to deny IE < 9 # Default is to deny IE < 9
def denyBrowsers(browsers=['ie<9'], errorResponse=lambda request: errors.errorView(request, errors.BROWSER_NOT_SUPPORTED)): def denyBrowsers(browsers=None, errorResponse=lambda request: errors.errorView(request, errors.BROWSER_NOT_SUPPORTED)):
''' '''
Decorator to set protection to access page Decorator to set protection to access page
Look for samples at uds.core.web.views Look for samples at uds.core.web.views
''' '''
if browsers is None:
browsers = ['ie<9']
def wrap(view_func): def wrap(view_func):
@wraps(view_func) @wraps(view_func)
def _wrapped_view(request, *args, **kwargs): def _wrapped_view(request, *args, **kwargs):

View File

@ -64,7 +64,7 @@ def longToIp(n):
while d > 0: while d > 0:
m, n = divmod(n, d) m, n = divmod(n, d)
q.append(str(m)) q.append(str(m))
d = d / 256 d /= 256
return '.'.join(q) return '.'.join(q)
except: except:

View File

@ -165,9 +165,9 @@ class ClusterMigrationTask(DelayedTask):
logger.debug("uService instance class: {0}".format(ci.__class__)) logger.debug("uService instance class: {0}".format(ci.__class__))
state = ci.checkState() state = ci.checkState()
ClusterMigrationTask.checkAndUpdateState(uService, ci, state) ClusterMigrationTask.checkAndUpdateState(uService, ci, state)
except UserService.DoesNotExist, e: except UserService.DoesNotExist as e:
logger.error('User service not found (erased from database?) {0} : {1}'.format(e.__class__, e)) logger.error('User service not found (erased from database?) {0} : {1}'.format(e.__class__, e))
except Exception, e: except Exception as e:
# Exception caught, mark service as errored # Exception caught, mark service as errored
logger.exception("Error {0}, {1} :".format(e.__class__, e)) logger.exception("Error {0}, {1} :".format(e.__class__, e))
if uService is not None: if uService is not None:

View File

@ -83,7 +83,7 @@ class DeployedServiceRemover(Job):
u.cancel() u.cancel()
# Nice start of removal, maybe we need to do some limitation later, but there should not be too much services nor publications cancelable at once # Nice start of removal, maybe we need to do some limitation later, but there should not be too much services nor publications cancelable at once
ds.state = State.REMOVING ds.state = State.REMOVING
ds.name = ds.name + ' (removed)' ds.name += ' (removed)'
ds.save() ds.save()
def continueRemovalOf(self, ds): def continueRemovalOf(self, ds):

View File

@ -34,9 +34,9 @@ from __future__ import unicode_literals
from django.http import HttpResponse from django.http import HttpResponse
from uds.models import TicketStore from uds.models import TicketStore
from uds.core.util import net
from uds.core.auths import auth from uds.core.auths import auth
import six
import logging import logging
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -48,7 +48,7 @@ CONTENT_TYPE = 'text/plain'
def dict2resp(dct): def dict2resp(dct):
return '\r'.join((k + '\t' + v for k, v in dct.iteritems())) return '\r'.join((k + '\t' + v for k, v in six.iteritems(dct)))
@auth.trustedSourceRequired @auth.trustedSourceRequired

View File

@ -225,8 +225,8 @@ class WindowsOsManager(osmanagers.OSManager):
'password': password, 'password': password,
'domain': domain 'domain': domain
} }
ticket = TicketStore.create(creds, validator=None, validity=300, owner=SECURE_OWNER, secure=True) ticket = TicketStore.create(creds, validator=None, validity=300) # , owner=SECURE_OWNER, secure=True)
return (ticket, '') return ticket, ''
else: else:
return osmanagers.OSManager.processUserPassword(self, service, username, password) return osmanagers.OSManager.processUserPassword(self, service, username, password)

View File

@ -39,4 +39,7 @@ __updated__ = '2015-06-21'
class ListReport(reports.Report): class ListReport(reports.Report):
def generate(self):
raise NotImplementedError('ListReport generate invoked and not implemented')
group = _('Lists') # So we can make submenus with reports group = _('Lists') # So we can make submenus with reports

View File

@ -64,6 +64,7 @@ from . import template
from . import vm from . import vm
from . import storage from . import storage
# Decorator # Decorator
def ensureConnected(fnc): def ensureConnected(fnc):
def inner(*args, **kwargs): def inner(*args, **kwargs):
@ -71,19 +72,22 @@ def ensureConnected(fnc):
return fnc(*args, **kwargs) return fnc(*args, **kwargs)
return inner return inner
# Result checker # Result checker
def checkResult(lst, parseResult=True): def checkResult(lst, parseResult=True):
if lst[0] == False: if not lst[0]:
raise Exception('OpenNebula error {}: "{}"'.format(lst[2], lst[1])) raise Exception('OpenNebula error {}: "{}"'.format(lst[2], lst[1]))
if parseResult: if parseResult:
return xml2dict.parse(lst[1]) return xml2dict.parse(lst[1])
else: else:
return lst[1] return lst[1]
def asList(element): def asList(element):
if isinstance(element, (tuple, list)): if isinstance(element, (tuple, list)):
return element return element
return (element,) return element,
class OpenNebulaClient(object): class OpenNebulaClient(object):
def __init__(self, username, password, endpoint): def __init__(self, username, password, endpoint):

View File

@ -65,7 +65,7 @@ def ensureResponseIsValid(response, errMsg=None):
if response.ok is False: if response.ok is False:
try: try:
_x, err = response.json().popitem() # Extract any key, in case of error is expected to have only one top key so this will work _x, err = response.json().popitem() # Extract any key, in case of error is expected to have only one top key so this will work
errMsg = errMsg + ': {message}'.format(**err) errMsg += ': {message}'.format(**err)
except Exception: except Exception:
pass # If error geting error message, simply ignore it (will be loged on service log anyway) pass # If error geting error message, simply ignore it (will be loged on service log anyway)
if errMsg is None: if errMsg is None:

View File

@ -37,13 +37,14 @@ from django.utils.translation import ugettext_noop as _
from uds.core.util.State import State from uds.core.util.State import State
from uds.core.services import ServiceProvider from uds.core.services import ServiceProvider
from uds.core.ui import gui from uds.core.ui import gui
from uds.core.util import validators # from uds.core.util import validators
from xen_client import XenServer from xen_client import XenServer
from xen_client import XenFailure, XenFault # from xen_client import XenFailure, XenFault
from XenLinkedService import XenLinkedService from XenLinkedService import XenLinkedService
import six
import logging import logging
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -395,4 +396,4 @@ class Provider(ServiceProvider):
xe.testConnection() xe.testConnection()
return [True, _('Connection test successful')] return [True, _('Connection test successful')]
except Exception as e: except Exception as e:
return [False, _("Connection failed: {0}").format(unicode(e))] return [False, _("Connection failed: {0}").format(six.text_type(e))]

View File

@ -68,7 +68,6 @@ class NXPassword(object):
for i in range(len(p)): for i in range(len(p)):
sPass += '{}:'.format(ord(p[i]) + i + 1) sPass += '{}:'.format(ord(p[i]) + i + 1)
print sPass
return sPass return sPass
@staticmethod @staticmethod

View File

@ -200,6 +200,6 @@ class NXTransport(Transport):
}.get(os['OS']) }.get(os['OS'])
if os is None: if os is None:
return super(NXTransport, self).getUDSTransportScript(self, userService, transport, ip, os, user, password, request) return super(self.__class__, self).getUDSTransportScript(userService, transport, ip, os, user, password, request)
return self.getScript('scripts/{}/direct.py'.format(os)).format(r=r) return self.getScript('scripts/{}/direct.py'.format(os)).format(r=r)

View File

@ -236,7 +236,7 @@ class TSNXTransport(Transport):
}.get(os['OS']) }.get(os['OS'])
if os is None: if os is None:
return super(TSNXTransport, self).getUDSTransportScript(self, userService, transport, ip, os, user, password, request) return super(self.__class__, self).getUDSTransportScript(userService, transport, ip, os, user, password, request)
return self.getScript('scripts/{}/tunnel.py'.format(os)).format( return self.getScript('scripts/{}/tunnel.py'.format(os)).format(
r=r, r=r,

View File

@ -145,6 +145,6 @@ class RDPTransport(BaseRDPTransport):
}.get(m.os) }.get(m.os)
if os is None: if os is None:
return super(RDPTransport, self).getUDSTransportScript(self, userService, transport, ip, os, user, password, request) return super(self.__class__, self).getUDSTransportScript(userService, transport, ip, os, user, password, request)
return self.getScript('scripts/{}/direct.py'.format(os)).format(m=m) return self.getScript('scripts/{}/direct.py'.format(os)).format(m=m)

View File

@ -179,6 +179,6 @@ class TRDPTransport(BaseRDPTransport):
}.get(m.os) }.get(m.os)
if os is None: if os is None:
return super(TRDPTransport, self).getUDSTransportScript(self, userService, transport, ip, os, user, password, request) return super(self.__class__, self).getUDSTransportScript(userService, transport, ip, os, user, password, request)
return self.getScript('scripts/{}/tunnel.py'.format(os)).format(m=m) return self.getScript('scripts/{}/tunnel.py'.format(os)).format(m=m)

View File

@ -83,6 +83,6 @@ class SPICETransport(BaseSpiceTransport):
}.get(os.OS) }.get(os.OS)
if os is None: if os is None:
return super(SPICETransport, self).getUDSTransportScript(self, userService, transport, ip, os, user, password, request) return super(self.__class__, self).getUDSTransportScript(userService, transport, ip, os, user, password, request)
return self.getScript('scripts/{}/direct.py'.format(os)).format(m=m) return self.getScript('scripts/{}/direct.py'.format(os)).format(m=m)

View File

@ -108,6 +108,6 @@ class TSPICETransport(BaseSpiceTransport):
}.get(os.OS) }.get(os.OS)
if os is None: if os is None:
return super(TSPICETransport, self).getUDSTransportScript(self, userService, transport, ip, os, user, password, request) return super(self.__class__, self).getUDSTransportScript(userService, transport, ip, os, user, password, request)
return self.getScript('scripts/{}/tunnel.py'.format(os)).format(m=m) return self.getScript('scripts/{}/tunnel.py'.format(os)).format(m=m)

View File

@ -140,6 +140,6 @@ class TX2GOTransport(BaseX2GOTransport):
}.get(m.os) }.get(m.os)
if os is None: if os is None:
return super(BaseX2GOTransport, self).getUDSTransportScript(self, userService, transport, ip, os, user, password, request) return super(self.__class__, self).getUDSTransportScript(userService, transport, ip, os, user, password, request)
return self.getScript('scripts/{}/tunnel.py'.format(os)).format(m=m) return self.getScript('scripts/{}/tunnel.py'.format(os)).format(m=m)

View File

@ -38,6 +38,7 @@ from uds.core.util import tools
from .BaseX2GOTransport import BaseX2GOTransport from .BaseX2GOTransport import BaseX2GOTransport
from . import x2gofile from . import x2gofile
import six
import logging import logging
__updated__ = '2017-01-30' __updated__ = '2017-01-30'
@ -113,6 +114,6 @@ class X2GOTransport(BaseX2GOTransport):
}.get(m.os) }.get(m.os)
if os is None: if os is None:
return super(X2GOTransport, self).getUDSTransportScript(self, userService, transport, ip, os, user, password, request) return super(self.__class__, self).getUDSTransportScript(userService, transport, ip, os, user, password, request)
return self.getScript('scripts/{}/direct.py'.format(os)).format(m=m) return self.getScript('scripts/{}/direct.py'.format(os)).format(m=m)

View File

@ -29,7 +29,7 @@ def updateAuthorizedKeys(user, pubKey):
sshFolder = '{}/.ssh'.format(home) sshFolder = '{}/.ssh'.format(home)
if not os.path.exists(sshFolder): if not os.path.exists(sshFolder):
try: try:
os.makedirs(sshFolder, 0700) os.makedirs(sshFolder, 0o0700)
os.chown(sshFolder, uid, -1) os.chown(sshFolder, uid, -1)
except OSError as e: except OSError as e:
if e.errno != errno.EEXIST: if e.errno != errno.EEXIST:
@ -53,7 +53,7 @@ def updateAuthorizedKeys(user, pubKey):
# Ensure access is correct # Ensure access is correct
os.chown(authorizedKeys, uid, -1) os.chown(authorizedKeys, uid, -1)
os.chmod(authorizedKeys, 0600) os.chmod(authorizedKeys, 0o0600)
# Done # Done

View File

@ -33,7 +33,7 @@
from __future__ import unicode_literals from __future__ import unicode_literals
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from django.shortcuts import render_to_response from django.shortcuts import render
from django.template import RequestContext from django.template import RequestContext
from django.http import HttpResponseRedirect from django.http import HttpResponseRedirect
@ -148,4 +148,4 @@ def error(request, idError):
if code != 0: if code != 0:
errStr += ' (code {0:04X})'.format(code) errStr += ' (code {0:04X})'.format(code)
return render_to_response(theme.template('error.html'), {'errorString': errStr}, context_instance=RequestContext(request)) return render(request, theme.template('error.html'), {'errorString': errStr})

View File

@ -75,7 +75,7 @@ def index(request):
:param request: http request :param request: http request
''' '''
if request.session.get('ticket') == '1': if request.session.get('ticket') == '1':
return webLogout() return webLogout(request)
# Session data # Session data
os = request.os os = request.os
@ -222,7 +222,8 @@ def index(request):
logger.debug('Groups: {}'.format(groups)) logger.debug('Groups: {}'.format(groups))
response = render(request, response = render(
request,
theme.template('index.html'), theme.template('index.html'),
{ {
'groups': groups, 'groups': groups,