Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix Path Resolution in process_log_dir_replacements Function #279

Merged
merged 2 commits into from
Aug 7, 2024

Conversation

brf153
Copy link
Contributor

@brf153 brf153 commented Aug 5, 2024

Summary

This PR closes #278. It addresses an issue with the process_log_dir_replacements function, which is responsible for replacing the $pwd placeholder in log directory paths with the path to the Kai project directory. The original function returned paths with redundant components and did not correctly resolve relative paths.

Issue

The original function produced paths with incorrect or redundant components, such as /home/k8smaster/Desktop/kai/kai/..//logs, leading to errors and misconfiguration in the log directory paths.

Changes Made

Function Update: Updated the process_log_dir_replacements function to:
Replace $pwd with the path to the Kai project directory, one level up from the current working directory.
Normalize the resulting path to remove redundant components and resolve relative path issues.

def process_log_dir_replacements(log_dir: str) -> str:
    ##
    # We want to replace $pwd with the location of the Kai project directory,
    # this is needed to help with specifying from configuration
    ##
    if log_dir.startswith("$pwd"):
        current_directory = os.getcwd()
        kai_project_directory = os.path.abspath(os.path.join(current_directory, ".."))
        log_dir = log_dir.replace("$pwd", kai_project_directory, 1)
        log_dir = os.path.normpath(log_dir)
    return log_dir

Signed-off-by: brf153 <153hsb@gmail.com>
@brf153 brf153 changed the title fix: Fix Path Resolution in process_log_dir_replacements Function fix: Fix Path Resolution in process_log_dir_replacements Function Aug 5, 2024
@jwmatthews
Copy link
Member

Thank you for your contribution.

Before we consider merging, I would like to know if you ran into functional errors that necessitated this PR or if this was more of a stylistic concern to clean up how the path was displayed. Let's discuss in #278

@jwmatthews
Copy link
Member

@brf153 do you want to include in this PR the test you shared in
#278 (comment)

@brf153
Copy link
Contributor Author

brf153 commented Aug 7, 2024

Thank you so much, @jwmatthews ! I'm really glad the test example helped clarify things.

Regarding the test you mentioned from #278, I've actually set up a separate branch just for tests. I'd prefer to add the tests from that branch if that's alright with you. Please let me know if that works.

@jwmatthews
Copy link
Member

Works for me

@jwmatthews
Copy link
Member

We will need to make a small change before we can merge this; as of now, the log directory is being created 1 lever higher in the hierarchy than we want.

I tested this via make run-server, executed that while being in the kai checkout.
The log file ended up being written a level up than expected.

My kai checkout is at:
/Users/jmatthews/git/konveyor/kai

I ran make run-server being in: /Users/jmatthews/git/konveyor/kai
The logging file is not created under the 'konveyor/kai/logs' directory as expected, but is created at 'konveyor/logs'.

Example from console logging output:
File logging for 'kai' is set to level 'DEBUG' writing to file: '/Users/jmatthews/git/konveyor/logs/kai_server.log'

I also tried with something a little odder just to verify behavior.
I changed directory to /Users/jmatthews/git/konveyor
then ran: make -f ./kai/Makefile run-server

Confirmed the log directory is again going a level higher in heirarchy than expected
File logging for 'kai' is set to level 'DEBUG' writing to file: '/Users/jmatthews/git/logs/kai_server.log'

I made the below tweak to correct this

$ git diff kai/kai_logging.py 
diff --git a/kai/kai_logging.py b/kai/kai_logging.py
index cc274f9..2b79d0e 100644
--- a/kai/kai_logging.py
+++ b/kai/kai_logging.py
@@ -18,7 +18,7 @@ def process_log_dir_replacements(log_dir: str) -> str:
     ##
     if log_dir.startswith("$pwd"):
         current_directory = os.getcwd()
-        kai_project_directory = os.path.abspath(os.path.join(current_directory, ".."))
+        kai_project_directory = os.path.abspath(current_directory)
         log_dir = log_dir.replace("$pwd", kai_project_directory, 1)
         log_dir = os.path.normpath(log_dir)
     return log_dir

Tested with podman compose to confirm logging to disk is still OK with the above tweak and looks good
File logging for 'kai' is set to level 'DEBUG' writing to file: '/podman_compose/logs/kai_server.log'

Copy link
Member

@jwmatthews jwmatthews left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As of now, this is creating the logs directory 1 level higher than we want.

"$pwd", os.path.join(os.path.dirname(os.path.realpath(__file__)), "../")
)
current_directory = os.getcwd()
kai_project_directory = os.path.abspath(os.path.join(current_directory, ".."))
Copy link
Member

@jwmatthews jwmatthews Aug 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to change this and get rid of the "..", that was needed in past when we were basing the directory to the location of __file__.

Signed-off-by: brf153 <153hsb@gmail.com>
@brf153
Copy link
Contributor Author

brf153 commented Aug 7, 2024

@jwmatthews made the required changes

@brf153 brf153 requested a review from jwmatthews August 7, 2024 15:56
Copy link
Member

@jwmatthews jwmatthews left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested both local dev and podman compose and verified logs are going to right location

@jwmatthews jwmatthews merged commit 1c5e827 into konveyor:main Aug 7, 2024
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Issue: process_log_dir_replacements Function Not Handling Relative Paths Correctly
2 participants