Merge ~lihuiguo/charm-logrotated:bug/1864598 into charm-logrotated:master

Proposed by Linda Guo
Status: Work in progress
Proposed branch: ~lihuiguo/charm-logrotated:bug/1864598
Merge into: charm-logrotated:master
Diff against target: 76 lines (+24/-21)
1 file modified
src/lib/lib_cron.py (+24/-21)
Reviewer Review Type Date Requested Status
🤖 prod-jenkaas-bootstack (community) continuous-integration Needs Fixing
Gabriel Cocenza Needs Information
Eric Chen Approve
BootStack Reviewers Pending
Review via email: mp+428357@code.launchpad.net

Commit message

Clear up previous cron job file when cron job config is changed

To post a comment you must log in.
Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote :

This merge proposal is being monitored by mergebot. Change the status to Approved to merge.

Revision history for this message
Eric Chen (eric-chen) wrote :

It's dangerous for the logic when we want to modify it in the future.
The hidden logic here is.. we only put the cronjob file with one frequency ["hourly", "daily", "weekly", "monthly"]. That is the design limitation in charm-logrotate, not a limitation in linux cronjob. But I don't have good idea to make this design limitation explict in the cleanup_cronjob function right now.

I have other ideas to remove some duplicated code and avoid additional parameter in the cleanup_cronjob.

1. There could be a cronjob filepath generator to be used in install_cronjob and cleanup_cronjob
2. We can use frequency index to generate the path and it's easy to know which one should be reserved in the cleanup_cronjob. We also can change the parameter as an array in the future if need.

/* Pseudo Code */
def cleanup_cronjob(self, reserve=-1):
    '''
    if reserve = -1, delete cronjob file in all frequencies
    else: only reserve the cronjob file in corresponding folder.
    '''

    for i in range(len(self.cronjob_check_paths)):
        if reserve == -1 or i != reserve:
            path = cronjob_path_generator(i)
            os.remove(path)

Revision history for this message
Eric Chen (eric-chen) wrote :

Mark it as WIP until Linda provide the 2nd patch or continue the discussion

Revision history for this message
Linda Guo (lihuiguo) wrote :

Thanks Eric, I refactored the code to make it more easy for maintenance. Can you please take another look?

Revision history for this message
Eric Chen (eric-chen) wrote :

LGTM

review: Approve
Revision history for this message
Eric Chen (eric-chen) :
review: Needs Information
Revision history for this message
Eric Chen (eric-chen) wrote :

I forget to request the log of unit&func test. Please prove the testing are pass. Thanks!

review: Needs Information
Revision history for this message
Linda Guo (lihuiguo) wrote :
Revision history for this message
Linda Guo (lihuiguo) wrote (last edit ):

functional test is broken at MASTER branch, it's stuck at test_deploy[xenial] https://pastebin.canonical.com/p/Yj63xsbSvM/

Unit Workload Agent Machine Public address Ports Message
ubuntu-xenial/0* error idle 0 10.5.3.224 hook failed: "install"

juju log: https://pastebin.canonical.com/p/g7t3Svt9ZM/

Revision history for this message
Eric Chen (eric-chen) wrote :

> functional test is broken at MASTER branch, it's stuck at test_deploy[xenial]
> https://pastebin.canonical.com/p/Yj63xsbSvM/
>
> Unit Workload Agent Machine Public address Ports Message
> ubuntu-xenial/0* error idle 0 10.5.3.224 hook
> failed: "install"
>
> juju log: https://pastebin.canonical.com/p/g7t3Svt9ZM/

That is a known issue according to this MP.

https://code.launchpad.net/~txiao/charm-logrotated/+git/charm-logrotated/+merge/427993

But could you pass the func test on bionic, focal and jammy ? Thanks!

Revision history for this message
Linda Guo (lihuiguo) wrote :

yes. func test on bionic, focal and jammy passed. sorry, forgot to post the result https://pastebin.canonical.com/p/BkDTykKV2R/

Revision history for this message
Eric Chen (eric-chen) :
review: Approve
Revision history for this message
Eric Chen (eric-chen) wrote :

Wait for one more engineer review it , then we can merge it.
I will let Garbriel to help it, but you can also call for help in MM.

