From 4a8a77715c30b577fbfcb2d3418c4e846b2e82df Mon Sep 17 00:00:00 2001 From: Marc Aymerich Date: Fri, 3 Apr 2015 10:14:45 +0000 Subject: [PATCH] More fixes for python 3 compat --- TODO.md | 12 +++++++- orchestra/apps/bills/models.py | 2 +- orchestra/apps/mailboxes/admin.py | 2 +- orchestra/apps/orchestration/methods.py | 13 +++++--- orchestra/apps/orders/models.py | 32 ++++++++++++-------- orchestra/apps/resources/methods.py | 7 +++-- orchestra/apps/systemusers/backends.py | 6 ++-- orchestra/bin/orchestra-admin | 13 ++++---- orchestra/management/commands/setupcelery.py | 6 ++-- setup.py | 2 -- 10 files changed, 58 insertions(+), 37 deletions(-) diff --git a/TODO.md b/TODO.md index 25c2bc65..d35791e1 100644 --- a/TODO.md +++ b/TODO.md @@ -238,7 +238,7 @@ require_once(‘/etc/moodles/’.$moodle_host.‘config.php’);``` moodle/drupl * billline quantity eval('10x100') instead of miningless description '(10*100)' -* IMPORTANT do more test, make sure billed until doesn't get uodated whhen services are billed with les metric, and don't upgrade billed_until when undoing under this circumstances +# FIXME do more test, make sure billed until doesn't get uodated whhen services are billed with les metric, and don't upgrade billed_until when undoing under this circumstances * line 513: change threshold and one time service metric change should update last value if not billed, only record for recurring invoicing. postpay services should store the last metric for pricing period. * add ini, end dates on bill lines and breakup quanity into size(defaut:1) and metric * threshold for significative metric accountancy on services.handler @@ -313,6 +313,16 @@ celery max-tasks-per-child * webapp has_website list filter + +# FIXME account deletion generates a integrity error + + apt-get install python3 python3-pip cp /usr/local/lib/python2.7/dist-packages/orchestra.pth /usr/local/lib/python3.4/dist-packages/ glic3rinu's django-fluent-dashboard +* gevent is not ported to python3 :'( +* uwsgi python3 + + +# FIXME what to do when deleting accounts? set fk null and fill a username charfield? issues, invoices.. we whant all this to go away? +* implement delete All related services diff --git a/orchestra/apps/bills/models.py b/orchestra/apps/bills/models.py index 2336c23b..4b9804bc 100644 --- a/orchestra/apps/bills/models.py +++ b/orchestra/apps/bills/models.py @@ -273,7 +273,7 @@ class BillLine(models.Model): rate = models.DecimalField(_("rate"), blank=True, null=True, max_digits=12, decimal_places=2) quantity = models.DecimalField(_("quantity"), max_digits=12, decimal_places=2) subtotal = models.DecimalField(_("subtotal"), max_digits=12, decimal_places=2) - tax = models.PositiveIntegerField(_("tax")) + tax = models.DecimalField(_("tax"), max_digits=2, decimal_places=2) # Undo # initial = models.DateTimeField(null=True) # end = models.DateTimeField(null=True) diff --git a/orchestra/apps/mailboxes/admin.py b/orchestra/apps/mailboxes/admin.py index c3860eed..455c4dd2 100644 --- a/orchestra/apps/mailboxes/admin.py +++ b/orchestra/apps/mailboxes/admin.py @@ -1,5 +1,5 @@ import copy -from urllib.parse import urlparse +from urllib.parse import parse_qs from django import forms from django.contrib import admin diff --git a/orchestra/apps/orchestration/methods.py b/orchestra/apps/orchestration/methods.py index 133d340e..7cbfc378 100644 --- a/orchestra/apps/orchestration/methods.py +++ b/orchestra/apps/orchestration/methods.py @@ -86,22 +86,25 @@ def SSH(backend, log, server, cmds, async=False): # Log results logger.debug('%s running on %s' % (backend, server)) if async: + second = False while True: # Non-blocking is the secret ingridient in the async sauce select.select([channel], [], []) if channel.recv_ready(): - part = channel.recv(1024) + part = channel.recv(1024).decode('utf-8') while part: log.stdout += part - part = channel.recv(1024) + part = channel.recv(1024).decode('utf-8') if channel.recv_stderr_ready(): - part = channel.recv_stderr(1024) + part = channel.recv_stderr(1024).decode('utf-8') while part: log.stderr += part - part = channel.recv_stderr(1024) + part = channel.recv_stderr(1024).decode('utf-8') log.save(update_fields=['stdout', 'stderr']) if channel.exit_status_ready(): - break + if second: + break + second = True else: log.stdout += channel.makefile('rb', -1).read().decode('utf-8') log.stderr += channel.makefile_stderr('rb', -1).read().decode('utf-8') diff --git a/orchestra/apps/orders/models.py b/orchestra/apps/orders/models.py index c5df12d3..13ebb8c9 100644 --- a/orchestra/apps/orders/models.py +++ b/orchestra/apps/orders/models.py @@ -268,40 +268,46 @@ class MetricStorage(models.Model): accounts.register(Order) -@receiver(pre_delete, dispatch_uid="orders.account_orders") -def account_orders(sender, **kwargs): - account = kwargs['instance'] - if isinstance(account, Order.account.field.rel.to): - account._deleted = True +#@receiver(pre_delete, dispatch_uid="orders.account_orders") +#def account_orders(sender, **kwargs): +# account = kwargs['instance'] +# if isinstance(account, Order.account.field.rel.to): +# account._deleted = True +# FIXME account deletion generates a integrity error # TODO build a cache hash table {model: related, model: None} @receiver(post_delete, dispatch_uid="orders.cancel_orders") def cancel_orders(sender, **kwargs): if sender._meta.app_label not in settings.ORDERS_EXCLUDED_APPS: instance = kwargs['instance'] # Account delete will delete all related orders, no need to maintain order consistency - if isinstance(instance, Order.account.field.rel.to): -# print 'aaaaaaaaaaaaaAAAAAAAAAAAAAAAAaa' +# if isinstance(instance, Order.account.field.rel.to): +# return + if sender is Order.account.field.rel.to: return -# print 'delete', sender, kwargs - try: - print(instance.account.pk) - except Exception as e: - pass + print('delete', sender, instance, instance.pk) if type(instance) in services: for order in Order.objects.by_object(instance).active(): order.cancel() elif not hasattr(instance, 'account'): related = helpers.get_related_object(instance) + # FIXME this shit returns objects that are already deleted + # Indeterminate behaviour if related and related != instance: +# if isinstance(related, Order.account.field.rel.to): +# return + print('related', type(related), related, related.pk) +# try: +# type(related).objects.get(pk=related.pk) +# except related.DoesNotExist: +# print('not exists', type(related), related, related.pk) Order.update_orders(related) @receiver(post_save, dispatch_uid="orders.update_orders") def update_orders(sender, **kwargs): if sender._meta.app_label not in settings.ORDERS_EXCLUDED_APPS: instance = kwargs['instance'] -# print 'save', sender, kwargs if type(instance) in services: Order.update_orders(instance) elif not hasattr(instance, 'account'): diff --git a/orchestra/apps/resources/methods.py b/orchestra/apps/resources/methods.py index 4d6cf009..bc275089 100644 --- a/orchestra/apps/resources/methods.py +++ b/orchestra/apps/resources/methods.py @@ -63,11 +63,14 @@ class MonthlyAvg(MonthlySum): ) def compute_usage(self, dataset): - last = dataset.latest() + result = 0 + try: + last = dataset.latest() + except dataset.model.DoesNotExist: + return result epoch = self.get_epoch() total = (last.created_at-epoch).total_seconds() ini = epoch - result = 0 for data in dataset: slot = (data.created_at-ini).total_seconds() result += data.value * decimal.Decimal(str(slot/total)) diff --git a/orchestra/apps/systemusers/backends.py b/orchestra/apps/systemusers/backends.py index ba2f7aa7..40af444a 100644 --- a/orchestra/apps/systemusers/backends.py +++ b/orchestra/apps/systemusers/backends.py @@ -40,8 +40,8 @@ class SystemUserBackend(ServiceController): self.append(textwrap.dedent("""\ { sleep 2 && killall -u %(user)s -s KILL; } & killall -u %(user)s || true - userdel %(user)s || true - groupdel %(group)s || true""") % context + userdel %(user)s || exit_code=1 + groupdel %(group)s || exit_code=1""") % context ) self.delete_home(context, user) @@ -52,7 +52,7 @@ class SystemUserBackend(ServiceController): def delete_home(self, context, user): if user.home.rstrip('/') == user.get_base_home().rstrip('/'): # TODO delete instead of this shit - self.append("mv %(home)s %(home)s.deleted" % context) + self.append("mv %(home)s %(home)s.deleted || exit_code=1" % context) def get_groups(self, user): if user.is_main: diff --git a/orchestra/bin/orchestra-admin b/orchestra/bin/orchestra-admin index a86531ad..889595e3 100755 --- a/orchestra/bin/orchestra-admin +++ b/orchestra/bin/orchestra-admin @@ -119,13 +119,14 @@ function install_requirements () { check_root ORCHESTRA_PATH=$(get_orchestra_dir) - APT="python-pip \ - python-psycopg2 \ + APT="python3 \ + python3-pip \ + python3-psycopg2 \ postgresql \ rabbitmq-server \ - python-dev \ + python3-dev \ bind9utils \ - python-cracklib \ + python3-cracklib \ libz-dev \ libxml2-dev \ libxslt1-dev \ @@ -166,7 +167,7 @@ function install_requirements () { APT="${APT} \ iceweasel \ dnsutils \ - python-mysqldb" + python3-mysqldb" PIP="${PIP} \ selenium \ xvfbwrapper \ @@ -199,7 +200,7 @@ function install_requirements () { update-ca-certificates fi - run pip install $PIP + run pip3 install $PIP # Some versions of rabbitmq-server will not start automatically by default unless ... sed -i "s/# Default-Start:.*/# Default-Start: 2 3 4 5/" /etc/init.d/rabbitmq-server diff --git a/orchestra/management/commands/setupcelery.py b/orchestra/management/commands/setupcelery.py index 2d79167b..33a284d5 100644 --- a/orchestra/management/commands/setupcelery.py +++ b/orchestra/management/commands/setupcelery.py @@ -42,7 +42,7 @@ class Command(BaseCommand): CELERYD_CHDIR="%(site_dir)s" # How to call "manage.py celeryd_multi" - CELERYD_MULTI="$CELERYD_CHDIR/manage.py celeryd_multi" + CELERYD_MULTI="python3 $CELERYD_CHDIR/manage.py celeryd_multi" # Extra arguments to celeryd CELERYD_OPTS="-P:w1 processes -c:w1 %(processes)s -Q:w1 celery" @@ -67,14 +67,14 @@ class Command(BaseCommand): CELERYD_STATE_DB="$CELERYD_CHDIR/persistent_revokes" # Celeryev - CELERYEV="$CELERYD_CHDIR/manage.py" + CELERYEV="python3 $CELERYD_CHDIR/manage.py" CELERYEV_CAM="djcelery.snapshot.Camera" CELERYEV_USER="$CELERYD_USER" CELERYEV_GROUP="$CELERYD_USER" CELERYEV_OPTS="celerycam" # Celerybeat - CELERYBEAT="${CELERYD_CHDIR}/manage.py celerybeat" + CELERYBEAT="python3 ${CELERYD_CHDIR}/manage.py celerybeat" CELERYBEAT_USER="$CELERYD_USER" CELERYBEAT_GROUP="$CELERYD_USER" CELERYBEAT_CHDIR="$CELERYD_CHDIR" diff --git a/setup.py b/setup.py index 2ca94c1e..10202805 100644 --- a/setup.py +++ b/setup.py @@ -1,5 +1,3 @@ -from __future__ import unicode_literals - import os, sys from distutils.sysconfig import get_python_lib from setuptools import setup, find_packages