From aa3b617c74b7f79881290aa25ba4994f61a0382e Mon Sep 17 00:00:00 2001 From: Pavel Raiskup Date: Thu, 22 Sep 2022 17:19:02 +0200 Subject: [PATCH] copr-be: a better cloud.ibm.com automation logging --- .../backend/templates/resalloc/ibm-cloud-list-vms.j2 | 7 +++++++ roles/copr/backend/templates/resalloc/ibm-cloud-vm.j2 | 11 +++++++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/roles/copr/backend/templates/resalloc/ibm-cloud-list-vms.j2 b/roles/copr/backend/templates/resalloc/ibm-cloud-list-vms.j2 index 9cfb84e02a..fe306f47c6 100755 --- a/roles/copr/backend/templates/resalloc/ibm-cloud-list-vms.j2 +++ b/roles/copr/backend/templates/resalloc/ibm-cloud-list-vms.j2 @@ -2,6 +2,7 @@ import argparse import datetime +import logging import os import subprocess import sys @@ -20,11 +21,15 @@ def _get_arg_parser(): parser = argparse.ArgumentParser() parser.add_argument("--token-file", default=DEFAULT_TOKEN_FILE) parser.add_argument("--pool") + parser.add_argument("--log-level", default="info") return parser def _main(): opts = _get_arg_parser().parse_args() + log_level = getattr(logging, opts.log_level.upper()) + logging.basicConfig(format='%(levelname)s: %(message)s', level=log_level) + log = logging.getLogger() pool_id = opts.pool or os.getenv("RESALLOC_POOL_ID") if not pool_id: @@ -47,6 +52,7 @@ def _main(): # Resalloc works with underscores, which is not allowed in IBM Cloud name = server["name"].replace("-", "_") if name.startswith(pool_id): + log.debug("found instance %s in state %s, id=%s", name, server["status"], server["id"]) resources.add(name) volumes = service.list_volumes(limit=LIMIT).result["volumes"] @@ -54,6 +60,7 @@ def _main(): # Resalloc works with underscores, which is not allowed in IBM Cloud name = volume["name"].replace("-", "_") if name.startswith(pool_id): + log.debug("found volume %s in state %s, id: %s", name, volume["status"], volume["id"]) name = name.rsplit("_", 1)[0] resources.add(name) diff --git a/roles/copr/backend/templates/resalloc/ibm-cloud-vm.j2 b/roles/copr/backend/templates/resalloc/ibm-cloud-vm.j2 index 5fadfb3d28..9a8d0fd284 100755 --- a/roles/copr/backend/templates/resalloc/ibm-cloud-vm.j2 +++ b/roles/copr/backend/templates/resalloc/ibm-cloud-vm.j2 @@ -225,27 +225,30 @@ def delete_instance(service, instance_name, opts): def delete_instance_attempt(service, instance_name, opts): """ one attempt to delete instance by it's name """ log = opts.log - log.info("Deleting instance %s", instance_name) + log.info("Searching for instance %s", instance_name) delete_instance_id = None response_list = service.list_instances().get_result()['instances'] for item in response_list: - log.debug("Available: %s %s %s", item['id'], item['name'], item['status']) if instance_name == item['name']: + log.debug("Found instance: %s %s %s", item['id'], item['name'], item['status']) delete_instance_id = item['id'] floating_ip_id = None response_list = service.list_floating_ips().get_result()['floating_ips'] for floating_ip in response_list: if floating_ip["name"].startswith(instance_name): + log.debug("Found floating IP %s", floating_ip["name"]) floating_ip_id = floating_ip["id"] if delete_instance_id: + log.info("Deleting instance %s: %s", delete_instance_id, instance_name) resp = service.delete_instance(delete_instance_id) assert resp.status_code == 204 log.debug("Delete instance request delivered") if floating_ip_id: + log.info("Deleting Floating IP %s", floating_ip_id) resp = service.delete_floating_ip(floating_ip_id) assert resp.status_code == 204 log.debug("Delete IP request delivered") @@ -258,6 +261,9 @@ def delete_instance_attempt(service, instance_name, opts): if not volume["name"].startswith(instance_name): continue + log.debug("Found volume %s %s %s", volume["name"], volume["status"], + volume["id"]) + # Otherwise Error: Delete volume failed. Volume can be deleted # only when its status is available or failed., Code: 409 if not volume["status"] in ["available", "failed"]: @@ -267,6 +273,7 @@ def delete_instance_attempt(service, instance_name, opts): if volume_ids: for volume_id in volume_ids: + log.info("Deleting volume %s", volume_id) resp = service.delete_volume(volume_id) assert resp.status_code == 204 log.debug("Delete volume request delivered")