Revision history for this message
Gabriel Cocenza (gabrielcocenza) wrote :

Hi Linda.

Thanks for this patch. I just have a small observation about the "cleanup_cronjob" that I don't think has a good readability.

review: Needs Information
Revision history for this message
Eric Chen (eric-chen) wrote :

Mark it as "WIP" until Linda have time to work it. Or please call for help from DOC. thanks

Revision history for this message
🤖 prod-jenkaas-bootstack (prod-jenkaas-bootstack) wrote :
review: Needs Fixing (continuous-integration)

Unmerged commits

ee2ae00... by Linda Guo

Clear up previous cron job file

Clear up previous cron job file when cron job config is changed.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
diff --git a/src/lib/lib_cron.py b/src/lib/lib_cron.py
index 5c17738..7114084 100644
--- a/src/lib/lib_cron.py
+++ b/src/lib/lib_cron.py
@@ -39,17 +39,12 @@ class CronHelper:
39 If logrotate-cronjob config option is set to True install cronjob,39 If logrotate-cronjob config option is set to True install cronjob,
40 otherwise cleanup.40 otherwise cleanup.
41 """41 """
42 clean_up_file = self.cronjob_frequency if self.cronjob_enabled else -142 cronjob_freq = self.cronjob_frequency if self.cronjob_enabled else -1
4343
44 if self.cronjob_enabled is True:44 if self.cronjob_enabled is True:
45 cronjob_path = os.path.realpath(__file__)45 cronjob_path = os.path.realpath(__file__)
46 cron_file_path = (46 cron_file_path = self.cronjob_path_generator(
47 self.cronjob_base_path47 self.cronjob_check_paths[cronjob_freq])
48 + self.cronjob_check_paths[clean_up_file]
49 + "/"
50 + self.cronjob_logrotate_cron_file
51 )
52
53 logrotate_unit = hookenv.local_unit()48 logrotate_unit = hookenv.local_unit()
54 python_venv_path = os.getcwd().replace("charm", "") + ".venv/bin/python3"49 python_venv_path = os.getcwd().replace("charm", "") + ".venv/bin/python3"
55 # upgrade to template if logic increases50 # upgrade to template if logic increases
@@ -63,22 +58,20 @@ class CronHelper:
63 cron_file.close()58 cron_file.close()
64 os.chmod(cron_file_path, 700)59 os.chmod(cron_file_path, 700)
6560
66 self.cleanup_cronjob(clean_up_file)61 self.cleanup_cronjob(cronjob_freq)
6762
68 def cleanup_cronjob(self, frequency=-1):63 def cleanup_cronjob(self, frequency=-1):
69 """Cleanup previous config."""64 """Cleanup previous config.
70 if frequency == -1:65
71 for check_path in self.cronjob_check_paths:66 If frequency=-1, clear all cron jobs
72 path = (67 """
73 self.cronjob_base_path68 for check_path in self.cronjob_check_paths:
74 + check_path69 path = self.cronjob_path_generator(check_path)
75 + "/"70 if os.path.exists(path):
76 + self.cronjob_logrotate_cron_file71 if frequency == -1 or self.cronjob_check_paths[frequency] != check_path:
77 )
78 if os.path.exists(path):
79 os.remove(path)72 os.remove(path)
80 if os.path.exists(self.cronjob_etc_config):73 if frequency == -1 and os.path.exists(self.cronjob_etc_config):
81 os.remove(self.cronjob_etc_config)74 os.remove(self.cronjob_etc_config)
8275
83 def update_logrotate_etc(self):76 def update_logrotate_etc(self):
84 """Run logrotate update config."""77 """Run logrotate update config."""
@@ -86,6 +79,16 @@ class CronHelper:
86 logrotate.read_config()79 logrotate.read_config()
87 logrotate.modify_configs()80 logrotate.modify_configs()
8881
82 def cronjob_path_generator(self, check_path):
83 """Generate the cron job file path."""
84 path = (
85 self.cronjob_base_path
86 + check_path
87 + "/"
88 + self.cronjob_logrotate_cron_file
89 )
90 return path
91
8992
90def main():93def main():
91 """Ran by cron."""94 """Ran by cron."""

Subscribers

People subscribed via source and target branches

to all changes: