Commit 648b6878 authored by David Seaward's avatar David Seaward

close ssh connections (FIXME: better to use try-except-finally)

parent 5c8cb10a
...@@ -54,9 +54,12 @@ def get_openvpn_ssh_connection(): ...@@ -54,9 +54,12 @@ def get_openvpn_ssh_connection():
return ssh return ssh
def managed_exec(ssh, command): def managed_exec(ssh, command, close=True):
stdin, stdout, stderr = ssh.exec_command(command) stdin, stdout, stderr = ssh.exec_command(command)
if close:
ssh.close()
output = "".join(stdout.readlines()).strip() output = "".join(stdout.readlines()).strip()
if output == "": if output == "":
output = "None." output = "None."
...@@ -91,7 +94,7 @@ def get_external_bundle(parser, external_key): ...@@ -91,7 +94,7 @@ def get_external_bundle(parser, external_key):
) )
def activate(ssh, limit, credit_timedelta=None, renewal_date=None): def activate_single_limit(ssh, limit, credit_timedelta=None, renewal_date=None):
is_credit = credit_timedelta is not None is_credit = credit_timedelta is not None
is_renewal = renewal_date is not None is_renewal = renewal_date is not None
...@@ -124,7 +127,8 @@ def activate(ssh, limit, credit_timedelta=None, renewal_date=None): ...@@ -124,7 +127,8 @@ def activate(ssh, limit, credit_timedelta=None, renewal_date=None):
# only create certificate if it doesn't exist # only create certificate if it doesn't exist
if not is_file: if not is_file:
managed_exec(ssh, "./create_new_ovpn_config --generate %s" % (user_identity,)) command = "./create_new_ovpn_config --generate %s" % (user_identity,)
managed_exec(ssh, command, close=False)
else: else:
# skip unsupported limits # skip unsupported limits
limit.is_active = False limit.is_active = False
...@@ -132,9 +136,10 @@ def activate(ssh, limit, credit_timedelta=None, renewal_date=None): ...@@ -132,9 +136,10 @@ def activate(ssh, limit, credit_timedelta=None, renewal_date=None):
limit.save() limit.save()
def deactivate(ssh, limit): def deactivate_single_limit(ssh, limit):
if limit.service == settings.LM_SERVICES.TUNNEL: if limit.service == settings.LM_SERVICES.TUNNEL:
managed_exec(ssh, "./create_new_ovpn_config --revoke %s" % (limit.user.get_identity(),)) command = "./create_new_ovpn_config --revoke %s" % (limit.user.get_identity(),)
managed_exec(ssh, command, close=False)
limit.is_active = False limit.is_active = False
limit.save() limit.save()
...@@ -199,13 +204,15 @@ def deactivate_all_expired_limits(): ...@@ -199,13 +204,15 @@ def deactivate_all_expired_limits():
overdue_list = Limit.objects.filter(expiry_date__lte=now, is_active=True) overdue_list = Limit.objects.filter(expiry_date__lte=now, is_active=True)
for limit in overdue_list: for limit in overdue_list:
deactivate(ssh, limit) deactivate_single_limit(ssh, limit)
ssh.close()
def debug_connection_task(): def debug_connection_task():
# make connection objects # make connection objects
woo_connection = get_woo_connection() woo_connection = get_woo_connection()
ssh = get_openvpn_ssh_connection() ssh = get_openvpn_ssh_connection()
managed_exec(ssh, "whoami") managed_exec(ssh, "whoami", close=True)
logger.info("Debug task with " + repr(woo_connection) + " and " + repr(ssh) + " completed successfully.") logger.info("Debug task with " + repr(woo_connection) + " and " + repr(ssh) + " completed successfully.")
...@@ -40,21 +40,9 @@ def update_limit_woo1(ssh, credit): ...@@ -40,21 +40,9 @@ def update_limit_woo1(ssh, credit):
user, limit, external_bundle = get_limit_objects(credit) user, limit, external_bundle = get_limit_objects(credit)
# # 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_days = int(external_bundle.time_credit * credit.quantity)
credit_timedelta = timezone.timedelta(days=credit_days) credit_timedelta = timezone.timedelta(days=credit_days)
activate(ssh, limit, credit_timedelta, None) activate_single_limit(ssh, limit, credit_timedelta, None)
def monitor_woo1(): def monitor_woo1():
...@@ -99,3 +87,5 @@ def monitor_woo1(): ...@@ -99,3 +87,5 @@ def monitor_woo1():
if count > 0: if count > 0:
logger.info("Added %i new results." % (count,)) logger.info("Added %i new results." % (count,))
ssh.close()
...@@ -86,11 +86,13 @@ def monitor_woosub1_new_subscriptions(): ...@@ -86,11 +86,13 @@ def monitor_woosub1_new_subscriptions():
except Exception as e: except Exception as e:
logger.exception("Skipped bad result " + str(result)) logger.exception("Skipped bad result " + str(result))
ssh.close()
def update_limit_woosub1(ssh, credit, renewal_date): def update_limit_woosub1(ssh, credit, renewal_date):
user, limit, external_bundle = get_limit_objects(credit) user, limit, external_bundle = get_limit_objects(credit)
activate(ssh, limit, None, renewal_date) activate_single_limit(ssh, limit, None, renewal_date)
def monitor_woosub1_renewals(): def monitor_woosub1_renewals():
...@@ -121,7 +123,7 @@ def monitor_woosub1_renewals(): ...@@ -121,7 +123,7 @@ def monitor_woosub1_renewals():
# deactivate expired limits # deactivate expired limits
if not result["active"]: if not result["active"]:
deactivate(ssh, limit) deactivate_single_limit(ssh, limit)
woosub_credit.is_converted = False woosub_credit.is_converted = False
woosub_credit.error_message = "Expired." woosub_credit.error_message = "Expired."
else: else:
...@@ -141,3 +143,5 @@ def monitor_woosub1_renewals(): ...@@ -141,3 +143,5 @@ def monitor_woosub1_renewals():
if count > 0: if count > 0:
logger.info("Updated %i subscriptions." % (count,)) logger.info("Updated %i subscriptions." % (count,))
ssh.close()
...@@ -108,7 +108,9 @@ class User(AbstractUser): ...@@ -108,7 +108,9 @@ class User(AbstractUser):
renewal_date = timezone.now() + timezone.timedelta(weeks=5200) renewal_date = timezone.now() + timezone.timedelta(weeks=5200)
for limit in limitmonitor_models.Limit.objects.filter(user=self, is_active=False): for limit in limitmonitor_models.Limit.objects.filter(user=self, is_active=False):
limitmonitor_common.activate(ssh, limit, renewal_date=renewal_date) limitmonitor_common.activate_single_limit(ssh, limit, renewal_date=renewal_date)
ssh.close()
def set_password(self, raw_password): def set_password(self, raw_password):
......
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