Commit f0240888 authored by David Seaward's avatar David Seaward
Browse files

refactor common functions, add missing migration

parent 5fe5b620
# -*- coding: utf-8 -*-
# Generated by Django 1.11.4 on 2017-08-28 06:15
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('limitmonitor', '0002_auto_20170815_1159'),
]
operations = [
migrations.AddField(
model_name='limit',
name='renewal_date',
field=models.DateTimeField(blank=True, default=None, null=True),
),
migrations.AlterField(
model_name='externalbundle',
name='parser',
field=models.CharField(choices=[('WOO1', 'WooCommerce v1'), ('WOOSUB1', 'WooCommerce Subscription v1')], max_length=30),
),
migrations.AlterField(
model_name='externalcredit',
name='parser',
field=models.CharField(choices=[('WOO1', 'WooCommerce v1'), ('WOOSUB1', 'WooCommerce Subscription v1')], max_length=30),
),
]
......@@ -4,6 +4,7 @@ from django.conf import settings
from django.db import transaction
from django.utils import timezone
from woocommerce import API as WOO_API
import django.contrib.auth
from ..models import ExternalCredit, ExternalBundle, Limit
......@@ -72,18 +73,32 @@ def get_external_bundle(parser, external_key):
)
def activate(ssh, limit, credit_timedelta):
if limit.is_active:
def activate(ssh, limit, credit_timedelta=None, renewal_date=None):
is_credit = credit_timedelta is not None
is_renewal = renewal_date is not None
if is_credit == is_renewal:
raise Exception("Invalid activation attempt. Need strictly one of credit or renewal data.")
if is_credit and limit.is_active:
limit.expiry_date += credit_timedelta
else:
elif is_credit and not limit.is_active:
limit.expiry_date = timezone.now() + credit_timedelta
limit.is_active = True
elif is_renewal:
limit.renewal_date = renewal_date
limit.is_active = True
else:
raise Exception("Invalid activation attempt. Unknown condition.")
# skip activation command if we are debugging
if settings.DEBUG_SKIP_ACTIVATION_COMMAND:
limit.save()
return
# otherwise, activate the limit before saving
if limit.service == "TUNNEL":
managed_exec(ssh, "./create_new_ovpn_config --generate %s" % (limit.user.get_identity(),))
......@@ -98,12 +113,59 @@ def deactivate(ssh, limit):
limit.save()
def get_account_from_woo_meta(meta_list):
account = "invalid"
for meta_item in meta_list:
if meta_item["key"] in settings.WOO1_FIELD_LIST: # for example, "Existing username,"
account = meta_item["value"]
at_count = account.count("@")
if at_count == 0: # nodomain
account += "@" + settings.SITE_DOMAIN # corrected to nodomain@example.com
elif at_count == 1:
pass # valid account format
else: # at_count > 1, for example bad@user@example.com
account = account.replace("@", ".AT.") # force invalid name bad.AT.user.AT.example.com
return account
def get_limit_objects(credit):
# get and validate local username
suffix = "@" + settings.SITE_DOMAIN
if credit.account_name is None or not str.endswith(credit.account_name, suffix):
raise Exception("Invalid account name: " + str(credit.account_name))
else:
suffix_len = 0 - len(suffix)
username = credit.account_name[:suffix_len]
# get objects (implicit validation that they exist)
external_bundle = get_external_bundle(credit.parser, credit.bundle_key)
limit = Limit.objects.get(
user__username=username,
service=external_bundle.service,
)
user = django.contrib.auth.get_user_model().objects.get(
username=username,
)
return user, limit, external_bundle
@transaction.atomic
def store_credit_and_update_limit(ssh, credit):
def store_credit_and_update_limit(ssh, credit, next_renewal=None):
try:
if credit.parser == "WOO1":
from .tunnel_credit import update_limit_woo1
update_limit_woo1(ssh, credit)
elif credit.parser == "WOOSUB1":
from .tunnel_subscription import update_limit_woosub1
update_limit_woosub1(ssh, credit, next_renewal)
else:
raise Exception("Unrecognised parser " + credit.parser)
......
import django.contrib.auth
from .common import *
......@@ -19,20 +17,7 @@ def parse_woo1(json_entry, product_id):
product_id = line_item["product_id"]
product_label = line_item["name"]
quantity = line_item["quantity"]
account = "invalid"
for meta_item in line_item["meta"]:
if meta_item["key"] in settings.WOO1_FIELD_LIST: # for example, "Existing username,"
account = meta_item["value"]
at_count = account.count("@")
if at_count == 0: # nodomain
account += "@" + settings.SITE_DOMAIN # corrected to nodomain@example.com
elif at_count == 1:
pass # valid account format
else: # at_count > 1, for example bad@user@example.com
account = account.replace("@", ".AT.") # force invalid name bad.AT.user.AT.example.com
account = get_account_from_woo_meta(line_item["meta"])
external_key = str(order_id) + ":" + str(item_id)
external_label = order_name + ":" + str(item_id)
......@@ -52,63 +37,24 @@ def parse_woo1(json_entry, product_id):
def update_limit_woo1(ssh, credit):
# validate credit
suffix = "@" + settings.SITE_DOMAIN
if credit.account_name is None or not str.endswith(credit.account_name, suffix):
raise Exception("Invalid account name: " + str(credit.account_name))
else:
suffix_len = 0 - len(suffix)
username = credit.account_name[:suffix_len]
# get external references (implicit validation that they exist)
external_bundle = get_external_bundle(credit.parser, credit.bundle_key)
limit = Limit.objects.get(
user__username=username,
service=external_bundle.service,
)
user = django.contrib.auth.get_user_model().objects.get(
username=username,
)
# set user email if missing
credit_has_email = not (credit.additional_data is None or credit.additional_data == "")
user_has_email = not (user.email is None or user.email == "")
user, limit, external_bundle = get_limit_objects(credit)
if user_has_email:
pass
elif credit_has_email:
user.email = credit.additional_data
user.save()
else:
logger.warn("No email address for credit " + credit.parser + " " + credit.external_key)
# # set user email if missing
# credit_has_email = not (credit.additional_data is None or credit.additional_data == "")
# user_has_email = not (user.email is None or user.email == "")
#
# if user_has_email:
# pass
# elif credit_has_email:
# user.email = credit.additional_data
# user.save()
# else:
# logger.warn("No email address for credit " + credit.parser + " " + credit.external_key)
credit_days = int(external_bundle.time_credit * credit.quantity)
credit_timedelta = timezone.timedelta(days=credit_days)
activate(ssh, limit, credit_timedelta)
@transaction.atomic
def store_credit_and_update_limit(ssh, credit):
try:
if credit.parser == "WOO1":
update_limit_woo1(ssh, credit)
else:
raise Exception("Unrecognised parser " + credit.parser)
credit.is_converted = True
credit.error_message = ""
except Exception as e:
message = "Skipped adding credit " + credit.parser + ":" + credit.external_key + ". "
logger.exception(message)
credit.error_message = message + repr(e)
finally:
credit.save()
state = "converted" if credit.is_converted else "skipped"
logger.info("Stored " + state + " credit " + credit.parser + ":" + credit.external_key)
activate(ssh, limit, credit_timedelta, None)
def monitor_woo1():
......
# import django.contrib.auth
# import paramiko
# from celery import shared_task
# from celery.utils.log import get_task_logger
# from django.conf import settings
# from django.db import transaction
# from django.utils import timezone
# from woocommerce import API as WOO_API
#
# from limitmonitor.models import ExternalCredit, ExternalBundle, Limit
#
# logger = get_task_logger(__name__)
#
#
# def get_woo_connection():
#
# return WOO_API(
# url=settings.WOO_URL,
# consumer_key=settings.WOO_CONSUMER_KEY,
# consumer_secret=settings.WOO_CONSUMER_SECRET,
# wp_api=settings.WOO_WP_API,
# version=settings.WOO_VERSION,
# )
#
#
# def get_openvpn_ssh_connection():
# # make ssh connection to OpenVPN server
# # (uses system host keys, warns if host is not recognised)
# ssh = paramiko.SSHClient()
# ssh.load_system_host_keys()
# ssh.set_missing_host_key_policy(paramiko.WarningPolicy()) # TODO: where is this logged?
# ssh.connect(
# hostname=settings.OVPN_HOSTNAME,
# port=settings.OVPN_PORT,
# username=settings.OVPN_USERNAME,
# )
#
# return ssh
#
#
# def managed_exec(ssh, command):
# stdin, stdout, stderr = ssh.exec_command(command)
#
# output = "".join(stdout.readlines()).strip()
# if output == "":
# output = "None."
#
# error = "".join(stderr.readlines()).strip()
#
# message = "Executed: %s Output: %s" % (command, output,)
#
# # on sucess, log output, otherwise raise exception
# if stdout.channel.recv_exit_status() == 0:
# logger.info(message)
# else:
# message += " Error: " + error
# raise Exception(message)
#
#
# def get_latest_woo1(connection, product_id):
# return connection.get("orders?product=" + str(product_id))
#
#
# def parse_woo1(json_entry, product_id):
# result_list = []
# order_name = json_entry["number"]
# order_id = json_entry["id"]
#
# for line_item in json_entry["line_items"]:
# if line_item["product_id"] == product_id:
#
# item_id = line_item["id"]
# product_id = line_item["product_id"]
# product_label = line_item["name"]
# quantity = line_item["quantity"]
#
# account = "invalid"
# for meta_item in line_item["meta"]:
# if meta_item["key"] in settings.WOO1_FIELD_LIST: # for example, "Existing username,"
# account = meta_item["value"]
#
# if account.count("@") == 0: # nodomain
# account += "@" + settings.SITE_DOMAIN # corrected to nodomain@example.com
# elif account.count("@") > 1: # bad@user@example.com
# raise Exception("Invalid username: " + account)
# elif not account.endswith(settings.SITE_DOMAIN): # user@unrecognised.com
# raise Exception("Bad username suffix: " + account)
#
# external_key = str(order_id) + ":" + str(item_id)
# external_label = order_name + ":" + str(item_id)
#
# result_list.append({
# "parser": "WOO1",
# "external_key": external_key,
# "label": external_label,
# "product_key": product_id,
# "product_label": product_label,
# "quantity": quantity,
# "account": account,
# "original_email": "",
# "isconverted": False,
# })
#
# return result_list
#
#
# def is_existing_credit(credit):
# matching_credits = ExternalCredit.objects.filter(
# parser=credit.parser,
# external_key=credit.external_key,
# )
#
# is_existing = len(matching_credits) > 0
#
# return is_existing
#
#
# def update_limit_woo1(ssh, credit):
# # validate credit
#
# suffix = "@" + settings.SITE_DOMAIN
# if credit.account_name is None or not str.endswith(credit.account_name, suffix):
# raise Exception("Invalid account name: " + str(credit.account_name))
# else:
# suffix_len = 0 - len(suffix)
# username = credit.account_name[:suffix_len]
#
# # get external references (implicit validation that they exist)
#
# external_bundle = ExternalBundle.objects.get(
# parser=credit.parser,
# external_key=credit.bundle_key,
# )
#
# limit = Limit.objects.get(
# user__username=username,
# service=external_bundle.service,
# )
#
# user = django.contrib.auth.get_user_model().objects.get(
# username=username,
# )
#
# # set user email if missing
# credit_has_email = not (credit.additional_data is None or credit.additional_data == "")
# user_has_email = not (user.email is None or user.email == "")
#
# if user_has_email:
# pass
# elif credit_has_email:
# user.email = credit.additional_data
# user.save()
# else:
# logger.warn("No email address for credit " + credit.parser + " " + credit.external_key)
#
# credit_days = int(external_bundle.time_credit * credit.quantity)
# credit_timedelta = timezone.timedelta(days=credit_days)
# activate(ssh, limit, credit_timedelta)
#
#
# def activate(ssh, limit, credit_timedelta):
#
# if limit.is_active:
# limit.expiry_date += credit_timedelta
# else:
# limit.expiry_date = timezone.now() + credit_timedelta
# limit.is_active = True
#
# if limit.service == "TUNNEL":
# managed_exec(ssh, "./create_new_ovpn_config --generate %s" % (limit.user.get_identity(),))
#
# limit.save()
#
#
# def deactivate(ssh, limit):
# if limit.service == "TUNNEL":
# managed_exec(ssh, "./create_new_ovpn_config --revoke %s" % (limit.user.get_identity(),))
#
# limit.is_active = False
# limit.save()
#
#
# @transaction.atomic
# def store_credit_and_update_limit(ssh, credit):
#
# try:
# if credit.parser == "WOO1":
# update_limit_woo1(ssh, credit)
# else:
# raise Exception("Unrecognised parser " + credit.parser)
#
# credit.is_converted = True
# credit.error_message = ""
# except Exception as e:
# message = "Skipped adding credit " + credit.parser + ":" + credit.external_key + ". "
# logger.exception(message)
# credit.error_message = message + repr(e)
# finally:
# credit.save()
# state = "converted" if credit.is_converted else "skipped"
# logger.info("Stored " + state + " credit " + credit.parser + ":" + credit.external_key)
#
#
# @shared_task
# def monitor_woo1():
# # make connection objects
# woo_connection = get_woo_connection()
# ssh = get_openvpn_ssh_connection()
#
# # get product sales and parse the results
# result_list = []
# for product_id in settings.WOO_PRODUCT_LIST:
# latest_woo1_json = get_latest_woo1(woo_connection, product_id).json()
# for json_entry in latest_woo1_json:
# try:
# result_list.extend(parse_woo1(json_entry, product_id))
# except Exception as e:
# logger.exception("Skipping JSON entry " + str(json_entry))
#
# # add new results
# count = 0
# for result in result_list:
# try:
#
# credit = ExternalCredit(
# parser=result["parser"],
# external_key=result["external_key"],
# label=result["label"],
# bundle_key=result["product_key"],
# bundle_label=result["product_label"],
# quantity=result["quantity"],
# account_name=result["account"],
# additional_data=result["original_email"],
# is_converted=False,
# )
#
# if not is_existing_credit(credit):
# store_credit_and_update_limit(ssh, credit)
# count += 1
# else:
# logger.debug("Skipped existing result " + str(result))
# except Exception as e:
# logger.exception("Skipped bad result " + str(result))
#
# if count > 0:
# logger.info("Added %i new results." % (count,))
#
# def parse_woosub1(json_entry):
#
# id = str(json_entry["id"])
# if len(json_entry["line_items"]) != 1:
# raise Exception("Too many line items in subscription %s" % (id, ))
#
# result = {}
# result["parser"] = "WOOSUB1"
# result["external_key"] = id + ":" + str(json_entry["line_items"][0]["product_id"])
# result["label"] = json_entry["line_items"][0]["name"]
#
#
# return
#
# @shared_task
# def monitor_woosub1_new_subscriptions():
# # make connection objects
# woo_connection = get_woo_connection()
# ssh = get_openvpn_ssh_connection()
#
# result_list = []
# latest_subscription_json = woo_connection.get("subscriptions?orderby=date&order=desc").json()
#
# # get product sales and parse the results
# for json_entry in latest_subscription_json:
# try:
# result_list.extend(parse_woosub1(json_entry))
# except Exception as e:
# logger.exception("Skipping JSON entry " + str(json_entry))
#
# # add new results
# count = 0
# for result in result_list:
# try:
#
# credit = ExternalCredit(
# parser=result["parser"],
# external_key=result["external_key"],
# label=result["label"],
# bundle_key=result["product_key"],
# bundle_label=result["product_label"],
# quantity=result["quantity"],
# account_name=result["account"],
# additional_data=None,
# is_converted=False,
# )
#
# if not is_existing_credit(credit):
# # store_woosub1_limit(ssh, credit, result["next_date"])
# count += 1
# else:
# logger.debug("Skipped existing result " + str(result))
# except Exception as e:
# logger.exception("Skipped bad result " + str(result))
#
# @shared_task
# def monitor_woosub1_renewals():
# # make connection objects
# woo_connection = get_woo_connection()
# ssh = get_openvpn_ssh_connection()
#
# # get objects due for renewal and deactivate them
# now = timezone.now()
# overdue_list = Limit.objects.filter(renewal_date__lte=now,expiry_date=None, is_active=True)
#
# for limit in overdue_list:
# woosub1_list = ExternalCredit.objects.filter(parser="WOOSUB1")
# for woosub in woosub1_list:
# subscription_query = "subscriptions/" + woosub.external_key
# woosub_json = woo_connection.get(subscription_query).json()
# result = parse_woosub1(woosub_json)
#
# if not result["active"]:
# deactivate(ssh, limit)
# else:
# woosub.updated_date = now
# limit.renewal_date = result["renewal"]
# limit.save()
#
# @shared_task
# def deactivate_all_expired_limits():
# # make connection objects
# ssh = get_openvpn_ssh_connection()
#
# # get overdue objects and deactivate them
# now = timezone.now()
# overdue_list = Limit.objects.filter(renewal_date=None,expiry_date__lte=now, is_active=True)
#
# for limit in overdue_list:
# deactivate(ssh, limit)
#
#
# @shared_task
# def debug_connection_task():
# # make connection objects
# woo_connection = get_woo_connection()
# ssh = get_openvpn_ssh_connection()
# managed_exec(ssh, "whoami")
#
# logger.info("Debug task with " + repr(woo_connection) + " and " + repr(ssh) + " completed successfully.")
File mode changed from 100755 to 100644
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment