From dff56b4051dc0229b9048b4210fdf080b8205c1e Mon Sep 17 00:00:00 2001
From: Thomas Florio <thomas.florio@suse.com>
Date: Tue, 8 Oct 2024 17:42:07 +0200
Subject: [PATCH] Adding additional logging

---
 code/src/com/redhat/rhn/taskomatic/task/payg/PaygAuthDataExtractor.java       |    7 +++
 code/src/com/redhat/rhn/taskomatic/task/payg/PaygAuthDataProcessor.java       |   18 ++++------
 code/src/com/redhat/rhn/taskomatic/task/payg/PaygUpdateAuthTask.java          |    5 ++
 code/src/com/redhat/rhn/taskomatic/task/payg/script/payg_extract_repo_data.py |    6 ++-
 4 files changed, 23 insertions(+), 13 deletions(-)

--- a/code/src/com/redhat/rhn/taskomatic/task/payg/PaygAuthDataExtractor.java
+++ b/code/src/com/redhat/rhn/taskomatic/task/payg/PaygAuthDataExtractor.java
@@ -110,6 +110,7 @@ public class PaygAuthDataExtractor {
     }
 
     private PaygInstanceInfo processOutput(int exitStatus, String error, String output) {
+        LOG.info("The extractor terminated with exit code {}.\nSTDOUT:\n{}\nSTDERR:\n{}\n", exitStatus, output, error);
         if (exitStatus != 0 || error.length() > 0) {
             LOG.error("Exit status: {}", exitStatus);
             LOG.error("stderr:\n{}", error);
@@ -134,6 +135,8 @@ public class PaygAuthDataExtractor {
     private PaygInstanceInfo extractAuthDataSSH(PaygSshData instance) throws Exception {
         Session sessionsTarget = null, sessionBastion = null;
         try {
+            LOG.info("Connecting to {} with user {}", instance.getHost(), instance.getUsername());
+
             JSch.setConfig("StrictHostKeyChecking", "no");
             JSch sshTarget = new JSch();
             if (!StringUtils.isEmpty(instance.getKey())) {
@@ -143,6 +146,8 @@ public class PaygAuthDataExtractor {
             int sshPortIn = instance.getPort() != null ? instance.getPort() : 22;
 
             if (!StringUtils.isEmpty(instance.getBastionHost())) {
+                LOG.info("Using bastion host {} with username {}",
+                    instance.getBastionHost(), instance.getBastionUsername());
                 JSch sshBastion = new JSch();
                 if (!StringUtils.isEmpty(instance.getBastionKey())) {
                     String bastionAuthKeyPassIn = instance.getBastionKeyPassword() != null ?
@@ -172,6 +177,7 @@ public class PaygAuthDataExtractor {
             sessionsTarget.connect();
 
             OsSpecificExtractor extractor = getOsSpecificExtractor(sessionsTarget);
+            LOG.info("Extractor for host {} is {}", instance.getHost(), extractor);
             ChannelExec channel = (ChannelExec) sessionsTarget.openChannel("exec");
 
             try {
@@ -271,6 +277,7 @@ public class PaygAuthDataExtractor {
      */
     public PaygInstanceInfo extractAuthData(PaygSshData instance) throws Exception {
         if (instance.isSUSEManagerPayg()) {
+            LOG.info("Extracting SUMa Payg info");
             return extractAuthDataLocal();
         }
 
--- a/code/src/com/redhat/rhn/taskomatic/task/payg/PaygAuthDataProcessor.java
+++ b/code/src/com/redhat/rhn/taskomatic/task/payg/PaygAuthDataProcessor.java
@@ -75,13 +75,13 @@ public class PaygAuthDataProcessor {
      */
     public void processPaygInstanceData(PaygSshData instance, PaygInstanceInfo paygData) throws URISyntaxException {
 
-        LOG.debug("Process data for {}", paygData.getType());
+        LOG.info("Process data for {}", paygData.getType());
         if (paygData.getType().equals("CLOUDRMT")) {
             CloudRMTCredentials credentials = processAndGetCloudRMTCredentials(instance, paygData);
-            LOG.debug("Number installed Products: {}", paygData.getProducts().size());
+            LOG.info("Number installed Products: {}", paygData.getProducts().size());
 
             // Update the PAYG products associated with the credentials
-            LOG.debug("Associating the installed products with the credentials # {}", credentials.getId());
+            LOG.info("Associating the installed products with the credentials # {}", credentials.getId());
             PaygProductFactory.updateProducts(credentials, paygData.getProducts());
 
             // Add the Tools and Proxy products that are accessible when in SUMA Payg environment
@@ -92,13 +92,13 @@ public class PaygAuthDataProcessor {
 
             // Update the authorizations for accessing the product repositories
             List<SCCRepositoryAuth> repoAuths = PaygProductFactory.refreshRepositoriesAuths(credentials, products);
-            LOG.debug("Total repository authentication processed: {}", repoAuths.size());
+            LOG.info("Total repository authentication processed: {}", repoAuths.size());
 
             // Store the information about the Cloud RMT server
             processCloudRmtHost(instance, paygData);
         }
         else if (paygData.getType().equals("RHUI")) {
-            LOG.debug("Number repositories: {}", paygData.getRepositories().size());
+            LOG.info("Number repositories: {}", paygData.getRepositories().size());
             long instanceId = instance.getId();
             Org org = OrgFactory.lookupById(ConfigDefaults.get().getRhuiDefaultOrgId());
             Map<String, SslCryptoKey> cryptoKeyMap = extractCryptoKeys(paygData, instanceId, org);
@@ -232,11 +232,9 @@ public class PaygAuthDataProcessor {
 
         instance.setRmtHosts(rmtHost);
         PaygSshDataFactory.savePaygSshData(instance);
-        if (LOG.isDebugEnabled()) {
-            LOG.debug("CloudRmtHost Hostname: {}", rmtHost.getHost());
-            LOG.debug("CloudRmtHost IP: {}", rmtHost.getIp());
-            LOG.debug("CloudRmtHost SSL Cert: {}", rmtHost.getSslCert());
-        }
+        LOG.info("CloudRmtHost Hostname: {}", rmtHost.getHost());
+        LOG.info("CloudRmtHost IP: {}", rmtHost.getIp());
+        LOG.info("CloudRmtHost SSL Cert: {}", rmtHost.getSslCert());
     }
 
     private CloudRMTCredentials processAndGetCloudRMTCredentials(PaygSshData instance, PaygInstanceInfo paygData)
--- a/code/src/com/redhat/rhn/taskomatic/task/payg/PaygUpdateAuthTask.java
+++ b/code/src/com/redhat/rhn/taskomatic/task/payg/PaygUpdateAuthTask.java
@@ -155,12 +155,15 @@ public class PaygUpdateAuthTask extends
 
     private void updateInstanceData(PaygSshData instance) {
         try {
+            LOG.info("Retrieving instance data for {}", instance.getHost());
             PaygInstanceInfo paygData = paygDataExtractor.extractAuthData(instance);
+            LOG.info("Payg data retrieved correctly. Processing");
             paygDataProcessor.processPaygInstanceData(instance, paygData);
             instance.setStatus(PaygSshData.Status.S);
             instance.setErrorMessage("");
+            LOG.info("Saving data into the database");
             PaygSshDataFactory.savePaygSshData(instance);
-
+            LOG.info("Process completed for {}", instance.getHost());
         }
         catch (PaygDataExtractException | JSchException e) {
             LOG.error("error getting instance data ", e);
--- a/code/src/com/redhat/rhn/taskomatic/task/payg/script/payg_extract_repo_data.py
+++ b/code/src/com/redhat/rhn/taskomatic/task/payg/script/payg_extract_repo_data.py
@@ -50,12 +50,14 @@ def is_payg_instance():
                         "For a correct PAYG detection please install the 'python-instance-billing-flavor-check' package"])
 
     try:
-        result = subprocess.call(flavor_check, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
+        result = subprocess.run(flavor_check, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
+        print("instant-flavor-check STDOUT:\n", result.stdout, file=sys.stderr)
+        print("instant-flavor-check STDERR:\n", result.stderr, file=sys.stderr)
     except subprocess.CalledProcessError as e:
         system_exit(1, ["Failed to execute instance-flavor-check tool.", e])
 
     # instance-flavor-check return 10 for PAYG. Other possible values are 11 -> BYOS and 12 -> Unknown
-    return result == 10
+    return result.returncode == 10
 
 
 SuseCloudInfo = namedtuple('SuseCloudInfo', ['header_auth', 'hostname'])
